回归神经网络的评估

时间:2017-11-16 23:25:59

标签: python tensorflow neural-network regression

HeJ小鼠,

我正在尝试编写一个小程序来解决回归问题。因此,我的数据集是4个随机x(x1,x2,x3和x4)和1 y值。其中一行看起来像这样:

0.634585    0.552366    0.873447    0.196890    8.75

我知道想要尽可能接近地预测y值,所以在训练之后我想通过显示损失来评估我的模型有多好。不幸的是我总是收到

Training cost= nan

最重要的一行可能是:

X_data = tf.placeholder(shape=[None, 4], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)

# Input neurons : 4
# Hidden neurons : 2 x 8
# Output neurons : 3
hidden_layer_nodes = 8

w1 = tf.Variable(tf.random_normal(shape=[4,hidden_layer_nodes])) # Inputs -> Hidden Layer1
b1 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes]))   # First Bias
w2 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes,1])) # Hidden layer2 -> Outputs
b2 = tf.Variable(tf.random_normal(shape=[1]))   # Third Bias

hidden_output = tf.nn.relu(tf.add(tf.matmul(X_data, w1), b1))
final_output = tf.nn.relu(tf.add(tf.matmul(hidden_output, w2), b2))

loss = tf.reduce_mean(-tf.reduce_sum(y_target * tf.log(final_output), axis=0))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()

steps = 10000

with tf.Session() as sess:

    sess.run(init)

    for i in range(steps):

        sess.run(train,feed_dict={X_data:X_train,y_target:y_train})

        # PRINT OUT A MESSAGE EVERY 100 STEPS
        if i%500 == 0:

            print('Currently on step {}'.format(i))

    training_cost = sess.run(loss, feed_dict={X_data:X_test,y_target:y_test})
    print("Training cost=", training_cost)

也许有人知道我的错误在哪里甚至更好,如何在训练期间不断显示错误:)我知道如何用tf.estimator完成这项工作,但不是没有。如果您需要数据集,请告诉我。

干杯!

1 个答案:

答案 0 :(得分:1)

这是因为 Relu 激活功能会导致爆炸渐变。因此,您需要相应地降低学习率。此外,您还可以尝试不同的激活功能(为此您可能必须首先规范化数据集)

在这里,(In simple multi-layer FFNN only ReLU activation function doesn't converge)与您的情况类似。按照答案,你会明白。

希望这有帮助。