TensorFlow在Microsoft Windows 11下编程

运行平台:Microsoft Windows 11

IDE:VScode

FrameWork:TensorFlow

programming language:python

一:TensorFlow在Microsoft Windows 11下的安装

Tensor Flow是Google的一个开源深度学习框架。

TensorFlow的安装:

python版本要求:TensorFlow支持Python3.7-3.11。(具体可能因TensorFlow版本而异)。

虚拟环境:建议先创建并激活虚拟环境

复制代码
python -m venv tf_env
.\tf_env\Scripts\activate #windows

安装CPU版本:

二:TensorFlow在Microsoft Windows 11下用VScode编程

可以用AI编程工具自动生成一段应用TensorFlow的代码,或者ChatGPT也行。将代码放入VScode进行调试

python 复制代码
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist

# 超参数
batch_size = 64
num_classes = 10
epochs = 5

# 下载和准备 MNIST 数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape((60000, 28, 28, 1)).astype('float32') / 255
x_test = x_test.reshape((10000, 28, 28, 1)).astype('float32') / 255

# 将标签转换为分类格式
y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)

# 构建模型
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(num_classes, activation='softmax'))

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

# 训练模型
model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.1)

# 评估模型
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'\nTest accuracy: {test_acc:.4f}')

以上内容仅供参考,如有不对,欢迎指正。

说明:包含AI辅助生成内容

相关推荐
正经教主1 小时前
【FDE系列】阶段2:Day 29:FastAPI 进阶 — Pydantic 模型与完整 CRUD 实战
人工智能·python·fde
gCode Teacher 格码致知1 小时前
Pandas教学-6:coerce详解
python·pandas
梦在远山后1 小时前
Electron 与 FastAPI 如何完成流式 Agent 对话
python·langchain·agent
kakakahahahaha1 小时前
Windows Steam 游戏存档在哪里?定位和恢复排查流程
windows·其他·电脑·笔记本电脑·内容运营·软件需求
Xy-unu1 小时前
Windows 开启外接显示器 HDR 后外接显示器无信号、无法扩展模式故障记录
windows
时速GEO系统1 小时前
初元 AI V1.1 版本升级,首个正式版发布一周・实现生成、部署、上线、优化全流程自动化闭环
人工智能·数据挖掘·node.js
大衛說1 小时前
12 · 文件 I/O 与序列化
python
咕泡科技1 小时前
咕泡科技FDE系列最新产品重磅发布!
人工智能·大模型·ai落地·fde·前沿部署工程师
犀利豆1 小时前
为什么全世界的 AI 都画不好一只骑自行车的鹈鹕
人工智能·llm·aigc
小静AI工程实验室1 小时前
Python 爬虫中文乱码排查:严格解码、UTF-8 BOM 与 JSON 转义的 12 项实验
字符编码·爬虫·python