好,它跑起来了,跑起来了啊,只不过 Loss 飞到 Nan 去了.
这篇文章 c7w 将把自己使用 TensorFlow 的过程分案例记录下来,旨在写下踩的所有坑供人观赏。我下次一定记得加 sigmoid。
- Hello World! (Fashion MNIST)
Hello World!
Overview
数据集:Fashion MNIST
主要技术要点:新手教程,Fully Connected NN
- 目标任务:图片分类
数据集介绍与数据预处理
So let’s get started. 首先我们并不知道 TensorFlow 的代码究竟该怎么写,只是久仰 Keras 的大名。
所以我们自然按照机器学习的基本流程开始(喂这根本没有因果逻辑联系吧),先进行数据预处理,然后 Declare 一个 Model,然后进行 Feedforward,然后进行 Backprops.
首先,我们的数据集可以极为方便的在 keras 中调用。具体而言,我们只需要:
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
不妨先看看这里面都是什么东西。
print(type(train_images))
print(train_images.shape)
print(type(train_labels))
print(train_labels.shape)
print(train_images[4])
print(train_labels[4])
<class 'numpy.ndarray'>
(60000, 28, 28)
<class 'numpy.ndarray'>
(60000,)
[[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 189 206 187 32 0 0 0 26 217 226
196 11 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 192 227 234 243 230 147 239 242 234 218
209 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 173 225 215 233 254 0 194 240 217 221
190 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 139 229 212 226 255 0 162 255 213 226
200 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 98 232 211 215 249 46 162 246 214 230
186 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 70 228 213 220 224 252 239 219 217 231
171 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 45 222 214 218 216 210 215 217 202 224
172 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 24 254 214 210 211 214 215 212 203 221
167 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 254 216 215 217 217 216 216 206 225
150 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 247 216 214 217 216 214 212 203 226
136 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 245 216 214 216 217 215 211 204 225
125 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 247 216 214 217 220 217 213 203 222
147 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 248 216 215 218 222 216 214 207 218
179 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 249 216 217 219 222 217 214 210 215
211 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 38 255 214 218 219 224 218 215 211 211
231 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 79 227 209 219 219 227 219 215 213 206
254 58 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 146 226 211 220 219 228 218 215 216 205
219 163 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 202 221 214 221 219 231 218 215 218 213
212 220 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 234 217 216 220 219 234 217 215 218 216
223 247 7 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 17 254 212 219 219 220 233 214 216 219 222
153 238 58 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 66 255 208 220 219 222 241 220 218 218 218
192 242 99 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 142 235 203 218 216 231 242 225 233 219 214
216 238 144 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 177 248 227 229 211 255 76 0 247 243 230
230 249 187 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 101 241 228 228 220 255 64 0 243 237 230
227 241 142 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 255 242 222 218 255 62 0 223 238 225
238 255 31 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 45 255 242 235 255 84 0 246 255 242
255 70 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 61 102 168 25 0 139 161 74
0 0 0 0 0 0 0 0 0 0]]
0
看起来只是 numpy 的 ndarray 嘛,我们已经很熟练了(关掉 numpy tutorial 网页)。
然后我们试着把这第 4 张图片画出来看看。
plt.figure()
plt.imshow(train_images[4])
plt.colorbar()
plt.grid(False)
plt.show()
似乎看起来确实是 “T-shirt/top”。 (像个鬼啊喂
于是我们考虑先对输入做 Normalization,方便我们后续的处理。
train_images = train_images / 255.0
test_images = test_images / 255.0
然后随便再画几张图看一看。
plt.figure(figsize=(10, 10))
for i in range(49):
plt.subplot(7, 7, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()
好,现在我们已经打开理解了这个训练数据集中都是什么东西了,该开始抄代码环节了(明明上面也在抄吧喂,变量名都没改
模型定义
我们要构建最简单的 Fully Connected Nerual Network。
因为过于简单就用 mspaint 画图了。
我们要知道,我们要描述这么一种线性的 Layer 结构。
Sequential
groups a linear stack of layers into a tf.keras.Model
.
Reference
后面又咕了