sample#
Sampling methods.
- gym_socks.sampling.sample.grid_sampler(grid_points)[source]#
Sample from a set of pre-defined grid points.
Hint
Use
boxgrid()orcartesian()fromgym_socks.utils.gridto generate a grid of points.- Parameters
grid_points (numpy.ndarray) – A collection of grid points in a list or array.
- Yields
A point in the grid. Note that points are yielded in the order they are given.
Example
>>> import numpy as np >>> from gym_socks.utils.grid import cartesian >>> from gym_socks.envs.sample import grid_sampler >>> grid = cartesian(np.linspace(-1, 1, 3), np.linspace(-2, 2, 3)) >>> sampler = grid_sampler(grid) >>> S = sampler.sample(size=100)
- gym_socks.sampling.sample.observation_sampler(env, state_sampler, action_sampler)[source]#
Observation sampler.
- Parameters
env (gym.Env) –
- gym_socks.sampling.sample.sample_fn(fn)[source]#
Sample function decorator.
Converts a sample function into a sample generator.
- Parameters
fn – Sample function that returns or yields an observation.
- Returns
A function that can be used to
islicea sample from the sample generator.
Example
>>> from gym_socks.envs.sample import sample_fn >>> @sample_fn ... def custom_sampler(env, policy): ... state = env.reset() ... action = policy(state=state) ... observation, *_ = env.step(action) ... yield state, action, observation >>> sampler = custom_sampler(env, policy) >>> S = sampler.sample(size=100)
- gym_socks.sampling.sample.space_sampler(space)[source]#
Randomly sample from a space.
- Parameters
space (gym.spaces.Space) – A
gym.space.Spacethat implements asample()function.- Yields
A random sample from the space.
Example
>>> from gym.spaces import Box >>> from gym_socks.envs.sample import space_sampler >>> sampler = space_sampler(Box(low=-1, high=1, shape=(2,), dtype=float)) >>> S = sampler.sample(size=100)