我有一个二元分类问题,我试图解决张量流问题。我使用的是简单的多层感知器。我试图理解我得到的混乱矩阵。示例输出为:
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 )
答案 0 :(得分:0)
关于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
接下来,将AWAKE解释为[0,1]或[1,0]取决于您在对其进行单热编码之前为AWAKE分配的标签(您没有附上该部分代码)。例如,如果您已将AWAKE指定为0,并且因为只有两个类,则单热编码将为您提供[1,0]。
希望这个答案可以帮到你!