首页 文章

回归的张量流神经网络

提问于
浏览
1

我正在使用张量流库来构建一个非常简单的2层人工神经网络来执行线性回归 . 我的问题是结果似乎远非预期 . 我一直试图发现我的错误几个小时,但没有希望 . 我是张力流和神经网络的新手,所以这可能是一个微不足道的错误 . 谁能知道我做错了什么?

from __future__ import print_function

 import tensorflow as tf
 import numpy as np
 # Python optimisation variables
 learning_rate = 0.02

data_size=100000
data_length=100
train_input=10* np.random.rand(data_size,data_length);
train_label=train_input.sum(axis=1);
train_label=np.reshape(train_label,(data_size,1));

test_input= np.random.rand(data_size,data_length);
test_label=test_input.sum(axis=1);
test_label=np.reshape(test_label,(data_size,1));

x = tf.placeholder(tf.float32, [data_size, data_length])
y = tf.placeholder(tf.float32, [data_size, 1])

W1 = tf.Variable(tf.random_normal([data_length, 1], stddev=0.03), name='W1')
b1 = tf.Variable(tf.random_normal([data_size, 1]), name='b1')

y_ = tf.add(tf.matmul(x, W1), b1)


cost = tf.reduce_mean(tf.square(y-y_))                   
optimiser=tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
.minimize(cost)

init_op = tf.global_variables_initializer()

correct_prediction = tf.reduce_mean(tf.square(y-y_))    
accuracy = tf.cast(correct_prediction, tf.float32)


with tf.Session() as sess:
  sess.run(init_op)
  _, c = sess.run([optimiser, cost], 
                     feed_dict={x:train_input , y:train_label})
  k=sess.run(b1)
  print(k)                   
  print(sess.run(accuracy, feed_dict={x: test_input, y: test_label}))

谢谢你的帮助!

1 回答

  • 1

    您必须在代码中进行许多更改 .

    首先,您必须对多个时期进行培训,并分批提供优化器培训数据 . 你的学习率非常高 . 对于每个密集(完全连接)层,偏差应该只是一个输入 . 您可以绘制成本(损失)值,以查看网络如何融合 . 为了批量提供数据,我也对占位符进行了更改 . 检查完整修改后的代码:

    from __future__ import print_function
    
    import tensorflow as tf
    import numpy as np
    import matplotlib.pyplot as plt
    # Python optimisation variables
    learning_rate = 0.001  
    
    data_size=1000  # Had to change these value to fit in my memory
    data_length=10
    train_input=10* np.random.rand(data_size,data_length);
    train_label=train_input.sum(axis=1);
    train_label=np.reshape(train_label,(data_size,1));
    
    test_input= np.random.rand(data_size,data_length);
    test_label=test_input.sum(axis=1);
    test_label=np.reshape(test_label,(data_size,1));
    
    tf.reset_default_graph()
    x = tf.placeholder(tf.float32, [None, data_length])
    y = tf.placeholder(tf.float32, [None, 1])
    
    W1 = tf.Variable(tf.random_normal([data_length, 1], stddev=0.03), name='W1')
    b1 = tf.Variable(tf.random_normal([1, 1]), name='b1')
    
    y_ = tf.add(tf.matmul(x, W1), b1)
    
    
    cost = tf.reduce_mean(tf.square(y-y_))                   
    optimiser=tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
    
    init_op = tf.global_variables_initializer()
    
    EPOCHS = 500
    BATCH_SIZE = 32
    with tf.Session() as sess:
        sess.run(init_op)
    
        loss_history = []
        for epoch_no in range(EPOCHS):
            for offset in range(0, data_size, BATCH_SIZE):
                batch_x = train_input[offset: offset + BATCH_SIZE]
                batch_y = train_label[offset: offset + BATCH_SIZE]
    
                _, c = sess.run([optimiser, cost], 
                         feed_dict={x:batch_x , y:batch_y})
                loss_history.append(c)
    
    
        plt.plot(range(len(loss_history)), loss_history)
        plt.show()
    
        # For running test dataset
        results, test_cost = sess.run([y_, cost], feed_dict={x: test_input, y: test_label})
        print('test cost: {:.3f}'.format(test_cost))
        for t1, t2 in zip(results, test_label):
            print('Prediction: {:.3f}, actual: {:.3f}'.format(t1[0], t2[0]))
    

相关问题