本文整理汇总了Python中chainer.functions.vstack方法的典型用法代码示例。如果您正苦于以下问题:Python functions.vstack方法的具体用法?Python functions.vstack怎么用?Python functions.vstack使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.functions
的用法示例。
在下文中一共展示了functions.vstack方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [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
示例2: original
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [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
示例3: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [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
示例4: test_vstack
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [as 别名]
def test_vstack(self):
class Test():
def forward(self):
F.vstack([np.zeros((1, 3, 4)), np.zeros((2, 3, 4))])
id2type = generate_id2type_from_forward(Test(), ())
self.assertEqual(str(id2type[1]), "class Test -> NoneType") # FunctionDef forward (line 1)
self.assertEqual(str(id2type[5]), "NoneType") # Expr
self.assertEqual(str(id2type[6]), "Variable(float64, (None, 3, 4))") # Call F.vstack([np.zeros((1, 3, 4)), np.zeros((2, 3, 4))]) (line 2)
self.assertEqual(str(id2type[11]), "ndarray(float64, (None, 3, 4)) list") # List [np.zeros((1, 3, 4)), np.zeros((2, 3, 4))] (line 2)
self.assertEqual(str(id2type[12]), "ndarray(float64, (1, 3, 4))") # Call np.zeros((1, 3, 4)) (line 2)
self.assertEqual(str(id2type[17]), "(int, int, int)") # Tuple (1, 3, 4) (line 2)
self.assertEqual(str(id2type[18]), "int") # Num 1 (line 2)
self.assertEqual(str(id2type[19]), "int") # Num 3 (line 2)
self.assertEqual(str(id2type[20]), "int") # Num 4 (line 2)
self.assertEqual(str(id2type[22]), "ndarray(float64, (2, 3, 4))") # Call np.zeros((2, 3, 4)) (line 2)
self.assertEqual(str(id2type[27]), "(int, int, int)") # Tuple (2, 3, 4) (line 2)
self.assertEqual(str(id2type[28]), "int") # Num 2 (line 2)
self.assertEqual(str(id2type[29]), "int") # Num 3 (line 2)
self.assertEqual(str(id2type[30]), "int") # Num 4 (line 2)
示例5: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [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
示例6: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [as 别名]
def forward(self, x, y):
y1 = F.vstack((x, y))
return y1
示例7: forward_expected
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [as 别名]
def forward_expected(self, inputs):
x = list(inputs)
y_expect = numpy.vstack(x)
return y_expect,
示例8: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [as 别名]
def forward(self, inputs, device):
x = list(inputs)
y = functions.vstack(x)
return y,
示例9: check_value_check
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [as 别名]
def check_value_check(self):
if self.valid:
# Check if it throws nothing
functions.vstack(self.xs)
else:
with pytest.raises(type_check.InvalidType):
functions.vstack(self.xs)
示例10: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [as 别名]
def __call__(self, xs, ts):
hy, cy, ys = self.lstm(None, None, xs)
ys = F.relu(self.ll1(F.vstack(ys)))
del hy, cy
ys = self.ll2(ys)
loss = F.mean_squared_error(ys, ts)
return loss
# This is used during testing
示例11: predict
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [as 别名]
def predict(self, xs):
hy, cy, ys = self.lstm(None, None, xs)
ys = F.relu(self.ll1(F.vstack(ys)))
del hy, cy
ys = self.ll2(ys)
return ys
示例12: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [as 别名]
def __call__(self, xs, ilens):
"""RNNP forward.
Args:
xs (chainer.Variable): Batch of padded charactor ids. (B, Tmax)
ilens (chainer.Variable): Batch of length of each input batch. (B,)
Returns:
xs (chainer.Variable):subsampled vector of xs.
chainer.Variable: Subsampled vector of ilens.
"""
logging.info(self.__class__.__name__ + " input lengths: " + str(ilens))
for layer in six.moves.range(self.elayers):
if "lstm" in self.typ:
_, _, ys = self[self.rnn_label + str(layer)](None, None, xs)
else:
_, ys = self[self.rnn_label + str(layer)](None, xs)
# ys: utt list of frame x cdim x 2 (2: means bidirectional)
# TODO(watanabe) replace subsample and FC layer with CNN
ys, ilens = _subsamplex(ys, self.subsample[layer + 1])
# (sum _utt frame_utt) x dim
ys = self["bt" + str(layer)](F.vstack(ys))
xs = F.split_axis(ys, np.cumsum(ilens[:-1]), axis=0)
# 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
示例13: lifted_struct_loss
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [as 别名]
def lifted_struct_loss(f_a, f_p, alpha=1.0):
"""Lifted struct loss function.
Args:
f_a (~chainer.Variable): Feature vectors as anchor examples.
All examples must be different classes each other.
f_p (~chainer.Variable): Positive examples corresponding to f_a.
Each example must be the same class for each example in f_a.
alpha (~float): The margin parameter.
Returns:
~chainer.Variable: Loss value.
See: `Deep Metric Learning via Lifted Structured Feature Embedding \
<http://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/\
Song_Deep_Metric_Learning_CVPR_2016_paper.pdf>`_
"""
assert f_a.shape == f_p.shape, 'f_a and f_p must have same shape.'
n = 2 * f_a.shape[0] # use shape[0] due to len(Variable) returns its size
f = F.vstack((f_a, f_p))
D_sq = squared_distance_matrix(f)
pairs_p = np.arange(n).reshape(2, -1) # indexes of positive pairs
row = []
col = []
for i, j in pairs_p.T:
row.append([i] * (n - 2) + [j] * (n - 2))
col.append(np.tile(np.delete(np.arange(n), (i, j)), 2))
row = np.ravel(row)
col = np.ravel(col)
pairs_n = np.vstack((row, col))
distances_p = F.sqrt(D_sq[pairs_p[0], pairs_p[1]])
distances_n = F.sqrt(D_sq[pairs_n[0], pairs_n[1]])
distances_n = distances_n.reshape((n // 2, -1))
loss_ij = F.logsumexp(alpha - distances_n, axis=1) + distances_p
return F.sum(F.relu(loss_ij) ** 2) / n
示例14: test_output
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import vstack [as 别名]
def test_output(self):
class Model(chainer.Chain):
def __init__(self):
super().__init__()
with self.init_scope():
self.l1 = L.Convolution2D(None, 16, 5, 1, 2)
self.l2 = L.Convolution2D(16, 8, 5, 1, 2)
def forward(self, *xs, **kwxs):
if kwxs:
h = F.vstack(list(kwxs.values()))
elif len(xs) > 1:
h = F.vstack(xs)
else:
h = xs[0]
h2 = self.l1(h)
h3 = F.relu(h2)
h4 = self.l2(h3)
return F.relu(h4)
def check_input_shape(onnx_model, path):
assert [v.type.tensor_type.shape.dim[0] == 'b' for
v in onnx_model.graph.input]
assert [v.type.tensor_type.shape.dim[0] == 'b' for
v in onnx_model.graph.output]
if isinstance(self.x_shape, tuple):
xs = np.zeros(self.x_shape, dtype=np.float32)
elif isinstance(self.x_shape, list):
xs = tuple(
np.zeros(shape, dtype=np.float32) for shape in self.x_shape)
else:
assert isinstance(self.x_shape, dict)
xs = {k: np.zeros(shape, dtype=np.float32) for
k, shape in self.x_shape.items()}
name = 'customized_input_shape'
if hasattr(self, 'condition'):
name += '_{}'.format(self.condition)
self.expect(
Model(), xs, name=name, input_shapes=self.shape_option,
custom_model_test_func=check_input_shape)