本文整理汇总了Python中chainer.dataset.convert.to_device方法的典型用法代码示例。如果您正苦于以下问题:Python convert.to_device方法的具体用法?Python convert.to_device怎么用?Python convert.to_device使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.dataset.convert
的用法示例。
在下文中一共展示了convert.to_device方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_xt_batch_seq
# 需要导入模块: from chainer.dataset import convert [as 别名]
# 或者: from chainer.dataset.convert import to_device [as 别名]
def convert_xt_batch_seq(xt_batch_seq, gpu):
batchsize = len(xt_batch_seq[0])
seq_len = len(xt_batch_seq)
xt_batch_seq = np.array(xt_batch_seq, 'i')
# (bproplen, batch, 2)
xt_batch_seq = convert.to_device(gpu, xt_batch_seq)
xp = cuda.get_array_module(xt_batch_seq)
x_seq_batch = xp.split(
xt_batch_seq[:, :, 0].T.reshape(batchsize * seq_len),
batchsize, axis=0)
t_seq_batch = xp.split(
xt_batch_seq[:, :, 1].T.reshape(batchsize * seq_len),
batchsize, axis=0)
return x_seq_batch, t_seq_batch
示例2: converter
# 需要导入模块: from chainer.dataset import convert [as 别名]
# 或者: from chainer.dataset.convert import to_device [as 别名]
def converter(batch, device, max_caption_length=None):
"""Optional preprocessing of the batch before forward pass."""
pad = max_caption_length is not None
imgs = []
captions = []
for img, caption in batch:
# Preproess the caption by either fixing the length by padding (LSTM)
# or by simply wrapping each caption in an ndarray (NStepLSTM)
if pad:
arr = np.full(max_caption_length, _ignore, dtype=np.int32)
# Clip to max length if necessary
arr[:len(caption)] = caption[:max_caption_length]
caption = arr
else:
caption = to_device(device, np.asarray(caption, dtype=np.int32))
imgs.append(img)
captions.append(caption)
if pad:
captions = to_device(device, np.stack(captions))
imgs = to_device(device, np.stack(imgs))
return imgs, captions
示例3: concat_examples
# 需要导入模块: from chainer.dataset import convert [as 别名]
# 或者: from chainer.dataset.convert import to_device [as 别名]
def concat_examples(batch, device=None):
# batch: img, mask, label, scale
if len(batch) == 0:
raise ValueError('batch is empty')
first_elem = batch[0]
result = []
for i in six.moves.range(len(first_elem)):
array = _concat_arrays([example[i] for example in batch], None)
if i == 0: # img
result.append(to_device(device, array))
else:
result.append(array)
return tuple(result)
示例4: concat_examples
# 需要导入模块: from chainer.dataset import convert [as 别名]
# 或者: from chainer.dataset.convert import to_device [as 别名]
def concat_examples(batch, device=None, padding=None,
indices_concat=None, indices_to_device=None):
if len(batch) == 0:
raise ValueError('batch is empty')
first_elem = batch[0]
elem_size = len(first_elem)
if indices_concat is None:
indices_concat = range(elem_size)
if indices_to_device is None:
indices_to_device = range(elem_size)
result = []
if not isinstance(padding, tuple):
padding = [padding] * elem_size
for i in six.moves.range(elem_size):
res = [example[i] for example in batch]
if i in indices_concat:
res = _concat_arrays(res, padding[i])
if i in indices_to_device:
if i in indices_concat:
res = to_device(device, res)
else:
res = [to_device(device, r) for r in res]
result.append(res)
return tuple(result)
示例5: cgcnn_converter
# 需要导入模块: from chainer.dataset import convert [as 别名]
# 或者: from chainer.dataset.convert import to_device [as 别名]
def cgcnn_converter(batch, device=None, padding=None):
"""CGCNN converter"""
if len(batch) == 0:
raise ValueError("batch is empty")
atom_feat, nbr_feat, nbr_idx = [], [], []
batch_atom_idx, target = [], []
current_idx = 0
xp = device.xp
for element in batch:
atom_feat.append(element[0])
nbr_feat.append(element[1])
nbr_idx.append(element[2] + current_idx)
target.append(element[3])
n_atom = element[0].shape[0]
atom_idx = numpy.arange(n_atom) + current_idx
batch_atom_idx.append(atom_idx)
current_idx += n_atom
atom_feat = to_device(device, functions.concat(atom_feat, axis=0).data)
nbr_feat = to_device(device, functions.concat(nbr_feat, axis=0).data)
# Always use numpy array for batch_atom_index
# this is list of variable length array
batch_atom_idx = numpy.array(batch_atom_idx)
nbr_idx = to_device(device, functions.concat(nbr_idx, axis=0).data)
target = to_device(device, xp.asarray(target))
result = (atom_feat, nbr_feat, batch_atom_idx, nbr_idx, target)
return result
示例6: megnet_converter
# 需要导入模块: from chainer.dataset import convert [as 别名]
# 或者: from chainer.dataset.convert import to_device [as 别名]
def megnet_converter(batch, device=None, padding=0):
"""MEGNet converter"""
if len(batch) == 0:
raise ValueError("batch is empty")
atom_feat, pair_feat, global_feat, target = [], [], [], []
atom_idx, pair_idx, start_idx, end_idx = [], [], [], []
batch_size = len(batch)
current_atom_idx = 0
for i in range(batch_size):
element = batch[i]
n_atom = element[0].shape[0]
n_pair = element[1].shape[0]
atom_feat.extend(element[0])
pair_feat.extend(element[1])
global_feat.append(element[2])
atom_idx.extend([i]*n_atom)
pair_idx.extend([i]*n_pair)
start_idx.extend(element[3][0] + current_atom_idx)
end_idx.extend(element[3][1] + current_atom_idx)
target.append(element[4])
current_atom_idx += n_atom
xp = device.xp
atom_feat = to_device(device, xp.asarray(atom_feat))
pair_feat = to_device(device, xp.asarray(pair_feat))
global_feat = to_device(device, xp.asarray(global_feat))
atom_idx = to_device(device, xp.asarray(atom_idx))
pair_idx = to_device(device, xp.asarray(pair_idx))
start_idx = to_device(device, xp.asarray(start_idx))
end_idx = to_device(device, xp.asarray(end_idx))
target = to_device(device, xp.asarray(target))
result = (atom_feat, pair_feat, global_feat, atom_idx, pair_idx,
start_idx, end_idx, target)
return result
示例7: concat_examples
# 需要导入模块: from chainer.dataset import convert [as 别名]
# 或者: from chainer.dataset.convert import to_device [as 别名]
def concat_examples(batch, device=None):
# batch: img, bboxes, whole_mask, labels, scale
if len(batch) == 0:
raise ValueError('batch is empty')
first_elem = batch[0]
result = []
for i in six.moves.range(len(first_elem)):
array = _concat_arrays([example[i] for example in batch], None)
if i == 0: # img
result.append(to_device(device, array))
else:
result.append(array)
return tuple(result)