- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用来自预训练模型的 numpy 数组来初始化我的 keras 模型中的内核。因此,我正在编写自己的 Initializer 函数。该函数嵌入在可调用类中,以避免在使用 load_model 时出现问题。似乎将数组作为参数传递给初始化程序在我的情况下不起作用。
我当前的解决方案灵感来自:keras kernel initializers are called incorrectly when using load_model在将单个数字参数传递给初始化函数的情况下,提供的答案对我来说很好。使用数组时会发生错误。
我也查看了使用 set_weights 的解决方案,但它们不适用于我的情况,因为我只想初始化模型的一部分。
我使用以下导入:
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
使用 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.
如何更改我的初始化器类,以便我可以使用数组成功初始化并随后加载模型?
最佳答案
你必须使用 model.layers.set_weights([kernels,bias_vector])
使用随机权重编译模型,然后设置权重。
你不能设置特定的权重,你必须制作整个权重向量并将图层设置为一个整体。
关于python - 使用 numpy 数组的 Keras 内核初始化无法使用 load_model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56190405/
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!