检查模型目标时发生错误:预期density_39具有3个维度,但数组的形状为(940,1) [英] Error when checking model target: expected dense_39 to have 3 dimensions, but got array with shape (940, 1)

查看:46
本文介绍了检查模型目标时发生错误:预期density_39具有3个维度,但数组的形状为(940,1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试训练该卷积神经网络,但无法弄清楚我的最后一层是什么问题.

I'm trying to train this Convolutional Neural Network but can't figure out what the issue is with my last layer.

model = Sequential()
model.add(Conv1D(50, kernel_size=(1),
                 activation='relu',
                 input_dim=50))
model.add(Dense(32))
model.add(Dense(1))
model.summary()
model.compile(loss=keras.losses.mean_squared_error,
              optimizer=keras.optimizers.adam())

model.fit(X_train, y_train,
          batch_size=940,
          epochs=10,
          verbose=1,
          validation_data=(X_test, y_test))

型号:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_26 (Conv1D)           (None, None, 50)          2550      
_________________________________________________________________
dense_38 (Dense)             (None, None, 32)          1632      
_________________________________________________________________
dense_39 (Dense)             (None, None, 1)           33        
=================================================================
Total params: 4,215.0
Trainable params: 4,215
Non-trainable params: 0.0
_________________________________________________________________

我总是收到以下错误消息:

I always get the following error message:

ValueError:检查模型目标时出错:预期density_39具有3维,但数组的形状为(940,1)

ValueError: Error when checking model target: expected dense_39 to have 3 dimensions, but got array with shape (940, 1)

我怀疑问题在于,对于最后一层,我只有一个输出节点,因此输出尺寸减小到了两个.

I suspect the issue is that for the last layer I got only one output node, so the output dimensions are reduced to two.

推荐答案

一维卷积期望输入形式为(BatchSize,length,channels).
Keras会将其报告为(None,length,channels).

A 1D convolution expects inputs in the form (BatchSize,length,channels).
Keras would report that as (None,length,channels).

因此,您需要相应地传递input_shape.如果您的数据中只有一个渠道,则需要将其定义为:

So, you need to pass the input_shape accordingly. If you have only one channel in your data, you need to define it as:

model.add(Conv1D(50, kernel_size=(1),
             activation='relu',
             input_shape=(50,1)))

并确保您的X_train也遵循该形状,形状类似于(NumberOfSamples, 50, 1).

And make sure that your X_train also follows that, being shaped like (NumberOfSamples, 50, 1).

这将输出形状为(NumberOfSamples,50,50)的张量-第一个50来自输入的长度,第二个50来自图层中定义的50个过滤器.

This will output tensors in shape (NumberOfSamples,50,50) - The first 50 is from the length that came in, the second is from the 50 filters defined in the layer.

在那之后,密集层通常需要平坦的数据,而不是2D数据.

After that, dense layers often expect flattened data, not 2D data.

您可以像以前一样使用它们,但是它们会保留额外的尺寸,这似乎并不是您的目的.

You can use them as you did, but they will keep the extra dimensions, and that doesn't seem to be your purpose.

如果最后只需要一个类(我猜是这样),则需要在使用Dense层之前将数据弄平:

If you want only one class at the end (I'm guessing that), you need to flatten your data before using the Dense layers:

model.add(Flatten()) #this will transform (None, 50,50) into (None,2500)
model.add(Dense(32))
model.add(Dense(1))

然后您的输出确实将具有形状(None,1),该形状与您的Y_train (940,1)

Then your output will indeed have shape (None,1), which matches your Y_train (940,1)

这篇关于检查模型目标时发生错误:预期density_39具有3个维度,但数组的形状为(940,1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆