数据文件: https://gist.github.com/makark/eb859f50237edb9343f3ca32aeb3be2b 但是,当我运行我的代码时,我总是感到难".我不确定发生了什么...任何帮助将不胜感激! 警告:tensorflow:从< ipython-input-149-0164f4af7d52>:46:不推荐使用initialize_all_variables(来自tensorflow.python.ops.variables),并将在2017-03-02之后删除.更新说明:改用`tf.global_variables_initializer`.纪元0完成了10损失nan时代1完成了10损失nan时代2完成了10输nan时代3完成了10输nan时代4完成了10次亏损nan时代5完成了10失落nan时代6完成了10失落nan时代7完成了10失落nan时代8完成了10输nan时代9完成了10失落nan精度:0.589097 解决方案 输入具有nan,请通过 X [np.isnan(X)] = 0 对其进行修复. 输入未缩放,请使用sklearn的 StandardScaler 标准化输入. 使用random_normal中的stddev将权重设置为较小的初始值. 修复输出计算中的错误: output = tf.add(tf.matmul(l3,output_layer ['weights']),output_layer ['biases']).I've written a simple tensorflow program here that reads in a feature list and tries to predict the class.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() }))Link: https://gist.github.com/makark/79af6ca53ca27d51abb1d87c9b9bac07Data file: https://gist.github.com/makark/eb859f50237edb9343f3ca32aeb3be2bHowever, when I run my code I keep getting a loss of "nan". I'm not sure what is going on... any help would be much appreciated!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 nanEpoch 1 completed out of 10 loss nanEpoch 2 completed out of 10 loss nanEpoch 3 completed out of 10 loss nanEpoch 4 completed out of 10 loss nanEpoch 5 completed out of 10 loss nanEpoch 6 completed out of 10 loss nanEpoch 7 completed out of 10 loss nanEpoch 8 completed out of 10 loss nanEpoch 9 completed out of 10 loss nanAccuracy: 0.589097 解决方案the inputs have nan's, fix it by X[np.isnan(X)] = 0.the inputs are not scaled, use sklearn's StandardScaler to normalize your inputs.Set the weights to a small initial value use stddev in random_normal.Fix the bug in calculation of output: output = tf.add(tf.matmul(l3, output_layer['weights']),output_layer['biases'] ) . 这篇关于损失函数返回nan tensorflow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 13:17