Python chainer.Parameter() Examples
The following are 30
code examples of chainer.Parameter().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
chainer
, or try the search function
.
Example #1
Source File: test_link.py From chainer with MIT License | 6 votes |
def setUp(self): self.l1 = chainer.Link() with self.l1.init_scope(): self.l1.x = chainer.Parameter(shape=(2, 3)) self.l1.y = chainer.Parameter() self.l2 = chainer.Link() with self.l2.init_scope(): self.l2.x = chainer.Parameter(shape=2) self.l3 = chainer.Link() with self.l3.init_scope(): self.l3.x = chainer.Parameter(shape=3) self.l4 = chainer.Link() self.l5 = chainer.Link() self.l6 = chainer.Link() self.c1 = chainer.ChainList(self.l1) self.c1.add_link(self.l2) self.c2 = chainer.ChainList(self.c1) self.c2.append(self.l3) self.c3 = chainer.ChainList(self.l4)
Example #2
Source File: test_link.py From chainer with MIT License | 6 votes |
def setUp(self): x_shape_0 = 2 x_shape_1 = numpy.int64(3) self.link = chainer.Link(x=((x_shape_0, x_shape_1), 'd'), u=(None, 'd')) with self.link.init_scope(): self.link.y = chainer.Parameter(shape=(2,)) self.link.v = chainer.Parameter() self.p = numpy.array([1, 2, 3], dtype='f') self.link.add_persistent('p', self.p) self.link.name = 'a' self.link.x.update_rule = chainer.UpdateRule() self.link.x.update_rule.enabled = False self.link.u.update_rule = chainer.UpdateRule() if cuda.available: self.current_device_id = cuda.cupy.cuda.get_device_id()
Example #3
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_deserialize(self, backend_config): # Deserializes uninitialized parameters into uninitialied ones. call_record = [] def serializer(key, value): call_record.append((key, value)) return None # to be uninitialized l = chainer.Link() with l.init_scope(): l.x = chainer.Parameter() # uninitialized l.to_device(backend_config.device) l.serialize(serializer) # Link is kept uninitialized self.assertIsNone(l.x.array) # Check inputs to the serializer self.assertEqual(len(call_record), 1) self.assertEqual(call_record[0][0], 'x') self.assertIs(call_record[0][1], None)
Example #4
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_serialize(self, backend_config): call_record = [] def serializer(key, value): call_record.append((key, value)) return value l = chainer.Link() with l.init_scope(): l.x = chainer.Parameter() # uninitialized l.to_device(backend_config.device) l.serialize(serializer) # Link is kept uninitialized self.assertIsNone(l.x.array) # Check inputs to the serializer self.assertEqual(len(call_record), 1) self.assertEqual(call_record[0][0], 'x') self.assertIs(call_record[0][1], None)
Example #5
Source File: context.py From chainer with MIT License | 6 votes |
def set_name(self, variable, name, pinned=False): """Set ONNX node name Arguments: variable (var): target variable name (str): name to be exported as ONNX node name pinned (bool): if ``True``, the name will not be overwritten in subsequence process. """ str_id = id(variable) assert str_id not in self.name_list or not self.name_list[str_id][1] self.name_list[str_id] = (name, pinned) if isinstance(variable, (chainer.Variable, chainer.Parameter)): array_id = id(variable.array) self.name_list[array_id] = (name, pinned)
Example #6
Source File: initializer.py From chainer-compiler with MIT License | 6 votes |
def convert_parameter(parameter, name): if isinstance(parameter, chainer.Parameter): array = parameter.array elif isinstance(parameter, chainer.Variable): array = parameter.array elif isinstance(parameter, np.ndarray): array = parameter else: raise ValueError( 'The type of parameter is unknown. It should be either Parameter ' 'or Variable or ndarray, but the type was {}.'.format( type(parameter))) if array.shape == (): array = array[None] # print('initialize', name, array) return onnx.numpy_helper.from_array(array, name) # 入力xから次元を決める # モデルにxを流して最初の重みを決める
Example #7
Source File: initializer.py From chainer-compiler with MIT License | 6 votes |
def convert_parameter(parameter, name): if isinstance(parameter, chainer.Parameter): array = parameter.array elif isinstance(parameter, chainer.Variable): array = parameter.array elif isinstance(parameter, np.ndarray): array = parameter else: raise ValueError( 'The type of parameter is unknown. It should be either Parameter ' 'or Variable or ndarray, but the type was {}.'.format( type(parameter))) if array.shape == (): array = array[None] # print('initialize', name, array) return tensor_from_array(array, name) # 入力xから次元を決める # モデルにxを流して最初の重みを決める
Example #8
Source File: scale.py From chainer-stylegan with MIT License | 6 votes |
def __init__(self, axis=1, W_shape=None, bias_term=False, bias_shape=None, initialW=None, initial_bias=None): super(Scale, self).__init__() self.axis = axis with self.init_scope(): # Add W parameter and/or bias term. if W_shape is not None: if initialW is None: initialW = 1 W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer, W_shape) if bias_term: self.bias = Bias(axis, W_shape, initial_bias) else: if bias_term: if bias_shape is None: raise ValueError( 'bias_shape should be given if W is not ' 'learnt parameter and bias_term is True.') self.bias = Bias(axis, W_shape, initial_bias)
Example #9
Source File: net.py From chainer-stylegan with MIT License | 6 votes |
def __init__(self, ch=512, ch_in=512, w_ch=512, upsample=True, enable_blur=False): super().__init__() self.upsample = upsample self.ch = ch self.ch_in = ch_in self.w_ch = w_ch with self.init_scope(): if not upsample: self.W = chainer.Parameter(shape=(ch_in, 4, 4)) self.W.data[:] = 1 # w_data_tmp self.b0 = L.Bias(axis=1, shape=(ch,)) self.b1 = L.Bias(axis=1, shape=(ch,)) self.n0 = NoiseBlock(ch) self.n1 = NoiseBlock(ch) self.s0 = StyleBlock(w_ch, ch) self.s1 = StyleBlock(w_ch, ch) self.c0 = EqualizedConv2d(ch_in, ch, 3, 1, 1, nobias=True) self.c1 = EqualizedConv2d(ch, ch, 3, 1, 1, nobias=True) self.blur_k = None self.enable_blur = enable_blur
Example #10
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_copyparams(self): l1 = chainer.Link() with l1.init_scope(): l1.x = chainer.Parameter(shape=(2, 3)) l1.y = chainer.Parameter() l2 = chainer.Link() with l2.init_scope(): l2.x = chainer.Parameter(shape=2) l3 = chainer.Link() with l3.init_scope(): l3.x = chainer.Parameter(shape=3) c1 = chainer.ChainList(l1, l2) c2 = chainer.ChainList(c1, l3) l1.x.data.fill(0) l2.x.data.fill(1) l3.x.data.fill(2) self.c2.copyparams(c2) numpy.testing.assert_array_equal(self.l1.x.data, l1.x.data) numpy.testing.assert_array_equal(self.l2.x.data, l2.x.data) numpy.testing.assert_array_equal(self.l3.x.data, l3.x.data)
Example #11
Source File: test_link.py From chainer with MIT License | 6 votes |
def test_serialize(self): l1 = chainer.Link() with l1.init_scope(): l1.y = chainer.Parameter(shape=(1, 1)) l2 = chainer.Link() with l2.init_scope(): l2.x = chainer.Parameter(0, 2) c1 = chainer.ChainList(l1, l2) mocks = {'0': mock.MagicMock(), '1': mock.MagicMock()} serializer = mock.MagicMock() serializer.__getitem__.side_effect = lambda k: mocks[k] serializer.return_value = None mocks['0'].return_value = None mocks['1'].return_value = None c1.serialize(serializer) self.assertEqual(serializer.call_count, 0) self.assertEqual(serializer.__getitem__.call_count, 2) serializer.__getitem__.assert_any_call('0') serializer.__getitem__.assert_any_call('1') mocks['0'].assert_called_with('y', l1.y.data) mocks['1'].assert_called_with('x', l2.x.data)
Example #12
Source File: test_variable.py From chainer with MIT License | 6 votes |
def test_copydata_to_uninitialized_parameter( self, src_backend_config, dst_backend_config): shape = self.shape dtype = np.float32 src_arr_numpy = np.asarray(np.random.randn(*shape), dtype) src_arr = src_backend_config.get_array(src_arr_numpy.copy()) dst_var = chainer.Parameter() dst_var.to_device(dst_backend_config.device) src_var = chainer.Parameter(src_arr) src_arr_prev = src_var.array dst_var.copydata(src_var) assert src_var.array is src_arr_prev assert src_var.device == src_backend_config.device assert dst_var.device == dst_backend_config.device np.testing.assert_array_equal( _numpy_device.send(dst_var.data), src_arr_numpy)
Example #13
Source File: test_link.py From chainer with MIT License | 6 votes |
def setUp(self): self.link = chainer.Link() shape = (2, 2) dtype = numpy.float32 y_array = numpy.random.rand(*shape).astype(dtype) pa_array = numpy.random.rand(*shape).astype(dtype) ps_scalar = 2.4 with self.link.init_scope(): # Initialized parameter self.link.y = chainer.Parameter(y_array) # Uninitialized parameter self.link.v = chainer.Parameter() # Persistent ndarray self.link.add_persistent('pa', pa_array) # Persistent scalar self.link.add_persistent('ps', ps_scalar) self.y_array = y_array self.pa_array = pa_array self.ps_scalar = ps_scalar
Example #14
Source File: test_link.py From chainer with MIT License | 6 votes |
def setUp(self): self.link = chainer.Link() shape = (2, 2) dtype = numpy.float32 y_array = numpy.random.rand(*shape).astype(dtype) pa_array = numpy.random.rand(*shape).astype(dtype) ps_scalar = 2.4 with self.link.init_scope(): # Initialized parameter self.link.y = chainer.Parameter(y_array) # Uninitialized parameter self.link.v = chainer.Parameter() # Persistent ndarray self.link.add_persistent('pa', pa_array) # Persistent scalar self.link.add_persistent('ps', ps_scalar) self.y_array = y_array self.pa_array = pa_array self.ps_scalar = ps_scalar if cuda.available: self.current_device_id = cuda.cupy.cuda.get_device_id()
Example #15
Source File: test_variable.py From chainer with MIT License | 5 votes |
def check_initializer(self, shape, expected_xp): x = chainer.Parameter(shape=shape) self.check_from_chx(x, expected_xp)
Example #16
Source File: test_variable.py From chainer with MIT License | 5 votes |
def test_initialize_by_scalar(self): x = chainer.Parameter(2., (3,)) np.testing.assert_array_equal(x.data, np.array([2., 2., 2.]))
Example #17
Source File: test_variable.py From chainer with MIT License | 5 votes |
def check_initialize_by_initializer(self, shape, expected_xp): x = chainer.Parameter(initializers.One(), shape) self.check_from_chx(x, expected_xp)
Example #18
Source File: test_variable.py From chainer with MIT License | 5 votes |
def test_update_rule_without_grad(self): update_rule = mock.MagicMock() x = chainer.Parameter(self.a) x.update_rule = update_rule x.update() assert update_rule.update.call_count == 1
Example #19
Source File: test_variable.py From chainer with MIT License | 5 votes |
def test_update_rule(self): update_rule = mock.MagicMock() g = self.a.copy() x = chainer.Parameter(self.a) x.grad = g x.update_rule = update_rule x.update() assert update_rule.update.call_count == 1 assert update_rule.update.call_args_list[0] == [(x,), {}]
Example #20
Source File: test_variable.py From chainer with MIT License | 5 votes |
def check_initialize_by_scalar(self, shape, expected_xp): x = chainer.Parameter(2., shape) self.check_from_chx(x, expected_xp)
Example #21
Source File: test_variable.py From chainer with MIT License | 5 votes |
def check_initialize_by_none(self, shape, expected_xp): x = chainer.Parameter(None, shape) self.check_from_chx(x, expected_xp)
Example #22
Source File: test_variable.py From chainer with MIT License | 5 votes |
def test_initialize_by_chainerx_array(self): data = chainerx.array([1., 2., 3.], dtype='f') x = chainer.Parameter(data) assert isinstance(x.data, chainerx.ndarray) chainerx.testing.assert_array_equal(x.data, data)
Example #23
Source File: test_variable.py From chainer with MIT License | 5 votes |
def test_initialize_by_cupy_array(self): data = cuda.cupy.array([1., 2., 3.], dtype='f') x = chainer.Parameter(data, (3,)) assert isinstance(x.data, cuda.cupy.ndarray) cuda.cupy.testing.assert_array_equal(x.data, data)
Example #24
Source File: test_variable.py From chainer with MIT License | 5 votes |
def test_initialize_by_none(self): x = chainer.Parameter(None, (3,)) np.testing.assert_array_equal( x.data, np.full((3,), np.nan, dtype='f'))
Example #25
Source File: test_variable.py From chainer with MIT License | 5 votes |
def test_initialize_by_initializer(self): x = chainer.Parameter(initializers.One(), (3,)) np.testing.assert_array_equal( x.data, np.array([1., 1., 1.], dtype='f'))
Example #26
Source File: test_variable.py From chainer with MIT License | 5 votes |
def test_initialize_by_none_to_device(self, backend_config): x = chainer.Parameter(None, self.x_shape) self.check_to_device(x, backend_config.device)
Example #27
Source File: test_variable.py From chainer with MIT License | 5 votes |
def test_initializer(self): x = chainer.Parameter(shape=(1,)) assert x.initializer is not None
Example #28
Source File: test_variable.py From chainer with MIT License | 5 votes |
def test_copydata_from_to_uninitialized_parameters( self, src_backend_config, dst_backend_config): dst_var = chainer.Parameter() src_var = chainer.Parameter() src_var.to_device(src_backend_config.device) dst_var.to_device(dst_backend_config.device) dst_var.copydata(src_var) assert src_var.device == src_backend_config.device assert dst_var.device == dst_backend_config.device assert src_var.array is None assert dst_var.array is None
Example #29
Source File: test_optimizers.py From chainer with MIT License | 5 votes |
def setUp(self): self.target = chainer.Link() with self.target.init_scope(): self.target.w = chainer.Parameter()
Example #30
Source File: test_optimizers.py From chainer with MIT License | 5 votes |
def __init__(self, shape=()): super(SimpleChain, self).__init__() w_np = np.asarray(np.random.randn(*shape)).astype(np.float32) with self.init_scope(): self.w = chainer.Parameter(w_np, name='w')