class FurnitureNASNetALarge350Finetunned(Module): def __init__(self, pretrained): super(FurnitureNASNetALarge350Finetunned, self).__init__() self.model = nasnetalarge(num_classes=1000, pretrained=pretrained) filters = self.model.penultimate_filters // 24 self.model.last_linear = Linear(24 * filters, 1024) self.final_classifier = Sequential(ReLU(inplace=True), Linear(1024, 512), ReLU(inplace=True), Linear(512, 128)) for m in self.model.last_linear.modules(): if isinstance(m, Linear): m.weight.data.normal_(0, 0.01) m.bias.data.zero_() for m in self.final_classifier.modules(): if isinstance(m, Linear): m.weight.data.normal_(0, 0.01) m.bias.data.zero_() # create aliases: self.stem = ModuleList([ self.model.conv0, self.model.cell_stem_0, self.model.cell_stem_1, ]) self.features = ModuleList([ self.model.cell_0, self.model.cell_1, self.model.cell_2, self.model.cell_3, self.model.cell_4, self.model.cell_5, self.model.reduction_cell_0, self.model.cell_6, self.model.cell_7, self.model.cell_8, self.model.cell_9, self.model.cell_10, self.model.cell_11, self.model.reduction_cell_1, self.model.cell_12, self.model.cell_13, self.model.cell_14, self.model.cell_15, self.model.cell_16, self.model.cell_17 ]) self.classifier = self.model.last_linear # Freeze stem and features for param in self.stem.parameters(): param.requires_grad = False for param in self.features.parameters(): param.requires_grad = False def train(self, mode=True): self.classifier.train(mode) self.final_classifier.train(mode) return self def forward(self, x): x = self.model.features(x) x = self.model.logits(x) x = self.final_classifier(x) return x
class SimpleDDPGAgent(Module): def __init__(self, **kwargs): super(SimpleDDPGAgent, self).__init__() hidden_size = kwargs['hidden_size'] # actor self.actor_linears = ModuleList( [Linear(kwargs['state_dim'], hidden_size[0])]) self.actor_linears.extend([ Linear(hidden_size[i - 1], hidden_size[i]) for i in range(1, len(hidden_size)) ]) self.action = Linear(hidden_size[-1], kwargs['action_dim']) # critic self.critic_linears = ModuleList([ Linear(kwargs['state_dim'] + kwargs['action_dim'], hidden_size[0]) ]) self.critic_linears.extend([ Linear(hidden_size[i - 1], hidden_size[i]) for i in range(1, len(hidden_size)) ]) self.q = Linear(hidden_size[-1], 1) self.relu = ReLU() self.sigmoid = Sigmoid() self.tanh = Tanh() self.apply(init_weights) # xavier uniform init def act(self, state): x = state for l in self.actor_linears: x = l(x) x = self.relu(x) action = self.tanh(self.action(x)) return action def Q(self, state, action): x = torch.cat([state, action], dim=1) for l in self.critic_linears: x = l(x) x = self.relu(x) q = self.q(x) return q def get_actor_parameters(self): return list(self.actor_linears.parameters()) + list( self.action.parameters()) def get_critic_parameters(self): return list(self.critic_linears.parameters()) + list( self.q.parameters())
class Net(Module): def __init__(self, input_size, hidden_sizes, output_size, lr): super(Net, self).__init__() max_potential = 3.0 self.layers = ModuleList() linear = Linear(input_size, hidden_sizes[0]) self.layers.append(linear) for i in range(len(hidden_sizes)): layer = Integrating(hidden_sizes[i], hidden_sizes[i], max_potential) self.layers.append(layer) # self.layers.append(Integrating(hidden_sizes[-1], output_size, max_potential, self.layers[-1])) self.layers.append(Linear(hidden_sizes[-1], output_size)) self.loss = CrossEntropyLoss() self.optimizer = torch.optim.Adam(self.layers.parameters(), lr=lr) def forward(self, x): for layer in self.layers: x = layer(x) return x def reset_potentials(self): for layer in self.layers: if type(layer).__name__ == "Integrating": layer.reset_potentials()
def __init__(self, num_classes: int, base_net: nn.ModuleList, source_layer_indexes: List[int], extras: nn.ModuleList, classification_headers: nn.ModuleList, regression_headers: nn.ModuleList, is_test=False, config=None, device=None): """Compose a SSD model using the given components. """ super(SSD, self).__init__() self.num_classes = num_classes self.base_net = base_net self.source_layer_indexes = source_layer_indexes self.extras = extras self.classification_headers = classification_headers self.regression_headers = regression_headers self.is_test = is_test self.config = config # register layers in source_layer_indexes by adding them to a module list self.source_layer_add_ons = nn.ModuleList([t[1] for t in source_layer_indexes if isinstance(t, tuple) and not isinstance(t, GraphPath)]) self.device = next(base_net.parameters()).device if device is not None: self.device = device self.base_net.to(device) if is_test: self.config = config self.priors = config.priors.to(self.device)
def initmodel(character_encoder, tag_encoder, embedded_dimension): """ :param character_encoder: :param tag_encoder: :param embedded_dimension: :return: """ character_encoder = copy(character_encoder) tag_encoder = copy(tag_encoder) tag_encoder[BD] = len(tag_encoder) character_encoder[BD] = len(character_encoder) character_embedding = Embedding(len(character_encoder), embedded_dimension) tag_embedding = Embedding(len(tag_encoder), embedded_dimension) encoder_part = LSTM(input_size=embedded_dimension, hidden_size=LSTMDIM, num_layers=1, bidirectional=1).type(DTYPE) ench0 = randn(2, 1, LSTMDIM).type(DTYPE) encc0 = randn(2, 1, LSTMDIM).type(DTYPE) decoder_part = LSTM(input_size=2 * LSTMDIM + embedded_dimension, hidden_size=LSTMDIM, num_layers=1).type(DTYPE) dech0 = randn(2, 1, 2 * LSTMDIM + embedded_dimension).type(DTYPE) decc0 = randn(2, 1, 2 * LSTMDIM + embedded_dimension).type(DTYPE) pred = Linear(LSTMDIM, len(character_encoder)).type(DTYPE) softmax = LogSoftmax().type(DTYPE) model = ModuleList([ character_embedding, tag_embedding, encoder_part, decoder_part, pred, softmax ]) optimizer = Adam(model.parameters(), lr=LEARNINGRATE, betas=BETAS) return { 'model': model, 'optimizer': optimizer, 'cencoder': character_encoder, 'tencoder': tag_encoder, 'cembedding': character_embedding, 'tembedding': tag_embedding, 'enc': encoder_part, 'ench0': ench0, 'encc0': encc0, 'dec': decoder_part, 'dech0': dech0, 'decc0': decc0, 'pred': pred, 'sm': softmax, 'embdim': embedded_dimension }
def initmodel(cencoder, tencoder, embdim): cencoder = copy(cencoder) tencoder = copy(tencoder) tencoder[BD] = len(tencoder) cencoder[BD] = len(cencoder) cembedding = Embedding(len(cencoder), embdim) tembedding = Embedding(len(tencoder), embdim) enc = LSTM(input_size=embdim, hidden_size=LSTMDIM, num_layers=1, bidirectional=1).type(DTYPE) ench0 = randn(2, 1, LSTMDIM).type(DTYPE) encc0 = randn(2, 1, LSTMDIM).type(DTYPE) dec = LSTM(input_size=2 * LSTMDIM + embdim, hidden_size=LSTMDIM, num_layers=1).type(DTYPE) dech0 = randn(2, 1, 2 * LSTMDIM + embdim).type(DTYPE) decc0 = randn(2, 1, 2 * LSTMDIM + embdim).type(DTYPE) pred = Linear(LSTMDIM, len(cencoder)).type(DTYPE) sm = LogSoftmax().type(DTYPE) model = ModuleList([cembedding, tembedding, enc, dec, pred, sm]) optimizer = Adam(model.parameters(), lr=LEARNINGRATE, betas=BETAS) return { 'model': model, 'optimizer': optimizer, 'cencoder': cencoder, 'tencoder': tencoder, 'cembedding': cembedding, 'tembedding': tembedding, 'enc': enc, 'ench0': ench0, 'encc0': encc0, 'dec': dec, 'dech0': dech0, 'decc0': decc0, 'pred': pred, 'sm': sm, 'embdim': embdim }
class FurnitureResNet101_350_finetune(Module): def __init__(self, pretrained=True): super(FurnitureResNet101_350_finetune, self).__init__() self.model = resnet101(num_classes=1000, pretrained=pretrained) self.model.avgpool = AdaptiveAvgPool2d(1) # create aliases: self.stem = ModuleList([ self.model.conv1, self.model.bn1, self.model.layer1, self.model.layer2, ]) self.features = ModuleList([ self.model.layer3, self.model.layer4, ]) self.classifier = self.model.fc self.final_classifiers = Sequential( Linear(1000, 1000), Linear(1000, 128), ) for m in self.final_classifiers.modules(): if isinstance(m, Linear): normal_(m.weight, 0, 0.01) constant_(m.bias, 0) # freeze internal layers: for param in self.stem.parameters(): param.requires_grad = False def forward(self, x): x = self.model(x) return self.final_classifiers(x)
class SimplePPOAgent(Module): def __init__(self, **kwargs): super(SimplePPOAgent, self).__init__() hidden_size = kwargs['hidden_size'] # actor self.actor_bn = BatchNorm1d(kwargs['state_dim']) self.actor_linears = ModuleList( [Linear(kwargs['state_dim'], hidden_size[0])]) self.actor_linears.extend([ Linear(hidden_size[i - 1], hidden_size[i]) for i in range(1, len(hidden_size)) ]) self.mu = Linear(hidden_size[-1], kwargs['action_dim']) self.log_var = Linear(hidden_size[-1], kwargs['action_dim']) # self.log_var = torch.nn.Parameter(torch.zeros(kwargs['action_dim'])) # critic self.critic_bn = BatchNorm1d(kwargs['state_dim']) self.critic_linears = ModuleList( [Linear(kwargs['state_dim'], hidden_size[0])]) self.critic_linears.extend([ Linear(hidden_size[i - 1], hidden_size[i]) for i in range(1, len(hidden_size)) ]) self.v = Linear(hidden_size[-1], 1) self.relu = ReLU() self.tanh = Tanh() self.apply(init_weights) # xavier uniform init def forward(self, state, action=None): x = state x = self.actor_bn(x) for l in self.actor_linears: x = l(x) x = self.relu(x) mu = self.tanh(self.mu(x)) log_var = -self.relu(self.log_var(x)) sigmas = log_var.exp().sqrt() dists = Normal(mu, sigmas) if action is None: action = dists.sample() log_prob = dists.log_prob(action).sum(dim=-1, keepdim=True) x = state x = self.critic_bn(x) for l in self.critic_linears: x = l(x) x = self.relu(x) v = self.v(x) return action, log_prob, dists.entropy(), v def get_actor_parameters(self): return [ *self.actor_bn.parameters(), *self.actor_linears.parameters(), *self.mu.parameters(), *self.log_var.parameters() ] def get_critic_parameters(self): return [ *self.critic_bn.parameters(), *self.critic_linears.parameters(), *self.v.parameters() ]
def main(mode, dataset_name, expid, epochs, lr, batch_size, ntrials, struc_learn_method): network_dict = {} result_dict = {} losses_dict = {} bayesmodel_dict = {} # Get network network = get_network(dataset_name) length = len(network) bayesmodel, layout, encoders, nfeatures, nclass = get_bayesnet_model( dataset_name) # Obtain models generator = Generator(nfeatures=nfeatures, ndomain=ndomain) discriminator = ModuleList([ Discriminator(local_nfeatures=nfeatures[local].sum(), ndomain=ndomain) for local in network ]) classifier = Classifier(nfeatures=nfeatures, nclass=nclass) # Obtain dataloader data_loader, test_data_loader = get_bayesnet_dataloaders( dataset_name=dataset_name, length=length, batch_size=batch_size) # Initialize optimizers G_optimizer = torch.optim.Adam(generator.parameters(), lr=lr, betas=(.9, .99)) D_optimizer = torch.optim.Adam(discriminator.parameters(), lr=lr, betas=(.9, .99)) G_scheduler = torch.optim.lr_scheduler.StepLR(G_optimizer, step_size=1, gamma=1) # Set up trainer expname = '{}'.format(dataset_name.upper()) trainer = Trainer(mode, network, bayesmodel, layout, encoder, nfeatures, expname, generator, discriminator, G_optimizer, D_optimizer, G_scheduler) # Train model expdesc = "{}-e{}lr{}bs{}pe{}nt{}-{}".format(mode, epochs, int(10000 * lr), batch_size, pretrain_epochs, ntrials, expid) generator_state_dict = trainer.train(data_loader, test_data_loader, expdesc, epochs=epochs) # Get result display.clear_output(wait=True) #generator.load_state_dict(generator_state_dict) generator.eval() result_dict = {} ess, dss, rsss, bmps, spas = [], [], [], [], [] for trial in range(ntrials): iter_test_data = iter(test_data_loader) sampled_data = [next(iter_test_data).data.numpy() for i in range(16)] sampled_data = np.concatenate(sampled_data, axis=0) fixed_latents = Variable(generator.sample_latent( sampled_data.shape[0])) generated = generator(fixed_latents.cuda(), 0.1).detach().cpu().data.numpy() ess.append(energy_statistics(sampled_data, generated)) dss.append( discriminative_score(sampled_data, generated, diagnose=False)) rsss.append( relative_structure_score(sampled_data, generated, encoder, bayesmodel)) bmps.append( structure_prediction(generated, encoder, bayesmodel, method=struc_learn_method, score_type='bic')) spas.append(structure_prediction_accuracy(bayesmodel, bmps[-1])) print("Evaluating... Progress {:.2f}%".format( (trial + 1) / ntrials * 100), end='\r') ess, dss, rsss, spas = np.array(ess), np.array(dss), np.array( rsss), np.array(spas) result_dict['energy_statistics'] = (ess.mean(), ess.std() / np.sqrt(ntrials)) result_dict['discriminative_score'] = (dss.mean(), dss.std() / np.sqrt(ntrials)) result_dict['relative_structure_score'] = (rsss.mean(), rsss.std() / np.sqrt(ntrials)) bayesmodel_dict = bmps[0] result_dict['structure_prediction_accuracy'] = (spas.mean(), spas.std() / np.sqrt(ntrials)) losses_dict = {} losses_dict['wasserstein_loss'] = trainer.losses['D'] losses_dict['energy_statistics'] = trainer.losses['energy_statistics'] losses_dict['relative_structure_score'] = trainer.losses[ 'relative_structure_score'] losses_dict['structure_prediction_iteration'] = trainer.losses[ 'structure_prediction_iteration'] losses_dict['structure_prediction_accuracy'] = trainer.losses[ 'structure_prediction_accuracy'] losses_dict['bayesmodel_predicted'] = trainer.losses[ 'bayesmodel_predicted'] network_dict = network