本文整理汇总了Python中data.size方法的典型用法代码示例。如果您正苦于以下问题:Python data.size方法的具体用法?Python data.size怎么用?Python data.size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类data
的用法示例。
在下文中一共展示了data.size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: singletest
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def singletest(data,net,config,splitfun,combinefun,n_per_run,margin = 64,isfeat=False):
z, h, w = data.size(2), data.size(3), data.size(4)
print(data.size())
data = splitfun(data,config['max_stride'],margin)
data = Variable(data.cuda(async = True), volatile = True,requires_grad=False)
splitlist = range(0,args.split+1,n_per_run)
outputlist = []
featurelist = []
for i in range(len(splitlist)-1):
if isfeat:
output,feature = net(data[splitlist[i]:splitlist[i+1]])
featurelist.append(feature)
else:
output = net(data[splitlist[i]:splitlist[i+1]])
output = output.data.cpu().numpy()
outputlist.append(output)
output = np.concatenate(outputlist,0)
output = combinefun(output, z / config['stride'], h / config['stride'], w / config['stride'])
if isfeat:
feature = np.concatenate(featurelist,0).transpose([0,2,3,4,1])
feature = combinefun(feature, z / config['stride'], h / config['stride'], w / config['stride'])
return output,feature
else:
return output
示例2: make_std_mask
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def make_std_mask(tgt):
"""Create a mask to hide padding and future words."""
tgt_mask = (tgt != pad).unsqueeze(-2)
tgt_mask = tgt_mask & subsequent_mask(tgt.size(-1)).type_as(tgt_mask)
return tgt_mask
# get_batch subdivides the source data into chunks of length args.bptt.
# If source is equal to the example output of the batchify function, with
# a bptt-limit of 2, we'd get the following two Variables for i = 0:
# ┌ a g m s ┐ ┌ b h n t ┐
# └ b h n t ┘ └ c i o u ┘
# Note that despite the name of the function, the subdivison of data is not
# done along the batch dimension (i.e. dimension 1), since that was handled
# by the batchify function. The chunks are along dimension 0, corresponding
# to the seq_len dimension in the LSTM.
开发者ID:nadavbh12,项目名称:Character-Level-Language-Modeling-with-Deeper-Self-Attention-pytorch,代码行数:18,代码来源:main.py
示例3: get_batch
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def get_batch(source, i, train):
if train:
i = torch.randint(low=0, high=(len(source) - args.bptt), size=(1,)).long().item()
seq_len = args.bptt
target = source[i + 1:i + 1 + seq_len].t()
else:
seq_len = min(args.bptt, len(source) - 1 - i)
target = source[i + seq_len, :]
data = source[i:i + seq_len].t()
data_mask = (data != pad).unsqueeze(-2)
target_mask = make_std_mask(data.long())
# reshape target to match what cross_entropy expects
target = target.contiguous().view(-1)
return data, target, data_mask, target_mask
开发者ID:nadavbh12,项目名称:Character-Level-Language-Modeling-with-Deeper-Self-Attention-pytorch,代码行数:20,代码来源:main.py
示例4: evaluate
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def evaluate(data_source):
# Turn on evaluation mode which disables dropout.
model.eval()
total_loss = 0.
ntokens = len(corpus.dictionary)
memory = model.module.initial_state(eval_batch_size, trainable=False).to(device)
with torch.no_grad():
for i in range(0, data_source.size(0) - 1, args.bptt):
data, targets = get_batch(data_source, i)
data = torch.t(data)
loss, memory = model(data, memory, targets)
loss = torch.mean(loss)
# data has shape [T * B, N]
total_loss += args.bptt * loss.item()
return total_loss / len(data_source)
示例5: evaluate
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def evaluate(data_source):
# Turn on evaluation mode which disables dropout.
model.eval()
total_loss = 0.
ntokens = len(corpus.dictionary)
hidden = model.init_hidden(eval_batch_size)
with torch.no_grad():
for i in range(0, data_source.size(0) - 1, args.bptt):
data, targets = get_batch(data_source, i)
output, hidden = model(data, hidden)
if not args.adaptivesoftmax:
loss = criterion(output.view(-1, ntokens), targets)
else:
_, loss = criterion_adaptive(output.view(-1, args.nhid), targets)
total_loss += len(data) * loss.item()
hidden = repackage_hidden(hidden)
return total_loss / len(data_source)
示例6: evaluate
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def evaluate(data_source):
# Turn on evaluation mode which disables dropout.
model.eval()
total_loss = 0.
ntokens = len(corpus.dictionary)
if args.model != 'Transformer':
hidden = model.init_hidden(eval_batch_size)
with torch.no_grad():
for i in range(0, data_source.size(0) - 1, args.bptt):
data, targets = get_batch(data_source, i)
if args.model == 'Transformer':
output = model(data)
output = output.view(-1, ntokens)
else:
output, hidden = model(data, hidden)
hidden = repackage_hidden(hidden)
total_loss += len(data) * criterion(output, targets).item()
return total_loss / (len(data_source) - 1)
示例7: singletest
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def singletest(data, net, config, splitfun, combinefun, n_per_run, margin=64):
z, h, w = data.size(2), data.size(3), data.size(4)
print(data.size())
data = splitfun(data, config['max_stride'], margin)
data = Variable(data.cuda(async=True), volatile=True, requires_grad=False)
splitlist = range(0, args.split + 1, n_per_run)
outputlist = []
for i in range(len(splitlist) - 1):
output = net(data[splitlist[i]:splitlist[i + 1]])
output = output.data.cpu().numpy()
outputlist.append(output)
output = np.concatenate(outputlist, 0)
output = combinefun(output, z / config['stride'], h / config['stride'], w / config['stride'])
return output
示例8: evaluate
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def evaluate(data_source):
# Turn on evaluation mode which disables dropout.
model.eval()
total_loss = 0.
ntokens = len(corpus.dictionary)
if args.model != 'Transformer':
hidden = model.init_hidden(eval_batch_size)
with torch.no_grad():
for i in range(0, data_source.size(0) - 1, args.bptt):
data, targets = get_batch(data_source, i)
if args.model == 'Transformer':
output = model(data)
else:
output, hidden = model(data, hidden)
hidden = repackage_hidden(hidden)
output_flat = output.view(-1, ntokens)
total_loss += len(data) * criterion(output_flat, targets).item()
return total_loss / (len(data_source) - 1)
示例9: batchify
# 需要导入模块: import data [as 别名]
# 或者: from data import size [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
示例10: evaluate
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def evaluate(lm_data_source, ccg_data_source):
# Turn on evaluation mode which disables dropout.
model.eval()
total_loss = 0
ntokens = len(corpus.dictionary)
if (not args.single) and (torch.cuda.device_count() > 1):
#"module" is necessary when using DataParallel
hidden = model.module.init_hidden(eval_batch_size)
else:
hidden = model.init_hidden(eval_batch_size)
for i in range(0, lm_data_source.size(0) + ccg_data_source.size(0) - 1, args.bptt):
# TAG
if i > lm_data_source.size(0):
data, targets = get_batch(ccg_data_source, i - lm_data_source.size(0), evaluation=True)
# LM
else:
data, targets = get_batch(lm_data_source, i, evaluation=True)
output, hidden = model(data, hidden)
output_flat = output.view(-1, ntokens)
curr_loss = len(data) * criterion(output_flat, targets).data
total_loss += curr_loss
hidden = repackage_hidden(hidden)
if len(ccg_data_source) == 0:
return total_loss / len(lm_data_source)
return total_loss[0] / (len(lm_data_source)+len(ccg_data_source))
示例11: batchify
# 需要导入模块: import data [as 别名]
# 或者: from data import size [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
示例12: subsequent_mask
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def subsequent_mask(size):
"""Mask out subsequent positions."""
attn_shape = (1, size, size)
subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
return torch.from_numpy(subsequent_mask) == 0
开发者ID:nadavbh12,项目名称:Character-Level-Language-Modeling-with-Deeper-Self-Attention-pytorch,代码行数:7,代码来源:main.py
示例13: evaluate
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def evaluate(data_source):
# Turn on evaluation mode which disables dropout.
total_loss = AverageMeter()
model.eval()
ntokens = len(corpus.dictionary)
step = 1
with torch.no_grad():
for batch, i in enumerate(range(0, data_source.size(0) - 1 - args.bptt, step)):
data, target, data_mask, target_mask = get_batch(data_source, i, train=False)
output = model(data, target_mask)
_, last_loss = model.criterion(output, target)
total_loss.update(last_loss.item(), data.size(0))
return total_loss.avg
开发者ID:nadavbh12,项目名称:Character-Level-Language-Modeling-with-Deeper-Self-Attention-pytorch,代码行数:15,代码来源:main.py
示例14: batchify
# 需要导入模块: import data [as 别名]
# 或者: from data import size [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)
示例15: evaluate
# 需要导入模块: import data [as 别名]
# 或者: from data import size [as 别名]
def evaluate(data_source, batch_size, seq_len):
# Turn on evaluation mode which disables dropout.
model.eval()
total_loss = 0
tokens = 0
n = 0
save_all_losses = []
ntokens = len(corpus.dictionary)
hidden = model.init_hidden(batch_size)
for i in range(0, data_source.size(0) - 1, seq_len):
tokens += seq_len
data, targets = get_batch(data_source, i, args, evaluation=True, seq_len=seq_len)
output, hidden = model(data, hidden)
output = nn.functional.log_softmax(output.permute(2,1,0)).permute(2,1,0)
targets = targets.view(data.data.shape[0], batch_size, -1)
CELoss = torch.gather(output.data, dim=2, index=targets.data).squeeze()
CELoss = -1*CELoss
if tokens < args.start_token: continue # We are not ready to accumulate error yet
elif tokens >= args.start_token and tokens-seq_len < args.start_token:
data.data = data.data[-(tokens-args.start_token+1):]
CELoss = CELoss[-(tokens-args.start_token+1):]
print('First word: %s' % (corpus.dictionary.idx2word[data.data[-(tokens-args.start_token+1),0]]))
total_loss += torch.sum(CELoss)
n += data.size(0)
save_all_losses += CELoss.tolist()
hidden = repackage_hidden(hidden)
print('total: %d' % n)
print('Last word: %s' % (corpus.dictionary.idx2word[data.data[-1,0]]))
return total_loss / float(n), save_all_losses