Skip to content

Commit

Permalink
Merge pull request #17 from recohut/stage
Browse files Browse the repository at this point in the history
Stage
  • Loading branch information
sparsh-ai authored Feb 12, 2022
2 parents 4ed9c62 + b0c8224 commit 5580982
Show file tree
Hide file tree
Showing 30 changed files with 1,262 additions and 1 deletion.
1,234 changes: 1,233 additions & 1 deletion _notebooks/2022-01-21-mdp-inventory.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions _notebooks/2022-01-22-deepcross.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions _notebooks/2022-01-22-frozenlake-crossentropy.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions _notebooks/2022-01-22-graph-sdne.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions _notebooks/2022-01-22-gridworld-v2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"2022-01-22-gridworld-v2.ipynb","provenance":[{"file_id":"https://github.com/recohut/nbs/blob/main/raw/T195475%20%7C%20Building%20a%20simple%20Gridworld%20v2%20Environment.ipynb","timestamp":1644661456841}],"collapsed_sections":[],"authorship_tag":"ABX9TyOwZY30b1nrplHKfmgzUJ8x"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","metadata":{"id":"2_l6TVZdgsOl"},"source":["# Building a simple Gridworld v2 Environment"]},{"cell_type":"code","metadata":{"id":"IB4O1CBZtogr"},"source":["import gym\n","import numpy as np"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"fLP8mGdPtrxQ"},"source":["class GridworldV2Env(gym.Env):\n"," def __init__(self, step_cost=-0.2, max_ep_length=500, explore_start=False):\n"," self.index_to_coordinate_map = {\n"," \"0\": [0, 0],\n"," \"1\": [0, 1],\n"," \"2\": [0, 2],\n"," \"3\": [0, 3],\n"," \"4\": [1, 0],\n"," \"5\": [1, 1],\n"," \"6\": [1, 2],\n"," \"7\": [1, 3],\n"," \"8\": [2, 0],\n"," \"9\": [2, 1],\n"," \"10\": [2, 2],\n"," \"11\": [2, 3],\n"," }\n"," self.coordinate_to_index_map = {\n"," str(val): int(key) for key, val in self.index_to_coordinate_map.items()\n"," }\n"," self.map = np.zeros((3, 4))\n"," self.observation_space = gym.spaces.Discrete(1)\n"," self.distinct_states = [str(i) for i in range(12)]\n"," self.goal_coordinate = [0, 3]\n"," self.bomb_coordinate = [1, 3]\n"," self.wall_coordinate = [1, 1]\n"," self.goal_state = self.coordinate_to_index_map[str(self.goal_coordinate)] # 3\n"," self.bomb_state = self.coordinate_to_index_map[str(self.bomb_coordinate)] # 7\n"," self.map[self.goal_coordinate[0]][self.goal_coordinate[1]] = 1\n"," self.map[self.bomb_coordinate[0]][self.bomb_coordinate[1]] = -1\n"," self.map[self.wall_coordinate[0]][self.wall_coordinate[1]] = 2\n","\n"," self.exploring_starts = explore_start\n"," self.state = 8\n"," self.done = False\n"," self.max_ep_length = max_ep_length\n"," self.steps = 0\n"," self.step_cost = step_cost\n"," self.action_space = gym.spaces.Discrete(4)\n"," self.action_map = {\"UP\": 0, \"RIGHT\": 1, \"DOWN\": 2, \"LEFT\": 3}\n"," self.possible_actions = list(self.action_map.values())\n","\n"," def reset(self):\n"," self.done = False\n"," self.steps = 0\n"," self.map = np.zeros((3, 4))\n"," self.map[self.goal_coordinate[0]][self.goal_coordinate[1]] = 1\n"," self.map[self.bomb_coordinate[0]][self.bomb_coordinate[1]] = -1\n"," self.map[self.wall_coordinate[0]][self.wall_coordinate[1]] = 2\n","\n"," if self.exploring_starts:\n"," self.state = np.random.choice([0, 1, 2, 4, 6, 8, 9, 10, 11])\n"," else:\n"," self.state = 8\n"," return self.state\n","\n"," def get_next_state(self, current_position, action):\n","\n"," next_state = self.index_to_coordinate_map[str(current_position)].copy()\n","\n"," if action == 0 and next_state[0] != 0 and next_state != [2, 1]:\n"," # Move up\n"," next_state[0] -= 1\n"," elif action == 1 and next_state[1] != 3 and next_state != [1, 0]:\n"," # Move right\n"," next_state[1] += 1\n"," elif action == 2 and next_state[0] != 2 and next_state != [0, 1]:\n"," # Move down\n"," next_state[0] += 1\n"," elif action == 3 and next_state[1] != 0 and next_state != [1, 2]:\n"," # Move left\n"," next_state[1] -= 1\n"," else:\n"," pass\n"," return self.coordinate_to_index_map[str(next_state)]\n","\n"," def step(self, action):\n"," assert action in self.possible_actions, f\"Invalid action:{action}\"\n","\n"," current_position = self.state\n"," next_state = self.get_next_state(current_position, action)\n","\n"," self.steps += 1\n","\n"," if next_state == self.goal_state:\n"," reward = 1\n"," self.done = True\n","\n"," elif next_state == self.bomb_state:\n"," reward = -1\n"," self.done = True\n"," else:\n"," reward = self.step_cost\n","\n"," if self.steps == self.max_ep_length:\n"," self.done = True\n","\n"," self.state = next_state\n"," return next_state, reward, self.done"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"m56HRuQatzsF"},"source":["---"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"5OJ01_h1tzsI","executionInfo":{"status":"ok","timestamp":1638441388194,"user_tz":-330,"elapsed":3753,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"db7eed13-feb9-4a28-88d5-cd8e5398bc76"},"source":["!pip install -q watermark\n","%reload_ext watermark\n","%watermark -a \"Sparsh A.\" -m -iv -u -t -d"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Author: Sparsh A.\n","\n","Last updated: 2021-12-02 10:36:30\n","\n","Compiler : GCC 7.5.0\n","OS : Linux\n","Release : 5.4.104+\n","Machine : x86_64\n","Processor : x86_64\n","CPU cores : 2\n","Architecture: 64bit\n","\n","numpy : 1.19.5\n","gym : 0.17.3\n","IPython: 5.5.0\n","\n"]}]},{"cell_type":"markdown","metadata":{"id":"eGbBJ6D9tzsJ"},"source":["---"]},{"cell_type":"markdown","metadata":{"id":"EXVMZ6h8tzsL"},"source":["**END**"]}]}
1 change: 1 addition & 0 deletions _notebooks/2022-01-22-hydra.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"2022-01-22-hydra.ipynb","provenance":[{"file_id":"https://github.com/recohut/nbs/blob/main/raw/T190327%20%7C%20Hydra%20Config%20Tutorial.ipynb","timestamp":1644661637862}],"collapsed_sections":[],"authorship_tag":"ABX9TyMpT76jrsxUy87Lt3Qc5c6r"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Hydra Config"],"metadata":{"id":"nBRHan-DeXBV"}},{"cell_type":"markdown","metadata":{"id":"jHVXYJ7MQYwd"},"source":["## Setup"]},{"cell_type":"code","metadata":{"id":"BPzpURI2Kp9l"},"source":["!pip install hydra-core --upgrade"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"71jYiqAmM2SI","executionInfo":{"status":"ok","timestamp":1632040631374,"user_tz":-330,"elapsed":13,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"ac7d2a11-eb8c-4256-d368-62325f7d21c3"},"source":["!mkdir -p myproject/src/conf\n","%cd myproject"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["/content/myproject\n"]}]},{"cell_type":"markdown","metadata":{"id":"4zugqsPaQasm"},"source":["### Simple example"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"J2mmqaHRMyE6","executionInfo":{"status":"ok","timestamp":1632040670527,"user_tz":-330,"elapsed":582,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"6e6b933e-4666-4453-c712-c18a49a58c0d"},"source":["%%writefile src/conf/config.yaml\n","db:\n"," driver: mysql\n"," user: omry\n"," pass: secret"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Writing src/conf/config.yaml\n"]}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"mh2ek6xINFAh","executionInfo":{"status":"ok","timestamp":1632040711122,"user_tz":-330,"elapsed":426,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"dd206dc7-14fc-427c-bf44-9131d7581fa4"},"source":["%%writefile src/myapp.py\n","import hydra\n","from omegaconf import DictConfig, OmegaConf\n","\n","@hydra.main(config_path=\"conf\", config_name=\"config\")\n","def my_app(cfg : DictConfig) -> None:\n"," print(OmegaConf.to_yaml(cfg))\n","\n","if __name__ == \"__main__\":\n"," my_app()"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Writing src/myapp.py\n"]}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Wynjkp-3NO-O","executionInfo":{"status":"ok","timestamp":1632040731123,"user_tz":-330,"elapsed":557,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"a6b12e5b-29b1-494a-eda6-7f91a830dbbe"},"source":["!python src/myapp.py"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["db:\n"," driver: mysql\n"," user: omry\n"," pass: secret\n","\n"]}]},{"cell_type":"markdown","metadata":{"id":"KMoYFqCeNTo2"},"source":["## Overriding with CLI"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"bd4xmNwLQf6Q","executionInfo":{"status":"ok","timestamp":1632041586777,"user_tz":-330,"elapsed":1210,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"766d3b88-0f63-4726-f95e-e16eaf177070"},"source":["!python src/myapp.py db.user=root"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["db:\n"," driver: mysql\n"," user: root\n"," pass: secret\n","\n"]}]},{"cell_type":"markdown","metadata":{"id":"1aRv4kg3QkkE"},"source":["## Composition"]},{"cell_type":"code","metadata":{"id":"z25SD81jQwvv"},"source":["# ├── conf\n","# │   ├── config.yaml\n","# │   ├── db\n","# │   │   ├── mysql.yaml\n","# │   │   └── postgresql.yaml\n","# │   └── __init__.py\n","# └── my_app.py"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"Wn-emgSiQvPJ"},"source":["!mkdir -p src/conf/db"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ELp5mvIZQ0ti","executionInfo":{"status":"ok","timestamp":1632041735034,"user_tz":-330,"elapsed":664,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"91bff666-3bee-41c0-bbd8-3543c7606057"},"source":["%%writefile src/conf/db/mysql.yaml\n","driver: mysql\n","user: omry\n","pass: secret"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Writing src/conf/db/mysql.yaml\n"]}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"wtblOWVuRBz0","executionInfo":{"status":"ok","timestamp":1632041735765,"user_tz":-330,"elapsed":9,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"059fdf5d-708b-4fbd-b0ba-1ffce98d6226"},"source":["%%writefile src/conf/db/postgresql.yaml\n","driver: postgresql\n","user: root\n","pass: password"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Writing src/conf/db/postgresql.yaml\n"]}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"W989d2l7QpUL","executionInfo":{"status":"ok","timestamp":1632041747156,"user_tz":-330,"elapsed":705,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"69bc3dbe-7da3-4eec-d7ab-b1c0118ec18b"},"source":["%%writefile src/conf/config.yaml\n","defaults:\n"," - db: mysql"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Overwriting src/conf/config.yaml\n"]}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"5ZKFdsXvRL1s","executionInfo":{"status":"ok","timestamp":1632041816763,"user_tz":-330,"elapsed":1378,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"1873b8bb-d287-44f2-c328-63f0c5c0eb1e"},"source":["!python src/myapp.py db=postgresql"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["db:\n"," driver: postgresql\n"," user: root\n"," pass: password\n","\n"]}]},{"cell_type":"markdown","metadata":{"id":"HzKORc9hRY_8"},"source":["## Multirun"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"LVAzTXItR078","executionInfo":{"status":"ok","timestamp":1632041935946,"user_tz":-330,"elapsed":1730,"user":{"displayName":"Sparsh Agarwal","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"13037694610922482904"}},"outputId":"377c6c5d-8730-475a-8187-78869b8c6140"},"source":["!python src/myapp.py --multirun db=mysql,postgresql"],"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["[2021-09-19 08:58:53,036][HYDRA] Launching 2 jobs locally\n","[2021-09-19 08:58:53,036][HYDRA] \t#0 : db=mysql\n","db:\n"," driver: mysql\n"," user: omry\n"," pass: secret\n","\n","[2021-09-19 08:58:53,190][HYDRA] \t#1 : db=postgresql\n","db:\n"," driver: postgresql\n"," user: root\n"," pass: password\n","\n"]}]}]}
1 change: 1 addition & 0 deletions _notebooks/2022-01-22-matn-yelp.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 5580982

Please sign in to comment.