深度学习训练脚本:使用 Python 和 TensorFlow 建立神经网络
深度学习的训练脚本通常是用 Python 编写的,可以通过命令行或者集成开发环境来运行。以下是一个简单的示例:
import tensorflow as tf
# 定义模型
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
tf.keras.layers.Dense(10, activation='softmax')
])
# 编译模型
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# 加载数据集
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# 数据预处理
x_train = x_train.reshape(60000, 784).astype('float32') / 255
x_test = x_test.reshape(10000, 784).astype('float32') / 255
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)
# 训练模型
model.fit(x_train, y_train, epochs=5, batch_size=32, validation_data=(x_test, y_test))
这个示例使用了 TensorFlow 库来建立一个简单的神经网络,训练一个手写数字分类器。您可以在命令行中运行此脚本,或使用 Python 集成开发环境(如 Jupyter Notebook)来逐步执行代码。在代码执行完成后,您将得到一个训练好的模型,可以用来进行预测。
原文地址: https://www.cveoy.top/t/topic/lQeY 著作权归作者所有。请勿转载和采集!