本文整理汇总了Python中chainer.functions.separate方法的典型用法代码示例。如果您正苦于以下问题:Python functions.separate方法的具体用法?Python functions.separate怎么用?Python functions.separate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.functions
的用法示例。
在下文中一共展示了functions.separate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_separate
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def test_separate(self):
class Test():
def forward(self):
F.separate(np.zeros((3, 4, 5)), axis=0)
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, (4, 5)), Variable(float64, (4, 5)), Variable(float64, (4, 5)))") # Call F.separate(np.zeros((3, 4, 5)), axis=0) (line 2)
self.assertEqual(str(id2type[11]), "ndarray(float64, (3, 4, 5))") # Call np.zeros((3, 4, 5)) (line 2)
self.assertEqual(str(id2type[16]), "(int, int, int)") # Tuple (3, 4, 5) (line 2)
self.assertEqual(str(id2type[17]), "int") # Num 3 (line 2)
self.assertEqual(str(id2type[18]), "int") # Num 4 (line 2)
self.assertEqual(str(id2type[19]), "int") # Num 5 (line 2)
self.assertEqual(str(id2type[22]), "int") # Num 0 (line 2)
示例2: calc_loss
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def calc_loss(self, x, t):
batch_predictions, _, grids = x
self.xp = cuda.get_array_module(batch_predictions, t)
loss = self.calc_actual_loss(batch_predictions, None, t)
# reshape grids
batch_size = t.shape[0]
grids = grids[-1]
grid_shape = grids.shape
grids = F.reshape(grids, (-1, batch_size) + grid_shape[1:])
grid_losses = []
for grid in F.separate(grids, axis=0):
with cuda.get_device_from_array(getattr(grid, 'data', grid[0].data)):
grid_losses.append(self.calc_direction_loss(grid))
return loss + (sum(grid_losses) / len(grid_losses))
示例3: calc_loss
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def calc_loss(self, x, t):
batch_predictions, _, _ = x
# concat all individual predictions and slice for each time step
batch_predictions = F.concat([F.expand_dims(p, axis=0) for p in batch_predictions], axis=0)
self.xp = cuda.get_array_module(batch_predictions[0], t)
batch_size = t.shape[0]
t = F.reshape(t, (batch_size, self.num_timesteps, -1))
losses = []
for predictions, labels in zip(F.separate(batch_predictions, axis=0), F.separate(t, axis=1)):
batch_size, num_chars, num_classes = predictions.shape
predictions = F.reshape(predictions, (batch_size * num_chars, num_classes))
labels = F.reshape(labels, (-1,))
losses.append(F.softmax_cross_entropy(predictions, labels))
return sum(losses)
示例4: attend
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def attend(self, encoded_features):
self.out_lstm.reset_state()
transformed_encoded_features = F.concat([F.expand_dims(self.transform_encoded_features(feature), axis=1) for feature in encoded_features], axis=1)
concat_encoded_features = F.concat([F.expand_dims(e, axis=1) for e in encoded_features], axis=1)
lstm_output = self.xp.zeros_like(encoded_features[0])
outputs = []
for _ in range(self.num_labels):
transformed_lstm_output = self.transform_out_lstm_feature(lstm_output)
attended_feats = []
for transformed_encoded_feature in F.separate(transformed_encoded_features, axis=1):
attended_feat = transformed_encoded_feature + transformed_lstm_output
attended_feat = F.tanh(attended_feat)
attended_feats.append(self.generate_attended_feat(attended_feat))
attended_feats = F.concat(attended_feats, axis=1)
alphas = F.softmax(attended_feats, axis=1)
lstm_input_feature = F.batch_matmul(alphas, concat_encoded_features, transa=True)
lstm_input_feature = F.squeeze(lstm_input_feature, axis=1)
lstm_output = self.out_lstm(lstm_input_feature)
outputs.append(lstm_output)
return outputs
示例5: decode_predictions
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def decode_predictions(self, predictions):
# concat all individual predictions and slice for each time step
predictions = F.concat([F.expand_dims(p, axis=0) for p in predictions], axis=0)
words = []
with cuda.get_device_from_array(predictions.data):
for prediction in F.separate(predictions, axis=0):
prediction = F.squeeze(prediction, axis=0)
prediction = F.softmax(prediction, axis=1)
prediction = self.xp.argmax(prediction.data, axis=1)
word = self.loss_metrics.strip_prediction(prediction[self.xp.newaxis, ...])[0]
if len(word) == 1 and word[0] == 0:
return ''
word = "".join(map(self.loss_metrics.label_to_char, word))
word = word.replace(chr(self.loss_metrics.char_map[str(self.loss_metrics.blank_symbol)]), '')
words.append(word)
text = " ".join(words)
return text
示例6: draw_bboxes
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def draw_bboxes(self, bboxes, image):
draw = ImageDraw.Draw(image)
for i, sub_box in enumerate(F.separate(bboxes, axis=1)):
for bbox, colour in zip(F.separate(sub_box, axis=0), self.colours):
bbox.data[...] = (bbox.data[...] + 1) / 2
bbox.data[0, :] *= self.image_size.width
bbox.data[1, :] *= self.image_size.height
x = self.xp.clip(bbox.data[0, :].reshape(self.out_size), 0, self.image_size.width) + i * self.image_size.width
y = self.xp.clip(bbox.data[1, :].reshape(self.out_size), 0, self.image_size.height)
top_left = (x[0, 0], y[0, 0])
top_right = (x[0, -1], y[0, -1])
bottom_left = (x[-1, 0], y[-1, 0])
bottom_right = (x[-1, -1], y[-1, -1])
corners = [top_left, top_right, bottom_right, bottom_left]
next_corners = corners[1:] + [corners[0]]
for first_corner, next_corner in zip(corners, next_corners):
draw.line([first_corner, next_corner], fill=colour, width=3)
示例7: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def __call__(self, input_ids, input_mask, token_type_ids):
final_hidden = self.bert.get_sequence_output(
input_ids,
input_mask,
token_type_ids)
batch_size = final_hidden.shape[0]
seq_length = final_hidden.shape[1]
hidden_size = final_hidden.shape[2]
final_hidden_matrix = F.reshape(
final_hidden, [batch_size * seq_length, hidden_size])
logits = self.output(final_hidden_matrix)
logits = F.reshape(logits, [batch_size, seq_length, 2])
logits = logits - (1 - input_mask[:, :, None]) * 1000. # ignore pads
logits = F.transpose(logits, [2, 0, 1])
unstacked_logits = F.separate(logits, axis=0)
(start_logits, end_logits) = (unstacked_logits[0], unstacked_logits[1])
return (start_logits, end_logits)
示例8: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def __call__(self, rois):
batch_size, num_bboxes, num_channels, height, width = rois.shape
rois = F.reshape(rois, (-1, num_channels, height, width))
# if not chainer.config.user_text_recognition_grayscale_input:
# # convert data to grayscale
# assert rois.shape[1] == 3, "rois are not in RGB, can not convert them to grayscale"
# r, g, b = F.separate(rois, axis=1)
# grey = 0.299 * r + 0.587 * g + 0.114 * b
# rois = F.stack([grey, grey, grey], axis=1)
h = self.feature_extractor(rois)
_, num_channels, feature_height, feature_width = h.shape
h = F.average_pooling_2d(h, (feature_height, feature_width))
h = F.reshape(h, (batch_size, num_bboxes, num_channels, -1))
all_predictions = []
for box in F.separate(h, axis=1):
# box_predictions = [self.classifier(self.lstm(box)) for _ in range(self.num_chars)]
box_predictions = [self.classifier(box) for _ in range(self.num_chars)]
all_predictions.append(F.stack(box_predictions, axis=1))
# return shape: batch_size, num_bboxes, num_chars, num_classes
return F.stack(all_predictions, axis=2)
示例9: draw_bboxes
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def draw_bboxes(self, bboxes, image):
if len(bboxes) == 0:
return
draw = ImageDraw.Draw(image)
for i, sub_box in enumerate(F.separate(bboxes, axis=1)):
for bbox, colour in zip(F.separate(sub_box, axis=0), self.colours()):
bbox.data[...] = (bbox.data[...] + 1) / 2
bbox.data[0, :] *= self.image_size.width
bbox.data[1, :] *= self.image_size.height
x = self.xp.clip(bbox.data[0, :].reshape(self.out_size), 0, self.image_size.width) + i * self.image_size.width
y = self.xp.clip(bbox.data[1, :].reshape(self.out_size), 0, self.image_size.height)
top_left = (x[0, 0], y[0, 0])
top_right = (x[0, -1], y[0, -1])
bottom_left = (x[-1, 0], y[-1, 0])
bottom_right = (x[-1, -1], y[-1, -1])
corners = [top_left, top_right, bottom_right, bottom_left]
self.draw_bbox(colour, corners, draw)
示例10: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def __call__(self, h):
# type: (chainer.Variable) -> chainer.Variable
xp = cuda.get_array_module(h)
mb, node, ch = h.shape # type: int, int, int
if self.q_star is None:
self.q_star = [
xp.zeros((1, self.in_channels * 2)).astype('f')
for _ in range(mb)
]
self.hx, self.cx, q = self.lstm_layer(self.hx, self.cx, self.q_star)
# self.hx: (mb, mb, ch)
# self.cx: (mb, mb, ch)
# q: List[(1, ch) * mb]
q = functions.stack(q) # q: (mb, 1, ch)
q_ = functions.transpose(q, axes=(0, 2, 1)) # q_: (mb, ch, 1)
e = functions.matmul(h, q_) # e: (mb, node, 1)
a = functions.softmax(e) # a: (mb, node, 1)
a = functions.broadcast_to(a, h.shape) # a: (mb, node, ch)
r = functions.sum((a * h), axis=1, keepdims=True) # r: (mb, 1, ch)
q_star_ = functions.concat((q, r), axis=2) # q_star_: (mb, 1, ch*2)
self.q_star = functions.separate(q_star_)
return functions.reshape(q_star_, (mb, ch * 2))
示例11: _extract_gates
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def _extract_gates(x):
r = F.reshape(x, (len(x), x.shape[1] // 4, 4) + x.shape[2:])
r = F.separate(r, axis=2)
return r[0], r[1], r[2], r[3]
示例12: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def forward(self, x):
return list(F.separate(x))
示例13: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def forward(self, xs, activation=None):
ilens = [x.shape[0] for x in xs]
# xs: (B, T, F)
xs = F.pad_sequence(xs, padding=-1)
pad_shape = xs.shape
# emb: (B*T, E)
emb = self.enc(xs)
# ys: (B*T, C)
ys = self.linear(emb)
if activation:
ys = activation(ys)
# ys: [(T, C), ...]
ys = F.separate(ys.reshape(pad_shape[0], pad_shape[1], -1), axis=0)
ys = [F.get_item(y, slice(0, ilen)) for y, ilen in zip(ys, ilens)]
return ys
示例14: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def forward(self, inputs, device):
x, = inputs
return functions.separate(x, self.axis)
示例15: test_output
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import separate [as 别名]
def test_output(self):
n_layers = self.n_layers
dropout_ratio = 0.0
batch_size = 3
input_size = 4
hidden_size = 5
seq_length = 6
class Model(chainer.Chain):
def __init__(self):
super().__init__()
def __call__(self, hx, ws1, ws2, ws3, bs, xs):
ws = [F.separate(ws1) + F.separate(ws2)]
if n_layers > 1:
ws.extend([F.separate(w) for w in F.separate(ws3)])
bs = [F.separate(b) for b in F.separate(bs)]
xs = F.separate(xs)
hy, ys = F.n_step_gru(n_layers, dropout_ratio,
hx, ws, bs, xs)
return hy, F.stack(ys, axis=0)
model = Model()
hx = input_generator.increasing(n_layers, batch_size, hidden_size)
ws1 = input_generator.increasing(3, hidden_size, input_size)
ws2 = input_generator.increasing(3, hidden_size, hidden_size)
ws3 = input_generator.increasing(
n_layers - 1, 6, hidden_size, hidden_size)
bs = input_generator.increasing(n_layers, 6, hidden_size)
xs = input_generator.increasing(seq_length, batch_size, input_size)
self.expect(model, (hx, ws1, ws2, ws3, bs, xs))