损失函数正在返回纳米张量流

时间:2017-07-10 23:27:46

标签: python machine-learning tensorflow deep-learning

我在这里编写了一个简单的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

1 个答案:

答案 0 :(得分:0)

  • 输入有nan,并按X[np.isnan(X)] = 0修复。
  • 输入未缩放,请使用sklearn&#39; StandardScaler来规范化您的输入。

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

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