[英]Understanding a tensorflow confusion matrix for binary classification
我正在尝试在张量流中解决一个二进制分类问题。 我正在使用一个简单的多层感知器。 我试图了解我所得到的混乱矩阵。 示例输出为:
Epoch: 0001 cost=882.103631592
Epoch: 0002 cost=496.739675903
Epoch: 0003 cost=403.711282349
Epoch: 0004 cost=389.798379517
Epoch: 0005 cost=324.857388306
Optimization Finished!
Accuracy: 0.889306
CM=
[[797 260]
[ 0 1071]]
标签为AWAKE和NOT_AWAKE。 看起来像是从一键编码开始,我将[1,0]设为AWAKE,将[0,1]设为NOT_AWAKE(我只是将数组保存到文件中并进行直观检查)。
如何解释混淆矩阵? 我相信这个输出:
CM=
[[797 260]
[ 0 1071]]
可以解释为:
Pred: 0 | Pred: 1
Actual 0: 797 | 260
Actual 1: 0 | 1071
[1,0](一种用于AWAKE的热编码)是否会成为混淆矩阵中的第1行? 下面是大多数运行mlp的代码。
# Parameters
learning_rate = 0.00001
training_epochs = 4
display_step = 1
keep_prob_training = 0.75
# Network Parameters
n_hidden_1 = 2048 # 1st layer number of neurons
n_hidden_2 = 2048 # 2nd layer number of neurons
n_input = 9 # channels
n_classes = 2 # total classes
print( "Some hyper params: training_epochs = %s,learning_rate = %f,keep_prob_training = %s, n_hidden_1 = %s,n_hidden_2 = %s" % ( training_epochs, learning_rate, keep_prob_training, n_hidden_1, n_hidden_2 ) )
print ( "Misc shape info: X_train.shape = %s, X_test.shape = %s, y_train.shape = %s, y_test.shape = %s" % ( np.shape( X_train ), np.shape( X_test ), np.shape( y_train ), np.shape( y_test ) ) )
# tf Graph input
X = tf.placeholder( "float", [None, n_input] )
Y = tf.placeholder( "float", [None, n_classes] )
keep_prob = tf.placeholder( tf.float32 )
# placeholder for confusion matrix
y_ = tf.placeholder( tf.float32, shape = [None, 2] )
# Store layers weight & bias
weights = {
'h1': tf.Variable( tf.random_normal( [n_input, n_hidden_1] ) ),
'h2': tf.Variable( tf.random_normal( [n_hidden_1, n_hidden_2] ) ),
'out': tf.Variable( tf.random_normal( [n_hidden_2, n_classes] ) )
}
biases = {
'b1': tf.Variable( tf.random_normal( [n_hidden_1] ) ),
'b2': tf.Variable( tf.random_normal( [n_hidden_2] ) ),
'out': tf.Variable( tf.random_normal( [n_classes] ) )
}
# Create model
def multilayer_perceptron( x ):
# Hidden fully connected layer
layer_1 = tf.add( tf.matmul( x, weights['h1'] ), biases['b1'] )
layer_1 = tf.nn.relu( layer_1 )
# apply DropOut to hidden layer
drop_out_1 = tf.nn.dropout( layer_1, keep_prob )
# Hidden fully connected layer
layer_2 = tf.add( tf.matmul( drop_out_1, weights['h2'] ), biases['b2'] )
layer_2 = tf.nn.relu( layer_2 )
drop_out_2 = tf.nn.dropout( layer_2, keep_prob )
# Output fully connected layer with a neuron for each class
out_layer = tf.matmul( drop_out_2, weights['out'] ) + biases['out']
return out_layer
# Construct model
logits = multilayer_perceptron( X )
# obtain cm after training
confusion_matrix_tf = tf.confusion_matrix( tf.argmax( logits, 1 ), tf.argmax( y_, 1 ) )
# Define loss and optimizer
loss_op = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits( logits = logits, labels = Y ) )
optimizer = tf.train.AdamOptimizer( learning_rate )
train_op = optimizer.minimize( loss_op )
# Initializing the variables
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run( init )
# Training cycle
for epoch in range( training_epochs ):
avg_cost = 0.
# Run optimization op (backprop) and cost op (to get loss value)
_, c = sess.run( [train_op, loss_op], feed_dict = {X: X_train, Y: y_train, keep_prob : keep_prob_training} )
# Compute average loss
# Display logs per epoch step
if epoch % display_step == 0:
print( "Epoch:", '%04d' % ( epoch + 1 ) , "cost={:.9f}".format( c ) )
print( "Optimization Finished!" )
# Test model
pred = tf.nn.softmax( logits ) # Apply softmax to logits
correct_prediction = tf.equal( tf.argmax( pred, 1 ), tf.argmax( Y, 1 ) )
# Calculate accuracy
accuracy = tf.reduce_mean( tf.cast( correct_prediction, "float" ) )
print( "Accuracy:", accuracy.eval( {X: X_test, Y: y_test, keep_prob : 1.0} ) )
cm = confusion_matrix_tf.eval( feed_dict = {X: X_train, y_: y_train, keep_prob: 1.0} )
print( "CM=\n", cm )
这是我编码标签的方式:
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform( df_combined['Label'] )
# binary encode
onehot_encoder = OneHotEncoder( sparse = False )
integer_encoded = integer_encoded.reshape( len( integer_encoded ), 1 )
all_y = onehot_encoder.fit_transform( integer_encoded )
关于Tensorflow混淆矩阵 ,您对其解释的假设是正确的。
例如:
number of classes = 2
Predicted labels = [0, 1, 1, 1, 0, 0, 0, 0, 1, 1]
Actual labels = [0, 1, 1, 1, 1, 1, 1, 1, 0, 0]
因此您的Tensorflow混淆矩阵将是:
Pred: 0 | Pred: 1
Actual 0: 1 | 2
Actual 1: 4 | 3
接下来,将AWAKE解释为[0,1]或[1,0]取决于您对AWAKE进行一次热编码之前为其分配了什么标签(您没有封装该部分代码)。 例如,如果您将AWAKE分配为0,并且由于总共只有两个类,则一键编码将为您提供[1,0]。
希望这个答案对您有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.