本文整理汇总了Python中chainer.functions.split_axis方法的典型用法代码示例。如果您正苦于以下问题:Python functions.split_axis方法的具体用法?Python functions.split_axis怎么用?Python functions.split_axis使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.functions
的用法示例。
在下文中一共展示了functions.split_axis方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def __call__(self, prev_hg, prev_he, prev_ce, x, v, r, u):
xu = cf.concat((x, u), axis=1)
xu = self.downsample_xu(xu)
v = self.broadcast_v(v)
if r.shape[2] == 1:
r = self.broadcast_r(r)
lstm_input = cf.concat((prev_he, prev_hg, xu, v, r), axis=1)
gate_inputs = self.lstm(lstm_input)
if self.use_cuda_kernel:
next_h, next_c = CoreFunction()(gate_inputs, prev_ce)
else:
forget_gate_input, input_gate_input, tanh_input, output_gate_input = cf.split_axis(
gate_inputs, 4, axis=1)
forget_gate = cf.sigmoid(forget_gate_input)
input_gate = cf.sigmoid(input_gate_input)
next_c = forget_gate * prev_ce + input_gate * cf.tanh(tanh_input)
output_gate = cf.sigmoid(output_gate_input)
next_h = output_gate * cf.tanh(next_c)
return next_h, next_c
示例2: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def __call__(self, prev_hg, prev_cg, prev_z, v, r, prev_u):
v = self.broadcast_v(v)
if r.shape[2] == 1:
r = self.broadcast_r(r)
lstm_input = cf.concat((prev_hg, v, r, prev_z), axis=1)
gate_inputs = self.lstm(lstm_input)
forget_gate_input, input_gate_input, tanh_input, output_gate_input = cf.split_axis(
gate_inputs, 4, axis=1)
forget_gate = cf.sigmoid(forget_gate_input)
input_gate = cf.sigmoid(input_gate_input)
next_c = forget_gate * prev_cg + input_gate * cf.tanh(tanh_input)
output_gate = cf.sigmoid(output_gate_input)
next_h = output_gate * cf.tanh(next_c)
next_u = self.upsample_h(next_h) + prev_u
return next_h, next_c, next_u
示例3: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def __call__(self, x):
h = x
for l in self.conv_layers:
h = self.activation(l(h))
# Advantage
batch_size = x.shape[0]
h = self.activation(self.main_stream(h))
h_a, h_v = F.split_axis(h, 2, axis=-1)
ya = F.reshape(self.a_stream(h_a),
(batch_size, self.n_actions, self.n_atoms))
mean = F.sum(ya, axis=1, keepdims=True) / self.n_actions
ya, mean = F.broadcast(ya, mean)
ya -= mean
# State value
ys = F.reshape(self.v_stream(h_v), (batch_size, 1, self.n_atoms))
ya, ys = F.broadcast(ya, ys)
q = F.softmax(ya + ys, axis=2)
return action_value.DistributionalDiscreteActionValue(q, self.z_values)
示例4: split_one_step_batch_input
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def split_one_step_batch_input(xs):
"""Split one-step batch input.
Args:
xs (chainer.Variable, ndarray or tuple): One-step batched input. It
should be either:
- a variable whose first axis is the batch axis.
- a tuple of such variables.
Returns:
list: Either a list of variables or a list of tuples of varialbes.
The length of the list is the batch size of the input.
"""
if isinstance(xs, tuple):
return list(zip(*[split_one_step_batch_input(x) for x in xs]))
else:
return list(F.split_axis(xs, len(xs), axis=0))
示例5: channelwise_inhibited
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def channelwise_inhibited(self, h):
self.c = random.randint(0, 2)
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)
示例6: channelwise_inhibited
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [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)
示例7: apply_to_seq
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def apply_to_seq(self, seq_list):
mb_size = len(seq_list)
mb_initial_cell, mb_initial_state = self.get_initial_states(mb_size)
return self.nstep_lstm(mb_initial_cell, mb_initial_state, seq_list)
# class DoubleGRU(Chain):
# def __init__(self, H, I):
# log.info("using double GRU")
# self.H1 = H/2
# self.H2 = H - self.H1
# super(DoubleGRU, self).__init__(
# gru1 = faster_gru.GRU(self.H1, I),
# gru2 = faster_gru.GRU(self.H2, self.H1)
# )
#
# def __call__(self, prev_state, inpt):
# prev_state1, prev_state2 = F.split_axis(prev_state, (self.H1,), axis = 1)
#
# prev_state1 = self.gru1(prev_state1, inpt)
# prev_state2 = self.gru2(prev_state2, prev_state1)
#
# return F.concat((prev_state1, prev_state2), axis = 1)
示例8: advance_state
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def advance_state(self, previous_states, prev_y):
current_mb_size = prev_y.data.shape[0]
assert self.mb_size is None or current_mb_size <= self.mb_size
if current_mb_size < len(previous_states[0].data):
truncated_states = [None] * len(previous_states)
for num_state in six.moves.range(len(previous_states)):
truncated_states[num_state], _ = F.split_axis(
previous_states[num_state], (current_mb_size,), 0)
previous_states = tuple(truncated_states)
output_state = previous_states[-1]
if self.decoder_chain.use_goto_attention:
ci, attn = self.compute_ctxt(output_state, prev_y)
else:
ci, attn = self.compute_ctxt(output_state)
concatenated = F.concat((prev_y, ci))
new_states = self.decoder_chain.gru(previous_states, concatenated)
return new_states, concatenated, attn
示例9: faster_call2
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def faster_call2(self, h, x):
r_z_h_x = self.W_r_z_h(x)
r_z_h = self.U_r_z(h)
r_x, z_x, h_x = split_axis(r_z_h_x, (self.n_units, self.n_units * 2), axis=1)
assert r_x.data.shape[1] == self.n_units
assert z_x.data.shape[1] == self.n_units
assert h_x.data.shape[1] == self.n_units
r_h, z_h = split_axis(r_z_h, (self.n_units,), axis=1)
# r = sigmoid.sigmoid(r_x + r_h)
# z = sigmoid.sigmoid(z_x + z_h)
# h_bar = tanh.tanh(h_x + self.U(sigm_a_plus_b_by_h(r_x, r_h, h)))
# h_new = (1 - z) * h + z * h_bar
# return h_new
return compute_output_GRU(z_x, z_h, h_x, h, self.U(sigm_a_plus_b_by_h_fast(r_x, r_h, h)))
示例10: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def forward(self, xs, ilens):
'''BLSTM forward (the modified version)
:param xs:
:param ilens:
:return:
'''
logging.info(self.__class__.__name__ + ' input lengths: ' + str(ilens))
# need to move ilens to cpu
ilens = cuda.to_cpu(ilens)
hy, cy, ys = self.nblstm(None, None, xs)
ys = self.l_last(F.vstack(ys)) # (sum _utt frame_utt) x dim
xs = F.split_axis(ys, np.cumsum(ilens[:-1]), axis=0)
del hy, cy
# final tanh operation
xs = F.split_axis(F.tanh(F.vstack(xs)), np.cumsum(ilens[:-1]), axis=0)
# EDIT(hamaji): Unnecessary, as `force_tuple` is True by default.
# # 1 utterance case, it becomes an array, so need to make a utt tuple
# if not isinstance(xs, tuple):
# xs = [xs]
return xs, ilens # x: utt list of frame x dim
示例11: original
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def original(self, xs, ilens):
'''BLSTM forward (the original implementation)
:param xs:
:param ilens:
:return:
'''
logging.info(self.__class__.__name__ + ' input lengths: ' + str(ilens))
# need to move ilens to cpu
ilens = cuda.to_cpu(ilens)
hy, cy, ys = self.nblstm(None, None, xs)
ys = self.l_last(F.vstack(ys)) # (sum _utt frame_utt) x dim
xs = F.split_axis(ys, np.cumsum(ilens[:-1]), axis=0)
del hy, cy
# final tanh operation
xs = F.split_axis(F.tanh(F.vstack(xs)), np.cumsum(ilens[:-1]), axis=0)
# 1 utterance case, it becomes an array, so need to make a utt tuple
if not isinstance(xs, tuple):
xs = [xs]
return xs, ilens # x: utt list of frame x dim
示例12: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def forward(self, xs, ilens):
'''BLSTM forward (the modified version)
:param xs:
:param ilens:
:return:
'''
logging.info(self.__class__.__name__ + ' input lengths: ' + str(ilens))
# need to move ilens to cpu
ilens = cuda.to_cpu(ilens)
hy, cy, ys = self.nblstm(None, None, xs)
ys = self.l_last(F.vstack(ys)) # (sum _utt frame_utt) x dim
xs = F.split_axis(ys, np.cumsum(ilens[:-1]), 0)
del hy, cy
# final tanh operation
xs = F.split_axis(F.tanh(F.vstack(xs)), np.cumsum(ilens[:-1]), 0)
# EDIT(hamaji): Unnecessary, as `force_tuple` is True by default.
# # 1 utterance case, it becomes an array, so need to make a utt tuple
# if not isinstance(xs, tuple):
# xs = [xs]
return xs, ilens # x: utt list of frame x dim
示例13: test_type_hints
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def test_type_hints(self):
class Test():
def forward(self, x: types.TyNdarray(np.float32, ('a', 'b'))):
h = F.split_axis(x, 2, 1)
return h
model, forward_args = Test(), (np.zeros((10, 10)).astype(np.float32),)
id2type = generate_id2type_from_forward(model, forward_args)
self.assertEqual(str(id2type[1]), "class Test -> ndarray(float32, (10 (a), 10 (b))) -> (Variable(float32, (10 (a), 5 (b // 2))), Variable(float32, (10 (a), 5 (b // 2))))") # FunctionDef forward (line 1)
self.assertEqual(str(id2type[20]), "NoneType") # Assign
self.assertEqual(str(id2type[21]), "(Variable(float32, (10 (a), 5 (b // 2))), Variable(float32, (10 (a), 5 (b // 2))))") # Name h (line 2)
self.assertEqual(str(id2type[23]), "(Variable(float32, (10 (a), 5 (b // 2))), Variable(float32, (10 (a), 5 (b // 2))))") # Call F.split_axis(x, 2, 1) (line 2)
self.assertEqual(str(id2type[28]), "ndarray(float32, (10 (a), 10 (b)))") # Name x (line 2)
self.assertEqual(str(id2type[30]), "int") # Constant 2 (line 2)
self.assertEqual(str(id2type[31]), "int") # Constant 1 (line 2)
self.assertEqual(str(id2type[32]), "(Variable(float32, (10 (a), 5 (b // 2))), Variable(float32, (10 (a), 5 (b // 2))))") # Return
self.assertEqual(str(id2type[33]), "(Variable(float32, (10 (a), 5 (b // 2))), Variable(float32, (10 (a), 5 (b // 2))))") # Name h (line 3)
示例14: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def __call__(self, s, xs):
"""Calculate all hidden states and cell states.
Args:
s (~chainer.Variable or None): Initial (hidden & cell) states. If ``None``
is specified zero-vector is used.
xs (list of ~chianer.Variable): List of input sequences.
Each element ``xs[i]`` is a :class:`chainer.Variable` holding
a sequence.
Return:
(hy,cy): a pair of hidden and cell states at the end of the sequence,
ys: a hidden state sequence at the last layer
"""
if len(xs) > 1:
sections = np.cumsum(np.array([len(x) for x in xs[:-1]], dtype=np.int32))
xs = F.split_axis(self.embed(F.concat(xs, axis=0)), sections, axis=0)
else:
xs = [ self.embed(xs[0]) ]
if s is not None:
hy, cy, ys = self.lstm(s[0], s[1], xs)
else:
hy, cy, ys = self.lstm(None, None, xs)
return (hy,cy), ys
示例15: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import split_axis [as 别名]
def forward(self, xs, hs=None, activation=None):
if hs is not None:
hx1, cx1, hx_emb, cx_emb = hs
else:
hx1 = cx1 = hx_emb = cx_emb = None
# forward to LSTM layers
hy_emb, cy_emb, ems = self.bi_lstm_emb(hx_emb, cx_emb, xs)
hy1, cy1, ys = self.bi_lstm1(hx1, cx1, ems)
# main branch
ys_stack = F.vstack(ys)
ys = self.linear1(ys_stack)
if activation:
ys = activation(ys)
ilens = [x.shape[0] for x in xs]
ys = F.split_axis(ys, np.cumsum(ilens[:-1]), axis=0)
# embedding branch
ems_stack = F.vstack(ems)
ems = F.normalize(F.tanh(self.linear2(ems_stack)))
ems = F.split_axis(ems, np.cumsum(ilens[:-1]), axis=0)
if not isinstance(ys, tuple):
ys = [ys]
ems = [ems]
return [hy1, cy1, hy_emb, cy_emb], ys, ems