Example #1
0
def test_parameter_logging_freq(wandb_init_run):
    net = ConvNet()
    default_log_freq = 100
    wandb.hook_torch(net, log="parameters")
    for i in range(210):
        output = net(dummy_torch_tensor((64, 1, 28, 28)))
        grads = torch.ones(64, 10)
        output.backward(grads)
        if (i + 1) % default_log_freq == 0:
            assert (len(wandb_init_run.history.row) == 8)
            assert (
                wandb_init_run.history.row['parameters/fc2.bias'].histogram[0]
                > 0)
        else:
            assert (len(wandb_init_run.history.row) == 0)
        wandb.log({"a": 2})
    assert (len(wandb_init_run.history.rows) == 210)
Example #2
0
def test_parameter_logging_freq(wandb_init_run):
    net = ConvNet()
    log_freq = 20
    wandb.hook_torch(net, log="parameters", log_freq=log_freq)
    for i in range(50):
        #TO debug timeouts
        print("i: %i, time: %s" % (i, time.time()))
        output = net(dummy_torch_tensor((64, 1, 28, 28)))
        grads = torch.ones(64, 10)
        output.backward(grads)
        if (i + 1) % log_freq == 0:
            assert(len(wandb_init_run.history.row) == 8)
            assert(
                wandb_init_run.history.row['parameters/fc2.bias'].histogram[0] > 0)
        else:
            assert(len(wandb_init_run.history.row) == 0)
        wandb.log({"a": 2})
    assert(len(wandb_init_run.history.rows) == 50)
                    help='Learning Rate for optimizer')
parser.add_argument('--episodes',
                    type=int,
                    default=3000,
                    metavar='N',
                    help='Number of Episodes to roll out over')
args = parser.parse_args()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net = model.pg_model().to(device)
optimizer = torch.optim.Adam(net.parameters(), lr=args.lr)
NUM_EPISODES = args.episodes
SAVE_TIME = 100
env = gym.make('CartPole-v1')
wandb.init()
wandb.config.update(args)
wandb.hook_torch(net)
weight_updates = 0
for i in range(NUM_EPISODES):
    env.reset()
    done = False
    state_holder = []  # Holds all the states for a single iteration
    reward_holder = []  # Holds all the rewards for an episodes
    action_holder = []  # Hold all the action for an episode
    observation = torch.tensor([0, 0, 0, 0]).float()
    epsiode_length = 0
    while not done:
        # env.render(mode='rgb_array')
        # Run Observation through network, get probs
        action = net(observation.unsqueeze(0).to(device))
        # Sample probs to find what action to take
        state_holder.append(observation)
Example #4
0
def run(train_batch_size, val_batch_size, epochs, lr,log_interval, log_dir):
    train_loader, val_loader = get_loaders(train_batch_size, val_batch_size)
    model=HeartDisDet(hdn_sz=[50,100,10])
	wandb.hook_torch(model,log='all')
Example #5
0
wandb.init()
wandb.config.update(net_options)
wandb.config.update(data_options)

###############
torch.manual_seed(seed)
if use_cuda:
    os.environ['CUDA_VISIBLE_DEVICES'] = gpus
    torch.cuda.manual_seed(seed)

model = Darknet(cfgfile)
region_loss = model.loss

model.load_weights(weightfile)
model.print_network()
wandb.hook_torch(model)

region_loss.seen = model.seen
processed_batches = model.seen // batch_size

init_width = model.width
init_height = model.height
init_epoch = model.seen // nsamples

kwargs = {'num_workers': num_workers, 'pin_memory': True} if use_cuda else {}
test_loader = torch.utils.data.DataLoader(dataset.listDataset(
    testlist,
    shape=(init_width, init_height),
    shuffle=False,
    transform=transforms.Compose([
        transforms.ToTensor(),
Example #6
0
def main(args):
    if args.gpu:
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  # see issue #152
        os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id

    device = torch.device(
        "cuda" if torch.cuda.is_available() and args.gpu else "cpu")
    '''
    First let's prepare the models.
    This is the first step so that we can check parameters and do assertions
    '''

    # TODO : Add abilitiy for more than Adam optimizer

    wandb.init()

    # args.wandb = wandb
    model, model_params = select_model(args)
    model = model.to(device)

    directory = create_directory(args)
    joblib.dump(model_params, directory + "/args.dump")

    config = wandb.config
    wandb.hook_torch(model)
    config.model_name = model_params['model_name']
    config.quantization = args.quantization
    config.input_image = args.input_image_size
    config.kl = model_params['coeff_kl']
    config.mmd = model_params['coeff_mmd']
    config.update(args)
    if model_params['model_name'] == "PixelCNN" or model_params[
            'model_name'] == "PixelVAE":
        config.num_pixelcnn_layers = args.num_pixelcnn_layers
    if model_params['model_name'] == "PixelVAE" or model_params[
            'model_name'] == "VAE":
        config.decoder_output_channels = model.decoder_out_channels

    optimizer = optim.Adam(list(model.parameters()))
    '''
    Next let's prepare data loading
    '''
    # First get kmeans to quantise
    kmeans_dict = joblib.load(args.data_dir + "/kmeans_" + str(args.dataset) +
                              "_" + str(args.quantization) + ".model")
    kmeans = kmeans_dict['kmeans']
    data_mean = kmeans_dict['data_mean']
    data_std = kmeans_dict['data_std']
    if not args.weighted_entropy:
        data_ratio_of_labels = torch.FloatTensor(
            [1] * int(args.quantization)).to(device)
    else:
        data_ratio_of_labels = torch.FloatTensor(1 - kmeans_dict['ratios']).to(
            device)

    # Cheap hack to add data ratio of labels
    args.data_ratio_of_labels = data_ratio_of_labels

    # Select the transformer
    transform = choose_transformer(kmeans, args)

    # Create Data Loaders
    training_dataset, testing_dataset = get_dataset(args.dataset,
                                                    args.data_dir,
                                                    transform=transform,
                                                    target_transform=None)
    train_loader = torch.utils.data.DataLoader(
        training_dataset,
        batch_size=args.train_batch_size,
        shuffle=True,
        num_workers=args.num_workers,
        pin_memory=True)
    test_loader = torch.utils.data.DataLoader(testing_dataset,
                                              batch_size=args.train_batch_size,
                                              shuffle=True,
                                              num_workers=args.num_workers,
                                              pin_memory=True)
    '''
        Start Training
    '''
    plot_every = len(training_dataset.train_data) / (args.train_batch_size *
                                                     int(args.plot_interval))

    total_loss = []
    total_px_given_z = []
    total_kl = []
    total_mmd = []
    for epoch in range(args.epochs):
        joblib.dump(epoch, directory + "/latest_epoch.dump")
        current_loss, current_px_given_z, current_kl, current_mmd = train(
            model,
            train_loader,
            optimizer,
            device,
            args,
            epoch,
            data_mean,
            data_std,
            plot_every=plot_every,
            directory=directory)
        total_loss.extend(current_loss)
        total_px_given_z.extend(current_px_given_z)
        total_kl.extend(current_kl)
        total_mmd.extend(current_mmd)
        joblib.dump(
            np.asarray([total_loss, total_px_given_z, total_kl, total_mmd]),
            directory + "/loss_files.dump")
        torch.save(
            {
                'epoch': epoch + 1,
                'state_dict': model.state_dict(),
                'optimizer': optimizer.state_dict(),
            }, directory + "/latest-model.model")
Example #7
0
        loss = F.l1_loss(pred, r)

        test_loss += float(loss.mean().data.cpu())
    test_loss /= len(test_loader)
    with open(testlosspath, 'a') as f:
        f.write('{}\t{}\n'.format(epoch, test_loss))
    print('[{}] test Loss : {:.6f}'.format(sec2hms(time.time() - st),
                                           test_loss))


if __name__ == '__main__':
    model = Net()
    if args.cuda:
        model.cuda()

    wandb.hook_torch(model, log='all')
    model = nn.DataParallel(model)

    print('[{:5.3f}] Model Created'.format(time.time() - st))

    train_loader = torch.utils.data.DataLoader(SimDataset(which_set='train',
                                                          cache=args.cache,
                                                          norm=args.norm,
                                                          demix=args.demix),
                                               collate_fn=collate_fn,
                                               batch_size=args.batch_size,
                                               shuffle=args.shuffle,
                                               **loader_kwargs)
    #valid_loader = torch.utils.data.DataLoader(
    #    SimDataset(which_set='dev', subset=args.subset, cache=args.cache), collate_fn=collate_fn,
    #    batch_size=args.valid_batch_size, shuffle=args.shuffle, **loader_kwargs)