【发布时间】:2020-08-31 17:35:26
【问题描述】:
这段代码有什么问题?
faces = datasets.fetch_oliveti_faces()
X_train, X_test, y_train, y_test = train_test_split(faces.data,faces.target, test_size=0.2)
X_train = X_train.reshape(-1,32 ,32 ,1)
X_test = X_test.reshape(-1,32 , 32 ,1)
# 规范化数据
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255.0
X_test /= 255.0
# 一热
班级=40
y_train = keras.utils.to_categorical(y_train, 类)
y_test = keras.utils.to_categorical(y_test, 类)
#使用 Keras 构建 LetNet 模型
def LetNet(宽度,高度,深度,类):
# 初始化模型
模型=顺序()
# 第一层,卷积和池化
model.add(Conv2D(input_shape=(width, height, depth), kernel_size=(5, 5), filters=6, strides=(1,1), activation='tanh'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# 第二层,卷积和池化
model.add(Conv2D(input_shape=(width, height, depth), kernel_size=(5, 5), filters=16, strides=(1,1), activation='tanh'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# 全连接层
model.add(展平())
model.add(密集(120,激活='tanh'))
model.add(密集(84,激活='tanh'))
# softmax分类器
model.add(密集(类))
model.add(激活(“softmax”))
返回模型
LetNet_model = LetNet(32,32,1,40)
LetNet_model.summary()
#Strat训练
LetNet_model.compile(optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08),loss = 'categorical_crossentropy',metrics=['accuracy'])
History = LetNet_model.fit(X_train, y_train, epochs=5, batch_size=32,validation_data=(X_test, y_test))
【问题讨论】:
-
你能打印
X_train.shape,y_train.shape吗?
标签: python keras deep-learning face-recognition