[英]Using conv1D “Error when checking input: expected conv1d_input to have 3 dimensions, but got array with shape (213412, 36)”
My input is simply a csv file with 237124 rows and 37 columns :我的输入只是一个有237124行和37列的 csv 文件:
The first 36 columns as features前36列作为特征
The last column is a Binary class label最后一列是一个Binary 类标签
I am trying to train my data on the conv1D model.我正在尝试在 conv1D 模型上训练我的数据。
I have tried to build a CNN with one layer , but I have some problems with it.我曾尝试用一层构建CNN ,但我遇到了一些问题。
The compiler outputs:编译器输出:
ValueError:Error when checking input: expected conv1d_9_input to have shape (213412, 36) but got array with shape (36, 1)
ValueError:检查输入时出错:预期 conv1d_9_input 具有形状 (213412, 36) 但得到形状为 (36, 1) 的数组
Code:代码:
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]
.
.
.
I tried to modify the code this way:我尝试以这种方式修改代码:
Xtrain = np.expand_dims(Xtrain, axis=2)
But the error remains the same.但错误仍然相同。
There's a couple of problems I notice with your code.我注意到您的代码有几个问题。
Xtrain
- Needs to be a 3D tensor. Xtrain
- 需要是一个 3D 张量。 Because anything else, Conv1D
cannot process.Conv1D
无法处理。 So if you have 2D data you need to add a new dimension to make it 3D.input_shape
needs to be changed to reflect that.input_shape
需要更改以反映这一点。 For example, if you added only a single channel, it should be [n_features, 1]
.[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)
You need to specifi only how many dimension X has, not how many samples you will pass for the input layer.您只需要指定 X 有多少维,而不需要为输入层传递多少个样本。
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_features,)))
This means that the input will be N samples of shape n_features这意味着输入将是形状为 n_features 的 N 个样本
For the last layer you should change the number of units to how many classes you have instead of how many rows your data has.对于最后一层,您应该将单位数更改为您拥有的类数,而不是您的数据有多少行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.