gpt4 book ai didi

python - 使用 Keras+Tensorflow 训练 ConvNet 时出现不兼容形状错误

转载 作者:行者123 更新时间:2023-12-01 02:14:06 25 4
gpt4 key购买 nike

我正在尝试构建一个简单的卷积神经网络,将时间序列分为六类之一。由于不兼容的形状错误,我在训练网络时遇到问题。

在以下代码中,n_feats = 1000n_classes = 6

Fs = 100
input_layer = Input(shape=(None, n_feats), name='input_layer')
conv_layer = Conv1D(filters=32, kernel_size=Fs*4, strides=int(Fs/2), padding='same', activation='relu', name='conv_net_coarse')(input_layer)
conv_layer = MaxPool1D(pool_size=4, name='c_maxp_1')(conv_layer)
conv_layer = Dropout(rate=0.5, name='c_dropo_1')(conv_layer)
output_layer = Dense(n_classes, name='output_layer')(conv_layer)

model = Model(input_layer, output_layer)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
print(model.summary())

这是模型摘要。

_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_layer (InputLayer) (None, None, 1000) 0
_________________________________________________________________
conv_net_coarse (Conv1D) (None, None, 32) 12800032
_________________________________________________________________
c_maxp_1 (MaxPooling1D) (None, None, 32) 0
_________________________________________________________________
c_dropo_1 (Dropout) (None, None, 32) 0
_________________________________________________________________
output_layer (Dense) (None, None, 6) 198
=================================================================
Total params: 12,800,230
Trainable params: 12,800,230
Non-trainable params: 0
_________________________________________________________________
None

当我运行时,model.fit(X_train, Y_train),其中 X_train 形状为 (30000, 1, 1000)Y_train 形状为 (30000, 1, 6),我收到形状不兼容错误:

InvalidArgumentError (see above for traceback): Incompatible shapes: [32,0,6] vs. [1,6,1]
[[Node: output_layer/add = Add[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](output_layer/Reshape_2, output_layer/Reshape_3)]]
[[Node: metrics_1/acc/Mean/_197 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_637_metrics_1/acc/Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

如果我删除 MaxPool1DDropout 层,模型训练得很好。我是否没有正确指定这些层?

如有任何帮助,我们将不胜感激!

最佳答案

所以 - 问题在于两个事实:

  1. 输入形状应为(number_of_examples, timesteps, features),其中特征是每个时间步记录的内容。这意味着您应该将数据 reshape 为 (number_of_examples, 1000, 1),因为您的时间序列有 1000 个时间步长和 1 个特征。
  2. 当您解决分类任务时 - 您需要将输入压缩为向量(来自序列)。我建议您在 Dropout 层之前使用 Flatten

关于python - 使用 Keras+Tensorflow 训练 ConvNet 时出现不兼容形状错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48505674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com