莫凡Python 3
Posted Howbin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了莫凡Python 3相关的知识,希望对你有一定的参考价值。
莫凡Python 3
CNN 卷积神经网络
参考资料
- https://morvanzhou.github.io/tutorials/machine-learning/keras/2-3-CNN/
- https://keras-cn.readthedocs.io/
数据预处理
- X_train = X_train.reshape(-1, 1, 28 , 28)
这种处理我不是很理解
建立模型
- batch_input_shape=(None, 1, 28, 28)
shape : 形状
None 可以参考: https://blog.csdn.net/qq_36490364/article/details/83594271
代码
# -*- coding: utf-8 -*-
""" CNN 卷积神经网络 """
import os
os.environ[\'TF_CPP_MIN_LOG_LEVEL\'] = \'2\'
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.models import Sequential
from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten
import matplotlib.pyplot as plt
from keras.utils import np_utils
from keras.optimizers import Adam
from keras.datasets import mnist
from keras import backend
backend.set_image_data_format(\'channels_first\')
# %% 数据预处理
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
X_train = X_train.reshape(-1, 1, 28 , 28)/255.
X_test = X_test.reshape(-1, 1,28, 28)/255.
Y_train = np_utils.to_categorical(Y_train, num_classes=10)
Y_test = np_utils.to_categorical(Y_test, num_classes=10)
# %% 建立模型
model = Sequential()
model.add(
Convolution2D(
batch_input_shape=(None, 1, 28, 28),
filters=32,
kernel_size=5,
strides=1,
padding=\'same\'
)
)
model.add(Activation(\'relu\'))
model.add(
MaxPooling2D(
pool_size=2,
strides=2,
padding=\'same\',
)
)
model.add(Activation(\'relu\'))
model.add(Convolution2D(64, 5, strides=1, padding=\'same\'))
model.add(Activation(\'relu\'))
model.add(MaxPooling2D(2,2,\'same\'))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation(\'relu\'))
model.add(Dense(10))
model.add(Activation(\'softmax\'))
adam = Adam(lr=1e-4)
model.compile(optimizer=adam, loss=\'categorical_crossentropy\', metrics=[\'accuracy\'])
# %% 训练
print(\'Training ------------\')
model.fit(X_train,Y_train,batch_size=64)
print(\'\\nTesting ------------\')
loss, accuracy = model.evaluate(X_test, Y_test)
# %% 评估
print(\'\\ntest loss: \', loss)
print(\'\\ntest accuracy: \', accuracy)
以上是关于莫凡Python 3的主要内容,如果未能解决你的问题,请参考以下文章
python 用于在终端中运行的sublime text 3的简单代码片段制作工具