def construct_forward_model(obs_dim=11, act_dim=3, rew_dim=1, hidden_dim=200, num_networks=7, num_elites=5, session=None): print( '[ BNN ] Observation dim {} | Action dim: {} | Hidden dim: {}'.format( obs_dim, act_dim, hidden_dim)) params = { 'name': 'BNN', 'num_networks': num_networks, 'num_elites': num_elites, 'sess': session } model = BNN(params) model.add( FC(hidden_dim, input_dim=obs_dim + act_dim, activation="swish", weight_decay=0.000025)) model.add(FC(hidden_dim, activation="swish", weight_decay=0.00005)) model.add(FC(hidden_dim, activation="swish", weight_decay=0.000075)) model.add(FC(hidden_dim, activation="swish", weight_decay=0.000075)) model.add(FC(obs_dim + rew_dim, weight_decay=0.0001)) model.finalize(tf.train.AdamOptimizer, {"learning_rate": 0.001}) return model
def get_model(self, run_matchdg_erm=0): if self.args.model_name == 'lenet': from models.lenet import LeNet5 phi = LeNet5() if self.args.model_name == 'fc': from models.fc import FC if self.args.method_name in ['csd', 'matchdg_ctr']: fc_layer = 0 else: fc_layer = self.args.fc_layer phi = FC(self.args.out_classes, fc_layer) if self.args.model_name == 'domain_bed_mnist': from models.domain_bed_mnist import DomainBed phi = DomainBed(self.args.img_c) if self.args.model_name == 'alexnet': from models.alexnet import alexnet if self.args.method_name in ['csd', 'matchdg_ctr']: fc_layer = 0 else: fc_layer = self.args.fc_layer phi = alexnet(self.args.model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained, self.args.os_env) if 'resnet' in self.args.model_name: from models.resnet import get_resnet if self.args.method_name in ['csd', 'matchdg_ctr']: fc_layer = 0 else: fc_layer = self.args.fc_layer phi = get_resnet(self.args.model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained, self.args.os_env) if 'densenet' in self.args.model_name: from models.densenet import get_densenet if self.args.method_name in ['csd', 'matchdg_ctr']: fc_layer = 0 else: fc_layer = self.args.fc_layer phi = get_densenet(self.args.model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained, self.args.os_env) print('Model Architecture: ', self.args.model_name) self.phi = phi.to(self.cuda) self.load_model(run_matchdg_erm) return
def main(_run): args = Namespace(**_run.config) logger.info(args) training_data = DataLoader(EyeLoader(args.data_path, augment=True), shuffle=True, batch_size=1) model = FC() optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) for i in range(1, args.num_epochs + 1): loss = train(i, model, training_data, optimizer, args) print(loss)
def multi_get_attention(self, frames): frames = self._reshape_to_conv(frames) cnn = CNN() cnn_output = cnn.create_model(frames, cnn.conv_filters) cnn_output = self._reshape_to_rnn(cnn_output) rnn = RNN() rnn_output = rnn.create_model(cnn_output) if self.is_attention: attention = Attention(self.batch_size) attention_output = attention.attention_analysis(rnn_output) return attention_output else: rnn_output = rnn_output[:, -1, :] fc = FC(self.num_classes) outputs = fc.create_model(rnn_output) return outputs
def get_multi_predictions(self, frames): frames = self._reshape_to_conv(frames) cnn = CNN() if self.operation == 'training': cnn_output = cnn.create_model(frames, cnn.conv_filters, keep_prob=self.keep_prob) else: cnn_output = cnn.create_model(frames, cnn.conv_filters, keep_prob=1.0) cnn_output = self._reshape_to_rnn(cnn_output) rnn = RNN() arousal_rnn_output = rnn.create_model(cnn_output, 'arousal_rnn') valence_rnn_output = rnn.create_model(cnn_output, 'valence_rnn') dominance_rnn_output = rnn.create_model(cnn_output, 'dominance_rnn') if self.is_attention: attention = Attention(self.batch_size) arousal_attention_output = attention.create_model( arousal_rnn_output, 'arousal_attention') valence_attention_output = attention.create_model( valence_rnn_output, 'valence_attention') dominance_attention_output = attention.create_model( dominance_rnn_output, 'dominance_attention') fc = FC(self.num_classes) arousal_fc_outputs = fc.create_model(arousal_attention_output, 'arousal_fc') valence_fc_outputs = fc.create_model(valence_attention_output, 'valence_fc') dominance_fc_outputs = fc.create_model(dominance_attention_output, 'dominance_fc') else: arousal_rnn_output = arousal_rnn_output[:, -1, :] valence_rnn_output = valence_rnn_output[:, -1, :] dominance_rnn_output = dominance_rnn_output[:, -1, :] fc = FC(self.num_classes) arousal_fc_outputs = fc.create_model(arousal_rnn_output, 'arousal_fc') valence_fc_outputs = fc.create_model(valence_rnn_output, 'valence_fc') dominance_fc_outputs = fc.create_model(dominance_rnn_output, 'dominance_fc') return arousal_fc_outputs, valence_fc_outputs, dominance_fc_outputs
def get_predictions(self, frames, scope): frames = self._reshape_to_conv(frames) cnn = CNN() if self.operation == 'training': cnn_output = cnn.create_model(frames, cnn.conv_filters, keep_prob=self.keep_prob) else: cnn_output = cnn.create_model(frames, cnn.conv_filters, keep_prob=1.0) cnn_output = self._reshape_to_rnn(cnn_output) rnn = RNN() rnn_output = rnn.create_model(cnn_output, scope + '_rnn') if self.is_attention: attention = Attention(self.batch_size) attention_output = attention.create_model(rnn_output, scope + '_attention') fc = FC(self.num_classes) outputs = fc.create_model(attention_output, scope + '_fc') else: rnn_output = rnn_output[:, -1, :] fc = FC(self.num_classes) outputs = fc.create_model(rnn_output, scope + '_fc') return outputs
def construct_model(sess, obs_dim, act_dim, model_hyperparams): # context_dim = 5 , rew_dim=1, hidden_dim=200, # ada_state_dynamics_pred = True, ada_rew_pred = True, # fast_adapt_steps = 2 , fast_adapt_lr = 0.01, # reg_weight = 1, pred_dynamics = True, fixed_preupdate_context = True, num_networks=1, num_elites=1): # output_dim = rew_dim + obs_dim model = BNN(sess, obs_dim, act_dim, model_hyperparams) # ada_state_dynamics_pred, ada_rew_pred, # fast_adapt_steps , fast_adapt_lr, reg_weight , fixed_preupdate_context ) model.add( FC(model.hidden_dim, input_dim=obs_dim + act_dim + model.context_dim, activation="swish", weight_decay=0.000025)) model.add(FC(model.hidden_dim, activation="swish", weight_decay=0.00005)) model.add(FC(model.hidden_dim, activation="swish", weight_decay=0.000075)) model.add(FC(model.hidden_dim, activation="swish", weight_decay=0.000075)) model.add(FC(model.output_dim, weight_decay=0.0001)) # model.finalize(tf.train.AdamOptimizer, {"learning_rate": 0.001}) model.build_graph() return model
def init_erm_phase(self): if self.args.ctr_model_name == 'lenet': from models.lenet import LeNet5 ctr_phi = LeNet5().to(self.cuda) if self.args.ctr_model_name == 'alexnet': from models.alexnet import alexnet ctr_phi = alexnet(self.args.out_classes, self.args.pre_trained, 'matchdg_ctr').to(self.cuda) if self.args.ctr_model_name == 'fc': from models.fc import FC fc_layer = 0 ctr_phi = FC(self.args.out_classes, fc_layer).to(self.cuda) if 'resnet' in self.args.ctr_model_name: from models.resnet import get_resnet fc_layer = 0 ctr_phi = get_resnet(self.args.ctr_model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained, self.args.os_env).to(self.cuda) if 'densenet' in self.args.ctr_model_name: from models.densenet import get_densenet fc_layer = 0 ctr_phi = get_densenet(self.args.ctr_model_name, self.args.out_classes, fc_layer, self.args.img_c, self.args.pre_trained, self.args.os_env).to(self.cuda) # Load MatchDG CTR phase model from the saved weights if self.args.os_env: base_res_dir = os.getenv( 'PT_DATA_DIR' ) + '/' + self.args.dataset_name + '/' + 'matchdg_ctr' + '/' + self.args.ctr_match_layer + '/' + 'train_' + str( self.args.train_domains) else: base_res_dir = "results/" + self.args.dataset_name + '/' + 'matchdg_ctr' + '/' + self.args.ctr_match_layer + '/' + 'train_' + str( self.args.train_domains) save_path = base_res_dir + '/Model_' + self.ctr_load_post_string + '.pth' ctr_phi.load_state_dict(torch.load(save_path)) ctr_phi.eval() #Inferred Match Case if self.args.match_case == -1: inferred_match = 1 data_match_tensor, label_match_tensor, indices_matched, perfect_match_rank = get_matched_pairs( self.args, self.cuda, self.train_dataset, self.domain_size, self.total_domains, self.training_list_size, ctr_phi, self.args.match_case, self.args.perfect_match, inferred_match) # x% percentage match initial strategy else: inferred_match = 0 data_match_tensor, label_match_tensor, indices_matched, perfect_match_rank = get_matched_pairs( self.args, self.cuda, self.train_dataset, self.domain_size, self.total_domains, self.training_list_size, ctr_phi, self.args.match_case, self.args.perfect_match, inferred_match) return data_match_tensor, label_match_tensor
def __init__(self): super(ConjunctionClassifier, self).__init__() self.sent_embed = RobertaWrapper("../pretrained_models/roberta-base", fine_tune=False, output_dropout=0.0, token_mask_prob=0.0) self.dep_label_vocab = BasicVocab("../vocabs/dep_labels.txt") # Embeddings self.deprel_embed = nn.Embedding(len(self.dep_label_vocab), 50) self.in_out_embed = nn.Embedding(2, 50) self.lin_direction_embed = nn.Embedding(3, 50) self.already_has_dependent_embed = nn.Embedding(2, 50) self.fc = FC(3 * 768 + 50 + 50 + 50 + 50 + 2 * len(self.dep_label_vocab) + 1, [1500, 500], 2, nn.ReLU(), p_dropout=0.33)
def _load_structure(self): """Uses the saved structure in self.model_dir with the name of this network to initialize the structure of this network. """ structure = [] with open(os.path.join(self.model_dir, "%s.nns" % self.name), "r") as f: for line in f: kwargs = { key: val for (key, val) in [argval.split("=") for argval in line[3:-2].split(", ")] } kwargs["input_dim"] = int(kwargs["input_dim"]) kwargs["output_dim"] = int(kwargs["output_dim"]) kwargs["weight_decay"] = None if kwargs[ "weight_decay"] == "None" else float( kwargs["weight_decay"]) kwargs["activation"] = None if kwargs[ "activation"] == "None" else kwargs["activation"][1:-1] kwargs["ensemble_size"] = int(kwargs["ensemble_size"]) structure.append(FC(**kwargs)) self.layers = structure
def __init__(self, in_dim, ae_en_h_dims, ae_de_h_dims, conv_lstm_in_size, conv_lstm_in_dim, conv_lstm_h_dim, conv_lstm_kernel_sizes, conv_lstm_n_layers, fc_in_dim, fc_h_dims, fc_out_dim, **kwargs): super(DeepAP, self).__init__() self.device = kwargs.get('device', 'cpu') ################ # masked layer # ################ mask = [[i for i in range(in_dim)], [i for i in range(in_dim)]] self.mask_layer = MaskNet(in_dim, in_dim, mask, device=self.device) self.mask_thre = kwargs.get('mask_thre', 0.0001) ###################### # auto_encoder layer # ###################### self.ae = AutoEncoder(in_dim=in_dim, en_h_dims=ae_en_h_dims, de_h_dims=ae_de_h_dims) if kwargs.get('ae_pretrain_weight') is not None: self.ae.load_state_dict(kwargs['ae_pretrain_weight']) else: raise ValueError('AutoEncoder not pretrained.') if kwargs.get('if_trainable'): for p in self.ae.parameters(): p.requires_grad = kwargs['if_trainable'] else: self.ae.weight.requires_grad = False #################### # conv_lstm layers # #################### self.conv_lstm_list = nn.ModuleList() for i in conv_lstm_kernel_sizes: i_kernel_size = (i, i) conv_lstm = ConvLSTM( in_size=conv_lstm_in_size, in_dim=conv_lstm_in_dim, h_dim=conv_lstm_h_dim, kernel_size=i_kernel_size, num_layers=conv_lstm_n_layers, batch_first=kwargs.get('conv_lstm_batch_first', True), bias=kwargs.get('conv_lstm_bias', True), only_last_state=kwargs.get('only_last_state', True), device=self.device) self.conv_lstm_list.append(conv_lstm) ######################### # fully-connected layer # ######################### self.fc = FC( in_dim=fc_in_dim, # assert in_size == n_conv_lstm * conv_lstm_h_dim h_dims=fc_h_dims, out_dim=fc_out_dim, p_dropout=kwargs.get('fc_p_dropout', 0.1))
def choose_type(model_type): if model_type == Models_types.FC: return FC() if model_type == Models_types.UNET_V1: return Unet()