Tensorflow神经网络预测始终是相同的 [英] Tensorflow neural network prediction is always the same

查看:281
本文介绍了Tensorflow神经网络预测始终是相同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个深层的CNN,可以为3d图像中的每个像素预测介于"0"和"2"之间的标签.我已经在每个像素都标记为"1"的图像上训练了模型.因此,在测试模型时,我相信每个预测都应该为"1".取而代之的是,模型仅预测"0".

I have a deep CNN that predicts a label between "0" and "2" for every pixel in a 3d image. I have trained the model on an image where every pixel is labeled "1". Therefore, when testing the model, I believe every prediction should be "1". Instead the model only predicts "0".

这是整个模型的存储库: https://github.com/dhasl002/Research-深度学习.

Here is the repository for the whole model: https://github.com/dhasl002/Research-DeepLearning.

由于代码几乎为300行,因此我将仅在下面包含相关代码.

Since the code is almost 300 lines, I will include only the relevant code below.

 x = tf.placeholder(tf.float32, shape=[None, 7168])
 y_ = tf.placeholder(tf.float32, shape=[None, 7168, 3])

 W_final = weight_variable([7168,7168,3])
 b_final = bias_variable([7168,3])

 #"final" is the result of the many convolutions
 final_conv = tf.tensordot(final, W_final, axes=[[1], [1]]) + b_final

 cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=final_conv))
 train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
 correct_prediction = tf.equal(tf.argmax(final_conv, 2), tf.argmax(y_, 2))
 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

 #a is a threshold associate with each pixel, b is the label of each pixel
 a = np.zeros((1,7168),dtype = float)
 b = np.zeros((1,7168, 3), dtype = float)

 #this is a little simplified for clarity of reader
 #TRAINING
 for line in inputFile:
   thresh, label = line.strip().split(",")
   a[0][it] = thresh
   b[0][it][label] = 1
 train_step.run(feed_dict={x: a, y_: b, keep_prob: .5})

 #TESTING
 for line in inputFile:
   thresh, label = line.strip().split(",")
   a[0][it] = thresh
   b[0][it][label] = 1
 temp = sess.run(tf.argmax(final_conv,2), feed_dict={x: a})

我认为,从最后一行开始的温度"应保持正确的预测(7168个标签-每个像素一个). 为什么仅在带有"1"标签的图像上实际训练"temp"时,总会产生所有"0"标签?

I believe that "temp" from the last line should hold the correct predictions (7168 labels - one per pixel). Why does "temp" always result in all "0" labels when it is actually trained on images only with "1" labels?

推荐答案

您提供的数据不仅包含1标签,而且还偶尔包含2(您可以浏览文本文件或简单地打印label值以查看此内容).训练常数函数不仅与您的想法背道而驰,而且还破坏了一次性编码,从而破坏了整个算法.

The data that you've provided contains not only 1 labels, but occasional 2 as well (you can skim through the text files or simply print the label values to see this). Not only does it contradict your idea to train a constant function, it also breaks the one-hot encoding and, thus, the whole algorithm.

这是脚本的摘录:

a = np.zeros((1,N*M*P),dtype = float)
b = np.zeros((1,N*M*P, 3), dtype = float)
[...]

with tf.Session() as sess:
   sess.run(tf.global_variables_initializer())
   parent = "..."
   with open(parent) as inf1:
     next(inf1)
     for line5 in inf1:
       line1, maxNum = line5.strip().split(",")
       path = "..."
       num = 0
       while num < maxNum:
         it = 0
         with open(path + str(num) + ".txt") as inf:
           next(inf)
           num = num + 1
           for line in inf:
             [...]
             a[0][it] = thresh
             b[0][it][label] = 1
             it = it + 1

看看您的代码,b应该是一个热门向量.但是请注意,只有在定义变量后,它才会归零.之后,将其分配给不同索引处的1. while循环的后续迭代更新相同的b数组,因此最终在批处理的后续行中包含多个1. 交叉熵损失期望有效的概率分布,因此与数据,其输出变得完全没有意义:

Looking at your code, b is supposed to be a one-hot vector. But note that it's zeroed only when the variable is defined. After that it's assigned to 1 at different indices. The later iterations of the while loop update the same b array, hence it ends up containing several 1 in the later rows of the batch. The cross-entropy loss expects a valid probability distribution, hence with your data its output becomes completely meaningless:

每行labels[i]必须是有效的概率分布.

Each row labels[i] must be a valid probability distribution.

摘要:您进行数据处理的方式过于复杂,因此容易出错.尝试简化输入文件的组织方式,以便可以将其读入numpy数组(或pandas数据框)中,并输入到会话中.

Summary: the way you do data processing is too complicated and, as a result, error-prone. Try to organize your input files simpler, so that it could be read into a numpy array (or pandas dataframe) and fed to the session.

这篇关于Tensorflow神经网络预测始终是相同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆