|
|
|
@ -24,7 +24,8 @@ __all__ = [
|
|
|
|
|
'Constant', 'Uniform', 'Normal', 'TruncatedNormal', 'Xavier', 'Bilinear',
|
|
|
|
|
'MSRA', 'force_init_on_cpu', 'init_on_cpu', 'ConstantInitializer',
|
|
|
|
|
'UniformInitializer', 'NormalInitializer', 'TruncatedNormalInitializer',
|
|
|
|
|
'XavierInitializer', 'BilinearInitializer', 'MSRAInitializer'
|
|
|
|
|
'XavierInitializer', 'BilinearInitializer', 'MSRAInitializer',
|
|
|
|
|
'NumpyArrayInitializer'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
_force_init_on_cpu_ = False
|
|
|
|
@ -683,6 +684,64 @@ class BilinearInitializer(Initializer):
|
|
|
|
|
return op
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NumpyArrayInitializer(Initializer):
|
|
|
|
|
"""Init an parameter with an numpy array
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
value (numpy): numpy array to initialize the variable
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
fc = fluid.layers.fc(input=x, size=10,
|
|
|
|
|
param_attr=fluid.initializer.NumpyArrayInitializer(numpy.array([1,2])))
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, value):
|
|
|
|
|
import numpy
|
|
|
|
|
assert isinstance(value, numpy.ndarray)
|
|
|
|
|
super(NumpyArrayInitializer, self).__init__()
|
|
|
|
|
self._value = value
|
|
|
|
|
|
|
|
|
|
def __call__(self, var, block):
|
|
|
|
|
"""Add constant initialization ops for a variable
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
var: Variable that needs to be initialized
|
|
|
|
|
block: The block in which initialization ops
|
|
|
|
|
should be added
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
the initialization op
|
|
|
|
|
"""
|
|
|
|
|
assert isinstance(var, framework.Variable)
|
|
|
|
|
assert isinstance(block, framework.Block)
|
|
|
|
|
# Initialization Ops should be prepended and not appended
|
|
|
|
|
dtype = framework.convert_np_dtype_to_dtype_(self._value.dtype)
|
|
|
|
|
if dtype == VarDesc.VarType.FP32:
|
|
|
|
|
value_name = "fp32_values"
|
|
|
|
|
values = [float(v) for v in self._value.flat]
|
|
|
|
|
elif dtype == VarDesc.VarType.INT32:
|
|
|
|
|
value_name = "int32_values"
|
|
|
|
|
values = [int(v) for v in self._value.flat]
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError("Unsupported dtype %s", self._value.dtype)
|
|
|
|
|
if self._value.size > 1024 * 1024 * 5:
|
|
|
|
|
raise ValueError("The size of input is too big. Please consider "
|
|
|
|
|
"saving it to file and 'load_op' to load it")
|
|
|
|
|
op = block._prepend_op(
|
|
|
|
|
type='assign_value',
|
|
|
|
|
outputs={'Out': var},
|
|
|
|
|
attrs={
|
|
|
|
|
'dtype': dtype,
|
|
|
|
|
'shape': list(self._value.shape),
|
|
|
|
|
value_name: values
|
|
|
|
|
},
|
|
|
|
|
stop_gradient=True)
|
|
|
|
|
var.op = op
|
|
|
|
|
return op
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# We short the class name, since users will use the initializer with the package
|
|
|
|
|
# name. The sample code:
|
|
|
|
|
#
|
|
|
|
|