Python 带有numpy数组的Keras内核初始化无法使用load\u模型
我试图使用一个来自预训练模型的numpy数组来初始化keras模型中的内核。因此,我正在编写自己的初始化函数。该函数嵌入在一个可调用类中,以避免在使用load_模型时出现问题。在我的例子中,似乎将数组作为参数传递给初始值设定项不起作用 我目前的解决方案的灵感来自: 在向初始化器函数传递单个数字参数的情况下,提供的答案对我来说很好。使用数组时会发生错误 我也研究过使用set_权重的解决方案,但它们不适用于我的情况,因为我只想初始化模型的一部分 我使用以下导入:Python 带有numpy数组的Keras内核初始化无法使用load\u模型,python,tensorflow,keras,Python,Tensorflow,Keras,我试图使用一个来自预训练模型的numpy数组来初始化keras模型中的内核。因此,我正在编写自己的初始化函数。该函数嵌入在一个可调用类中,以避免在使用load_模型时出现问题。在我的例子中,似乎将数组作为参数传递给初始值设定项不起作用 我目前的解决方案的灵感来自: 在向初始化器函数传递单个数字参数的情况下,提供的答案对我来说很好。使用数组时会发生错误 我也研究过使用set_权重的解决方案,但它们不适用于我的情况,因为我只想初始化模型的一部分 我使用以下导入: import numpy as np
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential, load_model
from keras.layers import Dense, Activation, Dropout, Add
from keras import metrics, Input, Model, optimizers
from keras.utils.generic_utils import get_custom_objects
import keras.backend as K
from keras.initializers import Initializer
使用初始值设定项类:
class myInit( Initializer ):
def __init__(self, matrix):
self.matrix = matrix
def __call__(self, shape, dtype=None):
# array filled with matrix parameter'
return K.variable(value = self.matrix, dtype=dtype )
def get_config(self):
return {
'matrix' : self.matrix
}
模型如下:
val = np.ones((2, 2))
input_l=Input(shape=(2,))
hidden=Dropout(rate=0.3,seed=0)(input_l)
x1 = Dense(2, kernel_initializer=myInit(val),
activation=None, )(hidden)
x2 = Dense(2, activation='relu')(hidden)
energy=Add()([x1,x2])
output=Activation('softmax')(energy)
model = Model(input_l,output)
model.compile(loss='categorical_crossentropy', optimizer='adam' , metrics=['categorical_accuracy'])
model_info=model.get_config()
model.save("savedmodel_ex.h5")
model = load_model("savedmodel_ex.h5", custom_objects={'myInit':myInit})
加载模型时,我收到以下错误消息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-99f620c51ed9> in <module>()
12
13 model.save("savedmodel_ex.h5")
---> 14 model = load_model("savedmodel_ex.h5", custom_objects={'myInit':myInit})
/lib/python2.7/site-packages/keras/models.pyc in load_model(filepath, custom_objects, compile)
268 raise ValueError('No model found in config file.')
269 model_config = json.loads(model_config.decode('utf-8'))
--> 270 model = model_from_config(model_config, custom_objects=custom_objects)
271
272 # set weights
/lib/python2.7/site-packages/keras/models.pyc in model_from_config(config, custom_objects)
345 'Maybe you meant to use '
346 '`Sequential.from_config(config)`?')
--> 347 return layer_module.deserialize(config, custom_objects=custom_objects)
348
349
/lib/python2.7/site-packages/keras/layers/__init__.pyc in deserialize(config, custom_objects)
53 module_objects=globs,
54 custom_objects=custom_objects,
---> 55 printable_module_name='layer')
/lib/python2.7/site-packages/keras/utils/generic_utils.pyc in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
142 return cls.from_config(config['config'],
143 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 144 list(custom_objects.items())))
145 with CustomObjectScope(custom_objects):
146 return cls.from_config(config['config'])
/lib/python2.7/site-packages/keras/engine/topology.pyc in from_config(cls, config, custom_objects)
2533 if layer in unprocessed_nodes:
2534 for node_data in unprocessed_nodes.pop(layer):
-> 2535 process_node(layer, node_data)
2536
2537 name = config.get('name')
/lib/python2.7/site-packages/keras/engine/topology.pyc in process_node(layer, node_data)
2490 if input_tensors:
2491 if len(input_tensors) == 1:
-> 2492 layer(input_tensors[0], **kwargs)
2493 else:
2494 layer(input_tensors, **kwargs)
/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, inputs, **kwargs)
590 '`layer.build(batch_input_shape)`')
591 if len(input_shapes) == 1:
--> 592 self.build(input_shapes[0])
593 else:
594 self.build(input_shapes)
/lib/python2.7/site-packages/keras/layers/core.pyc in build(self, input_shape)
862 name='kernel',
863 regularizer=self.kernel_regularizer,
--> 864 constraint=self.kernel_constraint)
865 if self.use_bias:
866 self.bias = self.add_weight(shape=(self.units,),
/lib/python2.7/site-packages/keras/legacy/interfaces.pyc in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/lib/python2.7/site-packages/keras/engine/topology.pyc in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
411 if dtype is None:
412 dtype = K.floatx()
--> 413 weight = K.variable(initializer(shape),
414 dtype=dtype,
415 name=name,
<ipython-input-17-463931c2b557> in __call__(self, shape, dtype)
8 def __call__(self, shape, dtype=None):
9 # array filled with matrix parameter'
---> 10 return K.variable(value = self.matrix, dtype=dtype )
11
12 def get_config(self):
/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc in variable(value, dtype, name, constraint)
394 v._uses_learning_phase = False
395 return v
--> 396 v = tf.Variable(value, dtype=tf.as_dtype(dtype), name=name)
397 if isinstance(value, np.ndarray):
398 v._keras_shape = value.shape
/lib/python2.7/site-packages/tensorflow/python/ops/variables.pyc in __call__(cls, *args, **kwargs)
211 def __call__(cls, *args, **kwargs):
212 if cls is VariableV1:
--> 213 return cls._variable_v1_call(*args, **kwargs)
214 elif cls is Variable:
215 return cls._variable_v2_call(*args, **kwargs)
/lib/python2.7/site-packages/tensorflow/python/ops/variables.pyc in _variable_v1_call(cls, initial_value, trainable, collections, validate_shape, caching_device, name, variable_def, dtype, expected_shape, import_scope, constraint, use_resource, synchronization, aggregation)
174 use_resource=use_resource,
175 synchronization=synchronization,
--> 176 aggregation=aggregation)
177
178 def _variable_v2_call(cls,
/lib/python2.7/site-packages/tensorflow/python/ops/variables.pyc in <lambda>(**kwargs)
153 aggregation=VariableAggregation.NONE):
154 """Call on Variable class. Useful to force the signature."""
--> 155 previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
156 for getter in ops.get_default_graph()._variable_creator_stack: # pylint: disable=protected-access
157 previous_getter = _make_getter(getter, previous_getter)
/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in default_variable_creator(next_creator, **kwargs)
2493 caching_device=caching_device, name=name, dtype=dtype,
2494 constraint=constraint, variable_def=variable_def,
-> 2495 expected_shape=expected_shape, import_scope=import_scope)
2496
2497
/lib/python2.7/site-packages/tensorflow/python/ops/variables.pyc in __call__(cls, *args, **kwargs)
215 return cls._variable_v2_call(*args, **kwargs)
216 else:
--> 217 return super(VariableMetaclass, cls).__call__(*args, **kwargs)
218
219
/lib/python2.7/site-packages/tensorflow/python/ops/variables.pyc in __init__(self, initial_value, trainable, collections, validate_shape, caching_device, name, variable_def, dtype, expected_shape, import_scope, constraint)
1393 dtype=dtype,
1394 expected_shape=expected_shape,
-> 1395 constraint=constraint)
1396
1397 def __repr__(self):
/lib/python2.7/site-packages/tensorflow/python/ops/variables.pyc in _init_from_args(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, expected_shape, constraint)
1513 else:
1514 self._initial_value = ops.convert_to_tensor(
-> 1515 initial_value, name="initial_value", dtype=dtype)
1516 # pylint: disable=protected-access
1517 if self._initial_value.op._get_control_flow_context() is not None:
/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in convert_to_tensor(value, dtype, name, preferred_dtype)
1037 ValueError: If the `value` is a tensor not of given `dtype` in graph mode.
1038 """
-> 1039 return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
1040
1041
/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in convert_to_tensor_v2(value, dtype, dtype_hint, name)
1095 name=name,
1096 preferred_dtype=dtype_hint,
-> 1097 as_ref=False)
1098
1099
/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_symbolic_tensors)
1173
1174 if ret is None:
-> 1175 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1176
1177 if ret is NotImplemented:
/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in _constant_tensor_conversion_function(v, dtype, name, as_ref)
302 as_ref=False):
303 _ = as_ref
--> 304 return constant(v, dtype=dtype, name=name)
305
306
/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in constant(value, dtype, shape, name)
243 """
244 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 245 allow_broadcast=True)
246
247
/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
281 tensor_util.make_tensor_proto(
282 value, dtype=dtype, shape=shape, verify_shape=verify_shape,
--> 283 allow_broadcast=allow_broadcast))
284 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
285 const_tensor = g.create_op(
/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast)
464 nparray = np.empty(shape, dtype=np_dt)
465 else:
--> 466 _AssertCompatible(values, dtype)
467 nparray = np.array(values, dtype=np_dt)
468 # check to them.
/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in _AssertCompatible(values, dtype)
369 else:
370 raise TypeError("Expected %s, got %s of type '%s' instead." %
--> 371 (dtype.name, repr(mismatch), type(mismatch).__name__))
372
373
TypeError: Expected float32, got {u'type': u'ndarray', u'value': [[1.0, 1.0], [1.0, 1.0]]} of type 'dict' instead.
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
12
13型号保存(“保存的型号”例如h5)
--->14 model=load_model(“savedmodel_ex.h5”,自定义_对象={'myInit':myInit})
/load_模型中的lib/python2.7/site-packages/keras/models.pyc(文件路径、自定义_对象、编译)
268 raise VALUERROR('在配置文件中找不到模型')
269 model_config=json.load(model_config.decode('utf-8'))
-->270模型=来自配置的模型(模型配置,自定义对象=自定义对象)
271
272#设置重量
/模型_中的lib/python2.7/site-packages/keras/models.pyc from_config(配置,自定义_对象)
345“也许你想用”
346'`Sequential.from_config(config)`?')
-->347返回层\模块。反序列化(配置,自定义\对象=自定义\对象)
348
349
/反序列化中的lib/python2.7/site packages/keras/layers/_init__uu.pyc(配置,自定义_对象)
53模块_对象=全局,
54自定义对象=自定义对象,
--->55可打印\u模块\u name='layer')
/反序列化_keras_对象(标识符、模块_对象、自定义_对象、可打印的_模块_名称)中的lib/python2.7/site-packages/keras/utils/generic_utils.pyc
142从配置返回cls,
143 custom\u objects=dict(列表(\u GLOBAL\u custom\u objects.items())+
-->144列表(自定义对象.items())
145带有CustomObjectScope(自定义对象):
146从_config(config['config'])返回cls
/from_config(cls,config,custom_对象)中的lib/python2.7/site-packages/keras/engine/topology.pyc
2533未处理的_节点中的if层:
2534对于未处理的_nodes.pop(层)中的节点_数据:
->2535过程节点(层、节点数据)
2536
2537 name=config.get('name')
/lib/python2.7/site-packages/keras/engine/topology.pyc进程中\节点(层、节点\数据)
2490如果输入_张量:
2491如果len(输入张量)==1:
->2492层(输入_张量[0],**kwargs)
2493其他:
2494层(输入_张量,**kwargs)
/lib/python2.7/site-packages/keras/engine/topology.pyc in___调用(self,input,**kwargs)
590'`layer.build(批处理输入形状)`
591如果len(输入_形状)==1:
-->592自我构建(输入_形状[0])
593其他:
594自我构建(输入形状)
/lib/python2.7/site-packages/keras/layers/core.pyc in-build(self,input_-shape)
862 name='kernel',
863正则化器=self.kernel\u正则化器,
-->864约束=self.kernel\u约束)
865如果自我使用偏差:
866 self.bias=self.add_权重(形状=(自身单位,),
/包装器中的lib/python2.7/site-packages/keras/legacy/interfaces.pyc(*args,**kwargs)
89警告。警告('更新您的`+对象\u名称+
90'`对Keras 2 API的调用:'+签名,stacklevel=2)
--->91返回函数(*args,**kwargs)
92包装器._原始函数=func
93返回包装器
/lib/python2.7/site-packages/keras/engine/topology.pyc in add_weight(self、name、shape、dtype、初始化器、正则化器、可训练、约束)
411如果数据类型为无:
412 dtype=K.floatx()
-->413重量=K.变量(初始值设定项(形状),
414数据类型=数据类型,
415 name=名称,
在调用中(self、shape、dtype)
8定义调用(self、shape、dtype=None):
9#填充矩阵参数的数组'
--->10返回K.变量(值=self.matrix,dtype=dtype)
11
12 def get_配置(自):
/变量中的lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc(值、数据类型、名称、约束)
394 v._使用_学习_阶段=错误
395返回v
-->396 v=tf.Variable(value,dtype=tf.as_dtype(dtype),name=name)
397如果存在(数值,np.N):
398 v._keras_shape=value.shape
/lib/python2.7/site-packages/tensorflow/python/ops/variables.pyc在__调用中(cls,*args,**kwargs)
211定义调用(cls、*ARG、**kwargs):
212如果cls是可变的V1:
-->213返回cls.\u变量\u v1\u调用(*args,**kwargs)
214 elif cls是可变的:
215返回cls.\u变量\u v2\u调用(*args,**kwargs)
/lib/python2.7/site-packages/tensorflow/python/ops/variables.pyc in_variable_v1_调用(cls,初始值,可训练,