Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PickAndPlace training #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions examples/PickAndPlace_train.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install stable-baselines3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Train agent"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gymnasium as gym\n",
"from stable_baselines3 import DDPG, SAC, HerReplayBuffer\n",
"\n",
"import panda_gym\n",
"\n",
"env = gym.make(\"PandaPickAndPlace\", render_mode=\"human\")\n",
"\n",
"model = SAC(\n",
" policy=\"MultiInputPolicy\", \n",
" env=env, \n",
" replay_buffer_class=HerReplayBuffer, \n",
" verbose=1, \n",
" buffer_size=1000000,\n",
" replay_buffer_kwargs=dict(\n",
" n_sampled_goal=4,\n",
" goal_selection_strategy='future',\n",
" ),\n",
" gamma=0.95,\n",
" learning_starts=1000,\n",
" train_freq=1\n",
" )\n",
"\n",
"model.learn(total_timesteps=4000000)\n",
"model.save(\"./her_robot\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Evaluate"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gymnasium as gym\n",
"from stable_baselines3 import SAC, HerReplayBuffer\n",
"import panda_gym\n",
"from time import sleep\n",
"\n",
"env = gym.make(\"PandaPickAndPlace\", render_mode=\"human\")\n",
"\n",
"model = SAC.load(\"./her_robot\", env=env)\n",
"vec_env = model.get_env()\n",
"obs = vec_env.reset()\n",
"for i in range(10000):\n",
" sleep(0.08)\n",
" action, _states = model.predict(obs, deterministic=True)\n",
" obs, reward, done, info = vec_env.step(action)\n",
" vec_env.render()\n",
"\n",
" if done:\n",
" obs = vec_env.reset()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}