本文整理汇总了Python中torch.optim.Adam.step方法的典型用法代码示例。如果您正苦于以下问题:Python Adam.step方法的具体用法?Python Adam.step怎么用?Python Adam.step使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.optim.Adam
的用法示例。
在下文中一共展示了Adam.step方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pretrain
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
def pretrain(self, train_data, corrupter, tester):
src, rel, dst = train_data
n_train = len(src)
optimizer = Adam(self.mdl.parameters())
#optimizer = SGD(self.mdl.parameters(), lr=1e-4)
n_epoch = self.config.n_epoch
n_batch = self.config.n_batch
best_perf = 0
for epoch in range(n_epoch):
epoch_loss = 0
rand_idx = t.randperm(n_train)
src = src[rand_idx]
rel = rel[rand_idx]
dst = dst[rand_idx]
src_corrupted, dst_corrupted = corrupter.corrupt(src, rel, dst)
src_cuda = src.cuda()
rel_cuda = rel.cuda()
dst_cuda = dst.cuda()
src_corrupted = src_corrupted.cuda()
dst_corrupted = dst_corrupted.cuda()
for s0, r, t0, s1, t1 in batch_by_num(n_batch, src_cuda, rel_cuda, dst_cuda, src_corrupted, dst_corrupted,
n_sample=n_train):
self.mdl.zero_grad()
loss = t.sum(self.mdl.pair_loss(Variable(s0), Variable(r), Variable(t0), Variable(s1), Variable(t1)))
loss.backward()
optimizer.step()
self.mdl.constraint()
epoch_loss += loss.data[0]
logging.info('Epoch %d/%d, Loss=%f', epoch + 1, n_epoch, epoch_loss / n_train)
if (epoch + 1) % self.config.epoch_per_test == 0:
test_perf = tester()
if test_perf > best_perf:
self.save(os.path.join(config().task.dir, self.config.model_file))
best_perf = test_perf
return best_perf
示例2: pretrain
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
def pretrain(self, train_data, corrupter, tester):
src, rel, dst = train_data
n_train = len(src)
n_epoch = self.config.n_epoch
n_batch = self.config.n_batch
optimizer = Adam(self.mdl.parameters(), weight_decay=self.weight_decay)
best_perf = 0
for epoch in range(n_epoch):
epoch_loss = 0
if epoch % self.config.sample_freq == 0:
rand_idx = t.randperm(n_train)
src = src[rand_idx]
rel = rel[rand_idx]
dst = dst[rand_idx]
src_corrupted, rel_corrupted, dst_corrupted = corrupter.corrupt(src, rel, dst)
src_corrupted = src_corrupted.cuda()
rel_corrupted = rel_corrupted.cuda()
dst_corrupted = dst_corrupted.cuda()
for ss, rs, ts in batch_by_num(n_batch, src_corrupted, rel_corrupted, dst_corrupted, n_sample=n_train):
self.mdl.zero_grad()
label = t.zeros(len(ss)).type(t.LongTensor).cuda()
loss = t.sum(self.mdl.softmax_loss(Variable(ss), Variable(rs), Variable(ts), label))
loss.backward()
optimizer.step()
epoch_loss += loss.data[0]
logging.info('Epoch %d/%d, Loss=%f', epoch + 1, n_epoch, epoch_loss / n_train)
if (epoch + 1) % self.config.epoch_per_test == 0:
test_perf = tester()
if test_perf > best_perf:
self.save(os.path.join(config().task.dir, self.config.model_file))
best_perf = test_perf
return best_perf
示例3: train
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
def train(self, data):
data = data['data']
self.network.train()
optimizer = Adam(trainable_parameters(self.network), lr=1e-5)
for epoch, batch in self._driver(data):
self.network.zero_grad()
# choose a batch of anchors
indices, anchor = self._select_batch(data)
anchor_v = self._variable(anchor)
a = self._apply_network_and_normalize(anchor_v)
# choose negative examples
negative_indices, negative = self._select_batch(data)
negative_v = self._variable(negative)
n = self._apply_network_and_normalize(negative_v)
# choose a deformation for this batch and apply it to produce the
# positive examples
deformation = choice(self.deformations)
positive = deformation(anchor, data[indices, ...]) \
.astype(np.float32)
positive_v = self._variable(positive)
p = self._apply_network_and_normalize(positive_v)
error = self.loss.forward(a, p, n)
error.backward()
optimizer.step()
self.on_batch_complete(
epoch=epoch,
batch=batch,
error=float(error.data.cpu().numpy().squeeze()),
deformation=deformation.__name__)
return self.network
示例4: learn
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
def learn(learning_rate, iterations, x, y, validation=None, stop_early=False, run_comment=''):
# Define a neural network using high-level modules.
writer = SummaryWriter(comment=run_comment)
model = Sequential(
Linear(len(x[0]), len(y[0]), bias=True) # n inputs -> 1 output
)
loss_fn = BCEWithLogitsLoss(reduction='sum') # reduction=mean converges slower.
# TODO: Add an option to twiddle pos_weight, which lets us trade off precision and recall. Maybe also graph using add_pr_curve(), which can show how that tradeoff is going.
optimizer = Adam(model.parameters(),lr=learning_rate)
if validation:
validation_ins, validation_outs = validation
previous_validation_loss = None
with progressbar(range(iterations)) as bar:
for t in bar:
y_pred = model(x) # Make predictions.
loss = loss_fn(y_pred, y)
writer.add_scalar('loss', loss, t)
if validation:
validation_loss = loss_fn(model(validation_ins), validation_outs)
if stop_early:
if previous_validation_loss is not None and previous_validation_loss < validation_loss:
print('Stopping early at iteration {t} because validation error rose.'.format(t=t))
model.load_state_dict(previous_model)
break
else:
previous_validation_loss = validation_loss
previous_model = model.state_dict()
writer.add_scalar('validation_loss', validation_loss, t)
writer.add_scalar('training_accuracy_per_tag', accuracy_per_tag(model, x, y), t)
optimizer.zero_grad() # Zero the gradients.
loss.backward() # Compute gradients.
optimizer.step()
# Horizontal axis is what confidence. Vertical is how many samples were that confidence.
writer.add_histogram('confidence', confidences(model, x), t)
writer.close()
return model
示例5: netd
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
output = netd(real_img)
error_d_real = criterion(output, true_labels)
error_d_real.backward()
noises.data.copy_(torch.randn(opt.batch_size, opt.nz, 1, 1))
fake_img = netg(noises).detach()
fake_output = netd(fake_img)
error_d_fake = criterion(fake_output, fake_labels)
error_d_fake.backward()
error = error_d_real + error_d_fake
print('error_d:', error.data[0])
writer.add_scalar('data/error_d', error_d_fake.data[0], ii)
optimizer_d.step()
if (ii + 1) % opt.g_every == 0:
optimizer_g.zero_grad()
noises.data.copy_(torch.randn(opt.batch_size, opt.nz, 1, 1))
fake_img = netg(noises)
fake_output = netd(fake_img)
error_g = criterion(fake_output, true_labels)
print('error_g:,', error_g.data[0])
writer.add_scalar('data/error_g', error_g.data[0], ii)
error_g.backward()
optimizer_g.step()
示例6: _run
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
#.........这里部分代码省略.........
transfer batch to Torch: 1 ms, (1508424955295, 1508424955296)
Generating one example time: 2~5 ms, (1508458881118, 1508458881122)
Generating one document time: 50~400 ms, (1508458881118, 1508458881122)
Generating one batch time: 650~700 ms, (1508458880690, 1508458881122)
After changing to torch.sampler
Generating one example time: 4~7 ms
Generating one batch time: 900~1200 ms
'''
model = DistributedMemory(
vec_dim,
num_docs=len(dataset),
num_words=vocabulary_size)
cost_func = NegativeSampling()
optimizer = Adam(params=model.parameters(), lr=lr)
logger = logging.getLogger('root')
if torch.cuda.is_available():
model.cuda()
logger.info("Running on GPU - CUDA")
else:
logger.info("Running on CPU")
logger.info("Dataset comprised of {:d} documents.".format(len(dataset)))
logger.info("Vocabulary size is {:d}.\n".format(vocabulary_size))
logger.info("Training started.")
best_loss = float_info.max
prev_model_file_path = ""
progbar = Progbar(num_batches, batch_size=batch_size, total_examples = number_examples)
for epoch_i in range(num_epochs):
epoch_start_time = time.time()
loss = []
for batch_i in range(num_batches):
start_time = current_milli_time()
batch = next(data_generator)
current_time = current_milli_time()
print('data-prepare time: %d ms' % (round(current_time - start_time)))
start_time = current_milli_time()
x = model.forward(
batch.context_ids,
batch.doc_ids,
batch.target_noise_ids)
x = cost_func.forward(x)
loss.append(x.data[0])
print('forward time: %d ms' % round(current_milli_time() - start_time))
start_time = current_milli_time()
model.zero_grad()
x.backward()
optimizer.step()
print('backward time: %d ms' % round(current_milli_time() - start_time))
progbar.update(epoch_i, batch_i, )
# _print_progress(epoch_i, batch_i, num_batches)
# end of epoch
loss = torch.mean(torch.FloatTensor(loss))
is_best_loss = loss < best_loss
best_loss = min(loss, best_loss)
progbar.update(epoch_i, batch_i, [('loss', loss), ('best_loss', best_loss)])
model_file_name = MODEL_NAME.format(
data_file_name[:-4],
model_ver,
vec_combine_method,
context_size,
num_noise_words,
vec_dim,
batch_size,
lr,
epoch_i + 1,
loss)
model_file_path = join(MODELS_DIR, model_file_name)
if not os.path.exists(MODELS_DIR):
os.makedirs(MODELS_DIR)
state = {
'epoch': epoch_i + 1,
'model_state_dict': model.state_dict(),
'best_loss': best_loss,
'optimizer_state_dict': optimizer.state_dict()
}
if save_all:
torch.save(state, model_file_path)
elif is_best_loss:
try:
remove(prev_model_file_path)
except FileNotFoundError:
pass
torch.save(state, model_file_path)
prev_model_file_path = model_file_path
epoch_total_time = round(time.time() - epoch_start_time)
logger.info(" ({:d}s) - loss: {:.4f}".format(epoch_total_time, loss))
示例7: DDPG
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
class DDPG(object):
def __init__(self, nb_status, nb_actions, args, writer):
self.clip_actor_grad = args.clip_actor_grad
self.nb_status = nb_status * args.window_length
self.nb_actions = nb_actions
self.writer = writer
self.select_time = 0
# Create Actor and Critic Network
net_cfg = {
'hidden1':args.hidden1,
'hidden2':args.hidden2,
'init_method':args.init_method
}
self.actor = Actor(self.nb_status, self.nb_actions, **net_cfg)
self.actor_target = Actor(self.nb_status, self.nb_actions, **net_cfg)
self.actor_optim = Adam(self.actor.parameters(), lr=args.prate)
self.critic = Critic(self.nb_status, self.nb_actions, **net_cfg)
self.critic_target = Critic(self.nb_status, self.nb_actions, **net_cfg)
self.critic_optim = Adam(self.critic.parameters(), lr=args.rate)
hard_update(self.actor_target, self.actor) # Make sure target is with the same weight
hard_update(self.critic_target, self.critic)
#Create replay buffer
self.memory = rpm(args.rmsize)
self.random_process = Myrandom(size=nb_actions)
# Hyper-parameters
self.batch_size = args.batch_size
self.tau = args.tau
self.discount = args.discount
self.depsilon = 1.0 / args.epsilon
#
self.epsilon = 1.0
self.s_t = None # Most recent state
self.a_t = None # Most recent action
self.use_cuda = args.cuda
#
if self.use_cuda: self.cuda()
def update_policy(self, train_actor = True):
# Sample batch
state_batch, action_batch, reward_batch, \
next_state_batch, terminal_batch = self.memory.sample_batch(self.batch_size)
# Prepare for the target q batch
next_q_values = self.critic_target([
to_tensor(next_state_batch, volatile=True),
self.actor_target(to_tensor(next_state_batch, volatile=True)),
])
# print('batch of picture is ok')
next_q_values.volatile = False
target_q_batch = to_tensor(reward_batch) + \
self.discount * to_tensor((1 - terminal_batch.astype(np.float))) * next_q_values
# Critic update
self.critic.zero_grad()
q_batch = self.critic([to_tensor(state_batch), to_tensor(action_batch)])
# print(reward_batch, next_q_values*self.discount, target_q_batch, terminal_batch.astype(np.float))
value_loss = nn.MSELoss()(q_batch, target_q_batch)
value_loss.backward()
self.critic_optim.step()
self.actor.zero_grad()
policy_loss = -self.critic([
to_tensor(state_batch),
self.actor(to_tensor(state_batch))
])
policy_loss = policy_loss.mean()
policy_loss.backward()
if self.clip_actor_grad is not None:
torch.nn.utils.clip_grad_norm(self.actor.parameters(), float(self.clip_actor_grad))
if self.writer != None:
mean_policy_grad = np.array(np.mean([np.linalg.norm(p.grad.data.cpu().numpy().ravel()) for p in self.actor.parameters()]))
#print(mean_policy_grad)
self.writer.add_scalar('train/mean_policy_grad', mean_policy_grad, self.select_time)
if train_actor:
self.actor_optim.step()
# Target update
soft_update(self.actor_target, self.actor, self.tau)
soft_update(self.critic_target, self.critic, self.tau)
return -policy_loss, value_loss
def eval(self):
self.actor.eval()
self.actor_target.eval()
#.........这里部分代码省略.........
示例8: DDPG
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
class DDPG(object):
def __init__(self, gamma, tau, hidden_size, num_inputs, action_space):
self.num_inputs = num_inputs
self.action_space = action_space
self.actor = Actor(hidden_size, self.num_inputs, self.action_space)
self.actor_target = Actor(hidden_size, self.num_inputs, self.action_space)
self.actor_perturbed = Actor(hidden_size, self.num_inputs, self.action_space)
self.actor_optim = Adam(self.actor.parameters(), lr=1e-4)
self.critic = Critic(hidden_size, self.num_inputs, self.action_space)
self.critic_target = Critic(hidden_size, self.num_inputs, self.action_space)
self.critic_optim = Adam(self.critic.parameters(), lr=1e-3)
self.gamma = gamma
self.tau = tau
hard_update(self.actor_target, self.actor) # Make sure target is with the same weight
hard_update(self.critic_target, self.critic)
def select_action(self, state, action_noise=None, param_noise=None):
self.actor.eval()
if param_noise is not None:
mu = self.actor_perturbed((Variable(state)))
else:
mu = self.actor((Variable(state)))
self.actor.train()
mu = mu.data
if action_noise is not None:
mu += torch.Tensor(action_noise.noise())
return mu.clamp(-1, 1)
def update_parameters(self, batch):
state_batch = Variable(torch.cat(batch.state))
action_batch = Variable(torch.cat(batch.action))
reward_batch = Variable(torch.cat(batch.reward))
mask_batch = Variable(torch.cat(batch.mask))
next_state_batch = Variable(torch.cat(batch.next_state))
next_action_batch = self.actor_target(next_state_batch)
next_state_action_values = self.critic_target(next_state_batch, next_action_batch)
reward_batch = reward_batch.unsqueeze(1)
mask_batch = mask_batch.unsqueeze(1)
expected_state_action_batch = reward_batch + (self.gamma * mask_batch * next_state_action_values)
self.critic_optim.zero_grad()
state_action_batch = self.critic((state_batch), (action_batch))
value_loss = F.mse_loss(state_action_batch, expected_state_action_batch)
value_loss.backward()
self.critic_optim.step()
self.actor_optim.zero_grad()
policy_loss = -self.critic((state_batch),self.actor((state_batch)))
policy_loss = policy_loss.mean()
policy_loss.backward()
self.actor_optim.step()
soft_update(self.actor_target, self.actor, self.tau)
soft_update(self.critic_target, self.critic, self.tau)
return value_loss.item(), policy_loss.item()
def perturb_actor_parameters(self, param_noise):
"""Apply parameter noise to actor model, for exploration"""
hard_update(self.actor_perturbed, self.actor)
params = self.actor_perturbed.state_dict()
for name in params:
if 'ln' in name:
pass
param = params[name]
param += torch.randn(param.shape) * param_noise.current_stddev
def save_model(self, env_name, suffix="", actor_path=None, critic_path=None):
if not os.path.exists('models/'):
os.makedirs('models/')
if actor_path is None:
actor_path = "models/ddpg_actor_{}_{}".format(env_name, suffix)
if critic_path is None:
critic_path = "models/ddpg_critic_{}_{}".format(env_name, suffix)
print('Saving models to {} and {}'.format(actor_path, critic_path))
torch.save(self.actor.state_dict(), actor_path)
torch.save(self.critic.state_dict(), critic_path)
def load_model(self, actor_path, critic_path):
print('Loading models from {} and {}'.format(actor_path, critic_path))
if actor_path is not None:
self.actor.load_state_dict(torch.load(actor_path))
if critic_path is not None:
#.........这里部分代码省略.........
示例9: fit
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
#.........这里部分代码省略.........
batches_per_epoch = batch_generator.batches_per_epoch
# save list of classes (one speaker per line)
labels = batch_generator.labels
classes_txt = self.CLASSES_TXT.format(log_dir=log_dir)
with open(classes_txt, mode='w') as fp:
for label in labels:
fp.write(f'{label}\n')
# initialize classifier
n_classes = batch_generator.n_classes
classifier = Classifier(model.output_dim, n_classes,
linear=self.linear)
# load precomputed weights in case of restart
if restart > 0:
weights_pt = checkpoint.WEIGHTS_PT.format(
log_dir=log_dir, epoch=restart)
model.load_state_dict(torch.load(weights_pt))
classifier_pt = self.CLASSIFIER_PT.format(
log_dir=log_dir, epoch=restart)
# send models to GPU
if gpu:
model = model.cuda()
classifier = classifier.cuda(device=None)
model.internal = False
optimizer = Adam(list(model.parameters()) + \
list(classifier.parameters()))
if restart > 0:
optimizer_pt = checkpoint.OPTIMIZER_PT.format(
log_dir=log_dir, epoch=restart)
optimizer.load_state_dict(torch.load(optimizer_pt))
if gpu:
for state in optimizer.state.values():
for k, v in state.items():
if torch.is_tensor(v):
state[k] = v.cuda()
epoch = restart if restart > 0 else -1
while True:
epoch += 1
if epoch > epochs:
break
loss_avg = 0.
log_epoch = (epoch < 10) or (epoch % 5 == 0)
if log_epoch:
pass
desc = 'Epoch #{0}'.format(epoch)
for i in tqdm(range(batches_per_epoch), desc=desc):
model.zero_grad()
batch = next(batches)
X = batch['X']
y = batch['y']
if not getattr(model, 'batch_first', True):
X = np.rollaxis(X, 0, 2)
X = np.array(X, dtype=np.float32)
X = Variable(torch.from_numpy(X))
y = Variable(torch.from_numpy(y))
if gpu:
X = X.cuda()
y = y.cuda()
fX = model(X)
y_pred = classifier(fX)
loss = self.loss_(y_pred, y)
if log_epoch:
pass
# log loss
if gpu:
loss_ = float(loss.data.cpu().numpy())
else:
loss_ = float(loss.data.numpy())
loss_avg += loss_
loss.backward()
optimizer.step()
loss_avg /= batches_per_epoch
writer.add_scalar('train/softmax/loss', loss_avg,
global_step=epoch)
if log_epoch:
pass
checkpoint.on_epoch_end(epoch, model, optimizer,
extra={self.CLASSIFIER_PT: classifier})
示例10: __init__
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
class DQN_Distribution_Agent:
def __init__(self, args, exp_model, logging_func):
self.args = args
# Exploration Model
self.exp_model = exp_model
self.log = logging_func["log"]
# Experience Replay
self.replay = ExpReplay(args.exp_replay_size, args.stale_limit, exp_model, args, priority=self.args.prioritized)
# DQN and Target DQN
model = get_models(args.model)
self.dqn = model(actions=args.actions, atoms=args.atoms)
self.target_dqn = model(actions=args.actions, atoms=args.atoms)
dqn_params = 0
for weight in self.dqn.parameters():
weight_params = 1
for s in weight.size():
weight_params *= s
dqn_params += weight_params
print("Distrib DQN has {:,} parameters.".format(dqn_params))
self.target_dqn.eval()
if args.gpu:
print("Moving models to GPU.")
self.dqn.cuda()
self.target_dqn.cuda()
# Optimizer
self.optimizer = Adam(self.dqn.parameters(), lr=args.lr)
# self.optimizer = RMSprop(self.dqn.parameters(), lr=args.lr)
self.T = 0
self.target_sync_T = -self.args.t_max
def sync_target_network(self):
for target, source in zip(self.target_dqn.parameters(), self.dqn.parameters()):
target.data = source.data
def act(self, state, epsilon, exp_model, evaluation=False):
# self.T += 1
self.dqn.eval()
orig_state = state[:, :, -1:]
state = torch.from_numpy(state).float().transpose_(0, 2).unsqueeze(0)
q_values_distributions = self.dqn(Variable(state, volatile=True)).cpu().data[0]
# TODO: Log Q-Value distributions
# print(q_values_distributions)
values = torch.linspace(self.args.v_min, self.args.v_max, steps=self.args.atoms)
values = values.view(1, self.args.atoms)
values = values.expand(self.args.actions, self.args.atoms)
# print(values, q_values_distributions, torch.sum(q_values_distributions * values, dim=1))
q_value_expectations = torch.sum(q_values_distributions * values, dim=1)
q_values_numpy = q_value_expectations.numpy()
extra_info = {}
if self.args.optimistic_init and not evaluation:
raise NotImplementedError
q_values_pre_bonus = np.copy(q_values_numpy)
if not self.args.ucb:
for a in range(self.args.actions):
_, info = exp_model.bonus(orig_state, a, dont_remember=True)
action_pseudo_count = info["Pseudo_Count"]
# TODO: Log the optimism bonuses
optimism_bonus = self.args.optimistic_scaler / np.sqrt(action_pseudo_count + 0.01)
self.log("Bandit/Action_{}".format(a), optimism_bonus, step=self.T)
q_values[a] += optimism_bonus
else:
action_counts = []
for a in range(self.args.actions):
_, info = exp_model.bonus(orig_state, a, dont_remember=True)
action_pseudo_count = info["Pseudo_Count"]
action_counts.append(action_pseudo_count)
total_count = sum(action_counts)
for ai, a in enumerate(action_counts):
# TODO: Log the optimism bonuses
optimisim_bonus = self.args.optimistic_scaler * np.sqrt(2 * np.log(max(1, total_count)) / (a + 0.01))
self.log("Bandit/UCB/Action_{}".format(ai), optimisim_bonus, step=self.T)
q_values[ai] += optimisim_bonus
extra_info["Action_Bonus"] = q_values_numpy - q_values_pre_bonus
extra_info["Q_Values"] = q_values_numpy
if np.random.random() < epsilon:
action = np.random.randint(low=0, high=self.args.actions)
else:
action = int(np.argmax(q_values_numpy))
# action = q_values.max(0)[1][0] # Torch...
extra_info["Action"] = action
return action, extra_info
#.........这里部分代码省略.........
示例11: train
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
def train(args):
device = torch.device("cuda" if args.cuda else "cpu")
np.random.seed(args.seed)
torch.manual_seed(args.seed)
transform = transforms.Compose([
transforms.Resize(args.image_size),
transforms.CenterCrop(args.image_size),
transforms.ToTensor(),
transforms.Lambda(lambda x: x.mul(255))
])
train_dataset = datasets.ImageFolder(args.dataset, transform)
train_loader = DataLoader(train_dataset, batch_size=args.batch_size)
transformer = TransformerNet().to(device)
optimizer = Adam(transformer.parameters(), args.lr)
mse_loss = torch.nn.MSELoss()
vgg = Vgg16(requires_grad=False).to(device)
style_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x.mul(255))
])
style = utils.load_image(args.style_image, size=args.style_size)
style = style_transform(style)
style = style.repeat(args.batch_size, 1, 1, 1).to(device)
features_style = vgg(utils.normalize_batch(style))
gram_style = [utils.gram_matrix(y) for y in features_style]
for e in range(args.epochs):
transformer.train()
agg_content_loss = 0.
agg_style_loss = 0.
count = 0
for batch_id, (x, _) in enumerate(train_loader):
n_batch = len(x)
count += n_batch
optimizer.zero_grad()
x = x.to(device)
y = transformer(x)
y = utils.normalize_batch(y)
x = utils.normalize_batch(x)
features_y = vgg(y)
features_x = vgg(x)
content_loss = args.content_weight * mse_loss(features_y.relu2_2, features_x.relu2_2)
style_loss = 0.
for ft_y, gm_s in zip(features_y, gram_style):
gm_y = utils.gram_matrix(ft_y)
style_loss += mse_loss(gm_y, gm_s[:n_batch, :, :])
style_loss *= args.style_weight
total_loss = content_loss + style_loss
total_loss.backward()
optimizer.step()
agg_content_loss += content_loss.item()
agg_style_loss += style_loss.item()
if (batch_id + 1) % args.log_interval == 0:
mesg = "{}\tEpoch {}:\t[{}/{}]\tcontent: {:.6f}\tstyle: {:.6f}\ttotal: {:.6f}".format(
time.ctime(), e + 1, count, len(train_dataset),
agg_content_loss / (batch_id + 1),
agg_style_loss / (batch_id + 1),
(agg_content_loss + agg_style_loss) / (batch_id + 1)
)
print(mesg)
if args.checkpoint_model_dir is not None and (batch_id + 1) % args.checkpoint_interval == 0:
transformer.eval().cpu()
ckpt_model_filename = "ckpt_epoch_" + str(e) + "_batch_id_" + str(batch_id + 1) + ".pth"
ckpt_model_path = os.path.join(args.checkpoint_model_dir, ckpt_model_filename)
torch.save(transformer.state_dict(), ckpt_model_path)
transformer.to(device).train()
# save model
transformer.eval().cpu()
save_model_filename = "epoch_" + str(args.epochs) + "_" + str(time.ctime()).replace(' ', '_') + "_" + str(
args.content_weight) + "_" + str(args.style_weight) + ".model"
save_model_path = os.path.join(args.save_model_dir, save_model_filename)
torch.save(transformer.state_dict(), save_model_path)
print("\nDone, trained model saved at", save_model_path)
示例12: DDPG
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
class DDPG(object):
def __init__(self, nb_status, nb_actions, args, writer):
self.clip_actor_grad = args.clip_actor_grad
self.nb_status = nb_status * args.window_length
self.nb_actions = nb_actions
self.discrete = args.discrete
self.pic = args.pic
self.writer = writer
self.select_time = 0
if self.pic:
self.nb_status = args.pic_status
# Create Actor and Critic Network
net_cfg = {
'hidden1':args.hidden1,
'hidden2':args.hidden2,
'use_bn':args.bn,
'init_method':args.init_method
}
if args.pic:
self.cnn = CNN(1, args.pic_status)
self.cnn_target = CNN(1, args.pic_status)
self.cnn_optim = Adam(self.cnn.parameters(), lr=args.crate)
self.actor = Actor(self.nb_status, self.nb_actions, **net_cfg)
self.actor_target = Actor(self.nb_status, self.nb_actions, **net_cfg)
self.actor_optim = Adam(self.actor.parameters(), lr=args.prate)
self.critic = Critic(self.nb_status, self.nb_actions, **net_cfg)
self.critic_target = Critic(self.nb_status, self.nb_actions, **net_cfg)
self.critic_optim = Adam(self.critic.parameters(), lr=args.rate)
hard_update(self.actor_target, self.actor) # Make sure target is with the same weight
hard_update(self.critic_target, self.critic)
if args.pic:
hard_update(self.cnn_target, self.cnn)
#Create replay buffer
self.memory = rpm(args.rmsize) # SequentialMemory(limit=args.rmsize, window_length=args.window_length)
self.random_process = Myrandom(size=nb_actions)
# Hyper-parameters
self.batch_size = args.batch_size
self.tau = args.tau
self.discount = args.discount
self.depsilon = 1.0 / args.epsilon
#
self.epsilon = 1.0
self.s_t = None # Most recent state
self.a_t = None # Most recent action
self.use_cuda = args.cuda
#
if self.use_cuda: self.cuda()
def normalize(self, pic):
pic = pic.swapaxes(0, 2).swapaxes(1, 2)
return pic
def update_policy(self):
# Sample batch
state_batch, action_batch, reward_batch, \
next_state_batch, terminal_batch = self.memory.sample_batch(self.batch_size)
# Prepare for the target q batch
if self.pic:
state_batch = np.array([self.normalize(x) for x in state_batch])
state_batch = to_tensor(state_batch, volatile=True)
state_batch = self.cnn(state_batch)
next_state_batch = np.array([self.normalize(x) for x in next_state_batch])
next_state_batch = to_tensor(next_state_batch, volatile=True)
next_state_batch = self.cnn_target(next_state_batch)
next_q_values = self.critic_target([
next_state_batch,
self.actor_target(next_state_batch)
])
else:
next_q_values = self.critic_target([
to_tensor(next_state_batch, volatile=True),
self.actor_target(to_tensor(next_state_batch, volatile=True)),
])
# print('batch of picture is ok')
next_q_values.volatile = False
target_q_batch = to_tensor(reward_batch) + \
self.discount * to_tensor((1 - terminal_batch.astype(np.float))) * next_q_values
# Critic update
self.critic.zero_grad()
if self.pic: self.cnn.zero_grad()
if self.pic:
state_batch.volatile = False
q_batch = self.critic([state_batch, to_tensor(action_batch)])
else:
q_batch = self.critic([to_tensor(state_batch), to_tensor(action_batch)])
# print(reward_batch, next_q_values*self.discount, target_q_batch, terminal_batch.astype(np.float))
value_loss = criterion(q_batch, target_q_batch)
value_loss.backward()
self.critic_optim.step()
#.........这里部分代码省略.........
示例13: RnnGuesser
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
#.........这里部分代码省略.........
self.model = self.model.cuda()
log.info(f'Parameters:\n{self.parameters()}')
log.info(f'Model:\n{self.model}')
self.optimizer = Adam(self.model.parameters())
self.criterion = nn.CrossEntropyLoss()
self.scheduler = lr_scheduler.ReduceLROnPlateau(self.optimizer, patience=5, verbose=True, mode='max')
temp_prefix = get_tmp_filename()
self.model_file = f'{temp_prefix}.pt'
manager = TrainingManager([
BaseLogger(log_func=log.info), TerminateOnNaN(), EarlyStopping(monitor='test_acc', patience=10, verbose=1),
MaxEpochStopping(100), ModelCheckpoint(create_save_model(self.model), self.model_file, monitor='test_acc')
])
log.info('Starting training')
epoch = 0
while True:
self.model.train()
train_acc, train_loss, train_time = self.run_epoch(train_iter)
self.model.eval()
test_acc, test_loss, test_time = self.run_epoch(val_iter)
stop_training, reasons = manager.instruct(
train_time, train_loss, train_acc,
test_time, test_loss, test_acc
)
if stop_training:
log.info(' '.join(reasons))
break
else:
self.scheduler.step(test_acc)
epoch += 1
def run_epoch(self, iterator: Iterator):
is_train = iterator.train
batch_accuracies = []
batch_losses = []
batch_size = self.batch_size
hidden_init = self.model.init_hidden(batch_size)
epoch_start = time.time()
for batch in iterator:
text, lengths = batch.text
lengths = list(lengths.cpu().numpy())
if len(lengths) != batch_size:
batch_size = len(lengths)
hidden_init = self.model.init_hidden(batch_size)
page = batch.page
qanta_ids = batch.qanta_id.cuda()
if is_train:
self.model.zero_grad()
out, hidden = self.model(
text, lengths, hidden_init, qanta_ids
)
_, preds = torch.max(out, 1)
accuracy = torch.mean(torch.eq(preds, page).float()).data[0]
batch_loss = self.criterion(out, page)
if is_train:
batch_loss.backward()
torch.nn.utils.clip_grad_norm(self.model.parameters(), self.gradient_clip)
self.optimizer.step()
示例14: DanGuesser
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
#.........这里部分代码省略.........
self.model = self.model.cuda()
log.info(f'Parameters:\n{self.parameters()}')
log.info(f'Model:\n{self.model}')
self.optimizer = Adam(self.model.parameters())
self.criterion = nn.CrossEntropyLoss()
self.scheduler = lr_scheduler.ReduceLROnPlateau(self.optimizer, patience=5, verbose=True, mode='max')
temp_prefix = get_tmp_filename()
self.model_file = f'{temp_prefix}.pt'
manager = TrainingManager([
BaseLogger(log_func=log.info), TerminateOnNaN(), EarlyStopping(monitor='test_acc', patience=10, verbose=1),
MaxEpochStopping(100), ModelCheckpoint(create_save_model(self.model), self.model_file, monitor='test_acc')
])
log.info('Starting training')
epoch = 0
while True:
self.model.train()
train_acc, train_loss, train_time = self.run_epoch(train_iter)
self.model.eval()
test_acc, test_loss, test_time = self.run_epoch(val_iter)
stop_training, reasons = manager.instruct(
train_time, train_loss, train_acc,
test_time, test_loss, test_acc
)
if stop_training:
log.info(' '.join(reasons))
break
else:
self.scheduler.step(test_acc)
epoch += 1
def run_epoch(self, iterator: Iterator):
is_train = iterator.train
batch_accuracies = []
batch_losses = []
epoch_start = time.time()
for batch in iterator:
input_dict = {}
lengths_dict = {}
if hasattr(batch, 'text'):
text, lengths = batch.text
input_dict['text'] = text
lengths_dict['text'] = lengths
if hasattr(batch, 'unigram'):
text, lengths = batch.unigram
input_dict['unigram'] = text
lengths_dict['unigram'] = lengths
if hasattr(batch, 'bigram'):
text, lengths = batch.bigram
input_dict['bigram'] = text
lengths_dict['bigram'] = lengths
if hasattr(batch, 'trigram'):
text, lengths = batch.trigram
input_dict['trigram'] = text
lengths_dict['trigram'] = lengths
page = batch.page
qanta_ids = batch.qanta_id.cuda()
示例15: fit
# 需要导入模块: from torch.optim import Adam [as 别名]
# 或者: from torch.optim.Adam import step [as 别名]
#.........这里部分代码省略.........
domain_pdist_ = domain_pdist.data.numpy()
is_positive = pdist(domain_y.reshape((-1, 1)), metric='chebyshev') < 1
positive.append(domain_pdist_[np.where(is_positive)])
negative.append(domain_pdist_[np.where(~is_positive)])
# sample triplets
if self.sampling == 'all':
anchors, positives, negatives = self.batch_all(
domain_y, domain_pdist)
elif self.sampling == 'hard':
anchors, positives, negatives = self.batch_hard(
domain_y, domain_pdist)
# compute triplet loss
triplet_losses.append(
self.triplet_loss(domain_pdist, anchors,
positives, negatives))
tloss = 0.
for tl in triplet_losses:
tloss += torch.mean(tl)
tloss /= len(triplet_losses)
if gpu:
tloss_ = float(tloss.data.cpu().numpy())
else:
tloss_ = float(tloss.data.numpy())
tloss_avg += tloss_
# domain-adversarial
y_domain = Variable(torch.from_numpy(np.array(y_domain)))
if gpu:
y_domain = y_domain.cuda()
domain_score = domain_clf(fX)
dloss = domain_loss(domain_score, y_domain)
if gpu:
dloss_ = float(dloss.data.cpu().numpy())
else:
dloss_ = float(dloss.data.numpy())
dloss_avg += dloss_
# log domain classification accuracy
if gpu:
domain_score_ = domain_score.data.cpu().numpy()
y_domain_ = y_domain.data.cpu().numpy()
else:
domain_score_ = domain_score.data.numpy()
y_domain_ = y_domain.data.numpy()
dacc_ = np.mean(np.argmax(domain_score_, axis=1) == y_domain_)
dacc_avg += dacc_
loss = tloss + dloss
if gpu:
loss_ = float(loss.data.cpu().numpy())
else:
loss_ = float(loss.data.numpy())
loss_avg += loss_
loss_avg += loss_
loss.backward()
optimizer.step()
# if gpu:
# embeddings = fX.data.cpu()
# else:
# embeddings = fX.data
# metadata = list(batch['extra'][self.domain])
#
# writer.add_embedding(embeddings, metadata=metadata,
# global_step=epoch)
tloss_avg /= batches_per_epoch
writer.add_scalar('tloss', tloss_avg, global_step=epoch)
dloss_avg /= batches_per_epoch
writer.add_scalar('dloss', dloss_avg, global_step=epoch)
loss_avg /= batches_per_epoch
writer.add_scalar('loss', loss_avg, global_step=epoch)
dacc_avg /= batches_per_epoch
writer.add_scalar('dacc', dacc_avg, global_step=epoch)
positive = np.hstack(positive)
negative = np.hstack(negative)
writer.add_histogram(
'embedding/pairwise_distance/positive', positive,
global_step=epoch, bins='auto')
writer.add_histogram(
'embedding/pairwise_distance/negative', negative,
global_step=epoch, bins='auto')
if not model.normalize:
norms = np.hstack(norms)
writer.add_histogram(
'embedding/norm', norms,
global_step=epoch, bins='auto')
checkpoint.on_epoch_end(epoch, model, optimizer)