本文整理匯總了Python中data.narrow方法的典型用法代碼示例。如果您正苦於以下問題:Python data.narrow方法的具體用法?Python data.narrow怎麽用?Python data.narrow使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類data
的用法示例。
在下文中一共展示了data.narrow方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: batchify
# 需要導入模塊: import data [as 別名]
# 或者: from data import narrow [as 別名]
def batchify(data, bsz):
# Work out how cleanly we can divide the dataset into bsz parts.
if isinstance(data, tuple):
nbatch = data[0].size(0) // bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
tag_data = data[1].narrow(0, 0, nbatch * bsz)
data = data[0].narrow(0, 0, nbatch * bsz)
# Evenly divide the data across the bsz batches.
tag_data = tag_data.view(bsz, -1).t().contiguous()
else:
nbatch = data.size(0) // bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
data = data.narrow(0, 0, nbatch * bsz)
# Evenly divide the data across the bsz batches.
data = data.view(bsz, -1).t().contiguous()
# Turning the data over to CUDA at this point may lead to more OOM errors
#if args.cuda:
# data = data.cuda()
if isinstance(data,tuple):
return data, tag_data
return data
示例2: batchify
# 需要導入模塊: import data [as 別名]
# 或者: from data import narrow [as 別名]
def batchify(data, batch_size):
# Work out how cleanly we can divide the dataset into batch_size parts.
nbatch = data.size(0) // batch_size
# Trim off any extra elements that wouldn't cleanly fit (remainders).
data = data.narrow(0, 0, nbatch * batch_size)
# Evenly divide the data across the batch_size batches.
data = data.view(batch_size, -1).t().contiguous()
return data.to(device)
開發者ID:nadavbh12,項目名稱:Character-Level-Language-Modeling-with-Deeper-Self-Attention-pytorch,代碼行數:10,代碼來源:main.py
示例3: batchify
# 需要導入模塊: import data [as 別名]
# 或者: from data import narrow [as 別名]
def batchify(data, bsz):
# Work out how cleanly we can divide the dataset into bsz parts.
nbatch = data.size(0) // bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
data = data.narrow(0, 0, nbatch * bsz)
# Evenly divide the data across the bsz batches.
data = data.view(bsz, -1).t().contiguous()
return data.to(device)
示例4: batchify
# 需要導入模塊: import data [as 別名]
# 或者: from data import narrow [as 別名]
def batchify(data, bsz):
# Work out how cleanly we can divide the dataset into bsz parts.
nbatch = data.size(0) // bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
data = data.narrow(0, 0, nbatch * bsz)
# Evenly divide the data across the bsz batches.
data = data.view(bsz, -1).t().contiguous()
if args.cuda:
data = data.cuda()
return data
示例5: batchify
# 需要導入模塊: import data [as 別名]
# 或者: from data import narrow [as 別名]
def batchify(data, bsz, args):
# Work out how cleanly we can divide the dataset into bsz parts.
nbatch = data.size(0) // bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
data = data.narrow(0, 0, nbatch * bsz)
# Evenly divide the data across the bsz batches.
data = data.view(bsz, -1).t().contiguous()
if args.cuda:
data = data.cuda()
return data
示例6: batchify
# 需要導入模塊: import data [as 別名]
# 或者: from data import narrow [as 別名]
def batchify(data, bsz):
# Work out how cleanly we can divide the dataset into bsz parts.
nbatch = data.size(0) // bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
data = data.narrow(0, 0, nbatch * bsz)
# Evenly divide the data across the bsz batches.
data = data.view(bsz, -1).t().contiguous()
if args.cuda:
data = data.cuda()
return data
#######################################################################
示例7: batchify
# 需要導入模塊: import data [as 別名]
# 或者: from data import narrow [as 別名]
def batchify(data, bsz, random_start_idx=False):
# Work out how cleanly we can divide the dataset into bsz parts.
nbatch = data.size(0) // bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
if random_start_idx:
start_idx = random.randint(0, data.size(0) % bsz - 1)
else:
start_idx = 0
data = data.narrow(0, start_idx, nbatch * bsz)
# Evenly divide the data across the bsz batches.
data = data.view(bsz, -1).t().contiguous()
if args.cuda:
data = data.cuda()
return data