本文整理汇总了Python中chainer.functions.reshape方法的典型用法代码示例。如果您正苦于以下问题:Python functions.reshape方法的具体用法?Python functions.reshape怎么用?Python functions.reshape使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.functions
的用法示例。
在下文中一共展示了functions.reshape方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def __call__(self, x):
if self.dr:
with chainer.using_config('train', True):
x = F.dropout(x, self.dr)
if self.gap:
x = F.sum(x, axis=(2,3))
N = x.shape[0]
#Below code copyed from https://github.com/pfnet-research/chainer-gan-lib/blob/master/minibatch_discrimination/net.py
feature = F.reshape(F.leaky_relu(x), (N, -1))
m = F.reshape(self.md(feature), (N, self.B * self.C, 1))
m0 = F.broadcast_to(m, (N, self.B * self.C, N))
m1 = F.transpose(m0, (2, 1, 0))
d = F.absolute(F.reshape(m0 - m1, (N, self.B, self.C, N)))
d = F.sum(F.exp(-F.sum(d, axis=2)), axis=2) - 1
h = F.concat([feature, d])
h = self.l(h)
return h
示例2: generate_image
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def generate_image(self, v, r):
xp = cuda.get_array_module(v)
batch_size = v.shape[0]
h_t_gen, c_t_gen, u_t, _, _ = self.generate_initial_state(
batch_size, xp)
v = cf.reshape(v, v.shape[:2] + (1, 1))
for t in range(self.num_layers):
generation_core = self.get_generation_core(t)
mean_z_p, ln_var_z_p = self.z_prior_distribution.compute_parameter(
h_t_gen)
z_t = cf.gaussian(mean_z_p, ln_var_z_p)
h_next_gen, c_next_gen, u_next = generation_core(
h_t_gen, c_t_gen, z_t, v, r, u_t)
u_t = u_next
h_t_gen = h_next_gen
c_t_gen = c_next_gen
mean_x = self.map_u_x(u_t)
return mean_x.data
示例3: generate_image_from_zero_z
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def generate_image_from_zero_z(self, v, r):
xp = cuda.get_array_module(v)
batch_size = v.shape[0]
h_t_gen, c_t_gen, u_t, _, _ = self.generate_initial_state(
batch_size, xp)
v = cf.reshape(v, v.shape[:2] + (1, 1))
for t in range(self.num_layers):
generation_core = self.get_generation_core(t)
mean_z_p, _ = self.z_prior_distribution.compute_parameter(h_t_gen)
z_t = xp.zeros_like(mean_z_p.data)
h_next_gen, c_next_gen, u_next = generation_core(
h_t_gen, c_t_gen, z_t, v, r, u_t)
u_t = u_next
h_t_gen = h_next_gen
c_t_gen = c_next_gen
mean_x = self.map_u_x(u_t)
return mean_x.data
示例4: __init__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [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,
)
示例5: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def __call__(self, x):
h = x
for l in self.conv_layers:
h = self.activation(l(h))
# Advantage
batch_size = x.shape[0]
ya = self.a_stream(h)
mean = F.reshape(
F.sum(ya, axis=1) / self.n_actions, (batch_size, 1))
ya, mean = F.broadcast(ya, mean)
ya -= mean
# State value
ys = self.v_stream(h)
ya, ys = F.broadcast(ya, ys)
q = ya + ys
return action_value.DiscreteActionValue(q)
示例6: _compute_y_and_t
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def _compute_y_and_t(self, exp_batch):
batch_size = exp_batch['reward'].shape[0]
# Compute Q-values for current states
batch_state = exp_batch['state']
if self.recurrent:
qout, _ = self.model.n_step_forward(
batch_state,
exp_batch['recurrent_state'],
output_mode='concat',
)
else:
qout = self.model(batch_state)
batch_actions = exp_batch['action']
batch_q = F.reshape(qout.evaluate_actions(
batch_actions), (batch_size, 1))
with chainer.no_backprop_mode():
batch_q_target = F.reshape(
self._compute_target_values(exp_batch),
(batch_size, 1))
return batch_q, batch_q_target
示例7: _evaluate_psi_x_with_quantile_thresholds
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def _evaluate_psi_x_with_quantile_thresholds(psi_x, phi, f, taus):
assert psi_x.ndim == 2
batch_size, hidden_size = psi_x.shape
assert taus.ndim == 2
assert taus.shape[0] == batch_size
n_taus = taus.shape[1]
phi_taus = phi(taus)
assert phi_taus.ndim == 3
assert phi_taus.shape == (batch_size, n_taus, hidden_size)
psi_x_b = F.broadcast_to(
F.expand_dims(psi_x, axis=1), phi_taus.shape)
h = psi_x_b * phi_taus
h = F.reshape(h, (-1, hidden_size))
assert h.shape == (batch_size * n_taus, hidden_size)
h = f(h)
assert h.ndim == 2
assert h.shape[0] == batch_size * n_taus
n_actions = h.shape[-1]
h = F.reshape(h, (batch_size, n_taus, n_actions))
return QuantileDiscreteActionValue(h)
示例8: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def __call__(self, x, t):
h = F.relu(self.conv1(x))
h = F.max_pooling_2d(h, 2, 1)
h = F.relu(self.conv2(h))
h = F.relu(self.conv3(h))
h = F.relu(self.fc4(h))
h = self.fc5(h)
h = F.reshape(h, (x.data.shape[0], 3, 16, 16))
h = self.channelwise_inhibited(h)
if self.train:
self.loss = F.softmax_cross_entropy(h, t, normalize=False)
return self.loss
else:
self.pred = F.softmax(h)
return self.pred
示例9: channelwise_inhibited
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def channelwise_inhibited(self, h):
xp = cuda.get_array_module(h.data)
num = h.data.shape[0]
h = F.split_axis(h, 3, 1)
c = F.reshape(h[self.c], (num, 16, 16))
z = Variable(xp.zeros_like(c.data), 'AUTO')
c = F.batch_matmul(c, z)
c = F.reshape(c, (num, 1, 16, 16))
hs = []
for i, s in enumerate(h):
if i == self.c:
hs.append(c)
else:
hs.append(s)
return F.concat(hs, 1)
示例10: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def __call__(self, x, t):
h = F.relu(self.conv1(x))
h = F.max_pooling_2d(h, 2, 1)
h = F.relu(self.conv2(h))
h = F.relu(self.conv3(h))
h = F.dropout(F.relu(self.fc4(h)), train=self.train)
h = self.fc5(h)
h = F.reshape(h, (x.data.shape[0], 3, 16, 16))
h = self.channelwise_inhibited(h)
if self.train:
self.loss = F.softmax_cross_entropy(h, t, normalize=False)
return self.loss
else:
self.pred = F.softmax(h)
return self.pred
示例11: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def __call__(self, inpt, mask):
mb_size = inpt.data.shape[0]
max_length = inpt.data.shape[1]
precomp = F.reshape(F.tanh(self.lin(F.reshape(inpt, (-1, self.Hi)))), (mb_size, -1, self.Ho))
mask_offset = max_length - len(mask)
precomp_mask_penalties = self.xp.concatenate(
[
self.xp.zeros((mb_size, mask_offset), dtype=self.xp.float32),
-10000 * (1 - self.xp.concatenate([
self.xp.reshape(mask_elem, (mb_size, 1)).astype(self.xp.float32) for mask_elem in mask], 1))
], 1
)
def compute_copy_coefficients(state):
betas = F.reshape(batch_matmul(precomp, state), (mb_size, -1))
masked_betas = betas + precomp_mask_penalties
return masked_betas
return compute_copy_coefficients
示例12: get_initial_logits
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def get_initial_logits(self, mb_size = None):
if mb_size is None:
mb_size = self.src_mb_size
else:
assert self.src_mb_size == 1
assert mb_size is not None
bos_encoding = F.broadcast_to(self.decoder_chain.bos_encoding, (mb_size, 1, self.decoder_chain.d_model))
cross_mask = self.decoder_chain.xp.broadcast_to(self.mask_input[:,0:1,0:1,:], (self.mask_input.shape[0], self.decoder_chain.n_heads, 1, self.mask_input.shape[3]))
final_layer, prev_states = self.decoder_chain.encoding_layers.one_step(bos_encoding, None,
self.src_encoding, cross_mask)
logits = self.decoder_chain.logits_layer(F.reshape(final_layer, (mb_size, self.decoder_chain.d_model)))
return logits, DecoderState(pos=-1, prev_states=prev_states)
示例13: _initialize_params
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def _initialize_params(self):
lateral_init = initializers._get_initializer(self.lateral_init)
upward_init = initializers._get_initializer(self.upward_init)
bias_init = initializers._get_initializer(self.bias_init)
forget_bias_init = initializers._get_initializer(self.forget_bias_init)
for i in six.moves.range(0, 4 * self.state_size, self.state_size):
lateral_init(self.lateral.W.data[i:i + self.state_size, :])
upward_init(self.upward.W.data[i:i + self.state_size, :])
a, i, f, o = lstm._extract_gates(
self.upward.b.data.reshape(1, 4 * self.state_size, 1))
bias_init(a)
bias_init(i)
forget_bias_init(f)
bias_init(o)
示例14: pixel_shuffle
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def pixel_shuffle(self, x):
out = self.ps(x)
b = out.shape[0]
c = out.shape[1]
h = out.shape[2]
w = out.shape[3]
out = F.reshape(out, (b, 2, 2, c//4, h, w))
out = F.transpose(out, (0, 3, 4, 1, 5, 2))
out = F.reshape(out, (b, c//4, h*2, w*2))
return out
示例15: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import reshape [as 别名]
def __call__(self, x):
b = x.shape[0]
c = 1#x.shape[1]
h = x.shape[2]
w = x.shape[3]
x = F.reshape(x, (b*h,c,w))
x = add_noise(x)
for l in self:
x = l(x)
return x