gpt4 book ai didi

deep-learning - 如何 reshape 我的输入以将其输入一维卷积层以进行序列分类?

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

我有一个包含 339732 行和两列的 csv 文件:

  • 第一个是 29 个特征值,即 X
  • 第二个是二进制标签值,即 Y

    dataframe = pd.read_csv("features.csv", header = None) 数据集 = dataframe.values

    X = dataset[:, 0:29].astype(float)
    Y = dataset[:,29]
    X_train, y_train, X_test, y_test = train_test_split(X,Y, random_state = 42)

我正在尝试在一维卷积层上训练它:

model = Sequential()
model.add(Conv1D(64, 3, activation='relu', input_shape=(X_train.shape[0], 29)))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(3))
model.add(Conv1D(128, 3, activation='relu'))
model.add(Conv1D(128, 3, activation='relu'))
model.add(GlobalAveragePooling1D())
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

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

model.fit(X_train, y_train, batch_size=16, epochs=2)
score = model.evaluate(X_test, y_test, batch_size=16)

由于 Conv1D 层需要 3-D 输入,因此我按如下方式转换输入:

X_train = np.reshape(X_train, (1, X_train.shape[0], X_train.shape[1]))
X_test = np.reshape(X_test, (1, X_test.shape[0], X_test.shape[1]))

但是,这仍然会引发错误:

ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv1d_1/convolution/Conv2D' (op: 'Conv2D') with input shapes: [?,1,1,29], [1,3,29,64].

有什么方法可以正确地提供我的输入吗?

最佳答案

据我所知,一维卷积层接受批量大小 x 宽度 x channel 形式的输入。你正在 reshape

X_train = np.reshape(X_train, (1, X_train.shape[0], X_train.shape[1]))

但是我猜 X_train.shape[0] 是你的batchsize。我认为问题出在这里。你能告诉我 reshape 之前 X_train 的形状吗?

关于deep-learning - 如何 reshape 我的输入以将其输入一维卷积层以进行序列分类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44705896/

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