简体   繁体   English

Keras中的输入和输出尺寸错误

[英]Input and output dimension error in Keras

I am currently working on a churn management problem with a binary classification. 我目前正在处理二进制分类的客户流失管理问题。

I am getting an error when I am fitting the model. 拟合模型时出现错误。 There seems to be something in the input/output that is throwing this off that I haven't been able to catch. 输入/输出中似乎有些东西使我无法捕捉到。

Here is the code (df contains my data frame which I am creating vectors from): 这是代码(df包含我要创建矢量的数据框):

#Delete unimportant columns.
del df['RowNumber']
del df['CustomerId']
del df['Surname']

Then, I have to convert two categorical variables: 然后,我必须转换两个类别变量:

#Converting and creating dummy variables for categorical variables
df["Gender"] = df["Gender"].astype('category')
df["Geography"] = df["Geography"].astype('category')
df['Gender'] = pd.get_dummies(df['Gender'])
df['Geography'] = pd.get_dummies(df['Geography'])

y = df.iloc[:, -1] #Label variable
X = df.iloc[:, :10] #Features

Splitting dataset into test and training: 将数据集分为测试和培训:

# Splitting the dataset into the Training set and Test set
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)

Next, I am scaling the variables: 接下来,我缩放变量:

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train) 
X_test = sc.transform(X_test)
print(X_train.shape) #(8000, 10)
print(X_test.shape) #(2000, 10)
print(y_train.shape)#(8000,)
print(y_test.shape)#(2000,)

Building Network: 建筑网络:

model = models.Sequential()
model.add(layers.Dense(32, activation='relu', input_shape=(8000,)))
model.add(layers.Dense(1, activation='sigmoid'))
# Compiling Neural Network
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
#Fitting our model
model.fit(X_train, y_train, batch_size = 10, epochs = 10)

# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)

# Creating the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

The specific error code that I am getting is: 我得到的特定错误代码是:

ValueError: Error when checking input: expected dense_47_input to have shape (None, 8) but got array with shape (8000, 10) ValueError:检查输入时出错:预期density_47_input具有形状(None,8),但数组的形状为(8000,10)

Any help to combat this issue would be great! 解决这个问题的任何帮助将是巨大的!

Edit: model summary before model.compile: 编辑:model.compile之前的模型摘要: 在此处输入图片说明

Edit2: model summary after compiling: Edit2:编译后的模型摘要: 在此处输入图片说明

I think you need to correct this: 我认为您需要纠正此问题:

model.add(layers.Dense(32, activation='relu', input_shape=(10,)))

10 is your number of features used. 10是您使用的功能数。 Keras will automatically take the number of rows in the batch/dataset. Keras将自动获取批处理/数据集中的行数。

Edit Explanation : 编辑说明

from keras import models
from keras import layers

model = models.Sequential()
model.add(layers.Dense(32, input_shape=(10,)))
model.add(layers.Dense(1))

Here, The first layers is created that will only accept a 2D tensor with the dimension is 10 (the zero-th dimension, the batch dimension, is unspecified and thus any value would be accepted). 在这里,将创建仅接受尺寸为10的2D张量的第一层(未指定第零尺寸(批尺寸),因此可以接受任何值)。

Thus this layer can only be connected to an upstream that expects 32-dimensional vectors as its input. 因此,该层只能连接到以32维向量为输入的上游。 When using Keras you don't have to worry about compatibility, because the layers that you add to your models are dynamically built to match the shape of the incoming layer. 使用Keras时,您不必担心兼容性,因为添加到模型中的图层是动态构建的,以匹配传入图层的形状。

The second layer did not receive an input shape argument—instead it automatically inferred its input shape as being the output shape of the layer that came before. 第二层没有接收到输入形状参数,而是自动将其输入形状推断为之前图层的输出形状。

Decoding parameter values in the model: 解码模型中的参数值:

Suppose this is my model: 假设这是我的模型:

from keras import models
from keras import layers

model = models.Sequential()
model.add(layers.Dense(32, input_shape=(2,)))
model.add(layers.Dense(1))
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
print model.summary()

And this is the model summary: 这是模型摘要:

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
dense_1 (Dense)              (None, 32)                96
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 33
=================================================================
Total params: 129
Trainable params: 129
Non-trainable params: 0
_________________________________________________________________

For Dense Layer, we need to calculate this: 对于密集层,我们需要计算以下内容:

output = dot(W, input) + b

or 要么

output = relu(dot(W, input) + b) #relu here is the activation function

In this expression, W and b are tensors which are attributes of the layer. 在该表达式中,W和b是作为层属性的张量。 They are called the "weights", or "trainable parameters" of the layer (the kernel and bias attributes, respectively). 它们被称为层的“权重”或“可训练参数”(分别是内核和偏向属性)。 These weights contain the information learned by the network from exposure to training data. 这些权重包含网络从接触训练数据中学到的信息。

For Layer 1 (Parameters=96) = Hidden_units * Dimension_of_input_data + bias_value 对于第1层(参数= 96)=隐藏单位*输入数据的维数+偏置值

96 = 32 (Hidden Units) * 2 (Data Dimension) + 32 (Bias Value Same as Hidden Units)

For Layer 2 (Parameters=33) = Hidden_units * Dimension_of_data + bias_value 对于第2层(参数= 33)=隐藏单位*数据量+偏差值

33 = 1 (Hidden Units) * 32 (Data Dimension) + 1 (Bias Value Same as Hidden Units)

Total Params = 96+33 = 129

Hope this helps :) 希望这可以帮助 :)

Source of Explanation: Keras Documentation 解释源: Keras文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM