Coder Social home page Coder Social logo

Environment seeding about bsuite HOT 1 CLOSED

google-deepmind avatar google-deepmind commented on July 23, 2024
Environment seeding

from bsuite.

Comments (1)

ayakayal avatar ayakayal commented on July 23, 2024

How to set a seed in a bsuite environment instance? In the notebook, the output of sweep.SETTINGS has a seed attribute which is not None:

Loaded bsuite_id: bandit_noise/0.
bsuite_id=bandit_noise/0, settings={'noise_scale': 0.1, 'seed': 0}, num_episodes=10000
Loaded bsuite_id: bandit_noise/1.
bsuite_id=bandit_noise/1, settings={'noise_scale': 0.1, 'seed': 1}, num_episodes=10000
Loaded bsuite_id: bandit_noise/2.
bsuite_id=bandit_noise/2, settings={'noise_scale': 0.1, 'seed': 2}, num_episodes=10000
Loaded bsuite_id: bandit_noise/3.
bsuite_id=bandit_noise/3, settings={'noise_scale': 0.1, 'seed': 3}, num_episodes=10000

but when I printed it again on my own computer, seed was None (if I do that in the notebook, seed was None but there's an extra mapping_seed which was not None).

I tried two methods to seed the environment: (1) sweep.SETTINGS[bsuite_id]['seed']=0; (2) doing env.seed() after wrapping it with OpenAI env, but neither worked (multiple experiments, same seed, different results). A minimal example to demonstrate these two seeding methods are not working:

import random
import torch as t
import numpy as np
import bsuite
from bsuite import sweep
from bsuite.utils import gym_wrapper

def set_seed(seed, deterministic=True):
    random.seed(seed)
    np.random.seed(seed)
    t.manual_seed(seed)
    t.cuda.manual_seed_all(seed)
    t.cuda.manual_seed(seed)
    if deterministic:
        t.backends.cudnn.deterministic = True
        t.backends.cudnn.benchmark = False

set_seed(0)
bsuite_id = 'cartpole_swingup/0'
raw_env = bsuite.load_from_id(bsuite_id)

# method 1
sweep.SETTINGS[bsuite_id]['seed']=0
for episode in range(10):
    timestep = raw_env.reset()
    total_reward = 0
    while not timestep.last():
        action = np.random.choice(raw_env.action_spec().num_values)
        timestep = raw_env.step(action)
        total_reward += timestep.reward
    print(episode,total_reward)

# method 2
env = gym_wrapper.GymFromDMEnv(raw_env)
env.seed(seed=0)
for episode in range(10):
    timestep = env.reset()
    total_reward = 0
    done = False
    while not done:
        action = np.random.choice(raw_env.action_spec().num_values)
        sn,r,done,_ = env.step(action)
        total_reward += r
    print(episode,total_reward)

@adamxyang Did you find solution to this as I am having the same issue. I tried fixing the seed but I am still getting different results on different runs. Thanks.

from bsuite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.