简体   繁体   English

tensorflow-tf.confusion_matrix()引发错误ValueError:Shape(2,2048,2)必须具有等级2

[英]tensorflow - tf.confusion_matrix() throws error ValueError: Shape (2, 2048, 2) must have rank 2

I try to determine the confusion_matrix of my neural network model which is written in python by using google tensorflow. 我试图确定我的神经网络模型的confusion_matrix,它是使用google tensorflow用python编写的。 By using this piece of code: 通过使用这段代码:

cm = tf.zeros(shape=[2,2], dtype=tf.int32)

for i in range(0, validation_data.shape[0], batch_size_validation):
    batched_val_data = np.array(validation_data[i:i+batch_size_validation, :, :], dtype='float')
    batched_val_labels = np.array(validation_labels[i:i+batch_size_validation, :], dtype='float')

    batched_val_data = batched_val_data.reshape((-1, n_chunks, chunk_size))            

    _acc, _c, _p = sess.run([accuracy, correct, pred], feed_dict=({x:batched_val_data, y:batched_val_labels}))

    #batched_val_labels.shape ==> (2048, 2)
    #_p.shape                 ==> (2048, 2)
    #this piece of code throws the error!
    cm = tf.confusion_matrix(labels=batched_val_labels, predictions=_p)

I get the following error: ValueError: Shape (2, 2048, 2) must have rank 2 我收到以下错误: ValueError:形状(2、2048、2)必须具有等级2

At least you should know that the array for the validation labels batched_val_labels is an one hot array . 至少您应该知道验证标签batched_val_labels数组一个热数组 Can someone help me pls? 有人可以帮我吗? Thanks in advance! 提前致谢!

The problem was that I am using an one hot array . 问题是我正在使用一个热阵列 By following this instruction: Tensorflow confusion matrix using one-hot code 通过遵循以下指令: 使用一键编码的Tensorflow混淆矩阵

I changed this piece of code: 我更改了这段代码:

cm = tf.confusion_matrix(labels=batched_val_labels, predictions=_p)

into: 成:

cm = tf.confusion_matrix(labels=tf.argmax(batched_val_labels, 1), predictions=tf.argmax(_p, 1))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tensorflow LSTM抛出ValueError:Shape()的等级必须至少为2 - Tensorflow LSTM throws ValueError: Shape () must have rank at least 2 tf.confusion_matrix和InvalidArgumentError - tf.confusion_matrix and InvalidArgumentError TensorFlow: Value Error Shape and Rank Do Not Match: ValueError: Shape (?, 128, 128, 2) must have rank 2 - TensorFlow: Value Error Shape and Rank Do Not Match: ValueError: Shape (?, 128, 128, 2) must have rank 2 Tensorflow错误“形状Tensorshape()必须具有等级1” - Tensorflow error “shape Tensorshape() must have rank 1” TensorFlow 推荐者 - ValueError:形状必须为 2 级,但为 3 级 - TensorFlow Recommenders - ValueError: Shape must be rank 2 but is rank 3 Tensorflow:ValueError:Shape必须是等级2,但是等级3 - Tensorflow : ValueError: Shape must be rank 2 but is rank 3 Tensorflow: ValueError: Shape must be rank 4 but is rank 5 - Tensorflow: ValueError: Shape must be rank 4 but is rank 5 TensorFlow - 切片张量导致:ValueError: Shape (16491,) must have rank 3 - TensorFlow - Slicing tensor results in: ValueError: Shape (16491,) must have rank 3 使用LSTM RNN在tensorflow中进行分类,ValueError:Shape(1、10、5)必须具有等级2 - classification with LSTM RNN in tensorflow, ValueError: Shape (1, 10, 5) must have rank 2 如何在tensorflow中提取图层或ValueError:Shape(?,4096)必须具有等级1 - How extract layer in tensorflow or ValueError: Shape (?, 4096) must have rank 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM