Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
download_location="/tmp/mnist"):
transformations = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)),
lambda x: x.view(1, 1, 28, 28),
])
mnist_train = l2l.data.MetaDataset(MNIST(download_location, train=True, download=True, transform=transformations))
# mnist_test = MNIST(file_location, train=False, download=True, transform=transformations)
train_gen = l2l.data.TaskGenerator(mnist_train, ways=ways, tasks=10000)
# test_gen = l2l.data.TaskGenerator(mnist_test, ways=ways)
model = Net(ways)
model.to(device)
meta_model = l2l.algorithms.MAML(model, lr=maml_lr)
opt = optim.Adam(meta_model.parameters(), lr=lr)
loss_func = nn.NLLLoss(reduction="sum")
for iteration in range(iterations):
iteration_error = 0.0
iteration_acc = 0.0
for _ in range(tps):
learner = meta_model.clone()
train_task = train_gen.sample()
valid_task = train_gen.sample(task=train_task.sampled_task)
# Fast Adaptation
for step in range(fas):
train_error, _ = compute_loss(train_task, device, learner, loss_func, batch=shots * ways)
learner.adapt(train_error)
def main():
task_dist = dist.Normal(th.zeros(2 * DIM), th.ones(2 * DIM))
model = Model()
maml = l2l.algorithms.MAML(model, lr=1e-2)
opt = optim.Adam(maml.parameters())
for i in range(TIMESTEPS):
step_loss = 0.0
for t in range(TASKS_PER_STEP):
# Sample a task
task_params = task_dist.sample()
mu_i, sigma_i = task_params[:DIM], task_params[DIM:]
# Adaptation: Instanciate a copy of model
learner = maml.new()
proposal = learner()
# Adaptation: Compute and adapt to task loss
loss = (mu_i - proposal.mean).pow(2).sum() + (sigma_i - proposal.variance).pow(2).sum()
learner.adapt(loss)
th.manual_seed(seed)
def make_env():
env = gym.make(env_name)
env = ch.envs.ActionSpaceScaler(env)
return env
env = l2l.gym.AsyncVectorEnv([make_env for _ in range(num_workers)])
env.seed(seed)
env = ch.envs.ActionSpaceScaler(env)
env = ch.envs.Torch(env)
policy = DiagNormalPolicy(input_size=env.state_size,
output_size=env.action_size,
hiddens=[64, 64],
activation='tanh')
meta_learner = l2l.algorithms.MAML(policy, lr=meta_lr)
baseline = LinearValue(env.state_size, env.action_size)
opt = optim.Adam(meta_learner.parameters(), lr=meta_lr)
for iteration in range(num_iterations):
iteration_reward = 0.0
iteration_replays = []
iteration_policies = []
# Sample Trajectories
for task_config in tqdm(env.sample_tasks(meta_bsz), leave=False, desc='Data'):
clone = deepcopy(meta_learner)
env.set_task(task_config)
env.reset()
task = ch.envs.Runner(env)
task_replay = []
task_policies = []
download_location="/tmp/mnist", test=False):
transformations = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)),
lambda x: x.view(1, 1, 28, 28),
])
mnist_train = l2l.data.MetaDataset(MNIST(download_location, train=True, download=True, transform=transformations))
# mnist_test = MNIST(file_location, train=False, download=True, transform=transformations)
train_gen = l2l.data.TaskGenerator(mnist_train, ways=ways, tasks=10000)
# test_gen = l2l.data.TaskGenerator(mnist_test, ways=ways)
model = Net(ways)
model.to(device)
meta_model = l2l.algorithms.MAML(model, lr=maml_lr)
opt = optim.Adam(meta_model.parameters(), lr=lr)
loss_func = nn.NLLLoss(reduction="sum")
tqdm_bar = tqdm(range(iterations))
for iteration in tqdm_bar:
iteration_error = 0.0
iteration_acc = 0.0
for _ in range(tps):
learner = meta_model.clone()
train_task = train_gen.sample()
valid_task = train_gen.sample(task=train_task.sampled_task)
# Fast Adaptation
for step in range(fas):
train_error, _ = compute_loss(train_task, device, learner, loss_func, batch=shots * ways)
learner.adapt(train_error)
download=True,
transform=transformations))
train_tasks = l2l.data.TaskDataset(mnist_train,
task_transforms=[
l2l.data.transforms.NWays(mnist_train, ways),
l2l.data.transforms.KShots(mnist_train, 2*shots),
l2l.data.transforms.LoadData(mnist_train),
l2l.data.transforms.RemapLabels(mnist_train),
l2l.data.transforms.ConsecutiveLabels(mnist_train),
],
num_tasks=1000)
model = Net(ways)
model.to(device)
meta_model = l2l.algorithms.MAML(model, lr=maml_lr)
opt = optim.Adam(meta_model.parameters(), lr=lr)
loss_func = nn.NLLLoss(reduction='mean')
for iteration in range(iterations):
iteration_error = 0.0
iteration_acc = 0.0
for _ in range(tps):
learner = meta_model.clone()
train_task = train_tasks.sample()
data, labels = train_task
data = data.to(device)
labels = labels.to(device)
adaptation_indices = torch.zeros(data.size(0)).byte()
adaptation_indices[torch.arange(shots*ways) * 2] = 1
adaptation_data, adaptation_labels = data[adaptation_indices], labels[adaptation_indices]
evaluation_data, evaluation_labels = data[1 - adaptation_indices], labels[1 - adaptation_indices]
gamma=0.99,
num_workers=2,
seed=42,
):
random.seed(seed)
np.random.seed(seed)
th.manual_seed(seed)
def make_env():
return gym.make(env_name)
env = l2l.gym.AsyncVectorEnv([make_env for _ in range(num_workers)])
env.seed(seed)
env = ch.envs.Torch(env)
policy = DiagNormalPolicy(env.state_size, env.action_size)
meta_learner = l2l.algorithms.MAML(policy, lr=meta_lr)
baseline = LinearValue(env.state_size, env.action_size)
opt = optim.Adam(policy.parameters(), lr=meta_lr)
all_rewards = []
for iteration in range(num_iterations):
iteration_loss = 0.0
iteration_reward = 0.0
for task_config in tqdm(env.sample_tasks(meta_bsz), leave=False, desc='Data'): # Samples a new config
learner = meta_learner.clone()
env.set_task(task_config)
env.reset()
task = ch.envs.Runner(env)
# Fast Adapt
for step in range(adapt_steps):
train_episodes = task.run(learner, episodes=adapt_bsz)
th.set_num_threads(1)
random.seed(seed)
np.random.seed(seed)
th.manual_seed(seed)
def make_env():
return gym.make(env_name)
env = l2l.gym.AsyncVectorEnv([make_env for _ in range(num_workers)])
env.seed(seed)
env = ch.envs.Torch(env)
policy = DiagNormalPolicy(input_size=env.state_size,
output_size=env.action_size,
hiddens=[64, 64])
meta_learner = l2l.algorithms.MAML(policy, lr=meta_lr)
baseline = LinearValue(env.state_size, env.action_size)
opt = optim.Adam(meta_learner.parameters(), lr=meta_lr)
all_rewards = []
for iteration in range(num_iterations):
iteration_reward = 0.0
iteration_replays = []
iteration_policies = []
# Sample Trajectories
for task_config in tqdm(env.sample_tasks(meta_bsz), leave=False, desc='Data'):
clone = deepcopy(meta_learner)
env.set_task(task_config)
env.reset()
task = ch.envs.Runner(env)
task_replay = []
gamma=0.99,
num_workers=2,
seed=42,
):
random.seed(seed)
np.random.seed(seed)
th.manual_seed(seed)
def make_env():
return gym.make(env_name)
env = l2l.gym.AsyncVectorEnv([make_env for _ in range(num_workers)])
env.seed(seed)
env = ch.envs.Torch(env)
policy = DiagNormalPolicy(env.state_size, env.action_size)
meta_learner = l2l.algorithms.MetaSGD(policy, lr=meta_lr)
baseline = LinearValue(env.state_size, env.action_size)
opt = optim.Adam(policy.parameters(), lr=meta_lr)
all_rewards = []
for iteration in range(num_iterations):
iteration_loss = 0.0
iteration_reward = 0.0
for task_config in tqdm(env.sample_tasks(meta_bsz)): # Samples a new config
learner = meta_learner.clone()
env.set_task(task_config)
env.reset()
task = ch.envs.Runner(env)
# Fast Adapt
for step in range(adapt_steps):
train_episodes = task.run(learner, episodes=adapt_bsz)
test_transforms = [
NWays(test_dataset, ways),
KShots(test_dataset, 2*shots),
LoadData(test_dataset),
RemapLabels(test_dataset),
ConsecutiveLabels(train_dataset),
]
test_tasks = l2l.data.TaskDataset(test_dataset,
task_transforms=test_transforms,
num_tasks=600)
# Create model
model = l2l.vision.models.MiniImagenetCNN(ways)
model.to(device)
maml = l2l.algorithms.MAML(model, lr=fast_lr, first_order=False)
opt = optim.Adam(maml.parameters(), meta_lr)
loss = nn.CrossEntropyLoss(reduction='mean')
for iteration in range(num_iterations):
opt.zero_grad()
meta_train_error = 0.0
meta_train_accuracy = 0.0
meta_valid_error = 0.0
meta_valid_accuracy = 0.0
meta_test_error = 0.0
meta_test_accuracy = 0.0
for task in range(meta_batch_size):
# Compute meta-training loss
learner = maml.clone()
batch = train_tasks.sample()
evaluation_error, evaluation_accuracy = fast_adapt(batch,