module 'keras.engine.topology' has no attribute 'load_weights_from_hdf5_group_by_name'

参考:

https://blog.csdn.net/heiheiya/article/details/81111932

https://blog.csdn.net/c20081052/article/details/80745969

打开 model.py,找到load_weights  函数,大概在2842行,修改位置如下:

--------------------------------------------------------------------

def load_weights(self, filepath, by_name=False, exclude=None):
    """Modified version of the correspoding Keras function with
    the addition of multi-GPU support and the ability to exclude
    some layers from loading.
    exlude: list of layer names to excluce
    """
    import h5py

    #修改第一处
    #from keras.engine import topology
    from keras.engine import saving

    if exclude:
      by_name = True

    if h5py is None:
      raise ImportError('`load_weights` requires h5py.')
    f = h5py.File(filepath, mode='r')
    if 'layer_names' not in f.attrs and 'model_weights' in f:
      f = f['model_weights']

    # In multi-GPU training, we wrap the model. Get layers
    # of the inner model because they have the weights.
    keras_model = self.keras_model
    layers = keras_model.inner_model.layers if hasattr(keras_model, "inner_model")\
      else keras_model.layers

    # Exclude some layers
    if exclude:
      layers = filter(lambda l: l.name not in exclude, layers)

    if by_name:

      #修改第二处
      #topology.load_weights_from_hdf5_group_by_name(f, layers)
      saving.load_weights_from_hdf5_group_by_name(f, layers)
    else:

      #修改第三处
      #topology.load_weights_from_hdf5_group(f, layers)
      saving.load_weights_from_hdf5_group(f, layers)
    if hasattr(f, 'close'):
      f.close()

    # Update the log directory
    self.set_log_dir(filepath)

--------------------------------------------------------------------

修改成功以后保存,并重启,然后运行代码

posted @   西北逍遥  阅读(2865)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示