了解用于二进制分类的张量流混淆矩阵

Understanding a tensorflow confusion matrix for binary classification

我有一个二进制分类问题,我正试图在 tensorflow 中解决。我正在使用一个简单的多层感知器。我试图理解我得到的混淆矩阵。示例输出为:

 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 confusion matrix,您对其解释方式的假设是正确的。

例如:

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

接下来,on 被 AWAKE 解释为 [0, 1] 或 [1, 0] 取决于你在对它进行 one-hot 编码之前分配给 AWAKE 的标签(你没有附上那部分代码).例如,如果您将 AWAKE 指定为 0,并且由于总共只有两个 类,one-hot 编码将为您提供 [1, 0].

希望这个回答对您有所帮助!