本文整理汇总了Python中chainer.functions.stack方法的典型用法代码示例。如果您正苦于以下问题:Python functions.stack方法的具体用法?Python functions.stack怎么用?Python functions.stack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.functions
的用法示例。
在下文中一共展示了functions.stack方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: batch_pit_loss
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def batch_pit_loss(ys, ts, label_delay=0):
"""
PIT loss over mini-batch.
Args:
ys: B-length list of predictions
ts: B-length list of labels
Returns:
loss: (1,)-shape mean cross entropy over mini-batch
labels: B-length list of permuted labels
"""
loss_w_labels = [pit_loss(y, t)
for (y, t) in zip(ys, ts)]
losses, labels = zip(*loss_w_labels)
loss = F.sum(F.stack(losses))
n_frames = np.sum([t.shape[0] for t in ts])
loss = loss / n_frames
return loss, labels
示例2: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def __call__(self, xs, ts):
_, ys, ems = self.forward(xs)
# PIT loss
loss, labels = batch_pit_loss(ys, ts)
reporter.report({'loss_pit': loss}, self)
report_diarization_error(ys, labels, self)
# DPCL loss
loss_dc = F.sum(
F.stack([dc_loss(em, t) for (em, t) in zip(ems, ts)]))
n_frames = np.sum([t.shape[0] for t in ts])
loss_dc = loss_dc / (n_frames ** 2)
reporter.report({'loss_dc': loss_dc}, self)
# Multi-objective
loss = (1 - self.dc_loss_ratio) * loss + self.dc_loss_ratio * loss_dc
reporter.report({'loss': loss}, self)
return loss
示例3: inverse
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def inverse(self, y):
scale_sqr = self.scale * self.scale
batch, y_channels, y_height, y_width = y.shape
assert (y_channels % scale_sqr == 0)
x_channels = y_channels // scale_sqr
x_height = y_height * self.scale
x_width = y_width * self.scale
x = F.transpose(y, axes=(0, 2, 3, 1))
x = x.reshape(batch, y_height, y_width, scale_sqr, x_channels)
d3_split_seq = F.split_axis(x, indices_or_sections=(x.shape[3] // self.scale), axis=3)
d3_split_seq = [t.reshape(batch, y_height, x_width, x_channels) for t in d3_split_seq]
x = F.stack(d3_split_seq, axis=0)
x = F.transpose(F.swapaxes(x, axis1=0, axis2=1), axes=(0, 2, 1, 3, 4)).reshape(
batch, x_height, x_width, x_channels)
x = F.transpose(x, axes=(0, 3, 1, 2))
return x
示例4: test_hook_for_funcnode
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def test_hook_for_funcnode(self, test_type):
class Model(chainer.Chain):
def forward(self, x):
if test_type in ['variable', 'array']:
x = [chainer.as_variable(x)]
elif test_type == 'dict':
x = list(x.values())
x.append(chainer.Variable(np.array(7, np.float32)))
return F.stack(x)
model = Model()
x = self.get_x(test_type)
with RetainInputHook() as h:
model(x)
expected_count = 1
if test_type == 'array':
# input is ndarray and not checked in forward_preprocess
expected_count += 1
assert len(h.retain_inputs) == expected_count
示例5: predict
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def predict(self, images, return_visual_backprop=False):
with chainer.using_device(self.device):
if isinstance(images, list):
images = [self.xp.array(image) for image in images]
images = self.xp.stack(images, axis=0)
visual_backprop = None
with chainer.using_config('train', False):
roi, bbox = self(images)
rois = [roi]
bboxes = [bbox]
if return_visual_backprop:
if not hasattr(self, 'visual_backprop'):
self.visual_backprop = VisualBackprop()
visual_backprop = self.visual_backprop.perform_visual_backprop(self.visual_backprop_anchors[0])
bboxes = F.stack(bboxes, axis=1)
bboxes = F.reshape(bboxes, (-1,) + bboxes.shape[2:])
rois = F.stack(rois, axis=1)
rois = F.reshape(rois, (-1,) + rois.shape[2:])
return rois, bboxes, visual_backprop
示例6: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [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)
示例7: calc_bboxes
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def calc_bboxes(self, predicted_bboxes, image_size, out_size):
predicted_bboxes = (predicted_bboxes + 1) / 2
x_points = predicted_bboxes[:, 0, ...] * image_size.width
y_points = predicted_bboxes[:, 1, ...] * image_size.height
top_left_x = F.get_item(x_points, [..., 0, 0])
top_left_y = F.get_item(y_points, [..., 0, 0])
bottom_right_x = F.get_item(x_points, [..., out_size.height - 1, out_size.width - 1])
bottom_right_y = F.get_item(y_points, [..., out_size.height - 1, out_size.width - 1])
bboxes = F.stack(
[
F.minimum(top_left_x, bottom_right_x),
F.minimum(top_left_y, bottom_right_y),
F.maximum(top_left_x, bottom_right_x),
F.maximum(top_left_y, bottom_right_y),
],
axis=1
)
return bboxes
示例8: loadData
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def loadData(self, num):
# ===== Initialize variables ===== #
N = self.N
M = self.M
# ===== Load data ===== #
audio_spec = [xp.load(os.path.join(DATA_DIR_SPEC, "{}.npz".format(i)))["mix"].T[xp.newaxis,:,:] for i in num]
N_index = xp.random.randint(0,audio_spec[0].shape[1]-N)
audio_spec = xp.stack([i[:,N_index:(N_index+N),:] for i in audio_spec])
M_index = int(N_index/4)
face1 = [ (xp.array(pd.read_csv(os.path.join(DATA_DIR_VISUAL, "{}/speech1.csv".format(i)), header=None)).T[:,:,xp.newaxis].astype(xp.float32) / 255.) for i in num]
face1 = xp.stack([i[:,M_index:(M_index+M),:] for i in face1])
face2 = [ (xp.array(pd.read_csv(os.path.join(DATA_DIR_VISUAL, "{}/speech2.csv".format(i)), header=None)).T[:,:,xp.newaxis].astype(xp.float32) / 255.) for i in num]
face2 = xp.stack([i[:,M_index:(M_index+M),:] for i in face2])
true_spec = xp.stack(
[xp.load(os.path.join(DATA_DIR_SPEC, "{}.npz".format(i)))["true"].T[N_index:(N_index+N),:] \
for i in num])
true_spec /= ( xp.max(true_spec) * 1.1 )
return audio_spec, face1, face2, true_spec
示例9: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [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))
示例10: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def forward(self, x, y):
y1 = F.stack((x, y))
return y1
示例11: forward
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def forward(self, x, y):
y1 = F.stack((x, y))
y2 = np.stack([x, y])
return y1, y2
示例12: pit_loss
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def pit_loss(pred, label, label_delay=0):
"""
Permutation-invariant training (PIT) cross entropy loss function.
Args:
pred: (T,C)-shaped pre-activation values
label: (T,C)-shaped labels in {0,1}
label_delay: if label_delay == 5:
pred: 0 1 2 3 4 | 5 6 ... 99 100 |
label: x x x x x | 0 1 ... 94 95 | 96 97 98 99 100
calculated area: | <------------> |
Returns:
min_loss: (1,)-shape mean cross entropy
label_perms[min_index]: permutated labels
"""
# label permutations along the speaker axis
label_perms = [label[..., list(p)] for p
in permutations(range(label.shape[-1]))]
losses = F.stack(
[F.sigmoid_cross_entropy(
pred[label_delay:, ...],
l[:len(l) - label_delay, ...]) for l in label_perms])
xp = cuda.get_array_module(losses)
min_loss = F.min(losses) * (len(label) - label_delay)
min_index = cuda.to_cpu(xp.argmin(losses.data))
return min_loss, label_perms[min_index]
示例13: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def __call__(self, x):
import numpy as np
heatmap = x[:, :-4].array
wh = x[:, -4:-2].array
reg = x[:, -2:].array
batch, _, out_h, out_w = heatmap.shape
heatmap_flat = heatmap.reshape((batch, -1))
indices = np.argsort(heatmap_flat)[:, -self.topk:]
scores = np.take_along_axis(heatmap_flat, indices=indices, axis=-1)
topk_classes = (indices // (out_h * out_w)).astype(dtype=np.float32)
topk_indices = indices % (out_h * out_w)
topk_ys = (topk_indices // out_w).astype(dtype=np.float32)
topk_xs = (topk_indices % out_w).astype(dtype=np.float32)
center = reg.transpose((0, 2, 3, 1)).reshape((batch, -1, 2))
wh = wh.transpose((0, 2, 3, 1)).reshape((batch, -1, 2))
xs = np.take_along_axis(center[:, :, 0], indices=topk_indices, axis=-1)
ys = np.take_along_axis(center[:, :, 1], indices=topk_indices, axis=-1)
topk_xs = topk_xs + xs
topk_ys = topk_ys + ys
w = np.take_along_axis(wh[:, :, 0], indices=topk_indices, axis=-1)
h = np.take_along_axis(wh[:, :, 1], indices=topk_indices, axis=-1)
half_w = 0.5 * w
half_h = 0.5 * h
bboxes = F.stack((topk_xs - half_w, topk_ys - half_h, topk_xs + half_w, topk_ys + half_h), axis=-1)
bboxes = bboxes * self.scale
topk_classes = F.expand_dims(topk_classes, axis=-1)
scores = F.expand_dims(scores, axis=-1)
result = F.concat((bboxes, topk_classes, scores), axis=-1)
return result
示例14: __call__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def __call__(self, x):
batch, x_channels, x_height, x_width = x.shape
y_channels = x_channels * self.scale * self.scale
assert (x_height % self.scale == 0)
y_height = x_height // self.scale
y = F.transpose(x, axes=(0, 2, 3, 1))
d2_split_seq = F.split_axis(y, indices_or_sections=(y.shape[2] // self.scale), axis=2)
d2_split_seq = [t.reshape(batch, y_height, y_channels) for t in d2_split_seq]
y = F.stack(d2_split_seq, axis=1)
y = F.transpose(y, axes=(0, 3, 2, 1))
return y
示例15: __init__
# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import stack [as 别名]
def __init__(self,
axis=1,
stack=False):
super(Concurrent, self).__init__()
self.axis = axis
self.stack = stack