ValueError:期望的dense_1_input具有形状(None,4)但得到(78,2)

时间:2018-02-06 04:10:29

标签: pandas deep-learning keras keras-layer valueerror

我没有从根本上理解数组的形状或如何确定训练数据的时期和批量大小。我的数据有6列,第0列是自变量 - 一个字符串,第1-4列是深度神经网络输入,第5列是由输入引起的二进制结果。我有99行数据。 我想了解如何摆脱这个错误。

#Importing Datasets
dataset=pd.read_csv('TestDNN.csv')
x = dataset.iloc[:,[1,5]].values # lower bound independent variable to upper bound in a matrix (in this case up to not including column5)
y = dataset.iloc[:,5].values # dependent variable vector
#Splitting data into Training and Test Data
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.2, random_state=0)


#Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test=sc.transform(x_test)

# PART2 - Making ANN, deep neural network

#Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

#Initialising ANN
classifier = Sequential()

#Adding the input layer and first hidden layer
classifier.add(Dense(activation= 'relu', input_dim =4, units=2, 
kernel_initializer="uniform"))#rectifier activation function
#Adding second hidden layer
classifier.add(Dense(activation= 'relu', units=2, 
kernel_initializer="uniform")) #rectifier activation function
#Adding the Output Layer
classifier.add(Dense(activation= 'sigmoid', units=1, 
kernel_initializer="uniform"))

#Compiling ANN - stochastic gradient descent
classifier.compile(optimizer='adam', loss='binary_crossentropy',metrics=
['accuracy'])

#Fit ANN to training set

#PART 3 - Making predictions and evaluating the model

#Fitting classifier to the training set
classifier.fit(x_train, y_train, batch_size=32, epochs=5)#original batch is 
10 and epoch is 100

1 个答案:

答案 0 :(得分:1)

问题在于x定义。这一行:

x = dataset.iloc[:,[1,5]].values 

...告诉pandas将第1列和第5列,因此它的形状为[78, 2]。您可能需要在第5行之前使用所有列:

x = dataset.iloc[:,:5].values