【问题标题】:tensorflow - tf.confusion_matrix() throws error ValueError: Shape (2, 2048, 2) must have rank 2tensorflow - tf.confusion_matrix() 抛出错误 ValueError: Shape (2, 2048, 2) must have rank 2
【发布时间】:2018-10-20 20:21:43
【问题描述】:

我尝试确定我的神经网络模型的混淆矩阵 是使用 google tensorflow 用 python 编写的。 通过使用这段代码:

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)

我收到以下错误: ValueError: Shape (2, 2048, 2) 必须有等级 2

至少您应该知道验证标签 batched_val_labels 的数组是一个one hot 数组。 有人可以帮我吗?提前致谢!

【问题讨论】:

    标签: python python-3.x tensorflow confusion-matrix


    【解决方案1】:

    问题是我使用的是一个热阵列。 按照以下说明操作:Tensorflow confusion matrix using one-hot code

    我修改了这段代码:

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

    进入:

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

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-16
      • 2019-07-15
      • 2017-02-04
      • 1970-01-01
      相关资源
      最近更新 更多