[英]Unable to extract individual predictions after training a model
I have a model that reports around 0.67 accuracy on a testing dataset.我有一个 model 报告测试数据集的准确度约为 0.67。 However i would like to extract the individual predictions per sample to see how it performed on each.
但是,我想提取每个样本的单个预测,看看它在每个样本上的表现如何。 I read that i should run the logits layer in the session and would get an array of predictions per sample.
我读到我应该在 session 中运行 logits 层,并会得到每个样本的预测数组。 This does not seem to work, as it will just have a prediction for the first category for every sample in the verification set.
这似乎不起作用,因为它只会对验证集中的每个样本的第一个类别进行预测。
# predictions
logits = tf.layers.dense(flat_d, NUM_CLASSES, activation=tf.nn.relu)
# Cost function and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=labels))
# cost = -tf.reduce_sum(labels*tf.log(logits + 1e-10))
train_step = tf.train.AdamOptimizer(learning_rate_).minimize(cost)
# Accuracy
correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')
....
print("Initializing the model")
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for e in np.arange(num_epochs):
bx,by = load_batch(trainX,trainY,batch_size_)
sess.run(train_step,feed_dict={inputs:bx,labels:by,keep_prob:0.9,learning_rate_:learn_rate})
acc = sess.run(accuracy,feed_dict={inputs:testX,labels:testY,keep_prob:1.0,learning_rate_:learn_rate})
print("----------\nAccuracy:%.3f"%(acc))
# Extract individual precitions by class
print(sess.run(logits,feed_dict={inputs:testX,labels:testY,keep_prob:1.0}))
Here is the summary of the output:以下是output的总结:
Number of validation samples: 203
By category:
0: 34
1: 138
2: 31
print(sess.run(accuracy,feed_dict={inputs:testX,labels:testY,keep_prob:1.0,learning_rate_:learn_rate}))
Accuracy:0.680
print(sess.run(logits,feed_dict={inputs:testX,labels:testY,keep_prob:1.0}))
[[1.6453042 0. 0. ]
[1.454038 0. 0. ]
[1.6372575 0. 0. ]
[1.5505953 0. 0. ]
[1.6624011 0. 0. ]
[1.6376897 0. 0. ]
[1.5477558 0. 0. ]
[1.5303426 0. 0. ]
[1.3636262 0. 0. ]
[1.5397886 0. 0. ]
[1.8849531 0. 0. ]
.....
etc.
Any pointers of what i'm doing wrong, or how i could extract the predictions for each sample instead of just the overall accuracy?关于我做错了什么的任何指示,或者我如何提取每个样本的预测而不仅仅是整体准确性?
Thanks谢谢
The command, print(sess.run(logits,feed_dict={inputs:testX,labels:testY,keep_prob:1.0}))
prints the Probabilities
of each Class
.命令
print(sess.run(logits,feed_dict={inputs:testX,labels:testY,keep_prob:1.0}))
打印每个Class
的Probabilities
。
In your code, you are using NUM_CLASSES
and softmax_cross_entropy_with_logits
which suggests that your Machine Learning problem is Classification
with 3 Classes
.在您的代码中,您使用的是
NUM_CLASSES
和softmax_cross_entropy_with_logits
,这表明您的机器学习问题是具有 3 个Classes
的Classification
。
As of now, your Predictions are incorrect (截至目前,您的预测不正确(
[[1.6453042 0. 0. ]
[1.454038 0. 0. ]
) because you are using Activation
Function, ' Relu
' for Classification
. ) 因为您使用
Activation
Function, ' Relu
' 进行Classification
。
One correction which you need to make in your code for correct Predictions
is to replace ' relu
' with ' softmax
' ie, replace您需要在代码中为正确
Predictions
进行的一项更正是将“ relu
”替换为“ softmax
”,即替换
logits = tf.layers.dense(flat_d, NUM_CLASSES, activation=tf.nn.relu)
with和
logits = tf.layers.dense(flat_d, NUM_CLASSES, activation=tf.nn.softmax)
Then print(sess.run(logits,feed_dict={inputs:testX,labels:testY,keep_prob:1.0}))
will output some thing like,然后
print(sess.run(logits,feed_dict={inputs:testX,labels:testY,keep_prob:1.0}))
将 output 之类的东西,
[9.87792790e-01, 1.05240829e-02, 4.70216728e-05],
[8.80016625e-01, 1.13000005e-01, 5.14236337e-04]
with each value representing the Probability
corresponding to the respective Class
.每个值代表对应于相应
Class
的Probability
。
Please let me know if you face any other error and I will be Happy to help you.如果您遇到任何其他错误,请告诉我,我很乐意为您提供帮助。
Hope this helps.希望这可以帮助。 Happy Learning!
快乐学习!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.