tensorflow2.0通过梯度下降法实现手写数字识别

import tensorflow as tf
import pandas as pd
import numpy as np

print(tf.__version__)
print(tf.test.is_gpu_available())

(train_image, train_label), (test_image, testlabel) = tf.keras.datasets.mnist.load_data()

# (60000, 28, 28)
# (60000,)
# (10000, 28, 28)
# print(train_image.shape)
# print(train_label.shape)
# print(test_image.shape)

# 数据处理
train_image = tf.cast(train_image / 255, tf.float64)
test_image = tf.cast(test_image / 255, tf.float64)
train_label = tf.cast(train_label, tf.int16)
testlabel = tf.cast(testlabel, tf.int16)

train_image = tf.reshape(train_image, (-1, 28, 28, 1))
test_image = tf.reshape(test_image, (-1, 28, 28, 1))

# (60000, 28, 28, 1)
# (60000,)
# print(train_image.shape)
# print(train_label.shape)

# 封装数据集
train_dataset = tf.data.Dataset.from_tensor_slices((train_image, train_label)).shuffle(train_image.shape[0]).batch(64)
test_dataset = tf.data.Dataset.from_tensor_slices((test_image, testlabel)).batch(64)

# 定义模型
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(16, 3, 1, 'same', activation='relu', input_shape=(28, 28, 1)),
    tf.keras.layers.Conv2D(32, 3, 1, 'same', activation='relu'),
    tf.keras.layers.GlobalAveragePooling2D(),
    tf.keras.layers.Dense(10)
])

print(model.summary())

# 定义优化器
optimizers_adam = tf.keras.optimizers.Adam()

# 定义损失函数
loss_func = tf.losses.SparseCategoricalCrossentropy(from_logits=True)

train_loss = tf.keras.metrics.Mean('train_loss')
train_acc = tf.keras.metrics.SparseCategoricalAccuracy('train_acc')
test_loss = tf.keras.metrics.Mean('test_loss')
test_acc = tf.keras.metrics.SparseCategoricalAccuracy('test_acc')


def train_step(model, image, label):
    with tf.GradientTape() as t:
        pred = model(image)
        loss_step = loss_func(label, pred)

    gradients = t.gradient(loss_step, model.trainable_variables)
    optimizers_adam.apply_gradients(zip(gradients, model.trainable_variables))
    # 求训练的平均损失与准确率
    train_loss(loss_step)
    train_acc(label, pred)


def test_step(model, image, label):
    pred = model(image)
    loss_step = loss_func(label, pred)
    # 求测试的平均损失与准确率
    test_loss(loss_step)
    test_acc(label, pred)


def train():
    for epoch in range(10):
        for batch, (image, label) in enumerate(train_dataset):
            train_step(model, image, label)
        print('epoch {} train_loss: {} train_acc: {}'.format(epoch + 1, train_loss.result(), train_acc.result()))

        for batch, (image, label) in enumerate(test_dataset):
            test_step(model, image, label)
        print('epoch {} test_loss: {} test_acc: {}'.format(epoch + 1, test_loss.result(), test_acc.result()))

        train_loss.reset_states()
        train_acc.reset_states()
        test_loss.reset_states()
        test_acc.reset_states()


if __name__ == '__main__':
    train()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用TensorFlow 2.0实现梯度下降的示例代码: ``` import tensorflow as tf # 定义训练数据 x_train = [1.0, 2.0, 3.0, 4.0] y_train = [2.0, 4.0, 6.0, 8.0] # 定义模型参数 W = tf.Variable(1.0) b = tf.Variable(1.0) # 定义模型 def model(x): return W * x + b # 定义损失函数 def loss(predicted_y, desired_y): return tf.reduce_mean(tf.square(predicted_y - desired_y)) # 定义训练函数 def train(x, y, learning_rate): with tf.GradientTape() as t: current_loss = loss(model(x), y) dW, db = t.gradient(current_loss, [W, b]) W.assign_sub(learning_rate * dW) b.assign_sub(learning_rate * db) # 训练模型 for epoch in range(100): for x, y in zip(x_train, y_train): train(x, y, learning_rate=0.01) current_loss = loss(model(x_train), y_train) print(f"Epoch {epoch}: Loss: {current_loss.numpy()}") # 测试模型 x_test = [5.0, 6.0, 7.0, 8.0] y_test = [10.0, 12.0, 14.0, 16.0] predicted_y = model(x_test) print(f"Predicted Y: {predicted_y.numpy()}") print(f"Desired Y: {y_test}") ``` 在这个例子中,我们使用TensorFlow 2.0实现了一个简单的线性回归模型,并使用梯度下降算法对其进行了训练。我们首先定义了训练数据,然后定义了模型参数W和b。接下来,我们定义了模型函数,它将输入x映射到输出y。然后,我们定义了损失函数,它将模型的预测输出y与真实输出y进行比较,并计算它们之间的平方差。最后,我们定义了一个训练函数,它使用梯度带自动计算模型参数W和b的梯度,并使用学习率更新它们。在训练过程中,我们迭代地将训练数据馈送给训练函数,直到达到指定的训练轮数。最后,我们使用训练好的模型对测试数据进行预测,并将预测结果与真实结果进行比较。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值