简体   繁体   English

Tensorflow Keras 维度不等于多个标签的错误

[英]Tensorflow Keras dimensions not equal error for multiple labels

I am trying to predict from multidimensional input to multi-dimensional output using Tensorflow 2.0.0's Keras, with the Tensorflow Datasets API.我正在尝试使用 Tensorflow 2.0.0 的 Keras 和 Tensorflow Datasets API 从多维输入预测到多维输出。

I am using tensorflow 2.0.0 and tensorflow-datasets 1.3.0 on python 3.6.9 .我在python 3.6.9上使用tensorflow 2.0.0tensorflow-datasets 1.3.0

Below is my example code, I also reproduced it on [a Colab notebook] ( https://colab.research.google.com/drive/1WMccCeLOrQU4k5D2noC4S_5rMe7-krEk ) which you can run:下面是我的示例代码,我还在 [a Colab notebook] ( https://colab.research.google.com/drive/1WMccCeLOrQU4k5D2noC4S_5rMe7-krEk ) 上复制了它,您可以运行:

import tensorflow as tf
data = [[1,2],[11,22]]
label = [[3,4,5], [33,44,55]]
dataset = tf.data.Dataset.from_tensor_slices((data,label))
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(3))
model.compile('adam','mse',metrics=['mse'])
model.fit(dataset, validation_data=dataset)

In this example code I am trying to predict [1,2]->[3,4,5] and [11,22]->[33,44,55] .在这个示例代码中,我试图预测[1,2]->[3,4,5][11,22]->[33,44,55] However I get the error:但是我收到错误:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/tensorflow-2.0.0/python3.6/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1609   try:
-> 1610     c_op = c_api.TF_FinishOperation(op_desc)
   1611   except errors.InvalidArgumentError as e:

InvalidArgumentError: Dimensions must be equal, but are 2 and 3 for 'loss/output_1_loss/SquaredDifference' (op: 'SquaredDifference') with input shapes: [2,3], [3,1].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
29 frames
/tensorflow-2.0.0/python3.6/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1611   except errors.InvalidArgumentError as e:
   1612     # Convert to ValueError for backwards compatibility.
-> 1613     raise ValueError(str(e))
   1614 
   1615   return c_op

ValueError: Dimensions must be equal, but are 2 and 3 for 'loss/output_1_loss/SquaredDifference' (op: 'SquaredDifference') with input shapes: [2,3], [3,1].

As per thushv89's comment to the question, using batch on the dataset fixes the code.根据sohv89对该问题的评论,在数据集上使用批处理修复了代码。 The original code was more complicated than this but using batch fixed it.原始代码比这更复杂,但使用批处理修复了它。

import tensorflow as tf
data = [[1,2],[11,22]]
label = [[3,4,5], [33,44,55]]
dataset = tf.data.Dataset.from_tensor_slices((data,label)).batch(2)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(3))
model.compile('adam','mse',metrics=['mse'])
model.fit(dataset, validation_data=dataset)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM