2

나는 RNN 내 주요 코드 여기에 RNN를 사용하여 다중 클래스 분류를 가지고 : 클래스의 각Tensorflow 혼란 매트릭스

def RNN(x, weights, biases): 
    x = tf.unstack(x, input_size, 1) 
    lstm_cell = rnn.BasicLSTMCell(num_unit, forget_bias=1.0, state_is_tuple=True) 
    stacked_lstm = rnn.MultiRNNCell([lstm_cell]*lstm_size, state_is_tuple=True) 
    outputs, states = tf.nn.static_rnn(stacked_lstm, x, dtype=tf.float32) 

    return tf.matmul(outputs[-1], weights) + biases 

logits = RNN(X, weights, biases) 
prediction = tf.nn.softmax(logits) 

cost =tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y)) 
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) 
train_op = optimizer.minimize(cost) 

correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1)) 
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) 

내가 6 개 클래스에 모든 입력을 분류해야하고 있습니다 문제는 내가 tf.confusion_matrix() 기능을 사용하여 혼동 행렬을 인쇄 할 수 있습니다

happy = [1, 0, 0, 0, 0, 0] 
angry = [0, 1, 0, 0, 0, 0] 
neutral = [0, 0, 1, 0, 0, 0] 
excited = [0, 0, 0, 1, 0, 0] 
embarrassed = [0, 0, 0, 0, 1, 0] 
sad = [0, 0, 0, 0, 0, 1] 

: 추적으로 한 핫 코드 라벨로 구성.

이러한 레이블을 사용하여 혼동 행렬을 인쇄하는 방법이 있습니까?

그렇지 않은 경우 어떻게해야 혼성 행렬을 인쇄해야 할 때만 하나의 핫 코드를 정수 인덱스로 변환 할 수 있습니까?

답변

1

labelspredictions의 입력 매개 변수로 단일 핫 벡터를 사용하여 혼동 행렬을 생성 할 수 없습니다. 레이블을 직접 포함하는 1D 텐서를 공급해야합니다.

import tensorflow as tf 

num_classes = 2 
prediction_arr = tf.constant([1, 1, 1, 1, 0, 0, 0, 0, 1, 1]) 
labels_arr  = tf.constant([0, 1, 1, 1, 1, 1, 1, 1, 0, 0]) 

confusion_matrix = tf.confusion_matrix(labels_arr, prediction_arr, num_classes) 
with tf.Session() as sess: 
    print(confusion_matrix.eval()) 

출력 :

[[0 3] 
[4 3]] 
당신이 당신의 confusion_matrix과 같이 인쇄 할 수 있습니다 그 후

label = tf.argmax(one_hot_tensor, axis = 1) 

:

argmax 기능을 활용, 일반 라벨에 핫 벡터를 변환하려면