本文整理汇总了Python中chainer.links.Linear方法的典型用法代码示例。如果您正苦于以下问题:Python links.Linear方法的具体用法?Python links.Linear怎么用?Python links.Linear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.links
的用法示例。
在下文中一共展示了links.Linear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def __init__(self, n_actions, max_episode_steps):
super().__init__()
with self.init_scope():
self.embed = L.EmbedID(max_episode_steps + 1, 3136)
self.image2hidden = chainerrl.links.Sequence(
L.Convolution2D(None, 32, 8, stride=4),
F.relu,
L.Convolution2D(None, 64, 4, stride=2),
F.relu,
L.Convolution2D(None, 64, 3, stride=1),
functools.partial(F.reshape, shape=(-1, 3136)),
)
self.hidden2out = chainerrl.links.Sequence(
L.Linear(None, 512),
F.relu,
L.Linear(None, n_actions),
DiscreteActionValue,
)
示例2: parse_arch
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def parse_arch(arch, n_actions):
if arch == 'nature':
return links.Sequence(
links.NatureDQNHead(),
L.Linear(512, n_actions),
DiscreteActionValue)
elif arch == 'doubledqn':
return links.Sequence(
links.NatureDQNHead(),
L.Linear(512, n_actions, nobias=True),
SingleSharedBias(),
DiscreteActionValue)
elif arch == 'nips':
return links.Sequence(
links.NIPSDQNHead(),
L.Linear(256, n_actions),
DiscreteActionValue)
elif arch == 'dueling':
return DuelingDQN(n_actions)
else:
raise RuntimeError('Not supported architecture: {}'.format(arch))
示例3: make_model
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def make_model(self, env):
n_dim_obs = env.observation_space.low.size
n_dim_action = env.action_space.low.size
n_hidden_channels = 50
policy = Sequence(
L.Linear(n_dim_obs, n_hidden_channels),
F.relu,
L.Linear(n_hidden_channels, n_hidden_channels),
F.relu,
L.LSTM(n_hidden_channels, n_hidden_channels),
policies.FCGaussianPolicy(
n_input_channels=n_hidden_channels,
action_size=n_dim_action,
min_action=env.action_space.low,
max_action=env.action_space.high)
)
q_func = q_function.FCLSTMSAQFunction(
n_dim_obs=n_dim_obs,
n_dim_action=n_dim_action,
n_hidden_layers=2,
n_hidden_channels=n_hidden_channels)
return chainer.Chain(policy=policy, q_function=q_func)
示例4: make_q_func
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def make_q_func(self, env):
obs_size = env.observation_space.low.size
hidden_size = 64
return iqn.StatelessRecurrentImplicitQuantileQFunction(
psi=chainerrl.links.StatelessRecurrentSequential(
L.Linear(obs_size, hidden_size),
F.relu,
L.NStepRNNTanh(1, hidden_size, hidden_size, 0),
),
phi=chainerrl.links.Sequence(
chainerrl.agents.iqn.CosineBasisLinear(32, hidden_size),
F.relu,
),
f=L.Linear(hidden_size, env.action_space.n,
initialW=chainer.initializers.LeCunNormal(1e-1)),
)
示例5: test_manual
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def test_manual(self):
link1 = L.Linear(2, 3)
link2 = L.Linear(2, 5)
link3 = chainer.Sequential(
L.Linear(2, 7),
F.tanh,
)
plink = Branched(link1, link2, link3)
x = np.zeros((self.batch_size, 2), dtype=np.float32)
pout = plink(x)
self.assertIsInstance(pout, tuple)
self.assertEqual(len(pout), 3)
out1 = link1(x)
out2 = link2(x)
out3 = link3(x)
np.testing.assert_allclose(pout[0].array, out1.array)
np.testing.assert_allclose(pout[1].array, out2.array)
np.testing.assert_allclose(pout[2].array, out3.array)
示例6: test
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def test(self):
# Check if it can properly detect recurrent child links
link = stateless_recurrent.StatelessRecurrentChainList(
L.Linear(3, 4),
L.NStepLSTM(1, 3, 2, 0),
L.Linear(4, 5),
stateless_recurrent.StatelessRecurrentChainList(
L.NStepRNNTanh(1, 2, 5, 0),
),
)
self.assertEqual(len(link.recurrent_children), 2)
self.assertIs(link.recurrent_children[0], link[1])
self.assertIs(link.recurrent_children[1], link[3])
self.assertEqual(len(link.recurrent_children[1].recurrent_children), 1)
self.assertIs(
link.recurrent_children[1].recurrent_children[0], link[3][0])
示例7: _test
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def _test(self, gpu):
model = chainer.Chain(
a=L.Linear(1, 2, initialW=3, initial_bias=3),
b=chainer.Chain(c=L.Linear(2, 3, initialW=4, initial_bias=4)),
)
if gpu >= 0:
model.to_gpu(gpu)
xp = model.xp
else:
xp = np
optimizer = chainer.optimizers.SGD(self.lr)
optimizer.setup(model)
optimizer.add_hook(
chainerrl.optimizers.NonbiasWeightDecay(
rate=self.weight_decay_rate))
optimizer.update(lambda: chainer.Variable(xp.asarray(0.0)))
decay_factor = 1 - self.lr * self.weight_decay_rate
xp.testing.assert_allclose(model.a.W.array, 3 * decay_factor)
xp.testing.assert_allclose(model.a.b.array, 3)
xp.testing.assert_allclose(model.b.c.W.array, 4 * decay_factor)
xp.testing.assert_allclose(model.b.c.b.array, 4)
示例8: test_copy_param
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def test_copy_param(self):
a = L.Linear(1, 5)
b = L.Linear(1, 5)
s = chainer.Variable(np.random.rand(1, 1).astype(np.float32))
a_out = list(a(s).array.ravel())
b_out = list(b(s).array.ravel())
self.assertNotEqual(a_out, b_out)
# Copy b's parameters to a
copy_param.copy_param(a, b)
a_out_new = list(a(s).array.ravel())
b_out_new = list(b(s).array.ravel())
self.assertEqual(a_out_new, b_out)
self.assertEqual(b_out_new, b_out)
示例9: test_soft_copy_param
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def test_soft_copy_param(self):
a = L.Linear(1, 5)
b = L.Linear(1, 5)
a.W.array[:] = 0.5
b.W.array[:] = 1
# a = (1 - tau) * a + tau * b
copy_param.soft_copy_param(target_link=a, source_link=b, tau=0.1)
np.testing.assert_almost_equal(a.W.array, np.full(a.W.shape, 0.55))
np.testing.assert_almost_equal(b.W.array, np.full(b.W.shape, 1.0))
copy_param.soft_copy_param(target_link=a, source_link=b, tau=0.1)
np.testing.assert_almost_equal(a.W.array, np.full(a.W.shape, 0.595))
np.testing.assert_almost_equal(b.W.array, np.full(b.W.shape, 1.0))
示例10: __init__
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def __init__(self, n_input_channels, n_dim_action, n_hidden_channels,
n_hidden_layers, action_space, scale_mu=True,
normalize_input=True):
self.n_input_channels = n_input_channels
self.n_hidden_layers = n_hidden_layers
self.n_hidden_channels = n_hidden_channels
assert action_space is not None
self.scale_mu = scale_mu
self.action_space = action_space
super().__init__()
with self.init_scope():
assert n_hidden_layers >= 1
self.hidden_layers = MLPBN(
in_size=n_input_channels, out_size=n_hidden_channels,
hidden_sizes=[n_hidden_channels] * (n_hidden_layers - 1),
normalize_input=normalize_input)
self.v = L.Linear(n_hidden_channels, 1)
self.mu = L.Linear(n_hidden_channels, n_dim_action)
self.mat_diag = L.Linear(n_hidden_channels, n_dim_action)
non_diag_size = n_dim_action * (n_dim_action - 1) // 2
if non_diag_size > 0:
self.mat_non_diag = L.Linear(n_hidden_channels, non_diag_size)
示例11: __init__
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def __init__(self, mu_link, sigma_scale=0.4):
super(FactorizedNoisyLinear, self).__init__()
self._kernel = None
self.out_size = mu_link.out_size
self.nobias = not ('/b' in [name for name, _ in mu_link.namedparams()])
W_data = mu_link.W.array
in_size = None if W_data is None else W_data.shape[1]
device_id = mu_link._device_id
with self.init_scope():
self.mu = L.Linear(in_size, self.out_size, self.nobias,
initialW=LeCunUniform(1 / numpy.sqrt(3)))
self.sigma = L.Linear(in_size, self.out_size, self.nobias,
initialW=VarianceScalingConstant(
sigma_scale),
initial_bias=VarianceScalingConstant(
sigma_scale))
if device_id is not None:
self.to_gpu(device_id)
示例12: __init__
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def __init__(self, in_size, out_size, hidden_sizes, nonlinearity=F.relu,
last_wscale=1):
self.in_size = in_size
self.out_size = out_size
self.hidden_sizes = hidden_sizes
self.nonlinearity = nonlinearity
super().__init__()
with self.init_scope():
if hidden_sizes:
hidden_layers = []
hidden_layers.append(L.Linear(in_size, hidden_sizes[0]))
for hin, hout in zip(hidden_sizes, hidden_sizes[1:]):
hidden_layers.append(L.Linear(hin, hout))
self.hidden_layers = chainer.ChainList(*hidden_layers)
self.output = L.Linear(hidden_sizes[-1], out_size,
initialW=LeCunNormal(last_wscale))
else:
self.output = L.Linear(in_size, out_size,
initialW=LeCunNormal(last_wscale))
示例13: __init__
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def __init__(self):
super(VGG_multi, self).__init__(
conv1_1=L.Convolution2D(3, 64, 3, stride=1, pad=1),
conv1_2=L.Convolution2D(64, 64, 3, stride=1, pad=1),
conv2_1=L.Convolution2D(64, 128, 3, stride=1, pad=1),
conv2_2=L.Convolution2D(128, 128, 3, stride=1, pad=1),
conv3_1=L.Convolution2D(128, 256, 3, stride=1, pad=1),
conv3_2=L.Convolution2D(256, 256, 3, stride=1, pad=1),
conv3_3=L.Convolution2D(256, 256, 3, stride=1, pad=1),
conv4_1=L.Convolution2D(256, 512, 3, stride=1, pad=1),
conv4_2=L.Convolution2D(512, 512, 3, stride=1, pad=1),
conv4_3=L.Convolution2D(512, 512, 3, stride=1, pad=1),
conv5_1=L.Convolution2D(512, 512, 3, stride=1, pad=1),
conv5_2=L.Convolution2D(512, 512, 3, stride=1, pad=1),
conv5_3=L.Convolution2D(512, 512, 3, stride=1, pad=1),
fc6=L.Linear(2048, 4096),
fc7=L.Linear(4096, 4096),
fc8=L.Linear(4096, 768),
)
self.train = True
示例14: __init__
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def __init__(self):
super(VGG_single, self).__init__(
conv1_1=L.Convolution2D(3, 64, 3, stride=1, pad=1),
conv1_2=L.Convolution2D(64, 64, 3, stride=1, pad=1),
conv2_1=L.Convolution2D(64, 128, 3, stride=1, pad=1),
conv2_2=L.Convolution2D(128, 128, 3, stride=1, pad=1),
conv3_1=L.Convolution2D(128, 256, 3, stride=1, pad=1),
conv3_2=L.Convolution2D(256, 256, 3, stride=1, pad=1),
conv3_3=L.Convolution2D(256, 256, 3, stride=1, pad=1),
conv4_1=L.Convolution2D(256, 512, 3, stride=1, pad=1),
conv4_2=L.Convolution2D(512, 512, 3, stride=1, pad=1),
conv4_3=L.Convolution2D(512, 512, 3, stride=1, pad=1),
conv5_1=L.Convolution2D(512, 512, 3, stride=1, pad=1),
conv5_2=L.Convolution2D(512, 512, 3, stride=1, pad=1),
conv5_3=L.Convolution2D(512, 512, 3, stride=1, pad=1),
fc6=L.Linear(2048, 4096),
fc7=L.Linear(4096, 4096),
fc8=L.Linear(4096, 256),
)
self.train = True
示例15: __init__
# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Linear [as 别名]
def __init__(self, obs_size, n_actions, n_hidden_channels=[1024,256]):
super(QFunction,self).__init__()
net = []
inpdim = obs_size
for i,n_hid in enumerate(n_hidden_channels):
net += [ ('l{}'.format(i), L.Linear( inpdim, n_hid ) ) ]
net += [ ('norm{}'.format(i), L.BatchNormalization( n_hid ) ) ]
net += [ ('_act{}'.format(i), F.relu ) ]
inpdim = n_hid
net += [('output', L.Linear( inpdim, n_actions) )]
with self.init_scope():
for n in net:
if not n[0].startswith('_'):
setattr(self, n[0], n[1])
self.forward = net