【深度学习程序实例】

以下是一个使用Python编写的深度学习程序实例,用于训练一个简单的神经网络来分类手写数字:

python 复制代码
import tensorflow as tf
from tensorflow.keras.datasets import mnist

# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# 数据预处理
x_train = x_train / 255.0
x_test = x_test / 255.0

# 构建神经网络模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

# 编译模型
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 训练模型
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))

# 评估模型
loss, accuracy = model.evaluate(x_test, y_test)
print('Test loss:', loss)
print('Test accuracy:', accuracy)

# 使用模型进行预测
predictions = model.predict(x_test[:5])
print('Predictions:', predictions)

这个程序使用TensorFlow深度学习框架,通过加载MNIST数据集进行手写数字分类任务。首先,将数据进行预处理,将像素值缩放到0到1之间。然后,构建一个简单的神经网络模型,包括一个输入层、一个隐藏层和一个输出层。接下来,编译模型,指定优化器、损失函数和评估指标。然后,使用训练数据训练模型,并在测试数据上评估模型的性能。最后,使用模型进行预测并输出结果。

以下是一个使用Python编写的深度学习程序实例,用于训练一个简单的神经网络来分类手写数字:

python 复制代码
import tensorflow as tf
from tensorflow.keras.datasets import mnist

# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# 数据预处理
x_train = x_train / 255.0
x_test = x_test / 255.0

# 构建神经网络模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

# 编译模型
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 训练模型
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))

# 评估模型
loss, accuracy = model.evaluate(x_test, y_test)
print('Test loss:', loss)
print('Test accuracy:', accuracy)

# 使用模型进行预测
predictions = model.predict(x_test[:5])
print('Predictions:', predictions)

这个程序使用TensorFlow深度学习框架,通过加载MNIST数据集进行手写数字分类任务。首先,将数据进行预处理,将像素值缩放到0到1之间。然后,构建一个简单的神经网络模型,包括一个输入层、一个隐藏层和一个输出层。接下来,编译模型,指定优化器、损失函数和评估指标。然后,使用训练数据训练模型,并在测试数据上评估模型的性能。最后,使用模型进行预测并输出结果。

相关推荐
weixin_468466851 小时前
千问大模型在阿里生态中的实战应用指南
大数据·人工智能·深度学习·ai·大模型·智能交互·自动应答
wangqiaowq1 小时前
预训练 后预训练 微调
人工智能·深度学习·机器学习
lqqjuly2 小时前
知识蒸馏:理论、算法与可运行实现
人工智能·深度学习·算法
Rocky Ding*2 小时前
一文读懂HiDream-I1稀疏 DiT 图像生成基础模型
论文阅读·人工智能·深度学习·机器学习·ai作画·aigc·ai-native
weixin_429630263 小时前
3.46 基于改进孪生神经网络的手机摄影视觉定位
人工智能·深度学习·神经网络
憨波个5 小时前
【语音识别】Conformer: Convolution-augmented Transformer for Speech Recognition
人工智能·深度学习·transformer·语音识别
动物园猫6 小时前
睡岗检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·分类
大模型最新论文速读6 小时前
05-29 · LLM 最新论文速览
论文阅读·人工智能·深度学习·机器学习·自然语言处理
杀生丸学AI6 小时前
【三维重建】RT-Splatting:基于3DGS的联合反射-透射建模(CVPR 2026)
人工智能·深度学习·3d·三维重建·高斯泼溅·动态重建·镜面反射