使用 conv1D “检查输入时出错:预期 conv1d_input 有 3 个维度,但得到了形状为 (213412, 36) 的数组”

bas*_*har 3 python neural-network conv-neural-network lstm keras

我的输入只是一个有237124行和37列的 csv 文件:

  • 36列作为特征

  • 最后一列是一个二进制类标签

我正在尝试在 conv1D 模型上训练我的数据。

我曾尝试用一层构建CNN,但我遇到了一些问题。

编译器输出:

ValueError:检查输入时出错:预期 conv1d_9_input 具有形状 (213412, 36) 但得到形状为 (36, 1) 的数组

代码:

import pandas as pd
import numpy as np
import sklearn
from sklearn import metrics
from sklearn.model_selection import KFold
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import StandardScaler
import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.layers import Conv2D,Conv1D, MaxPooling2D,MaxPooling1D
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Dropout,BatchNormalization

dataset=pd.read_csv("C:/Users/User/Desktop/data.csv",encoding='cp1252')

dataset.shape
#output: (237124, 37)

array = dataset.values
X = array[:,0:36]
Y = array[:,36]

kf = KFold(n_splits=10)
kf.get_n_splits(X)

for trainindex, testindex in kf.split(X):
Xtrain, Xtest = X[trainindex], X[testindex]
Ytrain, Ytest = Y[trainindex], Y[testindex]

Xtrain.shape[0]
#output: 213412

Xtrain.shape[1]
#output: 36

Ytrain.shape[0]
#output: 213412

n_timesteps, n_features, n_outputs =Xtrain.shape[0], Xtrain.shape[1], 
Ytrain.shape[0]

model = Sequential()
model.add(Conv1D(filters=64, kernel_size=1, 
activation='relu',input_shape=(n_timesteps,n_features)))

model.add(Conv1D(filters=64, kernel_size=1, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=  
['accuracy'])
# fit network
model.fit(Xtrain, Ytrain, epochs=10, batch_size=32, verbose=0)

# Testing CNN model BY X test

Predictions = model.predict(Xtest,batch_size =100)
rounded = [round(x[0]) for x in Predictions]
Y_predection = pd.DataFrame(rounded)
Y_predection = Y_predection.iloc[:, 0]

.
.
.
Run Code Online (Sandbox Code Playgroud)

我尝试以这种方式修改代码:

Xtrain = np.expand_dims(Xtrain, axis=2) 
Run Code Online (Sandbox Code Playgroud)

但错误仍然相同。

thu*_*v89 5

我注意到您的代码有几个问题。

  • Xtrain- 需要是一个 3D 张量。因为别的,Conv1D无法处理。因此,如果您有 2D 数据,则需要添加一个新维度以使其成为 3D。
  • input_shape需要改变以反映这一点。例如,如果您只添加了一个频道,它应该是[n_features, 1].
# Here I'm assuming some dummy data
# Xtrain => [213412, 36, 1] (Note that you need Xtrain to be 3D not 2D - So we're adding a channel dimension of 1)
Xtrain = np.expand_dims(np.random.normal(size=(213412, 36)),axis=-1)
# Ytrain => [213412, 10]
Ytrain = np.random.choice([0,1], size=(213412,10))

n_timesteps, n_features, n_outputs =Xtrain.shape[0], Xtrain.shape[1], Ytrain.shape[1]

model = Sequential()
model.add(Conv1D(filters=64, kernel_size=1, 
activation='relu',input_shape=(n_features,1)))

model.add(Conv1D(filters=64, kernel_size=1, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(Xtrain, Ytrain, epochs=10, batch_size=32, verbose=0)
Run Code Online (Sandbox Code Playgroud)