本文整理汇总了Python中model.RNNModel方法的典型用法代码示例。如果您正苦于以下问题:Python model.RNNModel方法的具体用法?Python model.RNNModel怎么用?Python model.RNNModel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类model
的用法示例。
在下文中一共展示了model.RNNModel方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_flow
# 需要导入模块: import model [as 别名]
# 或者: from model import RNNModel [as 别名]
def generate_flow(epoch=3):
"""读取存储的模型,生成新词"""
corpus = Corpus(train_dir)
config = Config()
config.vocab_size = len(corpus.dictionary)
model = RNNModel(config)
model_file = os.path.join(save_dir, model_name.format(epoch))
assert os.path.exists(model_file), 'File %s does not exist.' % model_file
model.load_state_dict(torch.load(model_file, map_location=lambda storage, loc: storage))
word_list = generate(model, corpus.dictionary.idx2word, word_len=50)
print(''.join(word_list))
示例2: train
# 需要导入模块: import model [as 别名]
# 或者: from model import RNNModel [as 别名]
def train():
# 载入数据与配置模型
print("Loading data...")
corpus = Corpus(train_dir)
print(corpus)
config = Config()
config.vocab_size = len(corpus.dictionary)
train_data = batchify(corpus.train, config.batch_size)
train_len = train_data.size(0)
seq_len = config.seq_len
print("Configuring model...")
model = RNNModel(config)
if use_cuda:
model.cuda()
print(model)
criterion = nn.CrossEntropyLoss()
lr = config.learning_rate # 初始学习率
start_time = time.time()
print("Training and generating...")
for epoch in range(1, config.num_epochs + 1): # 多轮次训练
total_loss = 0.0
model.train() # 在训练模式下dropout才可用。
hidden = model.init_hidden(config.batch_size) # 初始化隐藏层参数
for ibatch, i in enumerate(range(0, train_len - 1, seq_len)):
data, targets = get_batch(train_data, i, seq_len) # 取一个批次的数据
# 在每批开始之前,将隐藏的状态与之前产生的结果分离。
# 如果不这样做,模型会尝试反向传播到数据集的起点。
hidden = repackage_hidden(hidden)
model.zero_grad()
output, hidden = model(data, hidden)
loss = criterion(output.view(-1, config.vocab_size), targets)
loss.backward() # 反向传播
# `clip_grad_norm` 有助于防止RNNs/LSTMs中的梯度爆炸问题。
torch.nn.utils.clip_grad_norm(model.parameters(), config.clip)
for p in model.parameters(): # 梯度更新
p.data.add_(-lr, p.grad.data)
total_loss += loss.data # loss累计
if ibatch % config.log_interval == 0 and ibatch > 0: # 每隔多少个批次输出一次状态
cur_loss = total_loss[0] / config.log_interval
elapsed = get_time_dif(start_time)
print("Epoch {:3d}, {:5d}/{:5d} batches, lr {:2.3f}, loss {:5.2f}, ppl {:8.2f}, time {}".format(
epoch, ibatch, train_len // seq_len, lr, cur_loss, math.exp(cur_loss), elapsed))
total_loss = 0.0
lr /= 4.0 # 在一轮迭代完成后,尝试缩小学习率
# 每隔多少轮次保存一次模型参数
if epoch % config.save_interval == 0:
torch.save(model.state_dict(), os.path.join(save_dir, model_name.format(epoch)))
print(''.join(generate(model, corpus.dictionary.idx2word)))
示例3: build_model
# 需要导入模块: import model [as 别名]
# 或者: from model import RNNModel [as 别名]
def build_model():
"""Build the model according to CLI arguments
Global Dependencies:
- corpus
- args
"""
# noise for soise sampling in NCE
noise = build_unigram_noise(
torch.FloatTensor(corpus.vocab.idx2count)
)
norm_term = 'auto' if args.norm_term == -1 else args.norm_term
# setting up NCELoss modules
if args.index_module == 'linear':
criterion = IndexLinear(
args.emsize,
ntoken,
noise=noise,
noise_ratio=args.noise_ratio,
norm_term=norm_term,
loss_type=args.loss,
reduction='none',
)
model = RNNModel(
ntoken, args.emsize, args.nhid, args.nlayers,
criterion=criterion, dropout=args.dropout,
)
elif args.index_module == 'gru':
if args.nlayers != 1:
logger.warning('Falling into one layer GRU due to Index_GRU supporting')
nce_criterion = IndexGRU(
ntoken, args.emsize, args.nhid,
args.dropout,
noise=noise,
noise_ratio=args.noise_ratio,
norm_term=norm_term,
)
model = GenModel(
criterion=nce_criterion,
)
else:
logger.error('The index module [%s] is not supported yet' % args.index_module)
raise(NotImplementedError('index module not supported'))
if args.cuda:
model.cuda()
logger.info('model definition:\n %s', model)
return model