Ejemplo n.º 1
0
	def env_fn():
		configuration = CTgraph_conf(env_config_path)
		env_conf = configuration.getParameters()
		env = gym.make('CTgraph-v0')
		imageDataset = CTgraph_images(env_conf)
		env.init(env_conf, imageDataset)
		return env, env_conf
Ejemplo n.º 2
0
 def __init__(self, name, seed = 0, log_dir = None,
              frame_skip = 4, history_length = 4, dataset = False, conf_data = {}):
     #attempt to load default dictionary if no config is given
     if not conf_data:
         configuration = CTgraph_conf("./DynamicMazeEnv/graph.json")
         conf_data = configuration.getParameters()
     BaseTask.__init__(self)
     env = gym.make(name)
     imageDataset = CTgraph_images(conf_data)
     observation, reward, done, info = env.init(conf_data, imageDataset)
     if dataset:
         env = DatasetEnv(env)
         self.dataset_env = env
     env = self.set_monitor(env, log_dir)
     #env = wrap_deepmind(env, history_length = history_length)
     self.env = env
     self.action_dim  = 3#self.env.action_space.n
     self.state_dim = 1#self.env.observation_space.shape
     self.name = name
     self.conf_data = conf_data
Ejemplo n.º 3
0
def make_env(env_conf):
    env = gym.make('CTgraph-v0')
    imageDataset = CTgraph_images(env_conf)
    env.init(env_conf, imageDataset)
    return env, env_conf
Ejemplo n.º 4
0
args = parser.parse_args()

# fetch the parameters from the json file
configuration = CTgraph_conf("graph.json")
conf_data = configuration.getParameters()
# print configration data
print(json.dumps(conf_data, indent=3))

# instantiate the maze
start = timeit.timeit()
env = gym.make('CTgraph-v0')
end = timeit.timeit()
#print(end - start)

imageDataset = CTgraph_images(conf_data)

# initialise and get initial observation and info
observation, reward, done, info = env.init(conf_data, imageDataset)

#plotting: uncomment the following line to plot the observations
#CTgraph_plot.plotImages(imageDataset, False)

# get a random path from the maze
high_reward_path = env.get_random_path()
# use this random path to set the path to the high reward. Note that the maze would have already a high_reward_path from the initialisation
env.set_high_reward_path(high_reward_path)

print("*--- Testing script ----*")

action = 0
Ejemplo n.º 5
0
def main():

    # Training settings
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch-size',
                        type=int,
                        default=200,
                        metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=1000,
                        metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--epochs',
                        type=int,
                        default=30,
                        metavar='N',
                        help='number of epochs to train')
    parser.add_argument('--lr',
                        type=float,
                        default=0.01,
                        metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.5,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=10,
        metavar='N',
        help='how many batches to wait before logging training status')
    parser.add_argument('--test',
                        type=int,
                        default=0,
                        metavar='N',
                        help='0 for training (default) 1 for testing')
    args = parser.parse_args()
    use_cuda = not args.no_cuda and torch.cuda.is_available()

    torch.manual_seed(args.seed)

    device = torch.device("cuda" if use_cuda else "cpu")
    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

    # fetch the parameters from the json file
    configuration = CTgraph_conf("graph.json")
    conf_data = configuration.getParameters()

    MimagesObj = CTgraph_images(conf_data)

    # when fetching the images twice, noise will be added differently, so effectively the train and test set will be different
    imgDataset = CustomDatasetFromImages(MimagesObj)
    testDataset = CustomDatasetFromImages(MimagesObj)

    #plotting
    CTgraph_plot.plotImages(MimagesObj, False)
    CTgraph_plot.plotImages(MimagesObj, False)
    CTgraph_plot.plotImages(MimagesObj, True)

    imgDataset_loader = torch.utils.data.DataLoader(imgDataset,
                                                    batch_size=args.batch_size,
                                                    shuffle=True,
                                                    **kwargs)

    imgDataset_loader = torch.utils.data.DataLoader(imgDataset,
                                                    batch_size=100,
                                                    shuffle=True,
                                                    **kwargs)

    testDataset_loader = torch.utils.data.DataLoader(
        testDataset, batch_size=args.batch_size, shuffle=True, **kwargs)

    import datetime
    stamp = '{:%Y-%m-%d-%H:%M:%S}'.format(datetime.datetime.now())
    logger = Logger('./logs/' + stamp, )
    model = ConvNet3l().to(device).double()
    #model = linearL().to(device).double()

    optimizer = optim.SGD(model.parameters(),
                          lr=args.lr,
                          momentum=args.momentum)

    if args.test == 0:
        for season in range(0, 1):
            for epoch in range(1, args.epochs + 1):
                train(args, model, device, imgDataset_loader, optimizer, epoch,
                      logger)
                # Save the model checkpoint
                torch.save(model.state_dict(), 'params.ckpt')
                test(args, model, device, testDataset_loader)
            for epoch in range(1, args.epochs + 1):
                train(args, model, device, imgDataset_loader2, optimizer,
                      epoch, logger)
                # Save the model checkpoint
                torch.save(model.state_dict(), 'params.ckpt')
                test(args, model, device, testDataset_loader2)

    else:
        # Save and load only the model parameters (recommended).
        model.load_state_dict(torch.load('params.ckpt'))
        test(args, model, device, testDataset_loader)