03. Tensorflow2 手写数字识别尝鲜

1. 手写数字识别简述

1.1. MNIST

  • 7000 images per category
  • train/test splitting: 60k vs 10k
    在这里插入图片描述

1.2. Image

  • [28,28,1]
  • →[784]
    在这里插入图片描述

1.3. Input and Output

在这里插入图片描述

1.4. Regression VS Classification

  • 𝑦 = 𝑤 ∗ 𝑥 + 𝑏
    • 𝑦 ∈ 𝑅𝑑
  • 𝑜𝑢𝑡 = 𝑋@𝑊 + b
    • out: [0.1, 0.8, 0.02, 0.08]
  • 𝑝𝑟𝑒𝑑 = 𝑎𝑟𝑔𝑚𝑎𝑥(𝑜𝑢𝑡)
    • pred: 1
    • pred: 2

在这里插入图片描述

1.5. Particularly

在这里插入图片描述

1.5. Compute Loss

  • 𝑜𝑢𝑡: [1, 10]
  • Y/label: 0~9
    • eg.: 1 → [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    • eg.: 3 → [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
  • Euclidean Distance(欧式距离): 𝑜𝑢𝑡 → Label
    • MSE

1.6. In a nutshell

  • 𝑜𝑢𝑡 = 𝑟𝑒𝑙𝑢{𝑟𝑒𝑙𝑢 𝑟𝑒𝑙𝑢 𝑋@𝑊1 + 𝑏1 @𝑊2 + 𝑏2 @𝑊3 + 𝑏3}
  • 𝑝𝑟𝑒𝑑 = 𝑎𝑟𝑔𝑚𝑎𝑥(𝑜𝑢𝑡)
  • 𝑙𝑜𝑠𝑠 = 𝑀𝑆𝐸(𝑜𝑢𝑡, 𝑙𝑎𝑏𝑒𝑙)
  • minimize 𝑙𝑜𝑠𝑠
    • [𝑊1, 𝑏1′, 𝑊2′, 𝑏2′, 𝑊3′ , 𝑏3′ ]

2. We need TensorFlow

在这里插入图片描述

3. Code

  • X and Y
  • 𝑜𝑢𝑡 = 𝑟𝑒𝑙𝑢{𝑟𝑒𝑙𝑢 𝑟𝑒𝑙𝑢 𝑋@𝑊1 + 𝑏1 @𝑊2 + 𝑏2 @𝑊3 + 𝑏3}
  • Compute out&loss
  • Compute gradient and optimize
  • Loop
import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, optimizers, datasets

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# X and Y
(x, y), (X_val, y_val) = datasets.mnist.load_data()
x = tf.convert_to_tensor(x, dtype=tf.float32) / 255
y = tf.convert_to_tensor(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10)
print(x.shape, y.shape)
train_dataset = tf.data.Dataset.from_tensor_slices((x, y))
train_dataset = train_dataset.batch(200)

# for step, (x, y) in enumerate(train_dataset):
#     print(step, x.shape, y, y.shape)

# 𝑜𝑢𝑡 = 𝑟𝑒𝑙𝑢{𝑟𝑒𝑙𝑢 𝑟𝑒𝑙𝑢 𝑋@𝑊1 + 𝑏1 @𝑊2 + 𝑏2 @𝑊3 + 𝑏3}
model = keras.Sequential([
    layers.Dense(512, activation='relu'),
    layers.Dense(256, activation='relu'),
    layers.Dense(10)
])

optimizer = optimizers.SGD(learning_rate=0.001)


def train_epoch(epoch):
    # loop
    for step, (x, y) in enumerate(train_dataset):
        with tf.GradientTape() as tape:
            # [b, 28, 28] => [b, 784]
            x = tf.reshape(x, (-1, 28 * 28))
            # compute output
            # [b, 784] => [b, 10]
            out = model(x)
            # compute loss
            loss = tf.reduce_sum(tf.square(out - y)) / x.shape[0]

        # optimize and update w1, w2, w3, b1, b2, b3
        grads = tape.gradient(loss, model.trainable_variables)
        # w' = w - lr * grad
        optimizer.apply_gradients((zip(grads, model.trainable_variables)))

        if step % 100 == 0:
            print(epoch, step, 'loss:', loss.numpy())


def train():
    for epoch in range(300):
        train_epoch(epoch)


if __name__ == '__main__':
    train()

4. Pycharm无法对TensorFlow2.0中的Keras模块进行自动补全的解决办法

  • TensorFlow2.0之后,Keras作为其主要模块被官方推荐使用。但是在pycharm中使用tensorflow.keras总是无法弹出自动补全,导致编码效率过慢。其实原因很简单,这就是Pycharm的一个bug。下载使用Pycharm2019.3之后的版本就能解决。
  • 当前(2019年11月21日)2019.3版本还没有正式发布,可以使用Pycharm EAP版本,也就是测试版,链接在此:点我下载

5. 需要全套课程视频+PPT+代码资源可以私聊我

  • 方式1:CSDN私信我!
  • 方式2:QQ邮箱:594042358@qq.com或者直接加我QQ: 594042358!
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值