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

cnn机器学习时python版本不兼容报错

在使用python执行CNN算法时,发生如下报错:

A module that was compiled using NumPy 1.x cannot be run in NumPy 2.1.1 as it may crash. 
To support both 1.x and 2.x versions of NumPy, modules must be compiled with NumPy 2.0. Some module may need to rebuild instead e.g. with 'pybind11>=2.12'. If you are a user of the module, the easiest solution will be to downgrade to 'numpy<2' or try to upgrade the affected module. 
We expect that some modules will need time to support NumPy 2.

这时候需要安装指定版本。

pip install numpy==1.26.4

 安装完成后重新运行代码。

import tensorflow as tf
from keras import datasets, layers, models
import matplotlib.pyplot as plt# 加载 MNIST 数据集
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
train_images, test_images = train_images / 255.0, 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'])# 训练模型
history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))# 评估模型
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f'\nTest accuracy: {test_acc:.4f}')# 绘制训练过程中的准确率
plt.figure(figsize=(12, 4))plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Accuracy Over Time')
plt.show()

成功运行得出结果。

 


http://www.mrgr.cn/news/33814.html

相关文章:

  • tb的数数问题(牛客小白月赛)
  • 算法打卡:第十一章 图论part04
  • “Boolean yes=TRUE;“是正确的boolean变量声明???
  • 干货 | 2024数智新时代制造业数字化创新实践白皮书(免费下载)
  • 制造解法 Manufactured Solutions 相关的论文的阅读笔记
  • linux-----进程控制
  • 妈妈再也不用担心字符串方法啦!——js String实例方法汇总
  • 分布式安装LNMP
  • 基于 Web 的工业设备监测系统:非功能性需求与标准化数据访问机制的架构设计
  • 传输层 III(TCP协议——可靠传输)【★★★★】
  • 【Spring 底层原理】手搓一个Spring框架
  • 【busybox记录】【shell指令】numfmt
  • 嵌入式系统基础讲解
  • 用apache httpd来实现反向代理
  • golang学习笔记3-变量的声明
  • CORS跨域+Nginx配置、Apache配置
  • 2024.9.22
  • screen使用——关机时在服务器上跑代码
  • 蓝桥杯嵌入式的学习总结
  • UE学习篇ContentExample解读-----------Blueprint_Overview