本文整理汇总了Python中config.lr方法的典型用法代码示例。如果您正苦于以下问题:Python config.lr方法的具体用法?Python config.lr怎么用?Python config.lr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类config
的用法示例。
在下文中一共展示了config.lr方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_train
# 需要导入模块: import config [as 别名]
# 或者: from config import lr [as 别名]
def setup_train(self, model_file_path=None):
self.model = Model(model_file_path)
params = list(self.model.encoder.parameters()) + list(self.model.decoder.parameters()) + \
list(self.model.reduce_state.parameters())
initial_lr = config.lr_coverage if config.is_coverage else config.lr
if config.mode == 'MLE':
self.optimizer = Adagrad(params, lr=0.15, initial_accumulator_value=0.1)
else:
self.optimizer = Adam(params, lr=initial_lr)
start_iter, start_loss = 0, 0
if model_file_path is not None:
state = torch.load(model_file_path, map_location= lambda storage, location: storage)
start_iter = state['iter']
start_loss = state['current_loss']
return start_iter, start_loss
示例2: main
# 需要导入模块: import config [as 别名]
# 或者: from config import lr [as 别名]
def main():
env = gym.make(env_name)
env.seed(500)
torch.manual_seed(500)
num_inputs = env.observation_space.shape[0]
num_actions = env.action_space.n
print('state size:', num_inputs)
print('action size:', num_actions)
online_net = QNet(num_inputs, num_actions)
target_net = QNet(num_inputs, num_actions)
target_net.load_state_dict(online_net.state_dict())
online_net.share_memory()
target_net.share_memory()
optimizer = SharedAdam(online_net.parameters(), lr=lr)
global_ep, global_ep_r, res_queue = mp.Value('i', 0), mp.Value('d', 0.), mp.Queue()
writer = SummaryWriter('logs')
online_net.to(device)
target_net.to(device)
online_net.train()
target_net.train()
workers = [Worker(online_net, target_net, optimizer, global_ep, global_ep_r, res_queue, i) for i in range(mp.cpu_count())]
[w.start() for w in workers]
res = []
while True:
r = res_queue.get()
if r is not None:
res.append(r)
[ep, ep_r, loss] = r
writer.add_scalar('log/score', float(ep_r), ep)
writer.add_scalar('log/loss', float(loss), ep)
else:
break
[w.join() for w in workers]
示例3: main
# 需要导入模块: import config [as 别名]
# 或者: from config import lr [as 别名]
def main():
env = gym.make(env_name)
env.seed(500)
torch.manual_seed(500)
num_inputs = env.observation_space.shape[0]
num_actions = env.action_space.n
env.close()
global_model = Model(num_inputs, num_actions)
global_average_model = Model(num_inputs, num_actions)
global_model.share_memory()
global_average_model.share_memory()
global_optimizer = SharedAdam(global_model.parameters(), lr=lr)
global_ep, global_ep_r, res_queue = mp.Value('i', 0), mp.Value('d', 0.), mp.Queue()
writer = SummaryWriter('logs')
n = mp.cpu_count()
workers = [Worker(global_model, global_average_model, global_optimizer, global_ep, global_ep_r, res_queue, i) for i in range(n)]
[w.start() for w in workers]
res = []
while True:
r = res_queue.get()
if r is not None:
res.append(r)
[ep, ep_r, loss] = r
writer.add_scalar('log/score', float(ep_r), ep)
writer.add_scalar('log/loss', float(loss), ep)
else:
break
[w.join() for w in workers]
示例4: __init__
# 需要导入模块: import config [as 别名]
# 或者: from config import lr [as 别名]
def __init__(self, global_online_model, global_target_model, global_memory, global_memory_pipe, res_queue):
super(Learner, self).__init__()
self.online_model = global_online_model
self.target_model = global_target_model
self.memory = global_memory
self.memory_pipe = global_memory_pipe
self.optimizer = optim.Adam(self.online_model.parameters(), lr=lr)
self.res_queue = res_queue
示例5: main
# 需要导入模块: import config [as 别名]
# 或者: from config import lr [as 别名]
def main():
env = gym.make(env_name)
env.seed(500)
torch.manual_seed(500)
num_inputs = env.observation_space.shape[0]
num_actions = env.action_space.n
global_model = Model(num_inputs, num_actions)
global_model.share_memory()
global_optimizer = SharedAdam(global_model.parameters(), lr=lr)
global_ep, global_ep_r, res_queue = mp.Value('i', 0), mp.Value('d', 0.), mp.Queue()
writer = SummaryWriter('logs')
workers = [Worker(global_model, global_optimizer, global_ep, global_ep_r, res_queue, i) for i in range(mp.cpu_count())]
[w.start() for w in workers]
res = []
while True:
r = res_queue.get()
if r is not None:
res.append(r)
[ep, ep_r, loss] = r
writer.add_scalar('log/score', float(ep_r), ep)
writer.add_scalar('log/loss', float(loss), ep)
else:
break
[w.join() for w in workers]
示例6: train
# 需要导入模块: import config [as 别名]
# 或者: from config import lr [as 别名]
def train(self):
batch_num = len(self.train_loader)
best_loss = 1e10
for epoch in range(1, config.num_epochs + 1):
self.model.train()
print("epoch {}/{} :".format(epoch, config.num_epochs), end="\r")
start = time.time()
# halving the learning rate after epoch 8
if epoch >= 8 and epoch % 2 == 0:
self.lr *= 0.5
state_dict = self.optim.state_dict()
for param_group in state_dict["param_groups"]:
param_group["lr"] = self.lr
self.optim.load_state_dict(state_dict)
for batch_idx, train_data in enumerate(self.train_loader, start=1):
batch_loss = self.step(train_data)
self.model.zero_grad()
batch_loss.backward()
# gradient clipping
nn.utils.clip_grad_norm_(self.model.parameters(),
config.max_grad_norm)
self.optim.step()
batch_loss = batch_loss.detach().item()
msg = "{}/{} {} - ETA : {} - loss : {:.4f}" \
.format(batch_idx, batch_num, progress_bar(batch_idx, batch_num),
eta(start, batch_idx, batch_num), batch_loss)
print(msg, end="\r")
val_loss = self.evaluate(msg)
if val_loss <= best_loss:
best_loss = val_loss
self.save_model(val_loss, epoch)
print("Epoch {} took {} - final loss : {:.4f} - val loss :{:.4f}"
.format(epoch, user_friendly_time(time_since(start)), batch_loss, val_loss))
示例7: main
# 需要导入模块: import config [as 别名]
# 或者: from config import lr [as 别名]
def main():
env = gym.make(env_name)
env.seed(500)
torch.manual_seed(500)
num_inputs = env.observation_space.shape[0]
num_actions = env.action_space.n
print('state size:', num_inputs)
print('action size:', num_actions)
net = QNet(num_inputs, num_actions)
optimizer = optim.Adam(net.parameters(), lr=lr)
writer = SummaryWriter('logs')
net.to(device)
net.train()
running_score = 0
steps = 0
loss = 0
for e in range(3000):
done = False
score = 0
state = env.reset()
state = torch.Tensor(state).to(device)
state = state.unsqueeze(0)
while not done:
steps += 1
action = net.get_action(state)
next_state, reward, done, _ = env.step(action)
next_state = torch.Tensor(next_state)
next_state = next_state.unsqueeze(0)
mask = 0 if done else 1
reward = reward if not done or score == 499 else -1
transition = [state, next_state, action, reward, mask]
score += reward
state = next_state
loss = QNet.train_model(net, optimizer, transition)
score = score if score == 500.0 else score + 1
running_score = 0.99 * running_score + 0.01 * score
if e % log_interval == 0:
print('{} episode | score: {:.2f}'.format(
e, running_score))
writer.add_scalar('log/score', float(running_score), e)
writer.add_scalar('log/loss', float(loss), e)
if running_score > goal_score:
break
示例8: main
# 需要导入模块: import config [as 别名]
# 或者: from config import lr [as 别名]
def main():
env = gym.make(env_name)
env.seed(500)
torch.manual_seed(500)
num_inputs = env.observation_space.shape[0]
num_actions = env.action_space.n
print('state size:', num_inputs)
print('action size:', num_actions)
net = GAE(num_inputs, num_actions)
optimizer = optim.Adam(net.parameters(), lr=lr)
writer = SummaryWriter('logs')
net.to(device)
net.train()
running_score = 0
steps = 0
loss = 0
for e in range(30000):
done = False
memory = Memory()
score = 0
state = env.reset()
state = torch.Tensor(state).to(device)
state = state.unsqueeze(0)
while not done:
steps += 1
action = net.get_action(state)
next_state, reward, done, _ = env.step(action)
next_state = torch.Tensor(next_state)
next_state = next_state.unsqueeze(0)
mask = 0 if done else 1
reward = reward if not done or score == 499 else -1
action_one_hot = torch.zeros(2)
action_one_hot[action] = 1
memory.push(state, next_state, action_one_hot, reward, mask)
score += reward
state = next_state
loss = GAE.train_model(net, memory.sample(), optimizer)
score = score if score == 500.0 else score + 1
running_score = 0.99 * running_score + 0.01 * score
if e % log_interval == 0:
print('{} episode | score: {:.2f}'.format(
e, running_score))
writer.add_scalar('log/score', float(running_score), e)
writer.add_scalar('log/loss', float(loss), e)
if running_score > goal_score:
break
示例9: __init__
# 需要导入模块: import config [as 别名]
# 或者: from config import lr [as 别名]
def __init__(self, args):
# load dictionary and embedding file
with open(config.embedding, "rb") as f:
embedding = pickle.load(f)
embedding = torch.tensor(embedding,
dtype=torch.float).to(config.device)
with open(config.word2idx_file, "rb") as f:
word2idx = pickle.load(f)
# train, dev loader
print("load train data")
self.train_loader = get_loader(config.train_src_file,
config.train_trg_file,
word2idx,
use_tag=True,
batch_size=config.batch_size,
debug=config.debug)
self.dev_loader = get_loader(config.dev_src_file,
config.dev_trg_file,
word2idx,
use_tag=True,
batch_size=128,
debug=config.debug)
train_dir = os.path.join("./save", "seq2seq")
self.model_dir = os.path.join(
train_dir, "train_%d" % int(time.strftime("%m%d%H%M%S")))
if not os.path.exists(self.model_dir):
os.makedirs(self.model_dir)
self.model = Seq2seq(embedding)
self.model = self.model.to(config.device)
if len(args.model_path) > 0:
print("load check point from: {}".format(args.model_path))
state_dict = torch.load(args.model_path,
map_location="cpu")
self.model.load_state_dict(state_dict)
params = self.model.parameters()
self.lr = config.lr
self.optim = optim.SGD(params, self.lr, momentum=0.8)
# self.optim = optim.Adam(params)
self.criterion = nn.CrossEntropyLoss(ignore_index=0)