tiaya01 2022-12-18 20:04 采纳率: 85.7%
浏览 17

python 人工智能 神经网络神经网络模型,实现 MNIST 手写数字分类。

这是为什么啊?

img

#建立模型
import numpy as np
import tensorflow as tf
#读取数据
data = np.load('mnist.npz')
data.files
train_images, train_labels, test_images, test_labels = data['x_train'], data['y_train'], data['x_test'],data['y_test']
print(train_images.shape)
print(train_labels.shape)
print(test_images.shape)
print(test_labels.shape)
#交叉熵
target_y = np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
predicted_y1 = np.array([0.4, 0.5, 0.1, 0, 0, 0, 0, 0, 0, 0])
predicted_y2 = np.array([0.1, 0.2, 0.7, 0, 0, 0, 0, 0, 0, 0])
-np.sum( target_y * np.log(predicted_y1+0.0000001))
-np.sum( target_y * np.log(predicted_y2+0.0000001))
#搭建网络结构
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128, activation = 'relu'))
model.add(tf.keras.layers.Dense(10, activation = 'softmax'))
#编译模型
model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
#训练
model.fit(train_images, train_labels, verbose = 1, epochs = 20, validation_data = (test_images,test_labels))
#模型保存
model.save('model_mnist.h5')
#用模型进行预测
import tensorflow as tf
import matplotlib.pyplot as plt
model = tf.keras.models.load_model('model_mnist.h5')
model.summary()
for i in range(30):
    image = plt.imread('testimages/' + str(i) + '.jpg')
    image_new = image.reshape([1, 28, 28])
    result = model.predict(image_new)[0].argmax()
    print('The', i + 1, 'the picture shows:', result)

  • 写回答

2条回答 默认 最新

  • 阿_旭 2022-12-18 20:49
    关注

    用以下命令升级一下pillow在运行试一下

    pip install --upgrade Pillow
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 12月18日

悬赏问题

  • ¥300 寻抓云闪付tn组成网页付款链接
  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题