tensorflow08-分类任务coding
import pickle import gzip from pathlib import Path from matplotlib import pyplot # Pathlib 库来操作文件路径,用法如下所示: filepath=Path("mnist.pkl.gz")#https://github.com/mnielsen/neural-networks-and-deep-learning/tree/master/data with gzip.open(filepath.as_posix(),"rb") as f: ((x_t,y_t),(x_v,y_v),_)=pickle.load(f,encoding="latin_1") pyplot.imshow(x_t[0].reshape((28,28)),cmap="gray") print(x_t.shape)#(500
输入为一个样本多少个特征 但是这个图像为28*28*1 的三维矩阵
所以先转换样本
input 784pixel 像素点=》 输入神经网络
神经网络特征提取器 -》无论做分类还是回归都是一样的
import pickle import gzip from pathlib import Path from matplotlib import pyplot # Pathlib 库来操作文件路径,用法如下所示: filepath=Path("mnist.pkl.gz")#https://github.com/mnielsen/neural-networks-and-deep-learning/tree/master/data with gzip.open(filepath.as_posix(),"rb") as f: ((x_t,y_t),(x_v,y_v),_)=pickle.load(f,encoding="latin_1") pyplot.imshow(x_t[0].reshape((28,28)),cmap="gray") print(x_t.shape)#(50000, 784) 784 为每个样本的像素点个数 import tensorflow as tf # from tensorflow.keras import layers #2.0versiong # import keras # print(tf.version) # print(tf.version.VERSION) model = tf.keras.Sequential() model.add(tf.keras.layers.Dense(32, activation='relu')) model.add(tf.keras.layers.Dense(32, activation='relu')) model.add(tf.keras.layers.Dense(10, activation='softmax')) model.compile(optimizer=tf.keras.optimizers.Adam(0.01), loss=tf.keras.losses.sparse_categorical_crossentropy, metrics=[tf.keras.metrics.sparse_categorical_accuracy]) model.fit(x_t, y_t, epochs=5, batch_size=64, validation_data=(x_v, y_v))
可以看到对比回归 模型
隐藏layer
保存模型