[英]Input and output dimension error in Keras
我目前正在处理二进制分类的客户流失管理问题。
拟合模型时出现错误。 输入/输出中似乎有些东西使我无法捕捉到。
这是代码(df包含我要创建矢量的数据框):
#Delete unimportant columns.
del df['RowNumber']
del df['CustomerId']
del df['Surname']
然后,我必须转换两个类别变量:
#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 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)
接下来,我缩放变量:
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,)
建筑网络:
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)
我得到的特定错误代码是:
ValueError:检查输入时出错:预期density_47_input具有形状(None,8),但数组的形状为(8000,10)
解决这个问题的任何帮助将是巨大的!
我认为您需要纠正此问题:
model.add(layers.Dense(32, activation='relu', input_shape=(10,)))
10是您使用的功能数。 Keras将自动获取批处理/数据集中的行数。
编辑说明 :
from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Dense(32, input_shape=(10,)))
model.add(layers.Dense(1))
在这里,将创建仅接受尺寸为10的2D张量的第一层(未指定第零尺寸(批尺寸),因此可以接受任何值)。
因此,该层只能连接到以32维向量为输入的上游。 使用Keras时,您不必担心兼容性,因为添加到模型中的图层是动态构建的,以匹配传入图层的形状。
第二层没有接收到输入形状参数,而是自动将其输入形状推断为之前图层的输出形状。
解码模型中的参数值:
假设这是我的模型:
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()
这是模型摘要:
_________________________________________________________________
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
_________________________________________________________________
对于密集层,我们需要计算以下内容:
output = dot(W, input) + b
要么
output = relu(dot(W, input) + b) #relu here is the activation function
在该表达式中,W和b是作为层属性的张量。 它们被称为层的“权重”或“可训练参数”(分别是内核和偏向属性)。 这些权重包含网络从接触训练数据中学到的信息。
对于第1层(参数= 96)=隐藏单位*输入数据的维数+偏置值
96 = 32 (Hidden Units) * 2 (Data Dimension) + 32 (Bias Value Same as Hidden Units)
对于第2层(参数= 33)=隐藏单位*数据量+偏差值
33 = 1 (Hidden Units) * 32 (Data Dimension) + 1 (Bias Value Same as Hidden Units)
Total Params = 96+33 = 129
希望这可以帮助 :)
解释源: Keras文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.