Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 理解二元分类的张量流混淆矩阵_Python_Tensorflow_Confusion Matrix - Fatal编程技术网

Python 理解二元分类的张量流混淆矩阵

Python 理解二元分类的张量流混淆矩阵,python,tensorflow,confusion-matrix,Python,Tensorflow,Confusion Matrix,我在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 Optimizat

我在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]]
标签处于唤醒状态,但未处于唤醒状态。看起来,通过一次热编码,我将[1,0]设为唤醒状态,[0,1]设为未唤醒状态(我只是将数组保存到一个文件中并目视检查)。
我如何解释混淆矩阵? 我相信这个结果:

 CM=    
 [[797  260]  
 [   0 1071]]
可解释为:

             Pred: 0 | Pred: 1
 Actual 0:    797    |   260                
 Actual 1:    0      |   1071
[1,0](唤醒的一个热编码)是否成为混淆矩阵中的第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 )
关于这个问题,你关于如何解释它的假设是正确的

例如:

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 WAKE被解释为[0,1]或[1,0]取决于您在对其进行热编码之前为WAKE分配了什么标签(您没有包含该部分代码)。例如,如果您将AWAKE指定为0,并且由于总共只有两个类,一个热编码将为您提供[1,0]


希望这个答案对你有帮助

我打印出了上面更新代码中显示的
integer\u encoded
。看起来AWAKE被指定为0,然后[1,0]被指定为一个热编码。NOT_AWAKE的值为1,然后为[0,1]。所以我现在理解了混乱矩阵。“实际0”和“Pred:0”处于唤醒状态。非常感谢。