question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pre-Training Problem

See original GitHub issue

When I try to run the code below I get this error at the pretrain function: Error

  File "C:\Users\fabio\Desktop\wetransfer-08d028\Rope_ex_v1.5\RL_Training\behaviour_cloning.py", line 40, in <module>
    model.pretrain(dataset, n_epochs=1000)

  File "c:\users\fabio\desktop\virtual_env\env1\lib\site-packages\stable_baselines\common\base_class.py", line 346, in pretrain
    expert_obs, expert_actions = dataset.get_next_batch('train')

  File "c:\users\fabio\desktop\virtual_env\env1\lib\site-packages\stable_baselines\gail\dataset\dataset.py", line 152, in get_next_batch
    dataloader.start_process()

  File "c:\users\fabio\desktop\virtual_env\env1\lib\site-packages\stable_baselines\gail\dataset\dataset.py", line 231, in start_process
    self.process.start()

  File "C:\Python\Python37\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)

  File "C:\Python\Python37\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)

  File "C:\Python\Python37\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)

  File "C:\Python\Python37\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)

  File "C:\Python\Python37\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)

PicklingError: Can't pickle <function rebuild_pipe_connection at 0x0000024B185C2168>: it's not the same object as multiprocessing.connection.rebuild_pipe_connection

Code

import gym
from stable_baselines.gail import generate_expert_traj
env = gym.make("CartPole-v1")
def dummy_expert(_obs):
    return env.action_space.sample()

generate_expert_traj(dummy_expert, 'expert_cartpole', env, n_episodes=10)

from stable_baselines import PPO2
from stable_baselines.gail import ExpertDataset

dataset = ExpertDataset(expert_path='expert_cartpole.npz',
                        traj_limitation=1, batch_size=128)

model = PPO2('MlpPolicy', 'CartPole-v1', verbose=1)
model.pretrain(dataset, n_epochs=1000)
model.learn(int(1e5))

env = model.get_env()
obs = env.reset()

reward_sum = 0.0
for _ in range(1000):
        action, _ = model.predict(obs)
        obs, reward, done, _ = env.step(action)
        reward_sum += reward
        env.render()
        if done:
                print(reward_sum)
                reward_sum = 0.0
                obs = env.reset()

env.close()

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
Miffylicommented, Jul 9, 2020

The full traceback of all processes tells the issue: It is trying to load file expert_cartpole.npz when creating ExpertDataset, but data is stored in dummy_expert_cartpole.npz. Fixing this fixes the issue.

0reactions
FabioPINOcommented, Jul 10, 2020

I tried but I get the same error as in the main question.

import gym

from stable_baselines.gail import generate_expert_traj
from stable_baselines import PPO2
from stable_baselines.gail import ExpertDataset

if __name__ == "__main__":

    env = gym.make("CartPole-v1")
    # Here the expert is a random agent
    # but it can be any python function, e.g. a PID controller
    def dummy_expert(_obs):
        """
        Random agent. It samples actions randomly
        from the action space of the environment.
    
        :param _obs: (np.ndarray) Current observation
        :return: (np.ndarray) action taken by the expert
        """
        return env.action_space.sample()
    # Data will be saved in a numpy archive named `expert_cartpole.npz`
    # when using something different than an RL expert,
    # you must pass the environment object explicitly
    generate_expert_traj(dummy_expert, 'expert_cartpole', env, n_episodes=10)
    
    
    # Using only one expert trajectory
    # you can specify `traj_limitation=-1` for using the whole dataset
    dataset = ExpertDataset(expert_path='expert_cartpole.npz',
                            traj_limitation=1, batch_size=128)
    
    model = PPO2('MlpPolicy', 'CartPole-v1', verbose=1)
    # Pretrain the PPO2 model
    model.pretrain(dataset, n_epochs=1)
    
    # As an option, you can train the RL agent
    # model.learn(int(1e5))
    
    # Test the pre-trained model
    env = model.get_env()
    obs = env.reset()
    
    reward_sum = 0.0
    for _ in range(1000):
            action, _ = model.predict(obs)
            obs, reward, done, _ = env.step(action)
            reward_sum += reward
            env.render()
            if done:
                    print(reward_sum)
                    reward_sum = 0.0
                    obs = env.reset()
    
    env.close()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Greedy Layer-Wise Pretraining in Deep Learning ...
Generally, this problem prevented the training of very deep neural networks and was referred to as the vanishing gradient problem. An important ...
Read more >
8.7.4 Pretraining - CEDAR
Greedy Supervised Pretraining. • Greedy Algorithm: 1. Break a problem into many components. 2. Solve for the optimal version of each component in...
Read more >
The Negative Pretraining Effect in Sequential Deep Learning ...
Negative pretraining is a prominent sequential learning effect of neural networks where a pretrained model obtains a worse generalization performance than a ...
Read more >
What is pre training a neural network? - Cross Validated
The usual way of training a network: You want to train a neural network to perform a task (e.g. classification) on a data...
Read more >
Using Supervised Pretraining to Improve Generalization of ...
We propose a supervised pretraining technique that helps improve general- ization on binary classification problems. The experimental results on four UCI ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found