【问题标题】:loss function is returning nan tensorflow损失函数正在返回 nan tensorflow
【发布时间】:2017-12-14 20:12:20
【问题描述】:

我在这里编写了一个简单的 tensorflow 程序,它读取特征列表并尝试预测类别。

with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        for epoch in range (hm_epochs):
            epoch_loss = 0
            itere = int(X_train.shape[0]/batch_size)
            last = 0
            add = 1
            for start in range(itere):
                x_train_epoch = X_train[last: ((start + add) * batch_size),:]
                y_train_epoch = y_1Hot_train.eval()[last: ((start + add) * batch_size),:]
#                 print("shape of x", x_train_epoch.shape, "shape of y", y_train_epoch.shape)
                _, c = sess.run([optimizer, cost], feed_dict = {x: x_train_epoch, y: y_train_epoch})
                epoch_loss += c
                last = start * batch_size
                add = 0
            print('Epoch', epoch, 'completed out of', hm_epochs, 'loss', epoch_loss )
        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print('Accuracy:', accuracy.eval( {x: X_test, y: y_1Hot_test.eval() }))

链接:https://gist.github.com/makark/79af6ca53ca27d51abb1d87c9b9bac07

数据文件:https://gist.github.com/makark/eb859f50237edb9343f3ca32aeb3be2b

但是,当我运行我的代码时,我一直在丢失“nan”。我不确定发生了什么...任何帮助将不胜感激!

WARNING:tensorflow:From <ipython-input-149-0164f4af7d52>:46: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
Epoch 0 completed out of 10 loss nan
Epoch 1 completed out of 10 loss nan
Epoch 2 completed out of 10 loss nan
Epoch 3 completed out of 10 loss nan
Epoch 4 completed out of 10 loss nan
Epoch 5 completed out of 10 loss nan
Epoch 6 completed out of 10 loss nan
Epoch 7 completed out of 10 loss nan
Epoch 8 completed out of 10 loss nan
Epoch 9 completed out of 10 loss nan
Accuracy: 0.589097

【问题讨论】:

  • 请编写您的代码(相关部分),而不是提供指向它的链接

标签: python machine-learning tensorflow deep-learning


【解决方案1】:
  • 输入有 nan,请通过 X[np.isnan(X)] = 0 修复它。
  • 输入未缩放,请使用 sklearn 的 StandardScaler 标准化您的输入。

  • 在 random_normal 中使用 stddev 将权重设置为较小的初始值。

  • 修复输出计算错误:output = tf.add(tf.matmul(l3, output_layer['weights']),output_layer['biases'] )

【讨论】:

  • 非常感谢!这些步骤让我的准确率从 58.4% 提高到了 71.5%!对于进一步提高准确性,您还有其他建议吗?
  • 在上面的链接中更新你的代码,我可以看看。
  • 嗨 Vijay,我想知道您是否有机会查看新代码?在这里和那里做一些小的调整,我的准确率最高达到了 76%,但从那以后就不能再高了……很想听听你的想法。
  • 即使我得到了 75% 左右,我认为您需要检查您的输入功能,看看您是否可以改进它们。
猜你喜欢
  • 2021-03-06
  • 2021-07-21
  • 1970-01-01
  • 2017-08-04
  • 2019-02-03
  • 2017-04-10
  • 2017-10-12
  • 2021-09-06
  • 2017-02-20
相关资源
最近更新 更多