當前位置: 首頁>>代碼示例>>Python>>正文


Python convert.to_device方法代碼示例

本文整理匯總了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 
開發者ID:pfnet-research,項目名稱:contextual_augmentation,代碼行數:16,代碼來源:utils.py

示例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 
開發者ID:chainer,項目名稱:chainer,代碼行數:28,代碼來源:datasets.py

示例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) 
開發者ID:chainer,項目名稱:chainercv,代碼行數:16,代碼來源:train_sbd.py

示例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) 
開發者ID:chainer,項目名稱:chainercv,代碼行數:31,代碼來源:train_coco_multi.py

示例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 
開發者ID:chainer,項目名稱:chainer-chemistry,代碼行數:30,代碼來源:cgcnn_converter.py

示例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 
開發者ID:chainer,項目名稱:chainer-chemistry,代碼行數:38,代碼來源:megnet_converter.py

示例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) 
開發者ID:knorth55,項目名稱:chainer-fcis,代碼行數:17,代碼來源:convert.py


注:本文中的chainer.dataset.convert.to_device方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。