tiaya01 2022-12-17 16:58 采纳率: 85.7%
浏览 10
已结题

Python这是哪里的错误啊?

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, 'th picture shows:', result)

  • 写回答

1条回答 默认 最新

  • ShowMeAI 2022-12-17 17:15
    关注

    把你的第7行代码,data.files删除

    望采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月26日
  • 已采纳回答 12月18日
  • 创建了问题 12月17日

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加