简体   繁体   English

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

[英]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 features36列作为特征

  • 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.因此,如果您有 2D 数据,则需要添加一个新维度以使其成为 3D。
  • Your 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.

相关问题 Python“预计conv1d_input有3个维度,但得到了形状为..的数组”问题 - Python "expected conv1d_input to have 3 dimensions,but got array with shape .." problem 检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 (28708, 1) 的数组 - Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (28708, 1) ValueError:检查输入时出错:预期conv3d_1_input具有5个维,但数组的形状为(7,9,384,1) - ValueError: Error when checking input: expected conv3d_1_input to have 5 dimensions, but got array with shape (7, 9, 384, 1) ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了具有形状的数组 - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape ValueError:检查输入时出错:预期conv2d_1_input有4个维度,但得到的形状为数组(8020,1) - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (8020, 1) 检查模型输入时出错:预期conv2d_175_input具有4个维,但数组的形状为(0,1) - Error when checking model input: expected conv2d_175_input to have 4 dimensions, but got array with shape (0, 1) ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到的数组具有形状(无,1) - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (None, 1) 检查输入时出错:预期 conv2d_4_input 有 4 个维度,但得到了形状为 (32, 32) 的数组 - Error when checking input: expected conv2d_4_input to have 4 dimensions, but got array with shape (32, 32) ValueError:检查输入时出错:预期 conv2d_36_input 的形状为 (3, 32, 32) 但得到的数组的形状为 (1, 10, 10) - ValueError: Error when checking input: expected conv2d_36_input to have shape (3, 32, 32) but got array with shape (1, 10, 10) Keras model.predict检查输入时出错:预期conv2d_input具有4维,但数组的形状为(128,56) - Keras model.predict Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (128, 56)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM