简体   繁体   English

为序列无效参数构建/训练一维CNN

[英]Building/Training 1D CNN for sequence-InvalidArgument

I'm building and training a CNN for a sequence, and have been using RNN's successfully, but am running into issues with CNN. 我正在为序列构建和训练CNN,并且已经成功使用RNN,但遇到了CNN问题。 Here's the code, cnn1 is first (more complex model), tried getting a simpler one to fit and getting errors on both: 这是代码,cnn1是第一个(更复杂的模型),试图让一个更简单的代码适合并在两个代码上都出错:

The shapes are as follows: 形状如下:

xtrain (5206, 19, 4)
ytrain (5206, 4)
xvalid (651, 19, 4)
yvalid (651, 4)
xtest (651, 19, 4)
ytest (651, 4)

I've tried just about every combination of kernel sizes and nodes I can think of, tried 2 different model builds. 我已经尝试了几乎所有可以想到的内核大小和节点的组合,并尝试了2种不同的模型构建。

    model_cnn1.add(keras.layers.Conv1D(32, (4), activation='relu'))
    model_cnn1.add(keras.layers.MaxPooling1D((4)))
    model_cnn1.add(keras.layers.Conv1D(32, (4), activation='relu'))
    model_cnn1.add(keras.layers.MaxPooling1D((4)))
    model_cnn1.add(keras.layers.Conv1D(32, (4), activation='relu'))
    model_cnn1.add(keras.layers.Dense(4))


    model_cnn2 = keras.models.Sequential([
    keras.layers.Conv1D(100,(4),input_shape=(19,4),activation='relu'),
    keras.layers.MaxPooling1D(4),
    keras.layers.Dense(4)
])


    model_cnn2.compile(loss='mse',optimizer='adam',metrics= ['mse','accuracy'])

    model_cnn2.fit(X_train_tf,y_train_tf,epochs=25)

Output is 1/25 epochs, not entirely run, then on cnn1 I receive some variation of (final line): 输出为1/25个纪元,未完全运行,然后在cnn1上我收到了(最后一行)的一些变化:

ValueError: Negative dimension size caused by subtracting 4 from 1 for 'max_pooling1d_26/MaxPool' (op: 'MaxPool') with input shapes: [?,1,1,32] ValueError:负尺寸大小是由于输入形状为[?,1,1,32]的'max_pooling1d_26 / MaxPool'(op:'MaxPool')从1中减去4引起的

on cnn2 (simpler) I get error (final line): 在cnn2上(更简单),我得到了错误(最后一行):

InvalidArgumentError: Incompatible shapes: [32,4,4] vs. [32,4] InvalidArgumentError:不兼容的形状:[32,4,4]与[32,4]
[[{{node metrics_6/mse/SquaredDifference}}]] [Op:__inference_keras_scratch_graph_6917] [[{{nodemetrics_6 / mse / SquaredDifference}}]] [操作:__ inference_keras_scratch_graph_6917]

In general, is there some rule I should be following here for kernels/nodes/etc? 总的来说,对于内核/节点/ etc,我应该遵循一些规则吗? I always seem to get these errors on the shape. 我似乎总是在形状上出现这些错误。 I'm hoping after I build a model of each type I'll understand the ins and outs--no pun intended--but it's driving me crazy! 我希望在建立每种类型的模型后,我能理解我的来龙去脉-并非双关语-但这使我发疯!

I've tried every combination of 我尝试了各种组合

You can read up on the docs of the Conv1D and MaxPooling1D to read that these layers change the output shape depending on the value for strides . 您可以阅读Conv1DMaxPooling1D的文档,以Conv1D这些图层根据strides值更改输出形状。 In your case you can keep the output shape for Conv1D equal by specifying a padding. 在您的情况下,可以通过指定填充使Conv1D的输出形状保持相等。 MaxPooling1D changes the output shape by definition. MaxPooling1D通过定义更改输出形状。 With strides = 4 , the output shape will be 4 times smaller in fact. strides = 4 ,输出形状实际上将小4倍。 I'd suggest carefully reading the docs to figure out exactly what happens and learning about the underlying theory of CNNs as to why this happens. 我建议您仔细阅读文档以弄清楚到底发生了什么,并了解CNN的潜在理论。

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

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