python - 回归神经网络的评估

标签 python tensorflow neural-network regression

嘿,

我正在尝试编写一个小程序来解决回归问题。我的数据集是 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 来完成此操作,但不是没有。如果您需要数据集,请告诉我。

干杯!

最佳答案

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

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

希望这有帮助。

关于python - 回归神经网络的评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47340860/

相关文章:

keras : add layers to another model

python - 在 Python 中是否有任何合法使用 list[True]、list[False] 的方法?

machine-learning - 如何在训练期间切换 tf.train.Optimizers?

python - 具有任意数量输入 channel (多于 RGB)的卷积神经网络架构

neural-network - 如何在 Flux 中训练模型组合?

matlab - 如何检查relu梯度

python - 由于缺少编译器错误,带有 python 和 alpine 的 Docker 镜像失败

python - 自定义异常默认日志记录

python - 将选定的列保留为 DataFrame 而不是 Series

tensorflow - 使用 tensorflow 进行序列标记 : Synced sequence input and output