tensorflow - 使用 load_model 时,keras 内核初始化程序被错误调用

标签 tensorflow keras google-colaboratory

Keras 版本 2.2.4, tensorflow 版本1.13.1, 我正在使用 Colab 笔记本

我正在尝试创建自定义初始值设定项并使用 model.save() 保存模型,但是当我再次加载模型时,出现以下错误:

TypeError: myInit() missing 1 required positional argument: 'input_shape'

我有以下代码:

import numpy as np

import tensorflow as tf
import keras

from google.colab import drive

from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Flatten, Lambda, Reshape, Activation
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras import backend as K
K.set_image_data_format('channels_first')
K.backend()
# the output should be 'tensorflow'

'tensorflow'

def myInit( input_shape, dtype=None):
  weights = np.full( input_shape, 2019 )
  return K.variable( weights, dtype=dtype )

这个初始化器被赋予一个 input_shape 并返回一个 keras 张量,如文档中所示:https://keras.io/initializers/

model = Sequential()
model.add( 
  Dense( 40, input_shape=(784,) )
)
model.add(
  Dense( 30, kernel_initializer=myInit )
)
model.add(
  Dense( 5 )
)
model.build()

权重已正确初始化,因为当我调用 model.layers[1].get_weights() 时,我得到了一个充满 2019 的数组。 我使用 model.save 保存模型:

model.save(somepath)

然后在另一个笔记本中调用

model = load_model(somepath, 
           custom_objects={
               'tf' : tf,
               'myInit' : myInit
           }
          )

在此笔记本中 myInit 以及所有导入均已定义。 当我调用 load_model 时,出现以下错误:

TypeError: myInit() missing 1 required positional argument: 'input_shape'

所以看起来当模型加载时,input_shape没有传递给myInit。有人知道吗?

完整跟踪:

TypeError                                 Traceback (most recent call last)

<ipython-input-25-544d137de03f> in <module>()
      2            custom_objects={
      3                'tf' : tf,
----> 4                'myInit' : myInit
      5            }
      6           )

/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in load_model(filepath, custom_objects, compile)
    417     f = h5dict(filepath, 'r')
    418     try:
--> 419         model = _deserialize_model(f, custom_objects, compile)
    420     finally:
    421         if opened_new_file:

/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in _deserialize_model(f, custom_objects, compile)
    223         raise ValueError('No model found in config.')
    224     model_config = json.loads(model_config.decode('utf-8'))
--> 225     model = model_from_config(model_config, custom_objects=custom_objects)
    226     model_weights_group = f['model_weights']
    227 

/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in model_from_config(config, custom_objects)
    456                         '`Sequential.from_config(config)`?')
    457     from ..layers import deserialize
--> 458     return deserialize(config, custom_objects=custom_objects)
    459 
    460 

/usr/local/lib/python3.6/dist-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
     53                                     module_objects=globs,
     54                                     custom_objects=custom_objects,
---> 55                                     printable_module_name='layer')

/usr/local/lib/python3.6/dist-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    143                     config['config'],
    144                     custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 145                                         list(custom_objects.items())))
    146             with CustomObjectScope(custom_objects):
    147                 return cls.from_config(config['config'])

/usr/local/lib/python3.6/dist-packages/keras/engine/sequential.py in from_config(cls, config, custom_objects)
    298         for conf in layer_configs:
    299             layer = layer_module.deserialize(conf,
--> 300                                              custom_objects=custom_objects)
    301             model.add(layer)
    302         if not model.inputs and build_input_shape:

/usr/local/lib/python3.6/dist-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
     53                                     module_objects=globs,
     54                                     custom_objects=custom_objects,
---> 55                                     printable_module_name='layer')

/usr/local/lib/python3.6/dist-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    145                                         list(custom_objects.items())))
    146             with CustomObjectScope(custom_objects):
--> 147                 return cls.from_config(config['config'])
    148         else:
    149             # Then `cls` may be a function returning a class.

/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in from_config(cls, config)
   1107             A layer instance.
   1108         """
-> 1109         return cls(**config)
   1110 
   1111     def count_params(self):

/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

/usr/local/lib/python3.6/dist-packages/keras/layers/core.py in __init__(self, units, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
    846         self.activation = activations.get(activation)
    847         self.use_bias = use_bias
--> 848         self.kernel_initializer = initializers.get(kernel_initializer)
    849         self.bias_initializer = initializers.get(bias_initializer)
    850         self.kernel_regularizer = regularizers.get(kernel_regularizer)

/usr/local/lib/python3.6/dist-packages/keras/initializers.py in get(identifier)
    509     elif isinstance(identifier, six.string_types):
    510         config = {'class_name': str(identifier), 'config': {}}
--> 511         return deserialize(config)
    512     elif callable(identifier):
    513         return identifier

/usr/local/lib/python3.6/dist-packages/keras/initializers.py in deserialize(config, custom_objects)
    501                                     module_objects=globals(),
    502                                     custom_objects=custom_objects,
--> 503                                     printable_module_name='initializer')
    504 
    505 

/usr/local/lib/python3.6/dist-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    152             custom_objects = custom_objects or {}
    153             with CustomObjectScope(custom_objects):
--> 154                 return cls(**config['config'])
    155     elif isinstance(identifier, six.string_types):
    156         function_name = identifier

TypeError: myInit() missing 1 required positional argument: 'input_shape'

请注意,我也在 https://github.com/keras-team/keras/issues/12452 上发布了此内容,但我认为这是一个更好的地方。

最佳答案

查看源代码后,我得到了以下工作代码,这应该是定义初始值设定项的正确方法(特别是在使用 load_model 加载模型时):

import numpy as np

import tensorflow as tf
import keras

from google.colab import drive

from keras.models import Sequential, load_model
from keras.layers import Dense
from keras import backend as K
from keras.initializers import Initializer

K.backend()
# the output should be 'tensorflow'
class myInit( Initializer ):
  def __init__(self, myParameter):
    self.myParameter = myParameter

  def __call__(self, shape, dtype=None):

    # array filled entirely with 'myParameter'
    weights = np.full( shape, self.myParameter )
    return K.variable( weights, dtype=dtype )

  def get_config(self):
      return {
          'myParameter' : self.myParameter
      }

构建模型:

model = Sequential()
model.add( 
  Dense( 2, input_shape=(784,) )
)
model.add(
  Dense( 3, kernel_initializer=myInit( 2019 ) )
)
model.add(
  Dense( 5 )
)
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

保存模型:

model.save( somepath )

现在我们可以在不同的笔记本中加载模型。来自其他笔记本的导入也应在此处导入,并且 myInit 也应在此笔记本中定义。

model = load_model( somepath, 
           custom_objects={
               'tf' : tf,
               'myInit' : myInit
           }
          )

关于tensorflow - 使用 load_model 时,keras 内核初始化程序被错误调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55120395/

相关文章:

python - 将稠密张量转换为参差不齐的张量

tensorflow - Keras ImageDataGenerator 预处理

python - 如何应用经过训练的神经网络将输出写入 csv 文件?

python - keras 编码器和解码器上的分割自动编码器

python - model.summary() - 属性错误 : 'Tensor' object has no attribute 'summary'

jupyter-notebook - 无法在我的 Google Drive 中打开 Colab Notebook

jupyter-notebook - 如何在google colab中显示文件夹中的图像

python - 如何在脚本中加载 tflite 模型?

tensorflow - 在 Google Cloud ML Engine 上训练时出现 "Import error:No module named Cython.Build"

python - 如何在Google Colab中播放mp4视频