2018-04-13 07:14

# softmax分类器，分类概率都是nan是怎么回事？

import numpy as np
import tensorflow as tf
x=tf.placeholder("float", [None,115])
y=tf.placeholder("float", [None,2])
W=tf.Variable(tf.zeros([115,2]))
b=tf.Variable(tf.zeros([2]))

actv= tf.nn.softmax(tf.matmul(x,W)+b)
####网上查到说下面这个loss函数中如果是log（0）的话就会有nan，但那应该连训练结果都没有才对吧？我现在能得到训练结果，但是结果都是nan怎么办？
cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1))
learning_rate=0.01
pred=tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))
accr=tf.reduce_mean(tf.cast(pred,"float"))
init=tf.global_variables_initializer()
sess=tf.InteractiveSession()
sess=tf.Session()
sess.run(init)
training_lenth=len(G)####(回测长度）
training_epochs =50 #训练次数
batch_size = len(G) #每次迭代用多少样本(用全套）
##display_step = 5 #展示
print('Down')

lenth=2
for epoch in range(training_epochs):
avg_cost=0
num_batch=int(len(G)/batch_size)
for i in range((lenth-1),lenth):
batch_xs=np.array(G[i])
batch_ys=np.array(F[i])
sess.run(optm, feed_dict={x: batch_xs, y: batch_ys})
feeds={x:batch_xs, y: batch_ys}
avg_cost += sess.run (cost, feed_dict=feeds)/(num_batch*lenth)
feeds_train = {x: batch_xs, y: batch_ys}
feeds_test = {x: G[i+1], y: F[i+1]}
train_acc = sess.run(accr, feed_dict=feeds_train) #feed_dict 针对place holder占位
test_acc = sess.run(accr,feed_dict=feeds_test)
result=sess.run(actv,feed_dict=feeds_test)

result=sess.run(actv,feed_dict=feeds_train)
print (train_acc)#
print(test_acc)
train_acc
a=sess.run([accr,actv],feed_dict=feeds_test)
print(a)

0.930233
0.465116
[0.46511629, array([[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan],
[ nan, nan]], dtype=float32)]

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 3条回答默认 最新

• roguesir 2018-04-13 07:39
已采纳

你的测试准确率有问题，还不如随机分类的准确率，你在看一下。
出现nan的原因可能是：真是label为[1,0]，预测的label是[0,1]，这样反向传播的weight和bias会无穷大，可能的解决办法：
1、对输入数据进行归一化处理，如将输入的图片数据除以255将其转化成0-1之间的数据;
2、对于层数较多的情况，各层都做batch_nomorlization;
3、对设置Weights权重使用tf.truncated_normal(0, 0.01, [3,3,1,64])生成，同时值的均值为0，方差要小一些;
4、激活函数可以使用tanh;
5、减小学习率lr。

点赞 评论
• weixin_41138872 2018-04-16 01:01

应该不是概率太低的原因啊，我就编了一层神经网络模型，下面这个例子就是，训练和测试概率都90%多，给出的结果也还是nan
print(test_acc)
print(train_acc)

result=sess.run(actv,feed_dict=feeds_test)
print(result)

0.976744
0.906977
[[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]
[ nan nan]]

点赞 评论
• weixin_53132257 2021-03-02 12:48

请问博主这个事解决了吗 我也遇到了相同的问题

点赞 评论