Python Tensorflow:如何将混淆矩阵(tensor)显示为数组?
我已成功获得(7x7)的混淆矩阵。它是张量形式的 我想查看混淆矩阵。尝试过。eval和sess方法,但不起作用 我的代码:Python Tensorflow:如何将混淆矩阵(tensor)显示为数组?,python,tensorflow,confusion-matrix,tensor,Python,Tensorflow,Confusion Matrix,Tensor,我已成功获得(7x7)的混淆矩阵。它是张量形式的 我想查看混淆矩阵。尝试过。eval和sess方法,但不起作用 我的代码: n_classes = 7 prediction = neural_network(x) correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct, 'float')) con_mat = tf.confusion_matr
n_classes = 7
prediction = neural_network(x)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
con_mat = tf.confusion_matrix(labels=[0, 1, 2, 3, 4, 5, 6], predictions=correct, num_classes=n_classes, dtype=tf.int32, name=None)
print('Confusion Matrix: \n\n', tf.SparseTensor.eval(con_mat, feed_dict=None, session=None))
输出:
AttributeError: 'Tensor' object has no attribute 'indices'
神经网络:
weights = {
'out': tf.Variable(tf.truncated_normal([hidden_units, n_classes], dtype=tf.float32))
}
biases = {
'out': tf.Variable(tf.zeros([n_classes]))
}
x = tf.unstack(x, seq_len, 1)
# 3-layer LSTM with 128 units.
cell = rnn_cell_impl.MultiRNNCell([rnn_cell_impl.LSTMCell(hidden_units),
rnn_cell_impl.LSTMCell(hidden_units),
rnn_cell_impl.LSTMCell(hidden_units)])
outputs, states = rnn.static_rnn(cell, x, dtype=tf.float32)
output = tf.matmul(outputs[-1], weights['out']) + biases['out']
return output
您可以通过运行
con_mat = tf.confusion_matrix(labels=[0, 1, 2, 3, 4, 5, 6], predictions=correct, num_classes=n_classes, dtype=tf.int32, name=None)
with tf.Session():
print('Confusion Matrix: \n\n', tf.Tensor.eval(con_mat,feed_dict=None, session=None))
希望这有帮助 定义混淆矩阵(在神经网络中,但同样的方法适用于其他网络): 运行和打印矩阵:
with tf.Session() as sess:
test_confusion = sess.run(confusion, feed_dict={X_place:X,y_place:y})
print(test_confusion)
请在您的问题中添加一个。已完成编辑@MPF82在该代码示例中,缩进似乎是错误的(并且它可能很重要,尤其是在使用
with
语句时)。另外,您是否可以扩大示例,以包括创建sess
的位置?我从tensorflow中找到了一个函数来计算混淆矩阵,它是一个稀疏张量。编辑我的代码和错误@mrryHi,我尝试了你的代码,但它有以下错误:InvalidArgumentError(回溯请参见上文):形状[-1100,88]具有负维度[[Node:Placeholder=Placeholder[dtype=DT\u FLOAT,形状=[?,100,88],\u device=“/job:localhost/replica:0/task:0/cpu:0”]()]你能在你的问题中添加预测和n_类值吗?完成:)我使用的是3层LSTM@Bharath ShettyI,意思是以矩阵或numpy数组形式的变量correct
。或correct
的形状。变量correct是张量。。不是矩阵或numpy数组。张量(“等于:0”,shape=(?,),dtype=bool)X_位和y_位是什么?
with tf.Session() as sess:
test_confusion = sess.run(confusion, feed_dict={X_place:X,y_place:y})
print(test_confusion)