# tensorflow 1.12.0
from tensorflow.python.keras.datasets import cifar100
from tensorflow.python import keras
import tensorflow as tf
class CNNMnist(object):
# 2.编写两层 + 两层全连接层网络模型
model = keras.models.Sequential([
# 卷积层1 32个 5*5*3的filter, strides=1, padding="same"
keras.layers.Conv2D(32, kernel_size=5, strides=1, padding="same", data_format="channels_last", activation=tf.nn.relu),
keras.layers.MaxPool2D(2, strides=2, padding="same"),
keras.layers.Conv2D(64, kernel_size=5, strides=1, padding="same", data_format="channels_last",
activation=tf.nn.relu),
keras.layers.MaxPool2D(2, strides=2, padding="same"),
keras.layers.Flatten(),
keras.layers.Dense(1024, activation=tf.nn.relu),
keras.layers.Dense(100, activation=tf.nn.softmax)
])
def __init__(self):
# 获取训练数据集
(self.x_train, self.y_train), (self.x_test, self.y_test) = cifar100.load_data()
# 进行数据归一化
self.x_train = self.x_train / 255.0
self.y_train = self.y_train / 255.0
def compile(self):
CNNMnist.model.compile(optimizer=keras.optimizers.Adam(),
loss=keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
return None
def fit(self):
CNNMnist.model.fit(self.x_train, self.y_train, epochs=1, batch_size=32)
return None
def evaluate(self):
test_loss, test_acc = CNNMnist.model.evaluate(self.x_test, self.y_test)
print(test_loss, test_acc)
return None
if __name__ == '__main__':
cnn = CNNMnist()
cnn.compile()
cnn.fit()
01-06
01-26
447
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)