def da_rnn(train_data: TrainData, n_targs: int, encoder_hidden_size=64, decoder_hidden_size=64, T=10, learning_rate=0.01, batch_size=128): train_cfg = TrainConfig(T, int(train_data.feats.shape[0] * 0.7), batch_size, nn.MSELoss()) logger.info(f"Training size: {train_cfg.train_size:d}.") enc_kwargs = {"input_size": train_data.feats.shape[1], "hidden_size": encoder_hidden_size, "T": T} encoder = Encoder(**enc_kwargs).to(device) with open(os.path.join("data", "enc_kwargs.json"), "w") as fi: json.dump(enc_kwargs, fi, indent=4) dec_kwargs = {"encoder_hidden_size": encoder_hidden_size, "decoder_hidden_size": decoder_hidden_size, "T": T, "out_feats": n_targs} decoder = Decoder(**dec_kwargs).to(device) with open(os.path.join("data", "dec_kwargs.json"), "w") as fi: json.dump(dec_kwargs, fi, indent=4) encoder_optimizer = optim.Adam( params=[p for p in encoder.parameters() if p.requires_grad], lr=learning_rate) decoder_optimizer = optim.Adam( params=[p for p in decoder.parameters() if p.requires_grad], lr=learning_rate) da_rnn_net = DaRnnNet(encoder, decoder, encoder_optimizer, decoder_optimizer) return train_cfg, da_rnn_net
def __init__(self, obs, nums, glimpse_size=(20, 20), inpt_encoder_hidden=[256]*2, glimpse_encoder_hidden=[256]*2, glimpse_decoder_hidden=[252]*2, transform_estimator_hidden=[256]*2, steps_pred_hidden=[50]*1, baseline_hidden=[256, 128]*1, transform_var_bias=-2., step_bias=0., *args, **kwargs): self.baseline = BaselineMLP(baseline_hidden) def _make_transform_estimator(x): est = StochasticTransformParam(transform_estimator_hidden, x, scale_bias=transform_var_bias) return est super(AIRonMNIST, self).__init__( *args, obs=obs, nums=nums, glimpse_size=glimpse_size, n_appearance=50, transition=snt.LSTM(256), input_encoder=(lambda: Encoder(inpt_encoder_hidden)), glimpse_encoder=(lambda: Encoder(glimpse_encoder_hidden)), glimpse_decoder=(lambda x: Decoder(glimpse_decoder_hidden, x)), transform_estimator=_make_transform_estimator, steps_predictor=(lambda: StepsPredictor(steps_pred_hidden, step_bias)), output_std=.3, **kwargs )
def __init__(self, config ): super(DHMM, self).__init__() self.input_dim = config['input_dim'] self.z_dim = config['z_dim'] self.emission_dim = config['emission_dim'] self.trans_dim = config['trans_dim'] self.rnn_dim = config['rnn_dim'] self.clip_norm = config['clip_norm'] self.emitter = nn.Sequential( #Parameterizes the bernoulli observation likelihood `p(x_t|z_t)` nn.Linear(self.z_dim, self.emission_dim), nn.ReLU(), nn.Linear(self.emission_dim, self.emission_dim), nn.ReLU(), nn.Linear(self.emission_dim, self.input_dim), nn.Sigmoid() ) self.trans = GatedTransition(self.z_dim, self.trans_dim) self.postnet = PostNet(self.z_dim, self.rnn_dim) self.rnn = Encoder(None, self.input_dim, self.rnn_dim, False, 1) #nn.RNN(input_size=self.input_dim, hidden_size=self.rnn_dim, nonlinearity='relu', \ #batch_first=True, bidirectional=False, num_layers=1) # define a (trainable) parameters z_0 and z_q_0 that help define the probability distributions p(z_1) and q(z_1) # (since for t = 1 there are no previous latents to condition on) self.z_0 = nn.Parameter(torch.zeros(self.z_dim)) self.z_q_0 = nn.Parameter(torch.zeros(self.z_dim)) self.h_0 = nn.Parameter(torch.zeros(1, 1, self.rnn_dim)) self.optimizer = Adam(self.parameters(), lr=config['lr'], betas= (config['beta1'], config['beta2']))
def __init__(self, config, vocab_size): super(DeepAPI, self).__init__() self.vocab_size = vocab_size self.maxlen = config['maxlen'] self.clip = config['clip'] self.temp = config['temp'] self.desc_embedder = nn.Embedding(vocab_size, config['emb_size'], padding_idx=PAD_ID) self.api_embedder = nn.Embedding(vocab_size, config['emb_size'], padding_idx=PAD_ID) # utter encoder: encode response to vector self.encoder = Encoder(self.desc_embedder, config['emb_size'], config['n_hidden'], True, config['n_layers'], config['noise_radius']) self.decoder = Decoder(self.api_embedder, config['emb_size'], config['n_hidden'] * 2, vocab_size, config['use_attention'], 1, config['dropout']) # utter decoder: P(x|c,z) self.optimizer = optim.Adadelta(list(self.encoder.parameters()) + list(self.decoder.parameters()), lr=config['lr_ae'], rho=0.95) self.criterion_ce = nn.CrossEntropyLoss()
def __init__(self, opt): super(TextCNN, self).__init__() self.opt = opt self.embedder = Embedder(emb_method=self.opt.emb_method, glove_param=self.opt.glove_param, elmo_param=self.opt.elmo_param, use_gpu=self.opt.use_gpu) if self.opt.enc_method == 'cnn': self.encoder = Encoder(enc_method=self.opt.enc_method, filters_num=self.opt.filters_num, filters=self.opt.filters, f_dim=self.embedder.word_dim) feature_dim = len(self.opt.filters) * self.opt.filters_num else: self.encoder = Encoder(enc_method=self.opt.enc_method, input_size=self.embedder.word_dim, hidden_size=self.opt.hidden_size, bidirectional=self.opt.bidirectional) self.Q = nn.Parameter(torch.randn(self.opt.q_num, self.opt.q_dim)) self.attenter = Attenter(att_method=self.opt.att_method, f_dim=2 * self.opt.hidden_size, q_dim=self.opt.q_dim, q_num=self.opt.q_num) feature_dim = self.opt.q_num * self.opt.hidden_size * ( int(self.opt.bidirectional) + 1) self.linear = nn.Linear(feature_dim, self.opt.num_labels) self.dropout = nn.Dropout(self.opt.dropout)
class RNN(object): def __init__(self, input_size, output_size): super(RNN, self).__init__() self.encoder = Encoder(input_size) self.decoder = Decoder(output_size) self.loss = nn.CrossEntropyLoss() self.encoder_optimizer = optim.Adam(self.encoder.parameters()) self.decoder_optimizer = optim.Adam(self.decoder.parameters()) sos, eos = torch.LongTensor(1, 1).zero_(), torch.LongTensor(1, 1).zero_() sos[0, 0], eos[0, 0] = 0, 1 self.sos, self.eos = sos, eos def train(self, input, target): self.encoder_optimizer.zero_grad() self.decoder_optimizer.zero_grad() hidden_state = self.encoder.first_hidden() # Encoder for ivec in input: _, hidden_state = self.encoder.forward(Variable(ivec), hidden_state) # Decoder target.insert(0, self.sos) target.append(self.eos) total_loss = 0 for i in range(len(target) - 1): _, softmax, hidden_state = self.decoder.forward( target[i], hidden_state) total_loss += self.loss(softmax, Variable(target[i + 1][0])) total_loss.backward() self.decoder_optimizer.step() self.encoder_optimizer.step() return total_loss def eval(self, input): hidden_state = self.encoder.first_hidden() # Encoder for ivec in input: _, hidden_state = self.encoder.forward(ivec, hidden_state) outputs = [] output = self.sos # Decoder while output is not self.eos: output, _, hidden_state = self.decoder.forward( output, hidden_state) outputs += output return outputs
def da_rnn(train_data, n_targs: int, encoder_hidden_size=64, decoder_hidden_size=64, T=10, learning_rate=0.01, batch_size=128): train_cfg = TrainConfig(T, int(train_data.feats.shape[0] * 0.7), batch_size, nn.MSELoss()) logging.info(f"Training size: {train_cfg.train_size:d}.") enc_params = pd.DataFrame([{ 'input_size': train_data.feats.shape[1], 'hidden_size': encoder_hidden_size, 'T': T }]) enc_params.to_csv(os.path.join('results', save_name, 'enc_params.csv')) encoder = Encoder(input_size=enc_params['input_size'][0].item(), hidden_size=enc_params['hidden_size'][0].item(), T=enc_params['T'][0].item()).cuda() dec_params = pd.DataFrame([{ 'encoder_hidden_size': encoder_hidden_size, 'decoder_hidden_size': decoder_hidden_size, 'T': T, 'out_feats': n_targs }]) dec_params.to_csv(os.path.join('results', save_name, 'dec_params.csv')) decoder = Decoder( encoder_hidden_size=dec_params['encoder_hidden_size'][0].item(), decoder_hidden_size=dec_params['decoder_hidden_size'][0].item(), T=dec_params['T'][0].item(), out_feats=dec_params['out_feats'][0].item()).cuda() encoder_optimizer = optim.Adam( params=[p for p in encoder.parameters() if p.requires_grad], lr=learning_rate, weight_decay=args.wdecay) decoder_optimizer = optim.Adam( params=[p for p in decoder.parameters() if p.requires_grad], lr=learning_rate, weight_decay=args.wdecay) encoder_scheduler = optim.lr_scheduler.CosineAnnealingLR( encoder_optimizer, train_data.feats.shape[0], eta_min=args.min_lr) decoder_scheduler = optim.lr_scheduler.CosineAnnealingLR( decoder_optimizer, train_data.feats.shape[0], eta_min=args.min_lr) model = DaRnnNet(encoder, decoder, encoder_optimizer, decoder_optimizer, encoder_scheduler, decoder_scheduler) return train_cfg, model
def __init__(self, input_size, output_size, resume=False): super(RNN, self).__init__() self.encoder = Encoder(input_size) self.decoder = Decoder(output_size) self.loss = nn.CrossEntropyLoss() self.encoder_optimizer = optim.Adam(self.encoder.parameters()) self.decoder_optimizer = optim.Adam(self.decoder.parameters()) if resume: self.encoder.load_state_dict(torch.load("models/encoder.ckpt")) self.decoder.load_state_dict(torch.load("models/decoder.ckpt"))
def da_rnn(train_data, n_targs: int, encoder_hidden_size=64, decoder_hidden_size=64, T=10, learning_rate=0.01, batch_size=128): train_cfg = TrainConfig(T, int(train_data.feats.shape[0] * 0.7), batch_size, nn.MSELoss()) logging.info(f"Training size: {train_cfg.train_size:d}.") enc_kwargs = { "input_size": train_data.feats.shape[1], "hidden_size": encoder_hidden_size, "T": T } encoder = Encoder(**enc_kwargs).cuda() with open(os.path.join("data", "enc_kwargs.json"), "w") as fi: json.dump(enc_kwargs, fi, indent=4) dec_kwargs = { "encoder_hidden_size": encoder_hidden_size, "decoder_hidden_size": decoder_hidden_size, "T": T, "out_feats": n_targs } decoder = Decoder(**dec_kwargs).cuda() with open(os.path.join("data", "dec_kwargs.json"), "w") as fi: json.dump(dec_kwargs, fi, indent=4) encoder_optimizer = optim.Adam( params=[p for p in encoder.parameters() if p.requires_grad], lr=learning_rate, weight_decay=args.wdecay) decoder_optimizer = optim.Adam( params=[p for p in decoder.parameters() if p.requires_grad], lr=learning_rate, weight_decay=args.wdecay) encoder_scheduler = optim.lr_scheduler.CosineAnnealingLR( encoder_optimizer, args.epochs, eta_min=args.min_lr) decoder_scheduler = optim.lr_scheduler.CosineAnnealingLR( decoder_optimizer, args.epochs, eta_min=args.min_lr) da_rnn_net = DaRnnNet(encoder, decoder, encoder_optimizer, decoder_optimizer, encoder_scheduler, decoder_scheduler) return train_cfg, da_rnn_net
def __init__(self, X_dim, Y_dim, encoder_hidden_size=64, decoder_hidden_size=64, linear_dropout=0, T=10, learning_rate=1e-5, batch_size=128, decay_rate=0.95): self.T = T self.decay_rate = decay_rate self.batch_size = batch_size self.X_dim = X_dim self.Y_dim = Y_dim self.encoder = Encoder(X_dim, encoder_hidden_size, T, linear_dropout).to(device) self.decoder = Decoder(encoder_hidden_size, decoder_hidden_size, T, linear_dropout, Y_dim).to(device) self.encoder_optim = torch.optim.Adam(params=self.encoder.parameters(), lr=learning_rate) self.decoder_optim = torch.optim.Adam(params=self.decoder.parameters(), lr=learning_rate) self.loss_func = torch.nn.MSELoss()
def __init__(self, input_size, output_size): super(RNN, self).__init__() self.encoder = Encoder(input_size) self.decoder = Decoder(output_size) self.loss = nn.CrossEntropyLoss() self.encoder_optimizer = optim.Adam(self.encoder.parameters()) self.decoder_optimizer = optim.Adam(self.decoder.parameters()) sos, eos = torch.LongTensor(1, 1).zero_(), torch.LongTensor(1, 1).zero_() sos[0, 0], eos[0, 0] = 0, 1 self.sos, self.eos = sos, eos
def __init__(self, args): super(SSM, self).__init__() self.s_dim = s_dim = args.s_dim self.a_dim = a_dim = args.a_dim self.o_dim = o_dim = args.o_dim self.h_dim = h_dim = args.h_dim self.device = args.device self.args = args self.encoder = torch.nn.DataParallel( Encoder(o_dim, h_dim).to(self.device[0]), self.device) self.decoder = torch.nn.DataParallel( Decoder(s_dim, o_dim).to(self.device[0]), self.device) self.prior = torch.nn.DataParallel( Prior(s_dim, a_dim).to(self.device[0]), self.device) self.posterior = torch.nn.DataParallel( Posterior(self.prior, s_dim, a_dim, h_dim).to(self.device[0]), self.device) self.distributions = nn.ModuleList( [self.prior, self.posterior, self.encoder, self.decoder]) init_weights(self.distributions) # for s_aux_loss self.prior01 = Normal(torch.tensor(0.), scale=torch.tensor(1.)) self.g_optimizer = optim.Adam(self.distributions.parameters())
def __init__(self, config): super(Transformer_EncoderDecoder, self).__init__() c = copy.deepcopy self.attn = MultiHeadedAttention(config['head'], config['emb_dim']) self.ff = PositionwiseFeedForward(config['emb_dim'], config['d_ff'], config['drop_out']) self.position = PositionalEncoding(config['emb_dim'], config['drop_out']) self.encoder = Encoder( EncoderLayer(config['emb_dim'], c(self.attn), c(self.ff), config['drop_out']), config['N_layers']) self.decoder = Decoder( DecoderLayer(config['emb_dim'], c(self.attn), c(self.attn), c(self.ff), config['drop_out']), config['N_layers']) self.src_embed = nn.Sequential( Embeddings(config['emb_dim'], config['vocab_size']), c(self.position)) self.tgt_embed = nn.Sequential( Embeddings(config['emb_dim'], config['vocab_size']), c(self.position)) self.generator = Generator(config['emb_dim'], config['vocab_size']) self.fc_out = nn.Linear(config['emb_dim'], config['vocab_size']) self.model = EncoderDecoder(self.encoder, self.decoder, self.src_embed, self.tgt_embed, self.generator)
def darnn(train_data: TrainingData, n_targets: int, encoder_hidden_size: int, decoder_hidden_size: int, T: int, learning_rate=0.002, batch_size=32): train_cfg = TrainingConfig(T, int(train_data.features.shape[0] * 0.7), batch_size, nn.MSELoss()) print(f"Training size: {train_cfg.train_size:d}.") enc_kwargs = {"input_size": train_data.features.shape[1], "hidden_size": encoder_hidden_size, "T": T} encoder = Encoder(**enc_kwargs).to(device) dec_kwargs = {"encoder_hidden_size": encoder_hidden_size,"decoder_hidden_size": decoder_hidden_size, "T": T, "out_features": n_targets} decoder = Decoder(**dec_kwargs).to(device) encoder_optimizer = optim.Adam(params=[p for p in encoder.parameters() if p.requires_grad],lr=learning_rate) decoder_optimizer = optim.Adam(params=[p for p in decoder.parameters() if p.requires_grad],lr=learning_rate) da_rnn_net = Darnn_Net(encoder, decoder, encoder_optimizer, decoder_optimizer) return train_cfg, da_rnn_net
def __init__(self, vocab, feats_size, kernel_size, rec_field, attn_size, hidden_size, mid_layer, dropout, which): super(TextNormalizer, self).__init__() self.vocab = vocab self.encoder = Encoder(len(vocab), feats_size, kernel_size, rec_field, dropout, which) self.decoder = Decoder(len(vocab), feats_size, attn_size, hidden_size, mid_layer, dropout) self.init_hidden = InitialWeights(hidden_size, mid_layer, 4)
def __init__(self, config, api, pad_token=0): super(Seq2Seq, self).__init__() self.vocab = api.vocab self.vocab_size = len(self.vocab) self.rev_vocab = api.rev_vocab self.go_id = self.rev_vocab["<s>"] self.eos_id = self.rev_vocab["</s>"] self.maxlen = config.maxlen self.embedder = nn.Embedding(self.vocab_size, config.emb_size, padding_idx=pad_token) self.encoder = Encoder(self.embedder, config.emb_size, config.n_hidden, True, config.n_layers, config.noise_radius) self.decoder = AttnDecoder(config=config, embedder=self.embedder, vocab_size=self.vocab_size) self.criterion = nn.NLLLoss(reduction='none') self.optimizer = optim.Adam(list(self.encoder.parameters()) + list(self.decoder.parameters()), lr=config.lr_s2s) self.lr_scheduler_AE = optim.lr_scheduler.StepLR(self.optimizer, step_size=10, gamma=0.6)
def run_encoder(args: argparse.Namespace): env_map = { "clean_days": None, "copy_pattern": None, "copy_pattern_opt": None, "dry_run": None, "enable_cleaner": None, "hevc_pattern": None, "hevc_pattern_opt": None, "listen_address": "ENC_LISTEN_ADDRESS", "no_notifications": None, "out_path": "ENC_OUT", "print_every": None, "src_path": "ENC_SRC", "time_format": "TIME_FORMAT", "warn_at": None, "webhook_url": "WEBHOOK_URL", } kwargs = merge_env_args(env_map, args) inst = Encoder(LOOP, **kwargs) inst.run_app()
def TCHA(train_data: TrainData, n_targs: int, bidirec=False, num_layer=1, encoder_hidden_size=64, decoder_hidden_size=64, T=10, learning_rate=0.01, batch_size=128, interval=1, split=0.7, isMean=False): train_cfg = TrainConfig(T, int(train_data.feats.shape[0] * split), batch_size, nn.MSELoss(), interval, T, isMean) logger.info(f"Training size: {train_cfg.train_size:d}.") enc_args = {"input_size": train_data.feats.shape[1], "hidden_size": encoder_hidden_size, "T": T, "bidirec": bidirec, "num_layer": num_layer} encoder = Encoder(**enc_args).to(device) dec_args = {"encoder_hidden_size": encoder_hidden_size, "decoder_hidden_size": decoder_hidden_size, "T": T, "out_feats": n_targs, "bidirec": bidirec, "num_layer": num_layer} decoder = Decoder(**dec_args).to(device) encoder_optimizer = optim.Adam( params=[p for p in encoder.parameters() if p.requires_grad], lr=learning_rate) decoder_optimizer = optim.Adam( params=[p for p in decoder.parameters() if p.requires_grad], lr=learning_rate) tcha = TCHA_Net(encoder, decoder, encoder_optimizer, decoder_optimizer) return train_cfg, tcha
def __init__(self,config): super(BiGAN,self).__init__() self._work_type = config.work_type self._epochs = config.epochs self._batch_size = config.batch_size self._encoder_lr = config.encoder_lr self._generator_lr = config.generator_lr self._discriminator_lr = config.discriminator_lr self._latent_dim = config.latent_dim self._weight_decay = config.weight_decay self._img_shape = (config.input_size,config.input_size) self._img_save_path = config.image_save_path self._model_save_path = config.model_save_path self._device = config.device if self._work_type == 'train': # Loss function self._adversarial_criterion = torch.nn.MSELoss() # Initialize generator, encoder and discriminator self._G = Generator(self._latent_dim,self._img_shape).to(self._device) self._E = Encoder(self._latent_dim,self._img_shape).to(self._device) self._D = Discriminator(self._latent_dim,self._img_shape).to(self._device) self._G.apply(self.weights_init) self._E.apply(self.weights_init) self._D.apply(self.discriminator_weights_init) self._G_optimizer = torch.optim.Adam([{'params' : self._G.parameters()},{'params' : self._E.parameters()}], lr=self._generator_lr,betas=(0.5,0.999),weight_decay=self._weight_decay) self._D_optimizer = torch.optim.Adam(self._D.parameters(),lr=self._discriminator_lr,betas=(0.5,0.999)) self._G_scheduler = lr_scheduler.ExponentialLR(self._G_optimizer, gamma= 0.99) self._D_scheduler = lr_scheduler.ExponentialLR(self._D_optimizer, gamma= 0.99)
def __init__(self, text_length, emb_size, device=torch.device('cpu'), fb_dropout=0.5, predictor_dropout=0.3): # TODO docstring super(DFGN, self).__init__( ) # TODO pass the device to the Encoder and the Predictor as well? self.encoder = Encoder.Encoder(text_length=text_length) self.fusionblock = FusionBlock.FusionBlock( emb_size, device=device, dropout=fb_dropout) # TODO sort out init self.predictor = Predictor.Predictor( text_length, emb_size, dropout=predictor_dropout) # TODO sort out init
def build_models(self): self.E = Encoder(self.c64, self.rb6) self.T_Hair = Transformer(self.hair_dim, self.c256, self.rb6) self.T_Gender = Transformer(self.attr_dim, self.c256, self.rb6) self.T_Smailing = Transformer(self.attr_dim, self.c256, self.rb6) self.R = Reconstructor(self.c256) self.D_Hair = Discriminator(self.hair_dim, self.c64) self.D_Gender = Discriminator(self.attr_dim, self.c64) self.D_Smailing = Discriminator(self.attr_dim, self.c64) self.e_optim = torch.optim.Adam(self.E.parameters(), self.e_lr, [self.beta1, self.beta2]) self.th_optim = torch.optim.Adam(self.T_Hair.parameters(), self.t_lr, [self.beta1, self.beta2]) self.tg_optim = torch.optim.Adam(self.T_Gender.parameters(), self.t_lr, [self.beta1, self.beta2]) self.ts_optim = torch.optim.Adam(self.T_Smailing.parameters(), self.t_lr, [self.beta1, self.beta2]) self.r_optim = torch.optim.Adam(self.R.parameters(), self.r_lr, [self.beta1, self.beta2]) self.dh_optim = torch.optim.Adam(self.D_Hair.parameters(), self.d_lr, [self.beta1, self.beta2]) self.dg_optim = torch.optim.Adam(self.D_Gender.parameters(), self.d_lr, [self.beta1, self.beta2]) self.ds_optim = torch.optim.Adam(self.D_Smailing.parameters(), self.d_lr, [self.beta1, self.beta2]) self.print_network(self.E, 'Encoder') self.print_network(self.T_Hair, 'Transformer for Hair Color') self.print_network(self.T_Gender, 'Transformer for Gender') self.print_network(self.T_Smailing, 'Transformer for Smailing') self.print_network(self.R, 'Reconstructor') self.print_network(self.D_Hair, 'D for Hair Color') self.print_network(self.D_Gender, 'D for Gender') self.print_network(self.D_Smailing, 'D for Smailing') self.E.to(self.device) self.T_Hair.to(self.device) self.T_Gender.to(self.device) self.T_Smailing.to(self.device) self.R.to(self.device) self.D_Gender.to(self.device) self.D_Smailing.to(self.device) self.D_Hair.to(self.device)
def set_params(train_data, device, **da_rnn_kwargs): train_configs = TrainConfig(da_rnn_kwargs["time_step"], int(train_data.shape[0] * 0.95), da_rnn_kwargs["batch_size"], nn.MSELoss()) enc_kwargs = { "input_size": train_data.shape[1], "hidden_size": da_rnn_kwargs["en_hidden_size"], "time_step": int(da_rnn_kwargs["time_step"] / self.predict_size) } dec_kwargs = { "encoder_hidden_size": da_rnn_kwargs["en_hidden_size"], "decoder_hidden_size": da_rnn_kwargs["de_hidden_size"], "time_step": int(da_rnn_kwargs["time_step"] / self.predict_size), "out_feats": da_rnn_kwargs["target_cols"] } encoder = Encoder(**enc_kwargs).to(device) decoder = Decoder(**dec_kwargs).to(device) encoder_optimizer = optim.Adam( params=[p for p in encoder.parameters() if p.requires_grad], lr=da_rnn_kwargs["learning_rate"], betas=(0.9, 0.999), eps=1e-08) decoder_optimizer = optim.Adam( params=[p for p in decoder.parameters() if p.requires_grad], lr=da_rnn_kwargs["learning_rate"], betas=(0.9, 0.999), eps=1e-08) da_rnn_net = DaRnnNet(encoder, decoder, encoder_optimizer, decoder_optimizer) return train_configs, da_rnn_net
def __init__(self, temp, latent_num, latent_dim): super(Model, self).__init__() if type(temp) != torch.Tensor: temp = torch.tensor(temp) self.__temp = temp self.latent_num = latent_num self.latent_dim = latent_dim self.encoder = Encoder(latent_num=latent_num, latent_dim=latent_dim) self.decoder = Decoder(latent_num=latent_num, latent_dim=latent_dim) if 'ExpTDModel' in str(self.__class__): self.prior = ExpRelaxedCategorical(temp, probs=torch.ones(latent_dim).cuda()) else: self.prior = dist.RelaxedOneHotCategorical(temp, probs=torch.ones(latent_dim).cuda()) self.initialize() self.softmax = nn.Softmax(dim=-1)
def __init__(self, opt): super(BERT_CNN_CRF, self).__init__() self.opt = opt self.bertForToken = BertForTokenClassification.from_pretrained(self.opt.bert_model_dir, num_labels=self.opt.tag_nums) # tag分类 self.num_labels = self.opt.tag_nums self.crf = CRF(self.opt.tag_nums, batch_first=True) # 关系分类 self.type_emb = nn.Embedding(3, self.opt.bert_hidden_size) self.rel_cnns = Encoder(enc_method='cnn', filters_num=self.opt.filter_num, filters=self.opt.filters, f_dim=self.opt.bert_hidden_size) self.classifier_rels = nn.Linear(len(self.opt.filters)*self.opt.filter_num, self.opt.rel_nums) self.id2tag = json.loads(open(opt.id2tag_dir, 'r').readline()) self.type2types = json.loads(open(opt.type2types_dir, 'r').readline()) self.init_weights()
def __init__(self, word2idx, emb_size, hidden_sizes, dropout, rnn_type="LSTM", pretrained_embs=None, fixed_embs=False, tied=None): super(RNNLanguageModel, self).__init__() self.encoder = Encoder(word2idx, emb_size, pretrained_embs, fixed_embs) self.decoder = Decoder(len(word2idx), hidden_sizes[-1], tied, self.encoder) self.rnn = StackedRNN(rnn_type, emb_size, hidden_sizes, dropout) self.drop = nn.Dropout(dropout)
def __init__(self, hpm): self.hpm = hpm # parameters initializer objetcs self.rand_unif_init = tf.random_uniform_initializer( -self.hpm['rand_unif_init_mag'], self.hpm['rand_unif_init_mag'], seed=123) self.rand_norm_init = tf.truncated_normal_initializer( stddev=self.hpm['trunc_norm_init_std']) # encoder and attentional decoder objects self.encoder = Encoder(self.hpm, self.rand_unif_init, self.rand_norm_init) self.decoder = Attention_decoder(self.hpm, self.rand_unif_init, self.rand_norm_init) # a global step counter for the training self.step = tf.train.get_or_create_global_step()
def __init__(self, input_dim_encoder: int, hidden_dim_encoder: int, output_dim_encoder: int, dropout_p_encoder: float, output_dim_h_decoder: int, nb_classes: int, dropout_p_decoder: float, max_out_t_steps: int) \ -> None: """Baseline method for audio captioning with Clotho dataset. :param input_dim_encoder: Input dimensionality of the encoder. :type input_dim_encoder: int :param hidden_dim_encoder: Hidden dimensionality of the encoder. :type hidden_dim_encoder: int :param output_dim_encoder: Output dimensionality of the encoder. :type output_dim_encoder: int :param dropout_p_encoder: Encoder RNN dropout. :type dropout_p_encoder: float :param output_dim_h_decoder: Hidden output dimensionality of the decoder. :type output_dim_h_decoder: int :param nb_classes: Amount of output classes. :type nb_classes: int :param dropout_p_decoder: Decoder RNN dropout. :type dropout_p_decoder: float :param max_out_t_steps: Maximum output time-steps of the decoder. :type max_out_t_steps: int """ super().__init__() self.max_out_t_steps: int = max_out_t_steps self.encoder: Module = Encoder(input_dim=input_dim_encoder, hidden_dim=hidden_dim_encoder, output_dim=output_dim_encoder, dropout_p=dropout_p_encoder) self.decoder: Module = Decoder(input_dim=output_dim_encoder * 2, output_dim=output_dim_h_decoder, nb_classes=nb_classes, dropout_p=dropout_p_decoder)
def __init__(self, args): super(S3RecModel, self).__init__() self.item_embeddings = nn.Embedding(args.item_size, args.hidden_size, padding_idx=0) self.attribute_embeddings = nn.Embedding(args.attribute_size, args.hidden_size, padding_idx=0) self.position_embeddings = nn.Embedding(args.max_seq_length, args.hidden_size) self.item_encoder = Encoder(args) self.LayerNorm = LayerNorm(args.hidden_size, eps=1e-12) self.dropout = nn.Dropout(args.hidden_dropout_prob) self.args = args # add unique dense layer for 4 losses respectively self.aap_norm = nn.Linear(args.hidden_size, args.hidden_size) self.mip_norm = nn.Linear(args.hidden_size, args.hidden_size) self.map_norm = nn.Linear(args.hidden_size, args.hidden_size) self.sp_norm = nn.Linear(args.hidden_size, args.hidden_size) self.criterion = nn.BCELoss(reduction='none') self.apply(self.init_weights)
def __init__(self, word2idx, emb_size, hidden_sizes, dropout, rnn_type="LSTM", pretrained_embs=None, fixed_embs=False, tied=None): super(BidirectionalLanguageModel, self).__init__() self.drop = nn.Dropout(dropout) self.encoder = Encoder(word2idx, emb_size, pretrained_embs, fixed_embs) self.decoder = Decoder(len(word2idx), hidden_sizes[-1], tied, self.encoder) self.forward_lstm = StackedRNN(rnn_type, emb_size, hidden_sizes, dropout) self.backward_lstm = StackedRNN(rnn_type, emb_size, hidden_sizes, dropout) self.rnn_type = rnn_type self.hidden_sizes = hidden_sizes self.nlayers = len(hidden_sizes)
def __init__(self, embed_dim=300, hidden_dim=256, inner_dim=2048, n_head=2, N_en=6, N_de=6, dropout=0.1, vocab_size=5000, sos_idx=2, eos_idx=3, pad_idx=0, unk_idx=1, max_src_len=100, max_tgt_len=20, args=False): super(Transformer, self).__init__() #===Test the GPU availability self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") #--Token indexes & Properties self.sos, self.eos, self.pad, self.unk = sos_idx, eos_idx, pad_idx, unk_idx self.max_src_len = max_src_len self.max_tgt_len = max_tgt_len self.scale = embed_dim ** 0.5 #===Base model(attn, enc, dec, ff) max_len = max(max_src_len, max_tgt_len) attn_enc_layer = ATTNLayer( embed_dim, n_head, hidden_dim, inner_dim, dropout, max_len, False) attn_dec_layer = ATTNLayer( embed_dim, n_head, hidden_dim, inner_dim, dropout, max_len, True) #===Main Archetecture(enc, dec) self.encoder = Encoder(attn_enc_layer, N_en, True) self.decoder = Decoder(attn_dec_layer, N_de, True) #===Embedding setting(src, tgt) self.embed = nn.Embedding(vocab_size, embed_dim) #===Fianl FC(logit2vocab) self.final = nn.Linear(embed_dim, vocab_size) #===Loss self.NLL = nn.NLLLoss(reduction='sum')