提问者:小点点

用于回归的Tensorflow神经网络


我正在使用张量流库构建一个非常简单的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个答案

匿名用户

您必须对代码进行许多更改。

首先,您必须执行纪元数的训练,并批量提供优化器训练数据。您的学习率非常高。偏差应该是每个密集(完全连接)层只有一个输入。您可以绘制成本(损失)值以查看您的网络是如何收敛的。为了批量提供数据,我还对占位符进行了更改。检查完整的修改代码:

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]))