gpt4 book ai didi

python-3.x - 值错误 : Negative dimension size caused by subtracting 3 from 1 for 'conv1d_1/convolution/Conv2D

转载 作者:行者123 更新时间:2023-12-03 22:12:51 25 4
gpt4 key购买 nike

二元分类问题:我想要一个输入层(可选),一个 Conv1D 层,然后输出 1 个神经元预测 1 或 0 的层。
这是我的模型:

x_train = np.expand_dims(x_train,axis=1)
x_valid = np.expand_dims(x_valid,axis=1)
#x_train = x_train.reshape(x_train.shape[0], 1, x_train.shape[1])
#x_valid = x_train.reshape(x_valid.shape[0], 1, x_train.shape[1])

model = Sequential()

#hidden layer
model.add(Convolution1D(filters = 1, kernel_size = (3),input_shape=(1,x_train.shape[2])))
#output layer
model.add(Flatten())
model.add(Dense(1, activation = 'softmax'))

sgd = SGD(lr=0.01, nesterov=True, decay=1e-6, momentum=0.9)

model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

print('model compiled successfully')
model.fit(x_train, y_train, nb_epoch = nb_epochs, validation_data=(x_valid,y_valid), batch_size=100)

输入形状 : x_train.shape = (5,1,133906) 分别是 (batch,steps,channels)。通过 expand_dims 添加的步骤。实际大小 (5,133906) 是长度为 133906 的时间序列数据的 5 个样本,有时在 2 ms 时随机采样,有时在 5 ms 时随机采样。
错误信息 : ValueError:输入形状为 [?,1,1,133906], [1,3,133906,1] 的 'conv1d_1/convolution/Conv2D'(操作:'Conv2D')由 1 减去 3 引起的负尺寸大小。
我该如何解决这个问题?在 Conv1D 中传递的 x_train 和 input_size 参数的大小应该是多少?

最佳答案

Convolution1D 层以 [batch、steps、channels] 的格式接收输入

您的卷积窗口长度(内核大小)不能大于步数。

因此,如果您想使用您定义的输入形状:

x_train.shape = (5,1,133906)

您需要将内核大小更改为 1

即将第 9 行更改为
model.add(Convolution1D(filters = 1, kernel_size = 1,input_shape=(1,x_train.shape[2])))

但是,这只会使您的示例能够工作。根据您的目标、数据类型等,您可能想要尝试不同的内核大小和输入数据维度组合以获得最佳结果。

关于python-3.x - 值错误 : Negative dimension size caused by subtracting 3 from 1 for 'conv1d_1/convolution/Conv2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53013555/

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