当前位置: 首页 > news >正文

tensorflow安装及简单例程学习

1、安装python

tensorflow对python的版本要求,安装python前需要确定要安装哪个版本的python

使用 pip 安装 TensorFlow

2、升级pip

pip install --upgrade pip

3、安装tensorflow

pip3 install tensorflow -i https://pypi.mirrors.ustc.edu.cn/simple

4、安装依赖库

pip install pandas matplotlib notebook -i https://pypi.mirrors.ustc.edu.cn/simple

5、例程

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

# 加载并预处理数据
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0
train_images = train_images.reshape((train_images.shape[0], 28, 28, 1))
test_images = test_images.reshape((test_images.shape[0], 28, 28, 1))

# 创建CNN模型
model = models.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

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

model.fit(train_images, train_labels, epochs=5, batch_size=64)

# 评估模型
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Test accuracy: {test_acc}")

# 训练后保存模型
model.save('license_plate_recognition_model.h5')

# 预测并展示结果

model = tf.keras.models.load_model('license_plate_recognition_model.h5')//加载模型
predictions = model.predict(test_images)
print(f"Predicted label for first image: {np.argmax(predictions[0])}")
plt.imshow(test_images[0].reshape(28, 28), cmap=plt.cm.binary)
plt.show()

相关文章:

  • Baklib知识中台赋能业务智能跃迁
  • 使用大模型预测急性结石性疾病技术方案
  • 现代计算机图形学Games101入门笔记(六)
  • HPE ProLiant DL360 Gen11 服务器,配置 RAID 5 教程!
  • C#11的特性
  • echarts中一些关键的配置
  • SCADA|KingSCADA中如何使用自定义函数优化脚本程序?
  • Java问题排查常用命令行工具速查表
  • Android多媒体——媒体start流程分析(十三)
  • 深入解析 PostgreSQL 外部数据封装器(FDW)的 SELECT 查询执行机制
  • 【vLLM 学习】基础教程
  • 4G物联网模块实现废气处理全流程数据可视化监控配置
  • Angular 知识框架
  • Lord Of The Root: 1.0.1通关
  • 如何在终端/命令行中把PDF的每一页转换成图片(PNG)
  • 《Effective Python》第2章 字符串和切片操作——深入理解 Python 中 __repr__ 与 __str__
  • 机器学习基础课程-6-课程实验
  • android studio导入项目
  • Hadoop集群故障节点隔离操作指南
  • 通用软件项目技术报告 - 导读III
  • 上海市国防动员办公室副主任吴斌接受审查调查
  • 足球少年郎7月试锋芒,明日之星冠军杯构建顶级青少年赛事
  • 京东美团饿了么等外卖平台被约谈
  • 兰州大学教授安成邦加盟复旦大学中国历史地理研究所
  • 优化营商环境,服务上海“五个中心”建设,北外滩有何举措?
  • 美国“贸易战”前线的本土受害者:安静的洛杉矶港和准备关门的小公司