[英]module 'h5py' has no attribute 'File' when trying to save a tensorflow model
So I Just made a small NN with the MNIST Digit Database and I'm trying to save it.所以我刚刚使用 MNIST 数字数据库制作了一个小型 NN,我正在尝试保存它。 Here is the full code:
这是完整的代码:
# 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")
And here is the traceback:这是回溯:
---------------------------------------------------------------------------
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'
My versions:我的版本:
Any help would be greatly appreciated.任何帮助将不胜感激。
Try this.Its working fine.试试这个。它工作正常。
from tensorflow.keras.models import load_model
model.save("model.h5")
print("Saved model to disk")
# load model
model = load_model('model.h5')
I solved this problem in my environment downgrading the h5py module to 3.1.0, witch in TensorFlow 2.6 compatible.我在将 h5py 模块降级到 3.1.0 的环境中解决了这个问题,与 TensorFlow 2.6 兼容。
Actually, this is a problem with versions of the different libraries.实际上,这是不同库版本的问题。 I have also faced the same problem when using TensorFlow.
我在使用 TensorFlow 时也遇到了同样的问题。 From the anaconda prompt, you can check which version of h5py is compatible with TensorFlow.
从 anaconda 提示符中,您可以检查哪个版本的 h5py 与 TensorFlow 兼容。 I have tried uninstalling and reinstalling many times but at last, installing a compatible version of h5py, the problem was solved.
我已经尝试卸载并重新安装了很多次,但最后,安装了兼容版本的 h5py,问题就解决了。 Conda prompt screenshot can be checked here .
可以在此处查看Conda 提示屏幕截图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.