[ML][Tensorflow] 训练模型的基本步骤

https://www.tensorflow.org/tutorials/keras/classification?hl=zh-cnicon-default.png?t=M85Bhttps://www.tensorflow.org/tutorials/keras/classification?hl=zh-cn 

from MY import wrapper1

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

# 获取数据集,以便用作训练和验证
fashion_mnist = keras.datasets.fashion_mnist

# 从数据集里面获取训练数据和验证数据
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# 规划神经网络
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])

# 为神经网络添加配套的 优化器 , 损失函数 和 指标
model.compile(optimizer='adam',
              loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

# 开始训练
model.fit(train_images, train_labels, epochs=10)

# 评估训练结果
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print('\nTest accuracy:', test_acc)

# ============ 以上是生成模型的步骤,模型的训练和生成 同时需要 训练集 和 验证集,训练集用来形成模型,验证集用来矫正模型精准度。

# ============ 下面开始使用模型

# 基于之前生成的model , 再在后面添加一个 softmax 层,这个层是一个数据展示层,不会对整个模型产生任何影响,只是为了方便展示结果
probability_model = keras.Sequential([model,keras.layers.Softmax()])

# 使用模型开始预测某个图像(这里用test_images来作为入参)
predictions = probability_model.predict(test_images)

# 输出预测结果
print(predictions[0])



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值