【问题标题】:Neural Network after first epoch generates NaN values as output, loss第一个 epoch 后的神经网络生成 NaN 值作为输出,损失
【发布时间】:2019-09-05 21:29:20
【问题描述】:

我正在尝试设置具有几层的神经网络,这将解决简单的回归问题,这应该是 f(x) = 0,1x 或 f(x) = 10x

所有代码如下图(生成数据和神经网络)

  • 使用 ReLu 的 4 个全连接层
  • 损失函数RMSE
  • 学习梯度下降

问题是在我运行它之后,输出和损失函数变成了 NaN 值:

  • epoch:0,优化器:无,损失:inf
  • epoch:1,优化器:无,损失:nan

还有输出层: [NaN,NaN,NaN,......,NaN]

我是 tensorflow 的新手,我不确定我可能做错了什么(下一批、学习、会话的实现不好)

import tensorflow as tf
import sys
import numpy

#prepraring input data -> X
learningTestData = numpy.arange(1427456).reshape(1394,1024)

#preparing output data -> f(X) =0.1X
outputData = numpy.arange(1427456).reshape(1394,1024)

xx = outputData.shape
dd = 0
while dd < xx[0]:
    jj = 0
    while jj < xx[1]:
        outputData[dd,jj] = outputData[dd,jj] / 10
        jj += 1
    dd += 1

#preparing the NN
x = tf.placeholder(tf.float32, shape=[None, 1024])
y = tf.placeholder(tf.float32, shape=[None, 1024])

full1 = tf.contrib.layers.fully_connected(inputs=x, num_outputs=1024, activation_fn=tf.nn.relu)
full1 = tf.layers.batch_normalization(full1)

full2 = tf.contrib.layers.fully_connected(inputs=full1, num_outputs=5000, activation_fn=tf.nn.relu)
full2 = tf.layers.batch_normalization(full2)

full3 = tf.contrib.layers.fully_connected(inputs=full2, num_outputs=2500, activation_fn=tf.nn.relu)
full3 = tf.layers.batch_normalization(full3)

full4 = tf.contrib.layers.fully_connected(inputs=full3, num_outputs=1024, activation_fn=tf.nn.relu)
full4 = tf.layers.batch_normalization(full4)


out = tf.contrib.layers.fully_connected(inputs=full4, num_outputs=1024, activation_fn=None)


epochs = 20
batch_size = 50
learning_rate = 0.001
batchOffset = 0

# Loss (RMSE) and Optimizer
cost = tf.losses.mean_squared_error(labels=y, predictions=out)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)


with tf.Session() as sess:
    # Initializing the variables
    sess.run(tf.global_variables_initializer())

    e = 0

    while e < epochs:

        #selecting next batch
        sb = batchOffset
        eb = batchOffset+batch_size
        x_batch = learningTestData[sb:eb, :]
        y_batch = outputData[sb:eb, :]

        #learn
        opt = sess.run(optimizer,feed_dict={x: x_batch, y: y_batch})
        #show RMSE
        c = sess.run(cost, feed_dict={x: x_batch, y: y_batch})
        print("epoch: {}, optimizer: {}, loss: {}".format(e, opt, c))

        batchOffset += batch_size
        e += 1

【问题讨论】:

    标签: python tensorflow neural-network nan


    【解决方案1】:

    您需要对数据进行规范化,因为您的梯度以及结果cost 正在爆炸式增长。尝试运行这段代码:

    learning_rate = 0.00000001
    x_batch = learningTestData[:10]
    y_batch = outputData[:10]
    with tf.Session() as sess:
        # Initializing the variables
        sess.run(tf.global_variables_initializer())
        opt = sess.run(optimizer,feed_dict={x: x_batch, y: y_batch})
    
        c = sess.run(cost, feed_dict={x: x_batch, y: y_batch})
        print(c) # 531492.3
    

    在这种情况下,您将获得有限值,因为梯度没有将cost 带到无穷大。使用归一化数据、降低学习率或减少批量大小以使其发挥作用。

    【讨论】:

    • 谢谢,学习日期调整和数据准备都有帮助。当我随机生成数据时,神经网络损失函数停止了寻找 inf 并开始寻找局部最小值。非常感谢!
    • 太好了,很高兴听到。如果有帮助,请考虑按接受我的回答按钮
    • 我这样做了,由于是新手并且没有显示低声望 id
    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    相关资源
    最近更新 更多