尝试保存张量流模型时,模块'h5py'没有属性'File'

卢卡斯

所以我只是用MNIST Digit数据库制作了一个小型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'

我的版本:

  1. 张量流:2.1.0
  2. 硬:2.2.4-tf
  3. h5py:2.10.0
  4. 水蟒: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')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用h5py(或其他方法)有效地保存和加载数据

在h5py文件上传中,np.array(file[key][:])和np.array(file[key])有什么区别

使用h5py沿新轴将数据添加到现有h5py文件

为什么在h5py数据集的并行分配中没有输出?

如何使用Python和h5py读取HDF5属性(元数据)

尝试打开h5py中的熊猫创建的hdf时缺少列

使用HDF5库和h5py模块交替显示错误

如何区分HDF5数据集和带有h5py的组?

尝试在h5py中扩展现有数据集时发生错误:ValueError:无法设置扩展数据集(维度不能超过现有的最大大小

ImportError:加载hdf5时,load_model需要h5py

AttributeError:尝试在 discord.py 中创建按钮时,模块“discord”没有属性“ui”

AttributeError:类型对象“ h5py.h5r.Reference”在使用“从keras.utils导入HDF5Matrix”时没有属性“ __reduce_cython__”

如何列出h5py文件中的所有数据集?

熊猫和h5py加载相同的数据(ndarray)的方式有所不同

有使用h5py在Python中处理大数据的经验吗?

对于带有Dask数组和/或h5py的循环

使用内存限制制作h5py文件的有效方法

h5py 的問題-“數據集”對像沒有屬性“值”

模块“ geopandas”没有属性“ read_file”

模块“<file_name>”没有属性“__path__”

如何将可变大小的数据保存到H5PY文件?

馈入多维数据集时的python h5py错误

使用python中的h5py调整和保存.h5格式的数据集的大小

模块张量流没有属性“get_default_graph”

尝试通过使用h5py更改索引字段类型来缩小HDF5文件的大小

使用h5py读取HDF5文件时使用python slice对象吗?

创建HDF5文件但不关闭它们时损坏文件(h5py)

从cmd运行时,“模块”对象没有属性“ py”

有没有办法在 h5py 中一次获取所有组中的数据集?