python - 尝试保存张量流模型时,模块“h5py”没有属性“文件”
问题描述
所以我刚刚使用 MNIST 数字数据库制作了一个小型 NN,我正在尝试保存它。这是完整的代码:
# Importing Libs
import h5py
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
# ---------- PART I: Importing and cleaning Data ----------
# Importing Data
train_data = np.genfromtxt('mnist_train.csv', delimiter=',')[1:]
test_data = np.genfromtxt('mnist_test.csv', delimiter=',')[1:]
train_images = train_data[:, 1:]
train_labels = train_data[:, 0]
test_images = test_data[:, 1:]
test_labels = test_data[:, 0]
class_names = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
train_images = train_images/255
test_images = test_images/255
train_images = train_images.reshape(60000, 28, 28)
test_images = test_images.reshape(10000, 28, 28)
# ---------- PART II: Making the model ----------
layers = [keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')]
model = keras.Sequential(layers)
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=1
model.save("network.h5")
这是回溯:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-36-0e5ebf05c427> in <module>
1 print('Saving Model...')
----> 2 model.save("network.h5")
~\Downloads\Anaconda\lib\site-packages\tensorflow_core\python\keras\engine\network.py in save(self, filepath, overwrite, include_optimizer, save_format, signatures, options)
1006 """
1007 save.save_model(self, filepath, overwrite, include_optimizer, save_format,
-> 1008 signatures, options)
1009
1010 def save_weights(self, filepath, overwrite=True, save_format=None):
~\Downloads\Anaconda\lib\site-packages\tensorflow_core\python\keras\saving\save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options)
97
98 if (save_format == 'h5' or
---> 99 (h5py is not None and isinstance(filepath, h5py.File)) or
100 os.path.splitext(filepath)[1] in _HDF5_EXTENSIONS):
101 # TODO(b/130258301): add utility method for detecting model type.
AttributeError: module 'h5py' has no attribute 'File'
我的版本:
- 张量流:2.1.0
- keras:2.2.4-tf
- h5py:2.10.0
- 蟒蛇:2019.10
任何帮助将不胜感激。
解决方案
试试这个。它工作正常。
from tensorflow.keras.models import load_model
model.save("model.h5")
print("Saved model to disk")
# load model
model = load_model('model.h5')
推荐阅读
- javascript - 使用 javascript 中的 if 条件验证用户名和密码时,警报框无法正常工作
- android - Delphi 10.3 Android 意图广播接收器
- java - Dropwizard 的自定义 gRPC 命令立即退出
- javascript - 让输入字段显示空白而不是 [object Object] 或 {
- php - 紧凑():未定义的变量
- reactjs - 用对象数组反应状态:可以保持一些对象不变吗?
- .net - .Net 核心和插件
- c# - 有没有正当理由在非静态类中使用静态方法?
- java - 有没有办法“获取”一个属性而不是在 Xodus 中“找到”它?
- ios - 通过使用泛型将结构作为参数传递来关闭回调