2

Ich habe Multi-Class-Klassifizierung RNN mit und hier ist mein Haupt-Code für RNN:Tensorflow Verwirrung Matrix One-Hot-Code

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)) 

Ich habe alle Eingaben in 6 Klassen zu klassifizieren und jede der Klassen ist bestehend aus einem Hot Code-Label als folgen:

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] 

Das Problem ist, ich nicht Konfusionsmatrix mit tf.confusion_matrix() Funktion drucken.

Gibt es eine Möglichkeit, Konfusionsmatrix mit diesen Etiketten zu drucken?

Wenn nicht, wie kann ich One-Hot-Code nur dann in Integer-Indizes konvertieren, wenn ich eine Konfusionsmatrix drucken muss?

Antwort

1

Sie können keine Konfusionsmatrix unter Verwendung einer Hot-Vektoren als Eingangsparameter von labels und predictions erzeugen. Sie müssen ihm einen 1D-Tensor geben, der Ihre Etiketten direkt enthält.

Um die ein hot-Vektor zu normalen Etikett, nutzen argmax Funktion zu konvertieren:

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()) 

Ausgabe::

[[0 3] 
[4 3]] 

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

Danach können Sie Ihre confusion_matrix diesen Druck mögen

Verwandte Themen