【深度学习程序实例】

以下是一个使用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之间。然后,构建一个简单的神经网络模型,包括一个输入层、一个隐藏层和一个输出层。接下来,编译模型,指定优化器、损失函数和评估指标。然后,使用训练数据训练模型,并在测试数据上评估模型的性能。最后,使用模型进行预测并输出结果。

相关推荐
ViperL117 小时前
[优化算法]神经网络结构搜索(一)
深度学习·神经网络·计算机视觉
Learn Beyond Limits17 小时前
The learning process of Decision Tree Model|决策树模型学习过程
人工智能·深度学习·神经网络·学习·决策树·机器学习·ai
数据爬坡ing17 小时前
从挑西瓜到树回归:用生活智慧理解机器学习算法
数据结构·深度学习·算法·决策树·机器学习
m0_6770343518 小时前
机器学习-异常检测
人工智能·深度学习·机器学习
Niuguangshuo19 小时前
深度学习基本模块:Conv2D 二维卷积层
人工智能·深度学习
A尘埃21 小时前
TensorFlow 和 PyTorch两大深度学习框架训练数据,并协作一个电商推荐系统
pytorch·深度学习·tensorflow
西猫雷婶1 天前
pytorch基本运算-分离计算
人工智能·pytorch·python·深度学习·神经网络·机器学习
程序员miki1 天前
RNN循环神经网络(一):基础RNN结构、双向RNN
人工智能·pytorch·rnn·深度学习
却道天凉_好个秋1 天前
深度学习(四):数据集划分
人工智能·深度学习·数据集
AI人工智能+1 天前
炫光活体检测技术:通过光学技术实现高效、安全的身份验证,有效防御多种伪造手段。
人工智能·深度学习·人脸识别·活体检测