本文整理汇总了Python中model.train方法的典型用法代码示例。如果您正苦于以下问题:Python model.train方法的具体用法?Python model.train怎么用?Python model.train使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类model
的用法示例。
在下文中一共展示了model.train方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: evaluate
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def evaluate(data_source, source_sampler, target_sampler, batch_size=10):
# Turn on evaluation mode which disables dropout.
model.eval()
if args.model == 'QRNN':
model.reset()
total_loss = 0
hidden = model.init_hidden(batch_size)
for source_sample, target_sample in zip(source_sampler, target_sampler):
model.train()
data = torch.stack([data_source[i] for i in source_sample])
targets = torch.stack([data_source[i] for i in target_sample]).view(-1)
with torch.no_grad():
output, hidden = model(data, hidden)
total_loss += len(data) * criterion(model.decoder.weight, model.decoder.bias, output,
targets).item()
hidden = repackage_hidden(hidden)
return total_loss / len(data_source)
示例2: train
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def train(i):
model.train()
total_loss = 0
for chars, words, position, sub_sidx, sub_eidx, obj_sidx, obj_eidx, sub_slidx, sub_elidx in tqdm(training_data, mininterval=1, desc='Train Processing', leave=False):
optimizer.zero_grad()
p_sub_sidx, p_sub_eidx, p_obj_sidx, p_obj_eidx, mask = model(
chars, words, position, sub_slidx, sub_elidx)
ss_loss = mask_binary_cross_entropy(p_sub_sidx, sub_sidx, mask)
se_loss = mask_binary_cross_entropy(p_sub_eidx, sub_eidx, mask)
os_loss = mask_binary_cross_entropy(p_obj_sidx, obj_sidx, mask)
oe_loss = mask_binary_cross_entropy(p_obj_eidx, obj_eidx, mask)
loss = ss_loss+se_loss+os_loss+oe_loss
loss.backward()
optimizer.step()
optimizer.update_learning_rate()
total_loss += loss.data.item()
print(
f"train epoch {i+1}/{args.epochs} loss: {total_loss/training_data.stop_step:.4f}")
示例3: save_model
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def save_model(model, sess, log_path, step):
"""
Save model using tensorflow checkpoint (also save hidden variables)
Args:
model : model to save variable from
sess : tensorflow session
log_path : where to save
step : number of step at time of saving
"""
path = log_path + '/' + model.name
if tf.gfile.Exists(path):
tf.gfile.DeleteRecursively(path)
tf.gfile.MakeDirs(path)
saver = tf.train.Saver()
checkpoint_path = os.path.join(path, 'model.ckpt')
saver.save(sess, checkpoint_path, global_step=step)
示例4: restore_model
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def restore_model(model, sess, log_path):
"""
Restore model (including hidden variable)
In practice use to resume the training of the same model
Args
model : model to restore variable to
sess : tensorflow session
log_path : where to save
Returns:
step_b : the step number at which training ended
"""
path = log_path + '/' + model.name
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state(path)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
return ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
else:
print('------------------------------------------------------')
print('No checkpoint file found')
print('------------------------------------------------------ \n')
exit()
示例5: main
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def main(split_name, checkpoint_dir, cycle_lambda, rec_lambda,
lsgan_lambda_a, lsgan_lambda_b, num_separate_layers_g,
num_separate_layers_d, num_no_skip_layers,
lr_g_mult, lr_d_mult, network_structure):
"""The main function."""
params = dict()
params['cycle_lambda'] = cycle_lambda
params['rec_lambda'] = rec_lambda
params['lsgan_lambda_a'] = lsgan_lambda_a
params['lsgan_lambda_b'] = lsgan_lambda_b
params['num_separate_layers_g'] = num_separate_layers_g
params['num_separate_layers_d'] = num_separate_layers_d
params['num_no_skip_layers'] = num_no_skip_layers
params['lr_g_mult'] = lr_g_mult
params['lr_d_mult'] = lr_d_mult
model = Img2Img(split_name, params, base_lr=.0002, max_step=200,
checkpoint_dir='',
network_structure=network_structure)
model.train()
示例6: main
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def main(hps):
# Initialize Horovod.
hvd.init()
# Create tensorflow session
sess = tensorflow_session()
# Download and load dataset.
tf.set_random_seed(hvd.rank() + hvd.size() * hps.seed)
np.random.seed(hvd.rank() + hvd.size() * hps.seed)
# Get data and set train_its and valid_its
train_iterator, test_iterator, data_init = get_data(hps, sess)
hps.train_its, hps.test_its, hps.full_test_its = get_its(hps)
# Create log dir
logdir = os.path.abspath(hps.logdir) + "/"
if not os.path.exists(logdir):
os.mkdir(logdir)
# Create model
import model
model = model.model(sess, hps, train_iterator, test_iterator, data_init)
# Initialize visualization functions
visualise = init_visualizations(hps, model, logdir)
if not hps.inference:
# Perform training
train(sess, model, hps, logdir, visualise)
else:
infer(sess, model, hps, test_iterator)
示例7: train
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def train():
# Turn on training mode which enables dropout.
model.train()
total_loss = 0
start_time = time.time()
ntokens = len(corpus.dictionary)
hidden = model.init_hidden(args.batch_size)
for batch, i in enumerate(range(0, train_data.size(0) - 1, args.bptt)):
data, targets = get_batch(train_data, i)
# Starting each batch, we detach the hidden state from how it was previously produced.
# If we didn't, the model would try backpropagating all the way to start of the dataset.
hidden = repackage_hidden(hidden)
model.zero_grad()
output, hidden = model(data, hidden)
loss = criterion(output.view(-1, ntokens), targets)
loss.backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
torch.nn.utils.clip_grad_norm(model.parameters(), args.clip)
for p in model.parameters():
p.data.add_(-lr, p.grad.data)
total_loss += loss.data
if batch % args.log_interval == 0 and batch > 0:
cur_loss = total_loss[0] / args.log_interval
elapsed = time.time() - start_time
print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | '
'loss {:5.2f} | ppl {:8.2f}'.format(
epoch, batch, len(train_data) // args.bptt, lr,
elapsed * 1000 / args.log_interval, cur_loss, math.exp(cur_loss)))
total_loss = 0
start_time = time.time()
# Loop over epochs.
示例8: grad
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def grad(model, inputs, pred, classes):
losses1 = torch.zeros(inputs.size()[0],inputs.size()[1])
dloss = torch.zeros(inputs.size()[0],inputs.size()[1])
if isinstance(model,torch.nn.DataParallel):
model = model.module
model.train()
embd,output = model(inputs, returnembd = True)
# embd.retain_grad()
loss = F.nll_loss(output,pred)
loss.backward()
score = (inputs<=2).float()
score = -score
score = embd.grad.norm(2,dim=2) + score * 1e9
return score
示例9: grad_unconstrained
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def grad_unconstrained(model, inputs, pred, classes):
losses1 = torch.zeros(inputs.size()[0],inputs.size()[1])
dloss = torch.zeros(inputs.size()[0],inputs.size()[1])
if isinstance(model,torch.nn.DataParallel):
model = model.module
model.train()
embd,output = model(inputs, returnembd = True)
loss = F.nll_loss(output,pred)
loss.backward()
score = embd.grad.norm(2,dim=2)
return score
示例10: main
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def main():
if args.mode == 'train':
print(args.path)
train(args.path)
if args.mode == 'test':
test(args.path, args.weight)
示例11: train
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def train():
# Turn on training mode which enables dropout.
model.train()
total_loss = 0.
start_time = time.time()
ntokens = len(corpus.dictionary)
hidden = model.init_hidden(args.batch_size)
for batch, i in enumerate(range(0, train_data.size(0) - 1, args.bptt)):
data, targets = get_batch(train_data, i)
# Starting each batch, we detach the hidden state from how it was previously produced.
# If we didn't, the model would try backpropagating all the way to start of the dataset.
hidden = repackage_hidden(hidden)
optimizer.zero_grad()
output, hidden = model(data, hidden)
loss = criterion(output.view(-1, ntokens), targets)
loss.backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip)
optimizer.step()
total_loss += loss.item()
if batch % args.log_interval == 0 and batch > 0:
cur_loss = total_loss / args.log_interval
elapsed = time.time() - start_time
print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | '
'loss {:5.2f} | ppl {:8.2f}'.format(
epoch, batch, len(train_data) // args.bptt, lr,
elapsed * 1000 / args.log_interval, cur_loss, math.exp(cur_loss)))
sys.stdout.flush()
total_loss = 0
start_time = time.time()
示例12: train
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def train():
# Turn on training mode which enables dropout.
model.train()
total_loss = 0.
start_time = time.time()
ntokens = len(corpus.dictionary)
if args.model != 'Transformer':
hidden = model.init_hidden(args.batch_size)
for batch, i in enumerate(range(0, train_data.size(0) - 1, args.bptt)):
data, targets = get_batch(train_data, i)
# Starting each batch, we detach the hidden state from how it was previously produced.
# If we didn't, the model would try backpropagating all the way to start of the dataset.
model.zero_grad()
if args.model == 'Transformer':
output = model(data)
output = output.view(-1, ntokens)
else:
hidden = repackage_hidden(hidden)
output, hidden = model(data, hidden)
loss = criterion(output, targets)
loss.backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip)
for p in model.parameters():
p.data.add_(-lr, p.grad)
total_loss += loss.item()
if batch % args.log_interval == 0 and batch > 0:
cur_loss = total_loss / args.log_interval
elapsed = time.time() - start_time
print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | '
'loss {:5.2f} | ppl {:8.2f}'.format(
epoch, batch, len(train_data) // args.bptt, lr,
elapsed * 1000 / args.log_interval, cur_loss, math.exp(cur_loss)))
total_loss = 0
start_time = time.time()
if args.dry_run:
break
示例13: train
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def train():
# Turn on training mode which enables dropout.
model.train()
total_loss = 0.
start_time = time.time()
ntokens = len(corpus.dictionary)
if args.model != 'Transformer':
hidden = model.init_hidden(args.batch_size)
for batch, i in enumerate(range(0, train_data.size(0) - 1, args.bptt)):
data, targets = get_batch(train_data, i)
# Starting each batch, we detach the hidden state from how it was previously produced.
# If we didn't, the model would try backpropagating all the way to start of the dataset.
model.zero_grad()
if args.model == 'Transformer':
output = model(data)
else:
hidden = repackage_hidden(hidden)
output, hidden = model(data, hidden)
loss = criterion(output.view(-1, ntokens), targets)
loss.backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip)
for p in model.parameters():
p.data.add_(-lr, p.grad.data)
total_loss += loss.item()
if batch % args.log_interval == 0 and batch > 0:
cur_loss = total_loss / args.log_interval
elapsed = time.time() - start_time
print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | '
'loss {:5.2f} | ppl {:8.2f}'.format(
epoch, batch, len(train_data) // args.bptt, lr,
elapsed * 1000 / args.log_interval, cur_loss, math.exp(cur_loss)))
total_loss = 0
start_time = time.time()
示例14: train
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def train():
# Turn on training mode which enables dropout.
model.train()
total_loss = 0
start_time = time.time()
ntokens = len(corpus.dictionary)
hidden = model.init_hidden(args.batch_size)
for batch, i in enumerate(range(0, train_data.size(0) - 1, args.bptt)):
data, targets = get_batch(train_data, i)
# Starting each batch, we detach the hidden state from how it was previously produced.
# If we didn't, the model would try backpropagating all the way to start of the dataset.
hidden = repackage_hidden(hidden)
model.zero_grad()
output, hidden = model(data, hidden)
loss = criterion(output.view(-1, ntokens), targets)
loss.backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
torch.nn.utils.clip_grad_norm(model.parameters(), args.clip)
for p in model.parameters():
p.data.add_(-lr, p.grad.data)
total_loss += loss.data
if batch % args.log_interval == 0 and batch > 0:
cur_loss = total_loss[0] / args.log_interval
elapsed = time.time() - start_time
print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | '
'loss {:5.2f} | ppl {:8.2f}'.format(
epoch, batch, len(train_data) // args.bptt, lr,
elapsed * 1000 / args.log_interval, cur_loss, math.exp(cur_loss)))
total_loss = 0
start_time = time.time()
# Loop over epochs.
示例15: save_weight_only
# 需要导入模块: import model [as 别名]
# 或者: from model import train [as 别名]
def save_weight_only(model, sess, log_path, step):
"""
Save model but only weight (meaning no hidden variable)
In practice use this to just transfer weights from one model to the other
Args:
model : model to save variable from
sess : tensorflow session
log_path : where to save
step : number of step at time of saving
"""
path = log_path + '/' + model.name + '_weight_only'
if tf.gfile.Exists(path):
tf.gfile.DeleteRecursively(path)
tf.gfile.MakeDirs(path)
variable_to_save = {}
for i in range(30):
name = 'conv_' + str(i)
variable_to_save[name] = model.parameters_conv[i]
if i in [2, 4] and model.concat:
name = 'deconv_' + str(i)
variable_to_save[name] = model.parameters_deconv[i][0]
name = 'deconv_' + str(i) + '_bis'
variable_to_save[name] = model.parameters_deconv[i][1]
else:
name = 'deconv_' + str(i)
variable_to_save[name] = model.parameters_deconv[i]
if i < 2:
name = 'deconv_bis_' + str(i)
variable_to_save[name] = model.deconv[i]
saver = tf.train.Saver(variable_to_save)
checkpoint_path = os.path.join(path, 'model.ckpt')
saver.save(sess, checkpoint_path, global_step=step)