def main(config): train_dataset = datasets.MNIST( root="", train=True, download=True, transform=transforms.ToTensor(), ) test_dataset = datasets.MNIST( root="", train=False, download=True, transform=transforms.ToTensor() ) model = ConvNet() model = model.cuda() trainer = TripletTrainer(config) trainer.train(train_dataset, test_dataset, model)
def __init__(self, dataset_dir, net_specs_dict, model_hp_dict, num_joints, dataset, group, network_type, input_channels=None, fusion_level=None, fusion_type=None): self.convnet = ConvNet(net_specs_dict, model_hp_dict, num_joints) self._datasets_dir = dataset_dir if dataset not in [self.ICVL, self.MSRA, self.NYU]: raise ValueError("dataset can take one of the following values:" + " 'MSRA', 'NYU', 'ICVL'") self._network_type = network_type self._dataset = dataset self._group = group self._input_channels = input_channels self._fusion_level = fusion_level self._fusion_type = fusion_type
def get_network(model, channel, num_classes, im_size=(32, 32)): torch.random.manual_seed(int(time.time() * 1000) % 100000) net_width, net_depth, net_act, net_norm, net_pooling = get_default_convnet_setting( ) if model == 'MLP': net = MLP(channel=channel, num_classes=num_classes) elif model == 'ConvNet': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act=net_act, net_norm=net_norm, net_pooling=net_pooling, im_size=im_size) elif model == 'LeNet': net = LeNet(channel=channel, num_classes=num_classes) elif model == 'AlexNet': net = AlexNet(channel=channel, num_classes=num_classes) elif model == 'VGG11': net = VGG11(channel=channel, num_classes=num_classes) elif model == 'VGG11BN': net = VGG11BN(channel=channel, num_classes=num_classes) elif model == 'ResNet18': net = ResNet18(channel=channel, num_classes=num_classes) elif model == 'ResNet18BN_AP': net = ResNet18BN_AP(channel=channel, num_classes=num_classes) elif model == 'ConvNetD1': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=1, net_act=net_act, net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetD2': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=2, net_act=net_act, net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetD3': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=3, net_act=net_act, net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetD4': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=4, net_act=net_act, net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetW32': net = ConvNet(channel=channel, num_classes=num_classes, net_width=32, net_depth=net_depth, net_act=net_act, net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetW64': net = ConvNet(channel=channel, num_classes=num_classes, net_width=64, net_depth=net_depth, net_act=net_act, net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetW128': net = ConvNet(channel=channel, num_classes=num_classes, net_width=128, net_depth=net_depth, net_act=net_act, net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetW256': net = ConvNet(channel=channel, num_classes=num_classes, net_width=256, net_depth=net_depth, net_act=net_act, net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetAS': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act='sigmoid', net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetAR': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act='relu', net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetAL': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act='leakyrelu', net_norm=net_norm, net_pooling=net_pooling) elif model == 'ConvNetNN': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act=net_act, net_norm='none', net_pooling=net_pooling) elif model == 'ConvNetBN': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act=net_act, net_norm='batchnorm', net_pooling=net_pooling) elif model == 'ConvNetLN': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act=net_act, net_norm='layernorm', net_pooling=net_pooling) elif model == 'ConvNetIN': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act=net_act, net_norm='instancenorm', net_pooling=net_pooling) elif model == 'ConvNetGN': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act=net_act, net_norm='groupnorm', net_pooling=net_pooling) elif model == 'ConvNetNP': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act=net_act, net_norm=net_norm, net_pooling='none') elif model == 'ConvNetMP': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act=net_act, net_norm=net_norm, net_pooling='maxpooling') elif model == 'ConvNetAP': net = ConvNet(channel=channel, num_classes=num_classes, net_width=net_width, net_depth=net_depth, net_act=net_act, net_norm=net_norm, net_pooling='avgpooling') else: net = None exit('DC error: unknown model') gpu_num = torch.cuda.device_count() if gpu_num > 0: device = 'cuda' if gpu_num > 1: net = nn.DataParallel(net) else: device = 'cpu' net = net.to(device) return net
def train(env, config): """ Execute training of Soft Actor Critic """ timesteps_elapsed = 0 episodes_elapsed = 0 STATE_SIZE = env.observation_space.shape[0] ACTION_SIZE = env.action_space.n #policy_net = Net(sizes=(STATE_SIZE, *config["hidden_size"], ACTION_SIZE)).to(device=config["device"]) policy_net = ConvNet(nChannels=1, nOut=ACTION_SIZE).to(config["device"]) policy_net.apply(init_weights) Q_net1 = ConvNet(nChannels=1, nOut=ACTION_SIZE).to(config["device"]) Q_net1.apply(init_weights) Q_net2 = ConvNet(nChannels=1, nOut=ACTION_SIZE).to(config["device"]) Q_net2.apply(init_weights) Q_target_net1 = copy.deepcopy(Q_net1) Q_target_net2 = copy.deepcopy(Q_net2) Q_target_net1.freeze() Q_target_net2.freeze() log_alpha = nn.Parameter( torch.tensor([math.log(config["alpha"])], device=config["device"])) entropy_target = -math.log(1 / ACTION_SIZE) * config[ "target_entropy_ratio"] # that is, maximum entropy times a ratio optimizer_policy = torch.optim.Adam(policy_net.parameters(), lr=config["learning_rate_policy"]) optimizer_q = torch.optim.Adam(list(Q_net1.parameters()) + list(Q_net2.parameters()), lr=config["learning_rate_value"]) optimizer_alpha = torch.optim.Adam([log_alpha], lr=config["learning_rate_alpha"], eps=1e-4) replay_buffer = ReplayBuffer(config["buffer_capacity"]) n_step_buffer = NstepBuffer(config["n_steps"]) eval_returns_all = [] eval_times_all = [] train_policy = False start_time = time.time() with tqdm(total=config["max_timesteps"]) as pbar: while timesteps_elapsed < config["max_timesteps"]: elapsed_seconds = time.time() - start_time if elapsed_seconds > config["max_time"]: pbar.write("Training ended after {}s.".format(elapsed_seconds)) break episode_timesteps, _ = play_episode( env, policy_net=policy_net, Q_net1=Q_net1, Q_net2=Q_net2, Q_target_net1=Q_target_net1, Q_target_net2=Q_target_net2, optimizer_policy=optimizer_policy, optimizer_q=optimizer_q, optimizer_alpha=optimizer_alpha, log_alpha=log_alpha, entropy_target=entropy_target, gamma=config["gamma"], replay_buffer=replay_buffer, n_step_buffer=n_step_buffer, train=True, train_policy=train_policy, render=config["render"], max_steps=config["episode_length"], steps_init_training=config["steps_init_training"], steps_per_learning_update=config["steps_per_learning_update"], batch_size=config["batch_size"], device=config["device"]) timesteps_elapsed += episode_timesteps episodes_elapsed += 1 pbar.update(episode_timesteps) if timesteps_elapsed % config[ "train_policy_freq"] < episode_timesteps: train_policy = True else: train_policy = False if timesteps_elapsed > config["steps_init_training"]: if (timesteps_elapsed - config["steps_init_training"] ) % config["target_update_freq"] < episode_timesteps: Q_target_net1.soft_update(Q_net1, 0.8) Q_target_net2.soft_update(Q_net2, 0.8) if timesteps_elapsed % config["eval_freq"] < episode_timesteps: eval_returns = 0 for _ in range(config["eval_episodes"]): _, episode_return = play_episode( env, policy_net, Q_net1, Q_net2, Q_target_net1, Q_target_net2, optimizer_policy, optimizer_q, optimizer_alpha=optimizer_alpha, log_alpha=log_alpha, entropy_target=entropy_target, gamma=config["gamma"], replay_buffer=replay_buffer, n_step_buffer=n_step_buffer, train=False, train_policy=train_policy, render=config["render"], max_steps=config["episode_length"], batch_size=config["batch_size"], device=config["device"]) eval_returns += episode_return eval_returns = eval_returns / config["eval_episodes"] eval_returns_all.append(eval_returns) pbar.write( "Evaluation at timestep {} and episode {} returned a mean returns of {}" .format(timesteps_elapsed, episodes_elapsed, eval_returns)) if eval_returns >= config["target_return"]: pbar.write( "Reached return {} >= target return of {}".format( eval_returns, config["target_return"])) break print("Saving policy to {}".format(config["save_filename"])) torch.save(policy_net, config["save_filename"]) return np.array(eval_returns_all)
sampler=train_sampler, num_workers=0) validation_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=valid_sampler, num_workers=0) test_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=test_sampler, num_workers=0) classes = ('humpback', 'bowhead') # Instantiate the model # TODO: Don't hardcode input/output dimensions model = ConvNet() # model = SmallFC(18382, 2) # TODO: Verify weight tensor is doing what you want it to do (stop network from biasing towards most common class) criterion = nn.CrossEntropyLoss( weight=torch.tensor([1 / dataset.num_hb, 1 / dataset.num_bh])) optimizer = optim.Adam(model.parameters(), lr=0.001) NUM_EPOCHS = 3 for epoch in range(NUM_EPOCHS): # loop over the dataset multiple times running_loss = 0.0 for i, data in enumerate(train_loader, 0): # get the inputs; data is a list of [inputs, labels] inputs, labels = data['image'].float(), data['classification']