Keras model.fit的响应:应定义“稠密”输入的最后一个维度。没有找到`

Keras model.fit的响应:应定义“稠密”输入的最后一个维度。没有找到`,keras,deep-learning,Keras,Deep Learning,我正在使用TensorFlow 2.0和Keras自学深度学习方法。我选择的方案是根据房屋的总电量读数来计算微波炉是否工作。我用一种简单的方式更好地解释了这一点 我的目标是复制colab笔记本中所示图表上的学习模型。笔记本解释了我的输入和目标数据 我希望能够在我在图层中设置的基础上运行model.fit。但是,我得到的应该定义密集输入的最后一个维度。找到无 我一定是误解了设置输入/输出形状 我尝试过各种整形。但是,我无法理解如何正确设置层的张量 以下是colab笔记本中的相关代码: model

我正在使用TensorFlow 2.0和Keras自学深度学习方法。我选择的方案是根据房屋的总电量读数来计算微波炉是否工作。我用一种简单的方式更好地解释了这一点

我的目标是复制colab笔记本中所示图表上的学习模型。笔记本解释了我的输入和目标数据

我希望能够在我在图层中设置的基础上运行model.fit。但是,我得到的
应该定义
密集
输入的最后一个维度。找到

我一定是误解了设置输入/输出形状

我尝试过各种整形。但是,我无法理解如何正确设置层的张量

以下是colab笔记本中的相关代码:


model = Sequential()
model.add(Reshape((-1,599,1)))
model.add(Conv2D(filters=30,kernel_size=(10,1),strides=(1,1),padding='same',activation='relu',input_shape=(599,1)))
model.add(Conv2D(filters=30,kernel_size=(8,1),strides=(1,1),padding='same',activation='relu'))
model.add(Conv2D(filters=40,kernel_size=(6,1),strides=(1,1),padding='same',activation='relu'))
model.add(Conv2D(filters=50,kernel_size=(5,1),strides=(1,1),padding='same',activation='relu'))
model.add(Conv2D(filters=50,kernel_size=(5,1),strides=(1,1),padding='same',activation='relu'))
model.add(Flatten())
model.add(Dense(1024,activation='relu'))
model.add(Dense(1, activation='linear', name='output'))

model.compile(optimizer='adam',
              loss='mean_squared_error',
              metrics=['accuracy'])

model.fit(dataset, epochs=10)
WARNING:tensorflow:Layer sequential_2 is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2.  The layer has dtype float32 because it's dtype defaults to floatx.

If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.

To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-f8c5e0c71664> in <module>()
----> 1 model.fit(dataset, epochs=10)

11 frames
/tensorflow-2.0.0/python3.6/tensorflow_core/python/keras/layers/core.py in build(self, input_shape)
   1013     input_shape = tensor_shape.TensorShape(input_shape)
   1014     if tensor_shape.dimension_value(input_shape[-1]) is None:
-> 1015       raise ValueError('The last dimension of the inputs to `Dense` '
   1016                        'should be defined. Found `None`.')
   1017     last_dim = tensor_shape.dimension_value(input_shape[-1])

ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.

模型=顺序()
模型添加(重塑(-1599,1)))
add(Conv2D(filters=30,kernel_size=(10,1),strips=(1,1),padding='same',activation='relu',input_shape=(599,1)))
add(Conv2D(filters=30,kernel_size=(8,1),strips=(1,1),padding='same',activation='relu'))
add(Conv2D(filters=40,kernel_size=(6,1),strips=(1,1),padding='same',activation='relu'))
add(Conv2D(filters=50,kernel_size=(5,1),strips=(1,1),padding='same',activation='relu'))
add(Conv2D(filters=50,kernel_size=(5,1),strips=(1,1),padding='same',activation='relu'))
model.add(展平())
添加(密集(1024,activation='relu'))
添加(密集(1,activation='linear',name='output'))
model.compile(优化器='adam',
损失=‘均方误差’,
指标=[‘准确度’])
model.fit(数据集,历代=10)
警告:tensorflow:Layer sequential_2正在将输入张量从dtype float64转换为Layer的dtype float32,这是tensorflow 2中的新行为。该层具有dtype float32,因为它的dtype默认为floatx。
如果您打算在float32中运行此层,您可以安全地忽略此警告。如果有疑问,只有在将TensorFlow 1.X模型移植到TensorFlow 2时,才可能出现此警告。
要将所有层的默认数据类型更改为float64,请调用'tf.keras.backend.set_floatx('float64')`。要仅更改此层,请将dtype='float64'传递给层构造函数。如果您是此层的作者,可以通过将autocast=False传递给基本层构造函数来禁用自动广播。
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
---->1 model.fit(数据集,历代=10)
11帧
/tensorflow-2.0.0/python3.6/tensorflow_-core/python/keras/layers/core.py内置(自我,输入_-shape)
1013输入形状=张量形状。张量形状(输入形状)
1014如果张量_形。尺寸_值(输入_形[-1])为无:
->1015 raise VALUE ERROR('输入到'Dense'的最后一个维度'
应定义“1016”。找到“无”。)
1017最后尺寸=张量形状。尺寸值(输入形状[-1])
ValueError:应定义'Dense'输入的最后一个维度。找到“无”。

(谢谢)

请在您的问题中添加相关代码,而不是作为外部链接。您必须将输入的形状放置在模型的第一层(重塑形状)。是的,谢谢。你帮了大忙。我为我学习缓慢而道歉。我努力使张量形状正确。