本文整理汇总了Python中model.Encoder方法的典型用法代码示例。如果您正苦于以下问题:Python model.Encoder方法的具体用法?Python model.Encoder怎么用?Python model.Encoder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类model
的用法示例。
在下文中一共展示了model.Encoder方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import model [as 别名]
# 或者: from model import Encoder [as 别名]
def __init__(self, config, args):
self.config = config
for k, v in args.__dict__.items():
setattr(self.config, k, v)
setattr(self.config, 'save_dir', '{}_log'.format(self.config.dataset))
disp_str = ''
for attr in sorted(dir(self.config), key=lambda x: len(x)):
if not attr.startswith('__'):
disp_str += '{} : {}\n'.format(attr, getattr(self.config, attr))
sys.stdout.write(disp_str)
sys.stdout.flush()
self.labeled_loader, self.unlabeled_loader, self.unlabeled_loader2, self.dev_loader, self.special_set = data.get_cifar_loaders(config)
self.dis = model.Discriminative(config).cuda()
self.gen = model.Generator(image_size=config.image_size, noise_size=config.noise_size).cuda()
self.enc = model.Encoder(config.image_size, noise_size=config.noise_size, output_params=True).cuda()
self.dis_optimizer = optim.Adam(self.dis.parameters(), lr=config.dis_lr, betas=(0.5, 0.999))
self.gen_optimizer = optim.Adam(self.gen.parameters(), lr=config.gen_lr, betas=(0.0, 0.999))
self.enc_optimizer = optim.Adam(self.enc.parameters(), lr=config.enc_lr, betas=(0.0, 0.999))
self.d_criterion = nn.CrossEntropyLoss()
if not os.path.exists(self.config.save_dir):
os.makedirs(self.config.save_dir)
log_path = os.path.join(self.config.save_dir, '{}.FM+VI.{}.txt'.format(self.config.dataset, self.config.suffix))
self.logger = open(log_path, 'wb')
self.logger.write(disp_str)
print self.dis
示例2: main
# 需要导入模块: import model [as 别名]
# 或者: from model import Encoder [as 别名]
def main():
args = parse_arguments()
hidden_size = 512
embed_size = 256
assert torch.cuda.is_available()
print("[!] preparing dataset...")
train_iter, val_iter, test_iter, DE, EN = load_dataset(args.batch_size)
de_size, en_size = len(DE.vocab), len(EN.vocab)
print("[TRAIN]:%d (dataset:%d)\t[TEST]:%d (dataset:%d)"
% (len(train_iter), len(train_iter.dataset),
len(test_iter), len(test_iter.dataset)))
print("[DE_vocab]:%d [en_vocab]:%d" % (de_size, en_size))
print("[!] Instantiating models...")
encoder = Encoder(de_size, embed_size, hidden_size,
n_layers=2, dropout=0.5)
decoder = Decoder(embed_size, hidden_size, en_size,
n_layers=1, dropout=0.5)
seq2seq = Seq2Seq(encoder, decoder).cuda()
optimizer = optim.Adam(seq2seq.parameters(), lr=args.lr)
print(seq2seq)
best_val_loss = None
for e in range(1, args.epochs+1):
train(e, seq2seq, optimizer, train_iter,
en_size, args.grad_clip, DE, EN)
val_loss = evaluate(seq2seq, val_iter, en_size, DE, EN)
print("[Epoch:%d] val_loss:%5.3f | val_pp:%5.2fS"
% (e, val_loss, math.exp(val_loss)))
# Save the model if the validation loss is the best we've seen so far.
if not best_val_loss or val_loss < best_val_loss:
print("[!] saving model...")
if not os.path.isdir(".save"):
os.makedirs(".save")
torch.save(seq2seq.state_dict(), './.save/seq2seq_%d.pt' % (e))
best_val_loss = val_loss
test_loss = evaluate(seq2seq, test_iter, en_size, DE, EN)
print("[TEST] loss:%5.2f" % test_loss)
示例3: build_model
# 需要导入模块: import model [as 别名]
# 或者: from model import Encoder [as 别名]
def build_model(vocab_size, load_checkpoint=False, checkpoint_epoch=-1, print_module=True):
hidden_size = config['MODEL']['HIDDEN_SIZE']
attn_method = config['MODEL']['ATTN_METHOD']
num_encoder_layers = config['MODEL']['N_ENCODER_LAYERS']
dropout = config['MODEL']['DROPOUT']
encoder = Encoder(vocab_size, hidden_size, num_encoder_layers, dropout=dropout)
decoder = Decoder(hidden_size, vocab_size, attn_method, num_encoder_layers, dropout=dropout)
model = Seq2Seq(
encoder=encoder,
decoder=decoder,
max_length=config['LOADER']['MAX_LENGTH'],
tie_weights=config['MODEL']['TIE_WEIGHTS']
)
if print_module:
print(model)
if load_checkpoint is True and os.path.exists(CHECKPOINT_PATH) is True:
# load checkpoint
prefix = config['TRAIN']['PREFIX']
model_path = None
if checkpoint_epoch >= 0:
model_path = '%s%s_%d' % (CHECKPOINT_PATH, prefix, checkpoint_epoch)
else:
# use last checkpoint
checkpoints = []
for root, dirs, files in os.walk(CHECKPOINT_PATH):
for f_name in files:
f_name_sp = f_name.split('_')
if len(f_name_sp) == 2:
checkpoints.append(int(f_name_sp[1]))
if len(checkpoints) > 0:
model_path = '%s%s_%d' % (CHECKPOINT_PATH, prefix, max(checkpoints))
if model_path is not None and os.path.exists(model_path):
if IMPORT_FROM_CUDA:
loaded = torch.load(model_path, map_location=lambda storage, loc: storage)
else:
loaded = torch.load(model_path)
model.load_state_dict(loaded)
print('Load %s' % model_path)
# print('Seq2Seq parameters:')
# for name, param in model.state_dict().items():
# print(name, param.size())
if USE_CUDA:
model = model.cuda()
return model
示例4: __init__
# 需要导入模块: import model [as 别名]
# 或者: from model import Encoder [as 别名]
def __init__(self, src_vocab_path, tgt_vocab_path, model_path, wlm_path,
max_len=1000, hidden_size=300, n_layers=2):
# load vocab
print("Loading vocab...")
with open(src_vocab_path, "rb") as f:
self.src_vocab = dill.load(f)
with open(tgt_vocab_path, "rb") as f:
self.tgt_vocab = dill.load(f)
print("Source vocab size:", len(self.src_vocab))
print("Target vocab size:", len(self.tgt_vocab))
# hyper-parameters
self.max_len = max_len
self.hidden_size = hidden_size
self.n_layers = n_layers
self.pad_idx = self.src_vocab.stoi[PAD]
# prepare model
self.encoder = Encoder(self.src_vocab, self.max_len, self.hidden_size, self.n_layers)
self.decoder = Decoder(self.tgt_vocab, self.max_len, self.hidden_size * 2, self.n_layers)
self.reverse_decoder = Decoder(self.tgt_vocab, self.max_len, self.hidden_size * 2, self.n_layers, reverse=True)
self.model = Seq2SeqConcat(self.encoder, self.decoder, self.reverse_decoder, self.pad_idx)
# load model
print("Loading model...")
if torch.cuda.is_available():
checkpoint = torch.load(model_path)
else:
checkpoint = torch.load(model_path, map_location="cpu")
self.model.load_state_dict(checkpoint["model_state_dict"])
self.model.to(device)
self.model.eval()
# initialize character and word language model
self.wlm = KenLM(wlm_path)
# prepare eligible tone map
self.tone_map = dict()
self.legal_vc = set()
self.tone_map['a'] = A_LIST
self.tone_map['e'] = E_LIST
self.tone_map['i'] = I_LIST
self.tone_map['o'] = O_LIST
self.tone_map['u'] = U_LIST
self.tone_map['y'] = Y_LIST
self.tone_map['d'] = D_LIST
with open("data/legal_vc.txt", 'r') as f:
for line in f:
vc = line.strip()
self.legal_vc.add(vc)
vc_no_tone = unidecode.unidecode(vc)
if vc_no_tone in self.tone_map:
self.tone_map[vc_no_tone].append(vc)
else:
self.tone_map[vc_no_tone] = [vc]
示例5: __init__
# 需要导入模块: import model [as 别名]
# 或者: from model import Encoder [as 别名]
def __init__(self, src_vocab, tgt_vocab,
max_len=300, hidden_size=300, n_layers=2, clip=5, n_epochs=30):
# hyper-parameters
self.max_len = max_len
self.hidden_size = hidden_size
self.n_layers = n_layers
self.clip = clip
self.n_epochs = n_epochs
# vocab
self.src_vocab = src_vocab
self.tgt_vocab = tgt_vocab
self.pad_idx = self.src_vocab.stoi[PAD]
# prepare model
self.encoder = Encoder(self.src_vocab, self.max_len, self.hidden_size, self.n_layers)
self.decoder = Decoder(self.tgt_vocab, self.max_len, self.hidden_size * 2, self.n_layers)
self.reverse_decoder = Decoder(self.tgt_vocab, self.max_len, self.hidden_size * 2, self.n_layers, reverse=True)
self.model = Seq2SeqConcat(self.encoder, self.decoder, self.reverse_decoder, self.pad_idx)
self.model.to(device)
print(self.model)
print("Total parameters:", sum([p.nelement() for p in self.model.parameters()]))
# initialize weights
for name, param in self.model.named_parameters():
if "lstm.bias" in name:
# set lstm forget gate to 1 (Jozefowicz et al., 2015)
n = param.size(0)
param.data[n//4:n//2].fill_(1.0)
elif "lstm.weight" in name:
nn.init.xavier_uniform_(param)
# prepare loss function; don't calculate loss on PAD tokens
self.criterion = nn.NLLLoss(ignore_index=self.pad_idx)
# prepare optimizer and scheduler
self.optimizer = Adam(self.model.parameters())
self.scheduler = CyclicLR(self.optimizer, base_lr=0.00001, max_lr=0.00005,
step_size_up=4000, step_size_down=4000,
mode="triangular", gamma=1.0, cycle_momentum=False)
# book keeping vars
self.global_iter = 0
self.global_numel = []
self.global_loss = []
self.global_acc = []
# visualization
self.vis_loss = Visualization(env_name="aivivn_tone", xlabel="step", ylabel="loss", title="loss (mean per 300 steps)")
self.vis_acc = Visualization(env_name="aivivn_tone", xlabel="step", ylabel="acc", title="training accuracy (mean per 300 steps)")