引入包并查看版本号
import tensorflow as tf import tensorflow.keras as keras
print(tf.__version__) print(keras.__version__)
2.0.0 2.2.4-tf
引入数据集
如果下载不顺利的话可以从 https://www.kaggle.com/vikramtiwari/mnist-numpy/data 手动下载好mnist文件,然后把它复制到keras/datasets这个文件夹下面。
mnist = keras.datasets.mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
print(train_images.shape) print(test_images.shape) print(train_labels.shape) print(test_labels.shape)
(60000, 28, 28) (10000, 28, 28) (60000,) (10000,)
搭建模型进行预测
model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
最下面输出的两个数字分别代表损失函数和准确率,要是能顺便把accu和loss的值画成曲线图就更好啦。乍看起来tf比pytorch更简单,可以少写很多代码,但其实高度封装的后果就是你不需要操心底层的实现,没办法把学到的原理落实到每个细节,头重脚轻,如果涉及到不能采取拿来主义的地方就很难办。另外一点就是你很难访问计算过程中的中间结果,就好像做饭时只能拿着大刀砍骨头,不能用小刀雕萝卜。
model.fit(train_images, train_labels, epochs=5) model.evaluate(test_images, test_labels, verbose=2)
Train on 60000 samples Epoch 1/5 60000/60000 [==============================] - 6s 93us/sample - loss: 2.5732 - accuracy: 0.7574 Epoch 2/5 60000/60000 [==============================] - 5s 84us/sample - loss: 0.6132 - accuracy: 0.8468 Epoch 3/5 60000/60000 [==============================] - 5s 81us/sample - loss: 0.4904 - accuracy: 0.8767 Epoch 4/5 60000/60000 [==============================] - 4s 59us/sample - loss: 0.4061 - accuracy: 0.8967 Epoch 5/5 60000/60000 [==============================] - 4s 59us/sample - loss: 0.3761 - accuracy: 0.9045 10000/1 - 0s - loss: 0.1850 - accuracy: 0.9370 [0.2977512352705002, 0.937]