Skip to content

Commit

Permalink
Adding, updating, and fixing docs. (#53)
Browse files Browse the repository at this point in the history
* adding docs

* Fix, update, and document mini-imagenet dataset.

* MiniImagenetDataset -> MiniImagenet for convention.

* Add omniglot docs.

* Add docs for RandomDiscreteRotations.

* Add docs for OmniglotFC.

* Add docs for OmniglotCNN and MiniImagenetCNN.

* Fix docs for particles env.

* Fix linting.
  • Loading branch information
debajyotidatta authored and seba-1511 committed Sep 12, 2019
1 parent 65908a8 commit 39eef5d
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 92 deletions.
3 changes: 2 additions & 1 deletion docs/pydocmd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ generate:
- learn2learn.vision.models
- learn2learn.vision.models.OmniglotFC
- learn2learn.vision.models.OmniglotCNN
- learn2learn.vision.models.MiniImagenetCNN
- learn2learn.vision.datasets
- learn2learn.vision.datasets.FullOmniglot
- learn2learn.vision.datasets.MiniImagenet
- learn2learn.vision.transforms
- learn2learn.vision.transforms.RandomDiscreteRotation
- docs/learn2learn.text.md:
Expand All @@ -53,7 +55,6 @@ pages:
- learn2learn.algorithms: docs/learn2learn.algorithms.md
- learn2learn.data: docs/learn2learn.data.md
- learn2learn.gym: docs/learn2learn.gym.md
- learn2learn.text: docs/learn2learn.text.md
- learn2learn.vision: docs/learn2learn.vision.md
- Examples: https://github.com/learnables/learn2learn/tree/master/examples
- GitHub: https://github.com/learnables/learn2learn/
Expand Down
19 changes: 5 additions & 14 deletions examples/vision/maml_miniimagenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,18 @@ def main(
cuda=True,
seed=42,
):
MI_PATH = '~/Dropbox/Temporary/mini-imagenet-l2l/miniimagenet/resized/'

random.seed(seed)
np.random.seed(seed)
th.manual_seed(seed)
device = th.device('cpu')
if cuda:
if cuda and th.cuda.device_count():
th.cuda.manual_seed(seed)
device = th.device('cuda')

# Create Dataset
transform = transforms.Compose([
transforms.ToTensor(),
lambda x: x.float() / 255.,
])
train_images_path = os.path.join(MI_PATH, 'train')
valid_images_path = os.path.join(MI_PATH, 'val')
test_images_path = os.path.join(MI_PATH, 'test')
train_dataset = ImageFolder(train_images_path, transform)
valid_dataset = ImageFolder(valid_images_path, transform)
test_dataset = ImageFolder(test_images_path, transform)
# Create Datasets
train_dataset = l2l.vision.datasets.MiniImagenet(root='./data', mode='train')
valid_dataset = l2l.vision.datasets.MiniImagenet(root='./data', mode='validation')
test_dataset = l2l.vision.datasets.MiniImagenet(root='./data', mode='test')
train_dataset = l2l.data.MetaDataset(train_dataset)
valid_dataset = l2l.data.MetaDataset(valid_dataset)
test_dataset = l2l.data.MetaDataset(test_dataset)
Expand Down
4 changes: 2 additions & 2 deletions learn2learn/algorithms/maml.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def adapt(self, loss, first_order=None):
* **loss** (Tensor) - Loss to minimize upon update.
* **first_order** (bool, *optional*, default=None) - Whether to use first- or
second-order updates. Defaults to self.first_order.
"""
if first_order is None:
first_order = self.first_order
Expand All @@ -145,7 +145,7 @@ def clone(self, first_order=None):
* **first_order** (bool, *optional*, default=None) - Whether the clone uses first-
or second-order updates. Defaults to self.first_order.
"""
if first_order is None:
first_order = self.first_order
Expand Down
8 changes: 4 additions & 4 deletions learn2learn/data/task_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def __getitem__(self, idx):


class MetaDataset(Dataset):
"""
"""
**Descritpion**
It wraps a torch dataset by creating a map of target to indices.
Expand All @@ -37,7 +37,7 @@ class MetaDataset(Dataset):
#TODO : Add example for wrapping a non standard l2l dataset
**Arguments**
* **dataset** (Dataset) - A torch dataset.
**Example**
Expand Down Expand Up @@ -191,7 +191,7 @@ def sample(self, shots=None, task=None):
**task** (list, *optional*, default=None) - List of labels you want to sample from.
**Returns**
* Dataset - Containing the sampled task.
"""
Expand Down
4 changes: 2 additions & 2 deletions learn2learn/gym/envs/meta_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def sample_tasks(self, num_tasks):
**Returns**
tasks ([dict]) - returns a list of num_tasks tasks. Tasks are
tasks ([dict]) - returns a list of num_tasks tasks. Tasks are
dictionaries of task specific parameters. A
minimal example for num_tasks = 1 is [{'goal': value}].
"""
Expand All @@ -57,7 +57,7 @@ def set_task(self, task):
"""
**Description**
Sets the task specific parameters defined in task.
Sets the task specific parameters defined in task.
**Arguments**
Expand Down
12 changes: 6 additions & 6 deletions learn2learn/gym/envs/mujoco/ant_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

class AntDirectionEnv(MetaEnv, MujocoEnv, gym.utils.EzPickle):
"""
[[Source]](https://github.com/learnables/learn2learn/blob/master/learn2learn/gym/envs/mujoco/ant_direction.py)
[[Source]](https://github.com/learnables/learn2learn/blob/master/learn2learn/gym/envs/mujoco/ant_direction.py)
**Description**
This environment requires the Ant to learn to run in a random direction in the
XY plane. At each time step the ant receives a signal composed of a
control cost and a reward equal to its average velocity in the direction
of the plane. The tasks are 2d-arrays sampled uniformly along the unit circle.
The target direction is indicated by the vector from the origin to the sampled point.
The target direction is indicated by the vector from the origin to the sampled point.
The velocity is calculated as the distance (in the target direction) of the ant's torso
position before and after taking the specified action divided by a small value dt.
As noted in [1], a small positive bonus is added to the reward to stop the ant from
As noted in [1], a small positive bonus is added to the reward to stop the ant from
prematurely ending the episode.
**Credit**
Expand All @@ -39,7 +39,7 @@ def __init__(self, task=None):
MujocoEnv.__init__(self, 'ant.xml', 5)
gym.utils.EzPickle.__init__(self)

# -------- MetaEnv Methods --------
# -------- MetaEnv Methods --------
def set_task(self, task):
MetaEnv.set_task(self, task)
self.goal_direction = task['direction']
Expand All @@ -50,7 +50,7 @@ def sample_tasks(self, num_tasks):
tasks = [{'direction': direction} for direction in directions]
return tasks

# -------- Mujoco Methods --------
# -------- Mujoco Methods --------
def _get_obs(self):
return np.concatenate([
self.sim.data.qpos.flat[2:],
Expand All @@ -71,7 +71,7 @@ def reset_model(self):
qvel = self.init_qvel + self.np_random.randn(self.model.nv) * .1
self.set_state(qpos, qvel)

# -------- Gym Methods --------
# -------- Gym Methods --------
def step(self, action):
posbefore = np.copy(self.get_body_com("torso")[:2])
self.do_simulation(action, self.frame_skip)
Expand Down
12 changes: 6 additions & 6 deletions learn2learn/gym/envs/mujoco/ant_forward_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

class AntForwardBackwardEnv(MetaEnv, MujocoEnv, gym.utils.EzPickle):
"""
[[Source]](https://github.com/learnables/learn2learn/blob/master/learn2learn/gym/envs/mujoco/ant_forward_backward.py)
[[Source]](https://github.com/learnables/learn2learn/blob/master/learn2learn/gym/envs/mujoco/ant_forward_backward.py)
**Description**
This environment requires the ant to learn to run forward or backward.
This environment requires the ant to learn to run forward or backward.
At each time step the ant receives a signal composed of a
control cost and a reward equal to its average velocity in the direction
of the plane. The tasks are Bernoulli samples on {-1, 1} with probability 0.5, where -1 indicates the ant should
move backward and +1 indicates the ant should move forward.
The velocity is calculated as the distance (in the direction of the plane) of the ant's torso
position before and after taking the specified action divided by a small value dt.
As noted in [1], a small positive bonus is added to the reward to stop the ant from
As noted in [1], a small positive bonus is added to the reward to stop the ant from
prematurely ending the episode.
**Credit**
Expand All @@ -39,7 +39,7 @@ def __init__(self, task=None):
MujocoEnv.__init__(self, 'ant.xml', 5)
gym.utils.EzPickle.__init__(self)

# -------- MetaEnv Methods --------
# -------- MetaEnv Methods --------
def set_task(self, task):
MetaEnv.set_task(self, task)
self.goal_direction = task['direction']
Expand All @@ -49,7 +49,7 @@ def sample_tasks(self, num_tasks):
tasks = [{'direction': direction} for direction in directions]
return tasks

# -------- Mujoco Methods --------
# -------- Mujoco Methods --------
def _get_obs(self):
return np.concatenate([
self.sim.data.qpos.flat[2:],
Expand All @@ -70,7 +70,7 @@ def reset_model(self):
qvel = self.init_qvel + self.np_random.randn(self.model.nv) * .1
self.set_state(qpos, qvel)

# -------- Gym Methods --------
# -------- Gym Methods --------
def step(self, action):
xposbefore = np.copy(self.get_body_com("torso")[0])
self.do_simulation(action, self.frame_skip)
Expand Down
10 changes: 5 additions & 5 deletions learn2learn/gym/envs/mujoco/halfcheetah_forward_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
class HalfCheetahForwardBackwardEnv(MetaEnv, MujocoEnv, gym.utils.EzPickle):
"""
[[Source]](https://github.com/learnables/learn2learn/blob/master/learn2learn/gym/envs/mujoco/halfcheetah_forward_backward.py)
**Description**
This environment requires the half-cheetah to learn to run forward or backward.
This environment requires the half-cheetah to learn to run forward or backward.
At each time step the half-cheetah receives a signal composed of a
control cost and a reward equal to its average velocity in the direction
of the plane. The tasks are Bernoulli samples on {-1, 1} with probability 0.5, where -1 indicates the half-cheetah should
Expand All @@ -37,7 +37,7 @@ def __init__(self, task=None):
MujocoEnv.__init__(self, 'half_cheetah.xml', 5)
gym.utils.EzPickle.__init__(self)

# -------- MetaEnv Methods --------
# -------- MetaEnv Methods --------
def set_task(self, task):
MetaEnv.set_task(self, task)
self.goal_direction = task['direction']
Expand All @@ -47,7 +47,7 @@ def sample_tasks(self, num_tasks):
tasks = [{'direction': direction} for direction in directions]
return tasks

# -------- Mujoco Methods --------
# -------- Mujoco Methods --------
def _get_obs(self):
return np.concatenate([
self.sim.data.qpos.flat[1:],
Expand All @@ -69,7 +69,7 @@ def reset_model(self):
self.set_state(qpos, qvel)
return self._get_obs()

# -------- Gym Methods --------
# -------- Gym Methods --------
def step(self, action):
xposbefore = self.sim.data.qpos[0]
self.do_simulation(action, self.frame_skip)
Expand Down
10 changes: 5 additions & 5 deletions learn2learn/gym/envs/mujoco/humanoid_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class HumanoidDirectionEnv(MetaEnv, MujocoEnv, gym.utils.EzPickle):
XY plane. At each time step the humanoid receives a signal composed of a
control cost and a reward equal to its average velocity in the target direction.
The tasks are 2d-arrays sampled uniformly along the unit circle.
The target direction is indicated by the vector from the origin to the sampled point.
The target direction is indicated by the vector from the origin to the sampled point.
The velocity is calculated as the distance (in the target direction) of the humanoid's torso
position before and after taking the specified action divided by a small value dt.
A small positive bonus is added to the reward to stop the humanoid from
A small positive bonus is added to the reward to stop the humanoid from
prematurely ending the episode.
**Credit**
Expand All @@ -45,7 +45,7 @@ def __init__(self, task=None):
MujocoEnv.__init__(self, 'humanoid.xml', 5)
gym.utils.EzPickle.__init__(self)

# -------- MetaEnv Methods --------
# -------- MetaEnv Methods --------
def set_task(self, task):
MetaEnv.set_task(self, task)
self.goal_direction = task['direction']
Expand All @@ -56,7 +56,7 @@ def sample_tasks(self, num_tasks):
tasks = [{'direction': direction} for direction in directions]
return tasks

# -------- Mujoco Methods --------
# -------- Mujoco Methods --------
def _get_obs(self):
data = self.sim.data
return np.concatenate([data.qpos.flat[2:],
Expand All @@ -79,7 +79,7 @@ def reset_model(self):
)
return self._get_obs()

# -------- Gym Methods --------
# -------- Gym Methods --------
def step(self, action):
pos_before = np.copy(mass_center(self.model, self.sim)[:2])
self.do_simulation(action, self.frame_skip)
Expand Down
10 changes: 5 additions & 5 deletions learn2learn/gym/envs/mujoco/humanoid_forward_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def mass_center(model, sim):
class HumanoidForwardBackwardEnv(MetaEnv, MujocoEnv, gym.utils.EzPickle):
"""
[[Source]](https://github.com/learnables/learn2learn/blob/master/learn2learn/gym/envs/mujoco/humanoid_forward_backward.py)
**Description**
This environment requires the humanoid to learn to run forward or backward.
This environment requires the humanoid to learn to run forward or backward.
At each time step the humanoid receives a signal composed of a
control cost and a reward equal to its average velocity in the target direction.
The tasks are Bernoulli samples on {-1, 1} with probability 0.5, where -1 indicates the humanoid should
Expand All @@ -43,7 +43,7 @@ def __init__(self, task=None):
MujocoEnv.__init__(self, 'humanoid.xml', 5)
gym.utils.EzPickle.__init__(self)

# -------- MetaEnv Methods --------
# -------- MetaEnv Methods --------
def set_task(self, task):
MetaEnv.set_task(self, task)
self.goal_direction = task['direction']
Expand All @@ -53,7 +53,7 @@ def sample_tasks(self, num_tasks):
tasks = [{'direction': direction} for direction in directions]
return tasks

# -------- Mujoco Methods --------
# -------- Mujoco Methods --------
def _get_obs(self):
data = self.sim.data
return np.concatenate([data.qpos.flat[2:],
Expand All @@ -76,7 +76,7 @@ def reset_model(self):
)
return self._get_obs()

# -------- Gym Methods --------
# -------- Gym Methods --------
def step(self, action):
pos_before = mass_center(self.model, self.sim)[0]
self.do_simulation(action, self.frame_skip)
Expand Down
17 changes: 6 additions & 11 deletions learn2learn/gym/envs/particles/particles_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ class Particles2DEnv(MetaEnv):
**Description**
Each task is defined by the location of the goal. A point mass
receives a directional force and moves accordingly
receives a directional force and moves accordingly
(clipped in [-0.1,0.1]). The reward is equal to the negative
distance from the goal.
**Credit**
Adapted from Jonas Rothfuss' implementation.
**Arguments**
task (dict, *optional*) - Environment can optionally be initialized
with a task. If no task is provided, one is randomly sampled.
"""

def __init__(self, task=None):
Expand All @@ -38,10 +33,10 @@ def __init__(self, task=None):
shape=(2,), dtype=np.float32)
self.reset()

# -------- MetaEnv Methods --------
# -------- MetaEnv Methods --------
def sample_tasks(self, num_tasks):
"""
Tasks correspond to a goal point chosen uniformly at random.
Tasks correspond to a goal point chosen uniformly at random.
"""
goals = self.np_random.uniform(-0.5, 0.5, size=(num_tasks, 2))
tasks = [{'goal': goal} for goal in goals]
Expand All @@ -51,7 +46,7 @@ def set_task(self, task):
self._task = task
self._goal = task['goal']

# -------- Gym Methods --------
# -------- Gym Methods --------
def seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
Expand All @@ -66,7 +61,7 @@ def reset(self, env=True):
def step(self, action):
"""
**Description**
Given an action, clips the action to be in the
appropriate range and moves the point mass position
according to the action.
Expand All @@ -76,7 +71,7 @@ def step(self, action):
action (2-element array) - Array specifying the magnitude
and direction of the forces to be applied in the x and y
planes.
**Returns**
*state, reward, done, task*
Expand Down
Loading

0 comments on commit 39eef5d

Please sign in to comment.