cse151b-final-project/dqn_wordle.ipynb

339 lines
12 KiB
Plaintext
Raw Normal View History

2024-03-13 18:04:30 +00:00
{
"cells": [
{
"cell_type": "code",
2024-03-16 01:19:58 +00:00
"execution_count": 1,
2024-03-13 18:04:30 +00:00
"metadata": {},
"outputs": [],
"source": [
"import gym\n",
"import gym_wordle\n",
"from stable_baselines3 import DQN, PPO, common\n",
2024-03-13 20:57:23 +00:00
"import numpy as np\n",
"import tqdm"
2024-03-13 18:04:30 +00:00
]
},
{
"cell_type": "code",
2024-03-16 01:19:58 +00:00
"execution_count": 2,
2024-03-13 18:04:30 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<Monitor<WordleEnv instance>>\n"
]
}
],
2024-03-13 18:04:30 +00:00
"source": [
"env = gym_wordle.wordle.WordleEnv()\n",
"env = common.monitor.Monitor(env)\n",
2024-03-13 18:04:30 +00:00
"\n",
"print(env)"
]
},
{
"cell_type": "code",
2024-03-19 18:52:10 +00:00
"execution_count": 3,
2024-03-13 18:04:30 +00:00
"metadata": {},
"outputs": [
2024-03-19 18:52:10 +00:00
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using cuda device\n",
"Wrapping the env in a DummyVecEnv.\n"
]
},
2024-03-18 18:25:14 +00:00
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2024-03-19 18:52:10 +00:00
"model_id": "6921a0721569456abf5bceac7e7b6b34",
2024-03-18 18:25:14 +00:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"----------------------------------\n",
"| rollout/ | |\n",
2024-03-19 18:52:10 +00:00
"| ep_len_mean | 4.97 |\n",
"| ep_rew_mean | -63.8 |\n",
2024-03-18 18:25:14 +00:00
"| exploration_rate | 0.05 |\n",
"| time/ | |\n",
2024-03-19 18:52:10 +00:00
"| episodes | 10000 |\n",
"| fps | 1628 |\n",
"| time_elapsed | 30 |\n",
"| total_timesteps | 49995 |\n",
2024-03-18 18:25:14 +00:00
"----------------------------------\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"----------------------------------\n",
"| rollout/ | |\n",
"| ep_len_mean | 5 |\n",
2024-03-19 18:52:10 +00:00
"| ep_rew_mean | -70.5 |\n",
2024-03-18 18:25:14 +00:00
"| exploration_rate | 0.05 |\n",
"| time/ | |\n",
2024-03-19 18:52:10 +00:00
"| episodes | 20000 |\n",
"| fps | 662 |\n",
"| time_elapsed | 150 |\n",
"| total_timesteps | 99992 |\n",
2024-03-18 18:25:14 +00:00
"| train/ | |\n",
"| learning_rate | 0.0001 |\n",
2024-03-19 18:52:10 +00:00
"| loss | 11.7 |\n",
"| n_updates | 12497 |\n",
2024-03-18 18:25:14 +00:00
"----------------------------------\n"
]
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
],
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
},
2024-03-14 22:00:19 +00:00
{
"data": {
2024-03-18 18:25:14 +00:00
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
"</pre>\n"
],
2024-03-14 22:00:19 +00:00
"text/plain": [
2024-03-18 18:25:14 +00:00
"\n"
2024-03-14 22:00:19 +00:00
]
},
2024-03-18 18:25:14 +00:00
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
2024-03-19 18:52:10 +00:00
"<stable_baselines3.dqn.dqn.DQN at 0x1bfd6cc0210>"
2024-03-18 18:25:14 +00:00
]
},
2024-03-19 18:52:10 +00:00
"execution_count": 3,
2024-03-14 22:00:19 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
2024-03-13 18:04:30 +00:00
"source": [
2024-03-19 18:52:10 +00:00
"total_timesteps = 100_000\n",
2024-03-18 18:25:14 +00:00
"model = DQN(\"MlpPolicy\", env, verbose=1, device='cuda')\n",
"model.learn(total_timesteps=total_timesteps, log_interval=10_000, progress_bar=True)"
2024-03-13 18:04:30 +00:00
]
},
{
"cell_type": "code",
2024-03-19 18:52:10 +00:00
"execution_count": 4,
2024-03-13 18:04:30 +00:00
"metadata": {},
"outputs": [],
"source": [
2024-03-19 18:52:10 +00:00
"model.save(\"dqn_new_state\")"
2024-03-13 18:04:30 +00:00
]
},
{
"cell_type": "code",
2024-03-16 01:19:58 +00:00
"execution_count": 5,
2024-03-13 18:04:30 +00:00
"metadata": {},
2024-03-16 01:19:58 +00:00
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2024-03-18 18:25:14 +00:00
"c:\\Repository\\cse151b-final-project\\env\\Lib\\site-packages\\stable_baselines3\\common\\save_util.py:166: UserWarning: Could not deserialize object lr_schedule. Consider using `custom_objects` argument to replace this object.\n",
2024-03-16 01:19:58 +00:00
"Exception: code() argument 13 must be str, not int\n",
" warnings.warn(\n",
2024-03-18 18:25:14 +00:00
"c:\\Repository\\cse151b-final-project\\env\\Lib\\site-packages\\stable_baselines3\\common\\save_util.py:166: UserWarning: Could not deserialize object exploration_schedule. Consider using `custom_objects` argument to replace this object.\n",
2024-03-16 01:19:58 +00:00
"Exception: code() argument 13 must be str, not int\n",
" warnings.warn(\n"
]
}
],
2024-03-13 18:04:30 +00:00
"source": [
2024-03-18 18:25:14 +00:00
"# model = DQN.load(\"dqn_wordle\")"
2024-03-13 18:04:30 +00:00
]
},
{
"cell_type": "code",
2024-03-18 18:25:14 +00:00
"execution_count": 7,
2024-03-13 18:04:30 +00:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-03-19 18:52:10 +00:00
"[1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 0. 1. 1. 1.\n",
" 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.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gym_wordle.wordle.WordleEnv()\n",
"\n",
2024-03-18 18:25:14 +00:00
"for i in range(1000):\n",
" \n",
2024-03-16 01:19:58 +00:00
" state, info = env.reset()\n",
"\n",
" done = False\n",
"\n",
" wins = 0\n",
"\n",
" while not done:\n",
"\n",
" action, _states = model.predict(state, deterministic=True)\n",
"\n",
2024-03-16 01:19:58 +00:00
" state, reward, done, truncated, info = env.step(action)\n",
"\n",
2024-03-19 18:52:10 +00:00
" print(state)\n",
" if info[\"correct\"]:\n",
" wins += 1\n",
2024-03-14 22:00:19 +00:00
"\n",
2024-03-18 18:25:14 +00:00
"print(wins)"
2024-03-13 20:57:23 +00:00
]
2024-03-16 01:19:58 +00:00
},
{
"cell_type": "code",
2024-03-19 18:52:10 +00:00
"execution_count": 6,
2024-03-18 18:25:14 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2024-03-19 18:52:10 +00:00
"(array([1., 0., 1., 1., 1., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,\n",
" 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.,\n",
" 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,\n",
" 1., 1., 0., 1., 1., 1., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,\n",
" 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 1., 1., 1., 0., 1.,\n",
" 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,\n",
" 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
" 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
" 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
" 0., 0., 0., 0., 0., 0., 0., 1.]),\n",
" -50)"
2024-03-18 18:25:14 +00:00
]
},
2024-03-19 18:52:10 +00:00
"execution_count": 6,
2024-03-18 18:25:14 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"state, reward"
]
},
2024-03-16 01:19:58 +00:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
2024-03-13 18:04:30 +00:00
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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",
2024-03-16 01:19:58 +00:00
"version": "3.11.5"
2024-03-13 18:04:30 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 2
}