我正在实现一些东西,发现批处理规范化层抛出了奇怪的Value Error。
我用来生成错误的代码如下:
x = Input(shape=(25,14,19))
bn = BatchNormalization(
momentum=0.1,
epsilon=0.00001,
gamma_regularizer=keras.initializers.ones(),
beta_constraint=keras.initializers.zeros())
y = bn(x)
,堆栈跟踪为:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-e16165265878> in <module>()
6 gamma_regularizer=keras.initializers.ones(),
7 beta_constraint=keras.initializers.zeros())
----> 8 y = bn(x)
9
/Users/jaejunlee/anaconda2/lib/python2.7/site-packages/keras/engine/base_layer.pyc in __call__(self, inputs, **kwargs)
430 '`layer.build(batch_input_shape)`')
431 if len(input_shapes) == 1:
--> 432 self.build(input_shapes[0])
433 else:
434 self.build(input_shapes)
/Users/jaejunlee/anaconda2/lib/python2.7/site-packages/keras/layers/normalization.pyc in build(self, input_shape)
105 initializer=self.gamma_initializer,
106 regularizer=self.gamma_regularizer,
--> 107 constraint=self.gamma_constraint)
108 else:
109 self.gamma = None
/Users/jaejunlee/anaconda2/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
/Users/jaejunlee/anaconda2/lib/python2.7/site-packages/keras/engine/base_layer.pyc in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
253 if regularizer is not None:
254 with K.name_scope('weight_regularizer'):
--> 255 self.add_loss(regularizer(weight))
256 if trainable:
257 self._trainable_weights.append(weight)
/Users/jaejunlee/anaconda2/lib/python2.7/site-packages/keras/initializers.pyc in __call__(self, shape, dtype)
44
45 def __call__(self, shape, dtype=None):
---> 46 return K.constant(1, shape=shape, dtype=dtype)
47
48
/Users/jaejunlee/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc in constant(value, dtype, shape, name)
425 if dtype is None:
426 dtype = floatx()
--> 427 return tf.constant(value, dtype=dtype, shape=shape, name=name)
428
429
/Users/jaejunlee/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in constant(value, dtype, shape, name, verify_shape)
212 tensor_value.tensor.CopyFrom(
213 tensor_util.make_tensor_proto(
--> 214 value, dtype=dtype, shape=shape, verify_shape=verify_shape))
215 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
216 const_tensor = g.create_op(
/Users/jaejunlee/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in make_tensor_proto(values, dtype, shape, verify_shape)
427 # If shape is None, numpy.prod returns None when dtype is not set, but raises
428 # exception when dtype is set to np.int64
--> 429 if shape is not None and np.prod(shape, dtype=np.int64) == 0:
430 nparray = np.empty(shape, dtype=np_dt)
431 else:
/Users/jaejunlee/anaconda2/lib/python2.7/site-packages/numpy/core/fromnumeric.pyc in prod(a, axis, dtype, out, keepdims)
2564
2565 return _methods._prod(a, axis=axis, dtype=dtype,
-> 2566 out=out, **kwargs)
2567
2568
/Users/jaejunlee/anaconda2/lib/python2.7/site-packages/numpy/core/_methods.pyc in _prod(a, axis, dtype, out, keepdims)
33
34 def _prod(a, axis=None, dtype=None, out=None, keepdims=False):
---> 35 return umr_prod(a, axis, dtype, out, keepdims)
36
37 def _any(a, axis=None, dtype=None, out=None, keepdims=False):
ValueError: setting an array element with a sequence.
当输入形状的批量大小未知时,伽马初始化似乎存在问题?输入应该是其他Conv2D生成的2D(25 x 14),因此其通道大小(即特征)为19。
有人可以帮助我解决这个问题吗?
答案 0 :(得分:1)
我认为您错误地使用了 regularizer 和 constraint 参数,而不是 initializer 参数:
bn = BatchNormalization(
momentum=0.1,
epsilon=0.00001,
gamma_initializer=keras.initializers.ones(),
beta_initializer=keras.initializers.zeros())