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

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()

http://www.xdnf.cn/news/6179.html

相关文章:

  • 字符田字格绘制
  • Java的多线程笔记
  • 企业报表平台如何实现降本增效
  • requestAnimationFrame 与 requestIdleCallback 对比
  • JavaScript中执行上下文和执行栈是什么?
  • Linux `whoami` 命令深度解析与高阶应用指南
  • 【USRP】在linux下安装python API调用
  • 保姆级!MeloTTS部署 且支持流式接口
  • Vue3在使用渲染函数h时候使用v-loading
  • 最小二乘拟合曲线
  • C#强类型枚举的入门理解
  • C++矩阵操作:正交矩阵(旋转矩阵)
  • 级联与端到端对话系统架构解析:以Moshi为例
  • 材料×工艺×AI:猎板PCB重构汽车电子四层板技术逻辑
  • 基于大模型的TIA诊疗全流程智能决策系统技术方案
  • 经典还原反应解析:Wolff-Kishner机制与黄鸣龙改进法
  • DeepSeek部署中常见的问题及解决方案
  • 在 Android 的`Handler` 和 `Binder
  • 学前数学思维:等量代换
  • 多因子线性回归实战
  • fiftyone-dataset使用基础
  • 零基础学Java——终章:核心知识点与面试总结
  • Vue百日学习计划Day4-8——Gemini版
  • 从 Vue3 回望 Vue2:逻辑复用新思维——从 Mixins 到 Composables
  • 基于EFISH-SCB-RK3576/SAIL-RK3576的智能零售柜技术方案
  • 「数学::博弈论」Nim游戏(尼姆游戏)/ Luogu P2197(C++)
  • 高压启动电路--学习记录
  • 反向操作:如何用AI检测工具优化自己的论文“人味”?
  • Lightpanda开源浏览器:专为 AI 和自动化而设计的无界面浏览器
  • 小白月赛——命运之弹