第一个TensorFlow程序

MNIST数据集
MNIST数据集是一个大型的手写体数字数据库,通常用于训练各种图像处理系统,也被广泛用于机器学习领域的训练和测试。MNIST数据库中的图像集是NIST(National Institute of Standards and Technology)的两个数据库的组合:专用数据库1和特殊数据库3。数据集是有250人手写数字组成,50%是高中学生,50%是美国人口普查局。
MNIST数据集分为60,000张的训练数据集合10,000张的测试数据集,每张图像的大小为28x28(像素);每张图像都为灰度图像,位深度为8(灰度图像是0-255)

程序代码部分

import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model

# 加载并准备 MNIST 数据集。
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

# 增加维数
x_train = x_train[..., tf.newaxis]
x_test = x_test[..., tf.newaxis]

# 切分数据集,以混淆数据集
# from_tensor_slices:它的作用是把给定的元组、列表和张量等数据进行特征切片。切片的范围是从最外层维度开始的。
# shuffle(buffer_size, seed=None, reshuffle_each_iteration=None)
train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)


# 构建tf.keras模型
class MyModel(Model):
    def __init__(self):
        super(MyModel, self).__init__()
        # ReLU(Rectified Linear Unit, 修正线性单元): max(0, x)
        self.conv1 = Conv2D(32, 3, activation='softmax')
        self.flatten = Flatten()
        self.d1 = Dense(128, activation='softmax')
        # softmax: si = e^i /(Σej)
        self.d2 = Dense(10, activation='softmax')

    def call(self, x):
        x = self.conv1(x)
        x = self.flatten(x)
        x = self.d1(x)
        return self.d2(x)


model = MyModel()

# 损失函数和优化器
# SparseCategoricalCrossentropy分类交叉熵函数
loss_obj = tf.keras.losses.SparseCategoricalCrossentropy()
# Adam随机梯度(SGD)下降与动量结合(Momentum)
optimizer = tf.keras.optimizers.Adam()

# 设定损失函数和准确率的评估标准
# SparseTopKCategoricalAccuracy (稀疏多分类TopK准确率,要求y_true(label)为序号编码形式)
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')

test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')


# 使用tf.GradientTape来训练模型
# @ :装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能.
@tf.function
def train_step(images, labels):
    # 求解梯度
    with tf.GradientTape() as tape:
        # 得到预测结构,和进行损失值的计算
        pre = model(images)
        los = loss_obj(labels, pre)
    # gadient是对los中的variabs自动求梯度,返回结果
    gradients = tape.gradient(los, model.trainable_variables)
    # 自动更新模型参数
    optimizer.apply_gradients(zip(gradients, model.trainable_variables))
    train_loss(los)
    train_accuracy(labels, pre)


# 测试模型
@tf.function
def test_step(images, labels):
    pred = model(images)
    t_loss = loss_obj(labels, pred)

    test_loss(t_loss)
    test_accuracy(labels, pred)


if __name__ == "__main__":
    EPOCH = 50

    for epoch in range(EPOCH):
        train_loss.reset_states()
        train_accuracy.reset_states()
        test_loss.reset_states()
        test_accuracy.reset_states()

        for images, labels in train_ds:
            train_step(images, labels)
        for test_images, test_labels in test_ds:
            test_step(test_images, test_labels)

        template = 'Epoch:{} , loss:{}, Accuracy:{}%, Test Loss:{}, Test Accuracy:{}%'
        print(template.format(epoch+1, train_loss.result(), train_accuracy.result()*100, test_loss.result(), test_accuracy.result()*100))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个二维卷积神经网络的tensorflow程序示例: ```python import tensorflow as tf # 定义输入和输出 x = tf.placeholder(tf.float32, shape=[None, 28, 28, 1]) y = tf.placeholder(tf.float32, shape=[None, 10]) # 定义卷积层 conv1 = tf.layers.conv2d(inputs=x, filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.relu) pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) # 定义第二个卷积层 conv2 = tf.layers.conv2d(inputs=pool1, filters=64, kernel_size=[5, 5], padding="same", activation=tf.nn.relu) pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) # 将卷积层的输出展平为一维向量 pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64]) # 定义全连接层 dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu) dropout = tf.layers.dropout(inputs=dense, rate=0.4) # 定义输出层 logits = tf.layers.dense(inputs=dropout, units=10) # 定义损失函数和优化器 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits)) train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) # 训练模型 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(1000): batch_x, batch_y = mnist.train.next_batch(100) sess.run(train_op, feed_dict={x: batch_x, y: batch_y}) if i % 100 == 0: acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}) print("Step %d, accuracy: %f" % (i, acc)) ``` 此程序使用了两个卷积层和一个全连接层,通过交叉熵损失函数和Adam优化器进行训练。在训练过程中,使用了MNIST数据集进行测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值