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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值