简体   繁体   English

训练 model 后无法提取单个预测

[英]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}))打印每个ClassProbabilities

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_CLASSESsoftmax_cross_entropy_with_logits ,这表明您的机器学习问题是具有 3 个ClassesClassification

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 .每个值代表对应于相应ClassProbability

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.

相关问题 XGBClassifier:训练、保存和加载 model 后的错误预测 - XGBClassifier: Bad predictions after training, saving and loading a model 训练时实时绘制模型预测 - Real time plotting the model predictions while training 哪个参数配置是 Keras 在为多个 epoch 训练 model 后默认使用预测 - Which parameter configuration is Keras using by default for predictions after training a model for multiple epochs 使用 scikit-learn 训练线性回归 model 后,如何对原始数据集中不存在的新数据点进行预测? - After training the Linear Regression model using scikit-learn , How to do predictions for new data points which are not there in original data set? 受过训练的keras模型比预测训练要慢得多 - Trained keras model much slower making its predictions than in training 为什么我的神经网络在训练后会做出如此不准确的预测? - Why does my neural network make such inaccurate predictions after training? 如何在训练连体网络后生成测试三元组数据集的预测 - How to generate predictions on testing triplets dataset after training Siamese network 如何明确地找到构造的Tensorflow模型并提取模型预测 - How to find the constructed Tensorflow model explicitly and extract model predictions 保存 model 后得到错误的预测 - Getting Wrong Predictions after saving the model 使用model.getVars()之后如何将变量提取为单个数组? - How do you extract variables as individual arrays after using model.getVars()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM