├── .gitignore ├── .ipynb_checkpoints ├── A2C-checkpoint.ipynb ├── Art_To_Small-checkpoint.ipynb ├── Art_To_Small_2-checkpoint.ipynb ├── Baby Names-checkpoint.ipynb ├── CRNN-checkpoint.ipynb ├── Chatbot_Seq2Seq_With_Attention-checkpoint.ipynb ├── DQN-Cart-Simple-checkpoint.ipynb ├── DQN-MLP-checkpoint.ipynb ├── DQN-checkpoint.ipynb ├── DS_Generator-checkpoint.ipynb ├── Encoder_Decoder_w_Attention-checkpoint.ipynb ├── GAE-Pong-checkpoint.ipynb ├── GAE-checkpoint.ipynb ├── GAN-Copy1-checkpoint.ipynb ├── GAN-checkpoint.ipynb ├── Neural_Conversational_Model-checkpoint.ipynb ├── Neural_Style-checkpoint.ipynb ├── Neural_Style_Transfer-checkpoint.ipynb ├── PPO-checkpoint.ipynb ├── PPO_Cartpole-checkpoint.ipynb ├── PPO_Pong-checkpoint.ipynb ├── Policy_Gradients_Pong-checkpoint.ipynb ├── Predict Nvidia-checkpoint.ipynb ├── RL-checkpoint.ipynb ├── Rec_Eng-checkpoint.ipynb ├── Recognize_Kids-checkpoint.ipynb ├── Run_ChatBot-checkpoint.ipynb ├── Run_DQN-checkpoint.ipynb ├── SAGAN-checkpoint.ipynb ├── Sparse Auto Encoder-checkpoint.ipynb ├── Stacked, Denoising Auto Encoder-checkpoint.ipynb ├── TF_Conv-checkpoint.ipynb ├── TF_RNN-checkpoint.ipynb ├── Theano RNN-checkpoint.ipynb ├── Tweet-to-Text-Copy1-checkpoint.ipynb ├── Tweet-to-Text-checkpoint.ipynb ├── Twiter Sentiment-checkpoint.ipynb ├── Untitled-checkpoint.ipynb ├── VAE-checkpoint.ipynb └── reinforcement_q_learning-checkpoint.ipynb ├── A2C.ipynb ├── Art_To_Small.ipynb ├── Art_To_Small_2.ipynb ├── Baby Names.ipynb ├── CRNN.ipynb ├── Chatbot_Seq2Seq_With_Attention.ipynb ├── DQN-Cart-Simple.ipynb ├── DQN-MLP.ipynb ├── DQN.ipynb ├── DS_Generator.ipynb ├── Encoder_Decoder_w_Attention.ipynb ├── GAE-Pong.ipynb ├── GAE.ipynb ├── GAN-Copy1.ipynb ├── GAN.ipynb ├── Neural_Conversational_Model.ipynb ├── Neural_Style.ipynb ├── Neural_Style_Transfer.ipynb ├── PPO.ipynb ├── PPO_Cartpole.ipynb ├── PPO_Pong.ipynb ├── Policy_Gradients_Pong.ipynb ├── Predict Nvidia.ipynb ├── README.md ├── RL.ipynb ├── Rec_Eng.ipynb ├── Recognize_Kids.ipynb ├── Run_ChatBot.ipynb ├── Run_DQN.ipynb ├── SAGAN.ipynb ├── Sparse Auto Encoder.ipynb ├── Stacked, Denoising Auto Encoder.ipynb ├── TF_Conv.ipynb ├── TF_RNN.ipynb ├── Theano RNN.ipynb ├── Tweet-to-Text-Copy1.ipynb ├── Tweet-to-Text.ipynb ├── Twiter Sentiment.ipynb ├── Untitled.ipynb ├── VAE.ipynb ├── chat.py ├── common ├── __init__.py └── multiprocessing_env.py ├── coursera-deep-learning ├── cnns │ ├── .ipynb_checkpoints │ │ ├── Autonomous+driving+application+-+Car+detection+-+v3-checkpoint.ipynb │ │ ├── Res_Nets_Pytorch-checkpoint.ipynb │ │ ├── Residual+Networks+-+v2-checkpoint.ipynb │ │ └── Siamese_Net-checkpoint.ipynb │ ├── Autonomous+driving+application+-+Car+detection+-+v3.ipynb │ ├── Res_Nets_Pytorch.ipynb │ ├── Residual+Networks+-+v2.ipynb │ ├── Siamese_Net.ipynb │ ├── resnets_utils.py │ └── yolo_utils.py └── sequence-modeling │ ├── .ipynb_checkpoints │ ├── Emojify-checkpoint.ipynb │ ├── Jazz_Pytorch-checkpoint.ipynb │ ├── Neural+machine+translation+with+attention+-+v4-checkpoint.ipynb │ ├── Neural_Machine_Translation_Pytorch-checkpoint.ipynb │ ├── Trigger+word+detection+-+v1-checkpoint.ipynb │ └── Trigger_Detection_Pytorch-checkpoint.ipynb │ ├── Emojify.ipynb │ ├── Improvise+a+Jazz+Solo+with+an+LSTM+Network+-+v3.ipynb │ ├── Jazz_Pytorch.ipynb │ ├── Neural+machine+translation+with+attention+-+v4.ipynb │ ├── Neural_Machine_Translation_Pytorch.ipynb │ ├── Trigger+word+detection+-+v1.ipynb │ ├── Trigger_Detection_Pytorch.ipynb │ ├── data_utils.py │ ├── grammar.py │ ├── inference_code.py │ ├── insert_test.wav │ ├── midi.py │ ├── music_utils.py │ ├── nmt_utils.py │ ├── preprocess.py │ ├── qa.py │ ├── td_utils.py │ └── train.wav ├── deep_rl_course ├── .ipynb_checkpoints │ ├── Deep Q learning with Doom-checkpoint.ipynb │ ├── Deep_Q_Learning-CartPole-checkpoint.ipynb │ ├── Deep_Q_Learning-checkpoint.ipynb │ ├── Deep_Q_Learning_No_Stack-checkpoint.ipynb │ ├── Q_LEARNING-checkpoint.ipynb │ └── reinforcement_q_learning-checkpoint.ipynb ├── Deep Q learning with Doom.ipynb ├── Deep_Q_Learning-CartPole.ipynb ├── Deep_Q_Learning.ipynb ├── Deep_Q_Learning_No_Stack.ipynb ├── Q_LEARNING.ipynb ├── README.md ├── _vizdoom.ini ├── basic.cfg ├── basic.wad └── reinforcement_q_learning.ipynb ├── fastai_course2 ├── .ipynb_checkpoints │ └── Style Transfer-checkpoint.ipynb ├── Style Transfer.ipynb └── vgg16_avg.py ├── inference_kids.py ├── models.py ├── reinforcement_q_learning.ipynb ├── small_data └── baby_names_all.txt └── untitled.txt /.gitignore: -------------------------------------------------------------------------------- 1 | amzn_model.* 2 | checkpoint 3 | MNIST_data/ 4 | fastai_course2/images/ 5 | __pycache__/ 6 | models/ 7 | images/ 8 | results/ 9 | coursera-deep-learning/sequence-modeling/XY_dev/ 10 | coursera-deep-learning/sequence-modeling/XY_train/ 11 | coursera-deep-learning/sequence-modeling/audio_examples/ 12 | coursera-deep-learning/sequence-modeling/raw_data/ 13 | coursera-deep-learning/sequence-modeling/chime_output.wav 14 | coursera-deep-learning/sequence-modeling/data/ 15 | coursera-deep-learning/cnns/data/ 16 | ds_tweets.csv 17 | ua.csv 18 | .data/ 19 | 20 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Art_To_Small-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Art_To_Small_2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import os\n", 10 | "import os\n", 11 | "import scipy.misc\n", 12 | "import random" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 3, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stderr", 22 | "output_type": "stream", 23 | "text": [ 24 | "/home/tyler/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:5: DeprecationWarning: `imread` is deprecated!\n", 25 | "`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n", 26 | "Use ``imageio.imread`` instead.\n", 27 | " \"\"\"\n", 28 | "/home/tyler/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:6: DeprecationWarning: `imresize` is deprecated!\n", 29 | "`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n", 30 | "Use ``skimage.transform.resize`` instead.\n", 31 | " \n", 32 | "/home/tyler/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:7: DeprecationWarning: `imsave` is deprecated!\n", 33 | "`imsave` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n", 34 | "Use ``imageio.imwrite`` instead.\n", 35 | " import sys\n", 36 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 109 bytes but only got 0. Skipping tag 33285\n", 37 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 38 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 117 bytes but only got 0. Skipping tag 33285\n", 39 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 40 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 165 bytes but only got 0. Skipping tag 37125\n", 41 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 42 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 173 bytes but only got 0. Skipping tag 37386\n", 43 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 44 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 181 bytes but only got 0. Skipping tag 37381\n", 45 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 46 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 189 bytes but only got 0. Skipping tag 37381\n", 47 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 48 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 192 bytes but only got 0. Skipping tag 40964\n", 49 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 50 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 144 bytes but only got 0. Skipping tag 40964\n", 51 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 52 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 252 bytes but only got 0. Skipping tag 40964\n", 53 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 54 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 31 bytes but only got 0. Skipping tag 41989\n", 55 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 56 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 1149042706 bytes but only got 0. Skipping tag 17552\n", 57 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 58 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 21758425 bytes but only got 0. Skipping tag 71\n", 59 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 60 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:756: UserWarning: Corrupt EXIF data. Expecting to read 12 bytes but only got 1. \n", 61 | " warnings.warn(str(msg))\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "path_out = \"/home/tyler/data/image/small_art/landscape/\"\n", 67 | "path_in = \"/media/tyler/slowdata/data/image/images/landscape/\"\n", 68 | "for i, f in enumerate(os.listdir(path_in)):\n", 69 | " try:\n", 70 | " image = scipy.misc.imread(path_in + \"/\" + f)\n", 71 | " image = scipy.misc.imresize(image,(64,64))\n", 72 | " scipy.misc.imsave(path_out + \"/\" + str(i) + \".png\",image)\n", 73 | " except Exception:\n", 74 | " print('missed it: ' + f)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [] 83 | } 84 | ], 85 | "metadata": { 86 | "kernelspec": { 87 | "display_name": "Python 3", 88 | "language": "python", 89 | "name": "python3" 90 | }, 91 | "language_info": { 92 | "codemirror_mode": { 93 | "name": "ipython", 94 | "version": 3 95 | }, 96 | "file_extension": ".py", 97 | "mimetype": "text/x-python", 98 | "name": "python", 99 | "nbconvert_exporter": "python", 100 | "pygments_lexer": "ipython3", 101 | "version": "3.6.1" 102 | } 103 | }, 104 | "nbformat": 4, 105 | "nbformat_minor": 2 106 | } 107 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/DQN-Cart-Simple-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import gym\n", 10 | "import math\n", 11 | "import random\n", 12 | "import numpy as np\n", 13 | "import matplotlib\n", 14 | "import matplotlib.pyplot as plt\n", 15 | "from collections import namedtuple\n", 16 | "from itertools import count\n", 17 | "from PIL import Image\n", 18 | "\n", 19 | "import torch\n", 20 | "import torch.nn as nn\n", 21 | "import torch.optim as optim\n", 22 | "import torch.nn.functional as F\n", 23 | "import torchvision.transforms as T\n", 24 | "import models\n", 25 | "from comet_ml import Experiment\n", 26 | "%matplotlib inline" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "experiment = Experiment(api_key=\"dZm2UV8sODS5eDYysEf8TzKNu\", project_name=\"cart\")" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "#env = gym.make(\"Pong-v0\")\n", 45 | "env = gym.make('CartPole-v0')" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "## Replay Memory\n", 53 | "\n", 54 | "Store transitions that the agent observes so can be re-used later. By sampling from this randomly improves stability" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "Transition = namedtuple('Transition',\n", 64 | " ('state', 'action', 'next_state', 'reward'))\n", 65 | "\n", 66 | "class ReplayMemory(object):\n", 67 | " \n", 68 | " def __init__(self, capacity):\n", 69 | " self.capacity = capacity\n", 70 | " self.memory = []\n", 71 | " self.position = 0\n", 72 | " \n", 73 | " def push(self, *args):\n", 74 | " if len(self.memory) < self.capacity:\n", 75 | " self.memory.append(None)\n", 76 | " self.memory[self.position] = Transition(*args)\n", 77 | " self.position = (self.position + 1) % self.capacity\n", 78 | " \n", 79 | " def sample(self, batch_size):\n", 80 | " return random.sample(self.memory, batch_size)\n", 81 | " \n", 82 | " def __len__(self):\n", 83 | " return len(self.memory)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "BATCH_SIZE = 32\n", 93 | "GAMMA = 0.99\n", 94 | "EPS_START = 0.0\n", 95 | "EPS_END = 0.0\n", 96 | "EPS_DECAY = 1000000 + 1000\n", 97 | "TARGET_UPDATE = 10000\n", 98 | "n_frames_min = 2000000\n", 99 | "SAVE_EVERY = 250\n", 100 | "REPLAY_SIZE = 100000\n", 101 | "LR = 1e-4\n", 102 | "BURN_IN = 1000\n", 103 | "RENDER = True\n", 104 | "model_save_name = \"./models/cart_dqn_simple_3.state\"\n", 105 | "start_from_trained = True\n", 106 | "trained_model = \"./models/cart_dqn_simple_2.state\"\n", 107 | "N_ACTIONS = 2\n", 108 | "\n", 109 | "experiment.log_parameter(\"batch size\", BATCH_SIZE)\n", 110 | "experiment.log_parameter(\"gamma\", GAMMA)\n", 111 | "experiment.log_parameter(\"eps start\", EPS_START)\n", 112 | "experiment.log_parameter(\"eps end\", EPS_END)\n", 113 | "experiment.log_parameter(\"eps decay\", EPS_DECAY)\n", 114 | "experiment.log_parameter(\"target update\", TARGET_UPDATE)\n", 115 | "experiment.log_parameter(\"min frames\", n_frames_min)\n", 116 | "experiment.log_parameter(\"start trained\", start_from_trained)\n", 117 | "experiment.log_parameter(\"replay size\", REPLAY_SIZE)\n", 118 | "experiment.log_parameter(\"learning rate\", LR)\n", 119 | "experiment.log_parameter(\"burn in\", BURN_IN)\n", 120 | "\n", 121 | "\n", 122 | "# the policy network is used to play the game - aka actor\n", 123 | "policy_net = models.NNPolicy().cuda()\n", 124 | "if start_from_trained:\n", 125 | " policy_net.load_state_dict(torch.load(trained_model))\n", 126 | "\n", 127 | "# the target net is used to predict Q values for next action\n", 128 | "# we need 2 otherwise we would be using the same network\n", 129 | "# in the actual and predicted values of our loss function\n", 130 | "target_net = models.NNPolicy().cuda()\n", 131 | "target_net.load_state_dict(policy_net.state_dict())\n", 132 | "# sets training to false \n", 133 | "target_net.eval()\n", 134 | "\n", 135 | "optimizer = optim.Adam(policy_net.parameters(), lr=LR)\n", 136 | "memory = ReplayMemory(REPLAY_SIZE)\n", 137 | "\n", 138 | "steps_done = 0\n", 139 | "\n", 140 | "def select_action(state):\n", 141 | " global steps_done\n", 142 | " # gen random number\n", 143 | " sample = random.random()\n", 144 | " # get threshold which decays from start to end \n", 145 | " eps_threshold = EPS_END + (EPS_START - EPS_END) * \\\n", 146 | " math.exp(-1. * steps_done / EPS_DECAY)\n", 147 | " steps_done += 1\n", 148 | " # if exceed, pick best\n", 149 | " if sample > eps_threshold:\n", 150 | " # No gradients b/c not learning, just getting best one\n", 151 | " with torch.no_grad():\n", 152 | " return policy_net(state).max(1)[1].view(1,1), eps_threshold\n", 153 | " # else, random\n", 154 | " else:\n", 155 | " return torch.tensor([[random.randrange(2)]], device='cuda', \n", 156 | " dtype=torch.long), eps_threshold " 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "def optimize_model():\n", 166 | " if len(memory) < BATCH_SIZE or len(memory) < BURN_IN:\n", 167 | " return None\n", 168 | " \n", 169 | " transitions = memory.sample(BATCH_SIZE)\n", 170 | " # transpose the batch...\n", 171 | " batch = Transition(*zip(*transitions))\n", 172 | " # compute mask of transitions which didn't lead to ending game\n", 173 | " non_final_mask = torch.tensor(tuple(map(lambda s: s is not None,\n", 174 | " batch.next_state)), device=\"cuda\", dtype=torch.uint8)\n", 175 | " non_final_next_states = torch.cat([s for s in batch.next_state\n", 176 | " if s is not None])\n", 177 | " state_batch = torch.cat(batch.state)\n", 178 | " action_batch = torch.cat(batch.action)\n", 179 | " reward_batch = torch.cat(batch.reward)\n", 180 | " \n", 181 | " # calculate the Q value of taking the state, action pairs which were taken\n", 182 | " # the gather basically takes the Q value for the action choosen\n", 183 | " # So if my input choose action 3 for the given state, that is what I would gather.\n", 184 | " # Basically, what is the Q value for what actually happened\n", 185 | " # Q value being the total expected value from taking an action given a state.\n", 186 | " # These are basically our predictions for learning\n", 187 | " state_action_values = policy_net(state_batch).gather(1, action_batch)\n", 188 | " \n", 189 | " next_state_values = torch.zeros(BATCH_SIZE).cuda()\n", 190 | " # get the best actions for the next states\n", 191 | " # detach is for speed so don't calc gradients\n", 192 | " target_net_results = target_net(non_final_next_states).max(1)[0].detach()\n", 193 | " next_state_values[non_final_mask] = target_net_results\n", 194 | " # the expected value of the Q(s,a) given from the policy net is the\n", 195 | " # reward given plus the discounted value of the Q value from taking the best\n", 196 | " # action at the next step\n", 197 | " expected_state_action_values = (next_state_values * GAMMA) + reward_batch\n", 198 | " \n", 199 | " loss = F.smooth_l1_loss(state_action_values, \n", 200 | " expected_state_action_values.unsqueeze(1))\n", 201 | " \n", 202 | " # Optimize the policy net to become better and predicting Q values\n", 203 | " optimizer.zero_grad()\n", 204 | " loss.backward()\n", 205 | " optimizer.step()" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": { 212 | "scrolled": true 213 | }, 214 | "outputs": [], 215 | "source": [ 216 | "running_reward = None\n", 217 | "reward_sum = 0\n", 218 | "n_frames = 0\n", 219 | "i_episode = 0\n", 220 | "while n_frames < n_frames_min:\n", 221 | " obs = env.reset()\n", 222 | " state = torch.from_numpy(obs).float().unsqueeze(0).cuda()\n", 223 | " losses = []\n", 224 | " for t in count():\n", 225 | " \n", 226 | " if RENDER:\n", 227 | " env.render()\n", 228 | " action, eps_threshold = select_action(state)\n", 229 | " obs, reward, done, _ = env.step(action.item())\n", 230 | " reward_sum += reward\n", 231 | " reward = torch.tensor([reward])\n", 232 | " reward = reward.cuda()\n", 233 | "\n", 234 | " n_frames += 1\n", 235 | " if n_frames % TARGET_UPDATE == 0:\n", 236 | " print(\"Frame: {}\".format(n_frames))\n", 237 | " target_net.load_state_dict(policy_net.state_dict())\n", 238 | " \n", 239 | " if not done:\n", 240 | " next_state = torch.from_numpy(obs).float().unsqueeze(0).cuda()\n", 241 | " else:\n", 242 | " next_state = None\n", 243 | " \n", 244 | " memory.push(state, action, next_state, reward)\n", 245 | " state = next_state\n", 246 | " optimize_model()\n", 247 | " if done:\n", 248 | " running_reward = reward_sum if running_reward is None else running_reward * 0.99 + reward_sum * 0.01\n", 249 | " print(reward_sum)\n", 250 | " experiment.log_metric(\"reward sum\", reward_sum, step=i_episode)\n", 251 | " reward_sum = 0\n", 252 | " experiment.log_metric(\"reward mean\", running_reward, step=i_episode)\n", 253 | " experiment.log_metric(\"eps\", eps_threshold, step=i_episode)\n", 254 | " i_episode += 1\n", 255 | " break\n", 256 | "\n", 257 | " if i_episode % SAVE_EVERY == 0:\n", 258 | " print(\"Episode: {}\".format(i_episode))\n", 259 | " print(\"Saving Model...\")\n", 260 | " torch.save(policy_net.state_dict(), model_save_name)" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | "torch.save(policy_net.state_dict(), model_save_name)" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [] 278 | } 279 | ], 280 | "metadata": { 281 | "kernelspec": { 282 | "display_name": "Python 3", 283 | "language": "python", 284 | "name": "python3" 285 | }, 286 | "language_info": { 287 | "codemirror_mode": { 288 | "name": "ipython", 289 | "version": 3 290 | }, 291 | "file_extension": ".py", 292 | "mimetype": "text/x-python", 293 | "name": "python", 294 | "nbconvert_exporter": "python", 295 | "pygments_lexer": "ipython3", 296 | "version": "3.6.1" 297 | } 298 | }, 299 | "nbformat": 4, 300 | "nbformat_minor": 2 301 | } 302 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Neural_Conversational_Model-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Neural_Style-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Policy_Gradients_Pong-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import gym\n", 10 | "from torch.autograd import Variable\n", 11 | "import torch.nn as nn\n", 12 | "import torch.nn.functional as F\n", 13 | "import torch.optim as optim\n", 14 | "import numpy as np\n", 15 | "import torch\n", 16 | "from torch.distributions import Categorical\n", 17 | "from comet_ml import Experiment\n", 18 | "%matplotlib inline\n", 19 | "import matplotlib.pyplot as plt\n", 20 | "import models\n", 21 | "from itertools import count\n", 22 | "import time" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "hyper_dict = {\"LEARNING_RATE\": 1e-4,\n", 32 | " \"GAMMA\": 0.99,\n", 33 | " \"N_UPDATES\": 100000,\n", 34 | " \"GAMES_PER_UPDATE\": 3,\n", 35 | " \"SAVE_EVERY_UPATE\": 25,\n", 36 | " \"SAVE_FILE\": \"./models/pong_policy_7.state\",\n", 37 | " \"RESUME_FILE\": \"./models/pong_policy_6.state\",\n", 38 | " \"RESUME\": True,\n", 39 | " \"RENDER\": True,\n", 40 | " \"SLOW_DOWN\": 0.02,\n", 41 | " \"PROJECT_NAME\": \"pong-policy\",\n", 42 | " \"ENV\": \"Pong-v0\"}\n", 43 | "\n", 44 | "env = gym.make(hyper_dict['ENV'])\n", 45 | "\n", 46 | "nnpolicy = models.ConvNet(env.action_space.n).cuda()\n", 47 | "if hyper_dict[\"RESUME\"]:\n", 48 | " nnpolicy.load_state_dict(torch.load(hyper_dict[\"RESUME_FILE\"]))\n", 49 | "\n", 50 | "def prepro(I):\n", 51 | " I = I[35:195] # crop\n", 52 | " I = I[::2,::2,0] # downsample by factor of 2\n", 53 | " I[I == 144] = 0 # erase background (background type 1)\n", 54 | " I[I == 109] = 0 # erase background (background type 2)\n", 55 | " I[I != 0] = 1 # everything else (paddles, ball) just set to 1\n", 56 | " return np.expand_dims(I.astype(np.float), axis=0)\n", 57 | "\n", 58 | "\n", 59 | "def get_outputs(obs):\n", 60 | " return nnpolicy(Variable(torch.from_numpy(obs).float().unsqueeze(0).cuda()))" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 3, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stderr", 70 | "output_type": "stream", 71 | "text": [ 72 | "COMET WARNING: We detected that you are running inside a Ipython/Jupyter notebook environment but we cannot save your notebook source code. Please be sure to have installed comet_ml as a notebook server extension by running:\n", 73 | "jupyter comet_ml enable\n", 74 | "COMET INFO: old comet version (1.0.9) detected. current: 1.0.12 please update your comet lib with command: `pip install --no-cache-dir --upgrade comet_ml`\n", 75 | "COMET WARNING: Comet.ml support for Ipython Notebook is limited at the moment, automatic monitoring and stdout capturing is deactivated\n", 76 | "COMET INFO: Experiment is live on comet.ml https://www.comet.ml/syrios/pong-policy/951bdfe2e9d74ff8bc928cfb2f2b099f\n", 77 | "\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "experiment = Experiment(api_key=\"dZm2UV8sODS5eDYysEf8TzKNu\", \n", 83 | " project_name=hyper_dict['PROJECT_NAME'])\n", 84 | "experiment.log_multiple_params(hyper_dict)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 4, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "def discount_rewards(rewards, discount_rate):\n", 94 | " \"\"\"\n", 95 | " Discount a list of rewards by GAMMA\n", 96 | " Where now the last value in the list is just the reward for that value\n", 97 | " Since no more moves happened\n", 98 | " And the first gets added upon by the future discounted\n", 99 | " Thus have the discounted reward value for each action during the game\n", 100 | " \"\"\"\n", 101 | " discounted_rewards = np.empty(len(rewards))\n", 102 | " cumulative_rewards = 0\n", 103 | " for step in reversed(range(len(rewards))):\n", 104 | " cumulative_rewards = rewards[step] + discount_rate * cumulative_rewards\n", 105 | " discounted_rewards[step] = cumulative_rewards\n", 106 | " return discounted_rewards\n", 107 | "\n", 108 | "def discount_and_normalize_rewards(rewards, discount_rate):\n", 109 | " \"\"\"\n", 110 | " Discount a list of list of rewards and normalize\n", 111 | " \"\"\"\n", 112 | " all_discounted_rewards = [discount_rewards(reward, discount_rate) \n", 113 | " for reward in rewards]\n", 114 | " flat_rewards = np.concatenate(all_discounted_rewards)\n", 115 | " reward_mean = flat_rewards.mean()\n", 116 | " reward_std = flat_rewards.std()\n", 117 | " return [(discounted_rewards - reward_mean)/(reward_std + np.finfo(np.float32).eps)\n", 118 | " for discounted_rewards in all_discounted_rewards]\n", 119 | "\n", 120 | "def update_model(all_rewards, all_gradients, discount_rate):\n", 121 | " \"\"\"\n", 122 | " Get discounted and normalized rewards for each move from each game\n", 123 | " Loop over every game and take the sum of r * -1 * gradient to understand\n", 124 | " How each gradient should be updated based on that game\n", 125 | " Then average over your batch\n", 126 | " \"\"\"\n", 127 | " # get discounted rewards -> discount rewards which happened \n", 128 | " # later more and normalize\n", 129 | " loss = []\n", 130 | " all_rewards = discount_and_normalize_rewards(all_rewards,discount_rate)\n", 131 | " # for every game played, sum up the total losses where you take the loss \n", 132 | " # (*-1 since log_prob is ASCENT)\n", 133 | " # and multiply by the discounted, normalized reward. Thus, actions that lead \n", 134 | " # to good rewards are applied\n", 135 | " # And actions that lead to be rewards (negative) are inversely applied.\n", 136 | " # Thus, that sum tells you how much to update based on that game\n", 137 | " for step in range(len(all_rewards)):\n", 138 | " r = torch.Tensor(all_rewards[step]).cuda()\n", 139 | " step_loss = []\n", 140 | " for value in range(len(all_rewards[step])):\n", 141 | " step_loss.append(r[value] * all_gradients[step][value] * -1)\n", 142 | " loss.append(sum(step_loss))\n", 143 | " loss = torch.cat(loss)\n", 144 | " optimizer.zero_grad()\n", 145 | " # Take the mean of all games losses to help smooth out the learning (mini-batch)\n", 146 | " policy_loss = loss.mean()\n", 147 | " policy_loss.backward()\n", 148 | " optimizer.step()\n", 149 | " \n", 150 | " \n", 151 | "def getGradients(n_iters, n_games_per_gradient, discount_rate):\n", 152 | " \"\"\"\n", 153 | " For each n_iters:\n", 154 | " Play n_games_per_gradients and use the policy to probabilistically\n", 155 | " Move based output probabilities\n", 156 | " Save gradients and rewards\n", 157 | " After playing the batch of games, update gradients\n", 158 | " \n", 159 | " \"\"\"\n", 160 | " running_reward = None\n", 161 | " reward_sum = 0\n", 162 | " for iteration in range(n_iters):\n", 163 | " all_rewards = []\n", 164 | " all_gradients = []\n", 165 | " for game in range(n_games_per_gradient):\n", 166 | " current_rewards = []\n", 167 | " current_gradients = []\n", 168 | "\n", 169 | " obs = env.reset()\n", 170 | " prev_x = None\n", 171 | " for step in count():\n", 172 | " if hyper_dict['RENDER']:\n", 173 | " env.render()\n", 174 | " # get the predicted action probabilities from our nn policy\n", 175 | " cur_x = prepro(obs)\n", 176 | " x = cur_x - prev_x if prev_x is not None else np.zeros((1,80,80))\n", 177 | " prev_x = cur_x\n", 178 | " outputs = get_outputs(x)\n", 179 | " #select an action with these probabilities\n", 180 | " categorical_distribution = Categorical(outputs)\n", 181 | " seleted_action = categorical_distribution.sample()\n", 182 | " #save the loss function\n", 183 | " current_gradients.append(\n", 184 | " categorical_distribution.log_prob(seleted_action))\n", 185 | " #apply the action\n", 186 | " action = seleted_action.item()\n", 187 | " #save the reward\n", 188 | " obs, reward, done, _ = env.step(action)\n", 189 | " reward_sum += reward\n", 190 | " current_rewards.append(reward)\n", 191 | " if hyper_dict[\"SLOW_DOWN\"] is not None:\n", 192 | " time.sleep(hyper_dict[\"SLOW_DOWN\"]) \n", 193 | " if done:\n", 194 | " running_reward = reward_sum if running_reward is None \\\n", 195 | " else running_reward * 0.99 + reward_sum * 0.01\n", 196 | " experiment.log_metric(\"reward sum\", reward_sum, \n", 197 | " step=game + iteration * n_games_per_gradient)\n", 198 | " reward_sum = 0\n", 199 | " experiment.log_metric(\"reward mean\", \n", 200 | " running_reward,\n", 201 | " step=game + iteration * n_games_per_gradient)\n", 202 | " break\n", 203 | " all_rewards.append(current_rewards)\n", 204 | " all_gradients.append(current_gradients)\n", 205 | " # apply saved loss functions\n", 206 | " update_model(all_rewards, all_gradients, discount_rate)\n", 207 | " if iteration % hyper_dict[\"SAVE_EVERY_UPATE\"] == 0:\n", 208 | " print(\"Iteration: {}\".format(iteration))\n", 209 | " print(\"Saving Model...\")\n", 210 | " torch.save(nnpolicy.state_dict(), hyper_dict[\"SAVE_FILE\"])\n" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [ 219 | "optimizer = optim.Adam(nnpolicy.parameters(), lr=hyper_dict['LEARNING_RATE'])" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": null, 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "getGradients(n_iters = hyper_dict['N_UPDATES'],\n", 229 | " n_games_per_gradient = hyper_dict['GAMES_PER_UPDATE'],\n", 230 | " discount_rate = hyper_dict['GAMMA'])" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "metadata": {}, 237 | "outputs": [], 238 | "source": [] 239 | } 240 | ], 241 | "metadata": { 242 | "kernelspec": { 243 | "display_name": "Python 3", 244 | "language": "python", 245 | "name": "python3" 246 | }, 247 | "language_info": { 248 | "codemirror_mode": { 249 | "name": "ipython", 250 | "version": 3 251 | }, 252 | "file_extension": ".py", 253 | "mimetype": "text/x-python", 254 | "name": "python", 255 | "nbconvert_exporter": "python", 256 | "pygments_lexer": "ipython3", 257 | "version": "3.6.1" 258 | } 259 | }, 260 | "nbformat": 4, 261 | "nbformat_minor": 2 262 | } 263 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Run_DQN-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import gym\n", 10 | "import math\n", 11 | "import random\n", 12 | "import numpy as np\n", 13 | "import matplotlib\n", 14 | "import matplotlib.pyplot as plt\n", 15 | "from collections import namedtuple\n", 16 | "from itertools import count\n", 17 | "from PIL import Image\n", 18 | "\n", 19 | "import torch\n", 20 | "import torch.nn as nn\n", 21 | "import torch.optim as optim\n", 22 | "import torch.nn.functional as F\n", 23 | "import torchvision.transforms as T\n", 24 | "import models\n", 25 | "%matplotlib inline" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 2, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "\u001b[33mWARN: gym.spaces.Box autodetected dtype as . Please provide explicit dtype.\u001b[0m\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "env = gym.make('CartPole-v0').unwrapped" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "resize = T.Compose([T.ToPILImage(),\n", 52 | " T.Resize(40, interpolation=Image.CUBIC),\n", 53 | " T.ToTensor()])\n", 54 | "\n", 55 | "# This is based on the code from gym.\n", 56 | "screen_width = 600\n", 57 | "\n", 58 | "\n", 59 | "def get_cart_location():\n", 60 | " world_width = env.x_threshold * 2\n", 61 | " scale = screen_width / world_width\n", 62 | " return int(env.state[0] * scale + screen_width / 2.0) # MIDDLE OF CART\n", 63 | "\n", 64 | "\n", 65 | "def get_screen():\n", 66 | " screen = env.render(mode='rgb_array').transpose(\n", 67 | " (2, 0, 1)) # transpose into torch order (CHW)\n", 68 | " # Strip off the top and bottom of the screen\n", 69 | " screen = screen[:, 160:320]\n", 70 | " view_width = 320\n", 71 | " cart_location = get_cart_location()\n", 72 | " if cart_location < view_width // 2:\n", 73 | " slice_range = slice(view_width)\n", 74 | " elif cart_location > (screen_width - view_width // 2):\n", 75 | " slice_range = slice(-view_width, None)\n", 76 | " else:\n", 77 | " slice_range = slice(cart_location - view_width // 2,\n", 78 | " cart_location + view_width // 2)\n", 79 | " # Strip off the edges, so that we have a square image centered on a cart\n", 80 | " screen = screen[:, :, slice_range]\n", 81 | " # Convert to float, rescare, convert to torch tensor\n", 82 | " # (this doesn't require a copy)\n", 83 | " screen = np.ascontiguousarray(screen, dtype=np.float32) / 255\n", 84 | " screen = torch.from_numpy(screen)\n", 85 | " # Resize, and add a batch dimension (BCHW)\n", 86 | " return resize(screen).unsqueeze(0).cuda()\n", 87 | "\n", 88 | "def select_action(state):\n", 89 | " return policy_net(state).max(1)[1].view(1,1)\n" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 4, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "N_ACTIONS = 2\n", 99 | "policy_net = models.DQN().cuda()\n", 100 | "policy_net.load_state_dict(torch.load(\"./models/cart_dqn_policy2.state\"))" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 5, 106 | "metadata": { 107 | "scrolled": true 108 | }, 109 | "outputs": [], 110 | "source": [ 111 | "num_episodes = 10\n", 112 | "for i_episode in range(num_episodes):\n", 113 | " env.reset()\n", 114 | " last_screen = get_screen()\n", 115 | " current_screen = get_screen()\n", 116 | " state = current_screen - last_screen\n", 117 | " prev_state = None\n", 118 | " \n", 119 | " for t in count():\n", 120 | " action = select_action(state)\n", 121 | " _, reward, done, _ = env.step(action.item())\n", 122 | "\n", 123 | " reward = torch.tensor([reward])\n", 124 | " reward = reward.cuda()\n", 125 | " \n", 126 | " last_screen = current_screen\n", 127 | " current_screen = get_screen()\n", 128 | " \n", 129 | " if not done:\n", 130 | " next_state = current_screen - last_screen\n", 131 | " else:\n", 132 | " next_state = None\n", 133 | " state = next_state\n", 134 | "\n", 135 | " if done:\n", 136 | " break" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [] 145 | } 146 | ], 147 | "metadata": { 148 | "kernelspec": { 149 | "display_name": "Python 3", 150 | "language": "python", 151 | "name": "python3" 152 | }, 153 | "language_info": { 154 | "codemirror_mode": { 155 | "name": "ipython", 156 | "version": 3 157 | }, 158 | "file_extension": ".py", 159 | "mimetype": "text/x-python", 160 | "name": "python", 161 | "nbconvert_exporter": "python", 162 | "pygments_lexer": "ipython3", 163 | "version": "3.6.1" 164 | } 165 | }, 166 | "nbformat": 4, 167 | "nbformat_minor": 2 168 | } 169 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/TF_Conv-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import tensorflow as tf\n", 12 | "from tensorflow.examples.tutorials.mnist import input_data" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 3, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\n", 25 | "Extracting MNIST_data/train-images-idx3-ubyte.gz\n", 26 | "Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\n", 27 | "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n", 28 | "Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\n", 29 | "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n", 30 | "Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\n", 31 | "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "mnist = input_data.read_data_sets('MNIST_data', one_hot=True)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 16, 42 | "metadata": { 43 | "collapsed": true 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "sess = tf.InteractiveSession()" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 17, 53 | "metadata": { 54 | "collapsed": true 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "x = tf.placeholder(tf.float32, [None, 784])\n", 59 | "y_ = tf.placeholder(tf.float32, [None, 10])" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 18, 65 | "metadata": { 66 | "collapsed": true 67 | }, 68 | "outputs": [], 69 | "source": [ 70 | "def weight_variable(shape):\n", 71 | " initial = tf.truncated_normal(shape, stddev=0.1)\n", 72 | " return tf.Variable(initial)\n", 73 | "\n", 74 | "def bias_variable(shape):\n", 75 | " initial = tf.constant(0.1, shape=shape)\n", 76 | " return tf.Variable(initial)\n", 77 | "\n", 78 | "def conv2d(x, W):\n", 79 | " return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')\n", 80 | "\n", 81 | "def maxpool(x):\n", 82 | " return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "### Conv Math\n", 90 | "\n", 91 | "#### Same Padding\n", 92 | "\n", 93 | "out_height = ceil(float(in_height) / float(strides[1]))\n", 94 | "\n", 95 | "out_width = ceil(float(in_width) / float(strides[2]))\n", 96 | "\n", 97 | "#### Valid Padding\n", 98 | "\n", 99 | "out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))\n", 100 | "\n", 101 | "out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))\n", 102 | "\n", 103 | "### Pooling Math\n", 104 | "\n", 105 | "Accepts a volume of size W1×H1×D1\n", 106 | "\n", 107 | "Requires two hyperparameters:\n", 108 | "\n", 109 | "their spatial extent F,\n", 110 | "\n", 111 | "the stride S,\n", 112 | "\n", 113 | "Produces a volume of size W2×H2×D2W2×H2×D2 where:\n", 114 | "\n", 115 | "W2=(W1−F)/S+1\n", 116 | "\n", 117 | "H2=(H1−F)/S+1\n", 118 | "\n", 119 | "D2=D1" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 40, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "# stride x, stride y, # channels, # filters\n", 129 | "W_conv1 = weight_variable([5,5,1,32])\n", 130 | "b_conv1 = bias_variable([32])\n", 131 | "\n", 132 | "# batch, height, width, channels\n", 133 | "x_image = tf.reshape(x, [-1, 28, 28, 1])\n", 134 | "\n", 135 | "###first conv layer\n", 136 | "h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)\n", 137 | "h_maxpool1 = maxpool(h_conv1)\n", 138 | "\n", 139 | "W_conv2 = weight_variable([5, 5, 32, 128])\n", 140 | "b_conv2 = bias_variable([128])\n", 141 | "\n", 142 | "###second conv layer\n", 143 | "h_conv2 = tf.nn.relu(conv2d(h_maxpool1, W_conv2) + b_conv2)\n", 144 | "h_maxpool2 = maxpool(h_conv2)\n", 145 | "\n", 146 | "\n", 147 | "###dense layer\n", 148 | "W_dense1 = weight_variable([7*7*128, 512])\n", 149 | "b_dense1 = bias_variable([512])\n", 150 | "\n", 151 | "h_maxpool2_flat = tf.reshape(h_maxpool2, [-1, 7*7*128])\n", 152 | "h_fc1 = tf.nn.relu(tf.matmul(h_maxpool2_flat, W_dense1) + b_dense1)\n", 153 | "\n", 154 | "###dense layer\n", 155 | "W_dense2 = weight_variable([512, 1024])\n", 156 | "b_dense2 = bias_variable([1024])\n", 157 | "\n", 158 | "h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_dense2) + b_dense2)\n", 159 | "\n", 160 | "###final dense layer\n", 161 | "W_fc3 = weight_variable([1024, 10])\n", 162 | "b_fc3 = bias_variable([10])\n", 163 | "\n", 164 | "y_conv = tf.matmul(h_fc2, W_fc3) + b_fc3" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 41, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "step 0, training accuracy 0.14\n", 177 | "test accuracy 0.0942\n", 178 | "test accuracy 0.0657\n", 179 | "step 2, training accuracy 0.06\n", 180 | "test accuracy 0.0922\n", 181 | "test accuracy 0.1283\n", 182 | "step 4, training accuracy 0.18\n", 183 | "test accuracy 0.1243\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "cross_entropy = tf.reduce_mean(\n", 189 | " tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))\n", 190 | "train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)\n", 191 | "correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))\n", 192 | "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", 193 | "\n", 194 | "with tf.Session() as sess:\n", 195 | " sess.run(tf.global_variables_initializer())\n", 196 | " for i in range(5):\n", 197 | " batch = mnist.train.next_batch(50)\n", 198 | " if i % 2 == 0:\n", 199 | " train_accuracy = accuracy.eval(feed_dict={\n", 200 | " x: batch[0], y_: batch[1]})\n", 201 | " print('step %d, training accuracy %g' % (i, train_accuracy))\n", 202 | " train_step.run(feed_dict={x: batch[0], y_: batch[1]})\n", 203 | "\n", 204 | " print('test accuracy %g' % accuracy.eval(feed_dict={\n", 205 | " x: mnist.test.images, y_: mnist.test.labels}))" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": { 212 | "collapsed": true 213 | }, 214 | "outputs": [], 215 | "source": [] 216 | } 217 | ], 218 | "metadata": { 219 | "kernelspec": { 220 | "display_name": "Python 3", 221 | "language": "python", 222 | "name": "python3" 223 | }, 224 | "language_info": { 225 | "codemirror_mode": { 226 | "name": "ipython", 227 | "version": 3 228 | }, 229 | "file_extension": ".py", 230 | "mimetype": "text/x-python", 231 | "name": "python", 232 | "nbconvert_exporter": "python", 233 | "pygments_lexer": "ipython3", 234 | "version": "3.6.1" 235 | } 236 | }, 237 | "nbformat": 4, 238 | "nbformat_minor": 2 239 | } 240 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Theano RNN-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 42, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from keras.utils.data_utils import get_file\n", 10 | "import numpy as np\n", 11 | "from theano import shared\n", 12 | "import theano.tensor as T\n", 13 | "import math\n", 14 | "import theano\n", 15 | "from itertools import chain\n", 16 | "from collections import OrderedDict" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 3, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "corpus length: 600893\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "path = get_file('nietzsche.txt', origin=\"https://s3.amazonaws.com/text-datasets/nietzsche.txt\")\n", 34 | "text = open(path).read()\n", 35 | "print('corpus length:', len(text))" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 4, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "total chars: 85\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "chars = sorted(list(set(text)))\n", 53 | "vocab_size = len(chars)+1\n", 54 | "print('total chars:', vocab_size)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 5, 60 | "metadata": { 61 | "collapsed": true 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "chars.insert(0, \"\\0\")\n", 66 | "char_indices = dict((c, i) for i, c in enumerate(chars))\n", 67 | "indices_char = dict((i, c) for i, c in enumerate(chars))" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "## Theano RNN" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 23, 80 | "metadata": { 81 | "collapsed": true 82 | }, 83 | "outputs": [], 84 | "source": [ 85 | "n_hidden = 256\n", 86 | "n_input = vocab_size\n", 87 | "n_output = vocab_size" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 20, 93 | "metadata": { 94 | "collapsed": true 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "def init_wgts(rows, cols): \n", 99 | " #xavier init for weights\n", 100 | " scale = math.sqrt(2/rows)\n", 101 | " ## shared basically means want to pass off to theano and it will use that dat aon GPU\n", 102 | " return shared(np.random.normal(scale=scale, size=(rows, cols)).astype(np.float32))\n", 103 | "def init_bias(rows): \n", 104 | " #zero init for bias\n", 105 | " return shared(np.zeros(rows, dtype=np.float32))\n", 106 | "def wgts_and_bias(n_in, n_out): \n", 107 | " return init_wgts(n_in, n_out), init_bias(n_out)\n", 108 | "def id_and_bias(n): \n", 109 | " #returns identity init for weights\n", 110 | " return shared(np.eye(n, dtype=np.float32)), init_bias(n)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 21, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [ 119 | "## These are our variables\n", 120 | "# input\n", 121 | "t_inp = T.matrix('inp')\n", 122 | "#output\n", 123 | "t_outp = T.matrix('outp')\n", 124 | "#init first hidden state\n", 125 | "t_h0 = T.vector('h0')\n", 126 | "#learning rate\n", 127 | "lr = T.scalar('lr')\n", 128 | "\n", 129 | "#all args give theano\n", 130 | "all_args = [t_h0, t_inp, t_outp, lr]" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 31, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "#hidden weights\n", 140 | "W_h = id_and_bias(n_hidden)\n", 141 | "#input weights\n", 142 | "W_x = wgts_and_bias(n_input, n_hidden)\n", 143 | "#output weights\n", 144 | "W_y = wgts_and_bias(n_hidden, n_output)\n", 145 | "w_all = list(chain.from_iterable([W_h, W_x, W_y]))" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 32, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "def step(x, h, W_h, b_h, W_x, b_x, W_y, b_y):\n", 155 | " # Calculate the hidden activations\n", 156 | " h = T.nnet.relu(T.dot(x, W_x) + b_x + T.dot(h, W_h) + b_h)\n", 157 | " # feed hidden out softmax for y. generalization of softmax to K classes\n", 158 | " y = T.nnet.softmax(T.dot(h, W_y) + b_y)\n", 159 | " return h, T.flatten(y, 1)" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 37, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "## sequences = sequences want to iterate over\n", 169 | "## outputs_info = initial state of necessary things\n", 170 | "[v_h, v_y], _ = theano.scan(step, sequences=t_inp, \n", 171 | " outputs_info=[t_h0, None], non_sequences=w_all)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 40, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "#cross entropy error predictions with actual\n", 181 | "error = T.nnet.categorical_crossentropy(v_y, t_outp).sum()\n", 182 | "#calc grad of error function wrt all weights\n", 183 | "g_all = T.grad(error, w_all)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 43, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "def upd_dict(wgts, grads, lr): \n", 193 | " return OrderedDict({w: w-g*lr for (w,g) in zip(wgts,grads)})\n", 194 | "\n", 195 | "## apply updates\n", 196 | "upd = upd_dict(w_all, g_all, lr)" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": { 203 | "collapsed": true 204 | }, 205 | "outputs": [], 206 | "source": [ 207 | "## combine everything. give it all the args. then what to calc. and pass the function to update every step.\n", 208 | "fn = theano.function(all_args, error, updates=upd, allow_input_downcast=True)" 209 | ] 210 | } 211 | ], 212 | "metadata": { 213 | "kernelspec": { 214 | "display_name": "Python 3", 215 | "language": "python", 216 | "name": "python3" 217 | }, 218 | "language_info": { 219 | "codemirror_mode": { 220 | "name": "ipython", 221 | "version": 3 222 | }, 223 | "file_extension": ".py", 224 | "mimetype": "text/x-python", 225 | "name": "python", 226 | "nbconvert_exporter": "python", 227 | "pygments_lexer": "ipython3", 228 | "version": "3.6.1" 229 | } 230 | }, 231 | "nbformat": 4, 232 | "nbformat_minor": 2 233 | } 234 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Tweet-to-Text-Copy1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tweepy\n", 10 | "import csv\n", 11 | "import time\n", 12 | "\n", 13 | "# Generate at https://apps.twitter.com/app\n", 14 | "CONSUMER_KEY = 'IVaf7RQIknSTczbMV5kB2onis'\n", 15 | "CONSUMER_SECRET = 'FJAPeipzPlgbBz5vuYxZ83N5YjAC5aySW43sX0djxN7VNiQKft'\n", 16 | "OAUTH_TOKEN = '823299052813090817-3WBWGzjiSEAiSLAFLZxteQSATfGEmsB'\n", 17 | "OAUTH_TOKEN_SECRET = 'I73ZKWPkz3NIHVZMetEGfBvR9Hxq92e3gRELNJjrWUGyt'\n", 18 | "\n", 19 | "auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)\n", 20 | "auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)\n", 21 | "api = tweepy.API(auth,wait_on_rate_limit=True)\n", 22 | "\n", 23 | "tweet_text = []\n", 24 | "\n", 25 | "for i, status in enumerate(tweepy.Cursor(api.search,q=\"#datascience\",count=100,\n", 26 | " lang=\"en\",\n", 27 | " since=\"2018-10-01\",\n", 28 | " tweet_mode='extended').items()):\n", 29 | " if i > 15000:\n", 30 | " break\n", 31 | " \n", 32 | " if hasattr(status, 'retweeted_status'):\n", 33 | " try:\n", 34 | " tweet = status.retweeted_status.full_text\n", 35 | " except:\n", 36 | " tweet = status.retweeted_status.text\n", 37 | " else:\n", 38 | " try:\n", 39 | " tweet = status.full_text\n", 40 | " except AttributeError:\n", 41 | " tweet = status.text\n", 42 | " tweet_text.append(tweet)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "import pandas as pd" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 4, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "df = pd.DataFrame(tweet_text)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 5, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/html": [ 71 | "
\n", 72 | "\n", 85 | "\n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | "
0
0UltraSoC extends tools offering with integrate...
1Lots of Free Open Source Datasets to Make Your...
2UltraSoC announces integrated multi-core debug...
3With $1 billion in funding, @MIT will create a...
4Over 50,000 positions in Data Science and Mach...
\n", 115 | "
" 116 | ], 117 | "text/plain": [ 118 | " 0\n", 119 | "0 UltraSoC extends tools offering with integrate...\n", 120 | "1 Lots of Free Open Source Datasets to Make Your...\n", 121 | "2 UltraSoC announces integrated multi-core debug...\n", 122 | "3 With $1 billion in funding, @MIT will create a...\n", 123 | "4 Over 50,000 positions in Data Science and Mach..." 124 | ] 125 | }, 126 | "execution_count": 5, 127 | "metadata": {}, 128 | "output_type": "execute_result" 129 | } 130 | ], 131 | "source": [ 132 | "df.head()" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 6, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "df.to_csv(\"ds_tweets.csv\", index=False)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.6.6" 169 | } 170 | }, 171 | "nbformat": 4, 172 | "nbformat_minor": 2 173 | } 174 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Art_To_Small_2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import os\n", 10 | "import os\n", 11 | "import scipy.misc\n", 12 | "import random" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 3, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stderr", 22 | "output_type": "stream", 23 | "text": [ 24 | "/home/tyler/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:5: DeprecationWarning: `imread` is deprecated!\n", 25 | "`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n", 26 | "Use ``imageio.imread`` instead.\n", 27 | " \"\"\"\n", 28 | "/home/tyler/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:6: DeprecationWarning: `imresize` is deprecated!\n", 29 | "`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n", 30 | "Use ``skimage.transform.resize`` instead.\n", 31 | " \n", 32 | "/home/tyler/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:7: DeprecationWarning: `imsave` is deprecated!\n", 33 | "`imsave` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n", 34 | "Use ``imageio.imwrite`` instead.\n", 35 | " import sys\n", 36 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 109 bytes but only got 0. Skipping tag 33285\n", 37 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 38 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 117 bytes but only got 0. Skipping tag 33285\n", 39 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 40 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 165 bytes but only got 0. Skipping tag 37125\n", 41 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 42 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 173 bytes but only got 0. Skipping tag 37386\n", 43 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 44 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 181 bytes but only got 0. Skipping tag 37381\n", 45 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 46 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 189 bytes but only got 0. Skipping tag 37381\n", 47 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 48 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 192 bytes but only got 0. Skipping tag 40964\n", 49 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 50 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 144 bytes but only got 0. Skipping tag 40964\n", 51 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 52 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 252 bytes but only got 0. Skipping tag 40964\n", 53 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 54 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 31 bytes but only got 0. Skipping tag 41989\n", 55 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 56 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 1149042706 bytes but only got 0. Skipping tag 17552\n", 57 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 58 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:739: UserWarning: Possibly corrupt EXIF data. Expecting to read 21758425 bytes but only got 0. Skipping tag 71\n", 59 | " \" Skipping tag %s\" % (size, len(data), tag))\n", 60 | "/home/tyler/anaconda3/lib/python3.6/site-packages/PIL/TiffImagePlugin.py:756: UserWarning: Corrupt EXIF data. Expecting to read 12 bytes but only got 1. \n", 61 | " warnings.warn(str(msg))\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "path_out = \"/home/tyler/data/image/small_art/landscape/\"\n", 67 | "path_in = \"/media/tyler/slowdata/data/image/images/landscape/\"\n", 68 | "for i, f in enumerate(os.listdir(path_in)):\n", 69 | " try:\n", 70 | " image = scipy.misc.imread(path_in + \"/\" + f)\n", 71 | " image = scipy.misc.imresize(image,(64,64))\n", 72 | " scipy.misc.imsave(path_out + \"/\" + str(i) + \".png\",image)\n", 73 | " except Exception:\n", 74 | " print('missed it: ' + f)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [] 83 | } 84 | ], 85 | "metadata": { 86 | "kernelspec": { 87 | "display_name": "Python 3", 88 | "language": "python", 89 | "name": "python3" 90 | }, 91 | "language_info": { 92 | "codemirror_mode": { 93 | "name": "ipython", 94 | "version": 3 95 | }, 96 | "file_extension": ".py", 97 | "mimetype": "text/x-python", 98 | "name": "python", 99 | "nbconvert_exporter": "python", 100 | "pygments_lexer": "ipython3", 101 | "version": "3.5.3" 102 | } 103 | }, 104 | "nbformat": 4, 105 | "nbformat_minor": 2 106 | } 107 | -------------------------------------------------------------------------------- /CRNN.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import torch\n", 10 | "import torch.nn as nn\n", 11 | "from torch.autograd import Variable\n", 12 | "import numpy as np\n", 13 | "import time\n", 14 | "import math\n", 15 | "import matplotlib.pyplot as plt\n", 16 | "import torch.optim as optim\n", 17 | "from collections import defaultdict\n", 18 | "import torch.distributions as distributions\n", 19 | "import pandas as pd\n", 20 | "from collections import Counter\n", 21 | "import os\n", 22 | "import torch.nn.functional as F\n", 23 | "%matplotlib inline" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "## Example of RNN and LSTM using pytorch\n", 31 | "\n", 32 | "Source: http://pytorch.org/tutorials/intermediate/char_rnn_generation_tutorial.html#sphx-glr-intermediate-char-rnn-generation-tutorial-py" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "input_directory = \"/Users/tfolkman/projects/elder_folkman/content/post/\"" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 3, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "all_lines = \"\"\n", 51 | "for file in os.listdir(input_directory):\n", 52 | " if file == \"Contact.md\":\n", 53 | " continue\n", 54 | " with open(input_directory + file, \"r\") as f:\n", 55 | " meta_counter = 0\n", 56 | " for i, line in enumerate(f):\n", 57 | " if \"+++\" in line:\n", 58 | " meta_counter += 1\n", 59 | " continue\n", 60 | " elif meta_counter > 1:\n", 61 | " clean_line = line.strip().lower()\n", 62 | " if len(clean_line) == 0:\n", 63 | " all_lines += \"\\n\"\n", 64 | " else:\n", 65 | " all_lines = all_lines + \" \" + clean_line" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "325408" 77 | ] 78 | }, 79 | "execution_count": 4, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "len(all_lines)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 5, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "class LSTM(nn.Module):\n", 95 | " def __init__(self, input_size, hidden_size, output_size, embedding_dim, n_layers=1, bs=1):\n", 96 | " super(LSTM, self).__init__()\n", 97 | " self.hidden_size = hidden_size\n", 98 | " self.n_layers = n_layers\n", 99 | " self.bs = bs\n", 100 | " self.word_embeddings = nn.Embedding(input_size, embedding_dim)\n", 101 | " self.lstm = nn.LSTM(embedding_dim, hidden_size, n_layers, dropout=0.5)\n", 102 | " self.hidden2tag = nn.Linear(hidden_size, output_size)\n", 103 | " self.hidden = self.initHidden()\n", 104 | "\n", 105 | " def forward(self, input):\n", 106 | " embeds = self.word_embeddings(input)\n", 107 | " lstm_out, self.hidden = self.lstm(\n", 108 | " embeds.view(len(input), 1, -1), self.hidden)\n", 109 | " last_lstm = lstm_out[-1]\n", 110 | " tag_space = self.hidden2tag(last_lstm)\n", 111 | " tag_scores = F.log_softmax(tag_space, dim=1)\n", 112 | " return tag_scores\n", 113 | "\n", 114 | " def initHidden(self):\n", 115 | " return (torch.zeros(self.n_layers, self.bs, self.hidden_size),\n", 116 | " torch.zeros(self.n_layers, self.bs, self.hidden_size))" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 6, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "letters = set([c.lower() for word in all_lines for c in word])\n", 126 | "letters_to_index = {l:i for i,l in enumerate(letters)}\n", 127 | "index_to_letter = {i:l for l, i in letters_to_index.items()}" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 7, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [ 136 | "n_letters = len(letters)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 8, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "sequence_length = 50\n", 146 | "batch_size = 1\n", 147 | "\n", 148 | "def inputTensor(line):\n", 149 | " return torch.LongTensor([letters_to_index[letter] for letter in line[:-1]])\n", 150 | "\n", 151 | "def targetTensor(line):\n", 152 | " return torch.LongTensor([letters_to_index[line[-1]]])\n", 153 | "\n", 154 | "def randomTrainingPair():\n", 155 | " starting_position = np.random.randint(len(all_lines)-(sequence_length+1))\n", 156 | " return all_lines[starting_position:starting_position+(sequence_length+1)]\n", 157 | "\n", 158 | "def randomTrainingExample():\n", 159 | " line = randomTrainingPair()\n", 160 | " input_line_tensor = inputTensor(line)\n", 161 | " target_line_tensor = targetTensor(line)\n", 162 | " return input_line_tensor, target_line_tensor, line\n", 163 | "\n", 164 | "def timeSince(since):\n", 165 | " now = time.time()\n", 166 | " s = now - since\n", 167 | " m = math.floor(s / 60)\n", 168 | " s -= m * 60\n", 169 | " return '%dm %ds' % (m, s)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 9, 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "name": "stderr", 179 | "output_type": "stream", 180 | "text": [ 181 | "/Users/tfolkman/anaconda3/lib/python3.5/site-packages/torch/nn/modules/rnn.py:38: UserWarning: dropout option adds dropout after all but last recurrent layer, so non-zero dropout expects num_layers greater than 1, but got dropout=0.5 and num_layers=1\n", 182 | " \"num_layers={}\".format(dropout, num_layers))\n" 183 | ] 184 | } 185 | ], 186 | "source": [ 187 | "criterion = nn.NLLLoss()\n", 188 | "lstm = LSTM(n_letters, 64, n_letters, 64)\n", 189 | "\n", 190 | "optimizer = optim.Adam(lstm.parameters(), lr=.001)\n", 191 | "\n", 192 | "def train(input_line_tensor, target_line_tensor):\n", 193 | "\n", 194 | " optimizer.zero_grad()\n", 195 | "\n", 196 | " output = lstm(input_line_tensor)\n", 197 | " loss = criterion(output, target_line_tensor)\n", 198 | "\n", 199 | " loss.backward(retain_graph=True)\n", 200 | "\n", 201 | " optimizer.step()\n", 202 | "\n", 203 | " return output, loss.data.item() / input_line_tensor.size()[0]" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "name": "stdout", 213 | "output_type": "stream", 214 | "text": [ 215 | "15m 36s (200 10%) 0.0423\n", 216 | "36m 41s (400 20%) 0.0734\n" 217 | ] 218 | } 219 | ], 220 | "source": [ 221 | "n_iters = 2000\n", 222 | "print_every = 200\n", 223 | "plot_every = 200\n", 224 | "all_losses = []\n", 225 | "total_loss = 0 # Reset every plot_every iters\n", 226 | "\n", 227 | "start = time.time()\n", 228 | "\n", 229 | "for iter in range(1, n_iters + 1):\n", 230 | " input_t, target_t, _ = randomTrainingExample()\n", 231 | " output, loss = train(input_t, target_t)\n", 232 | " total_loss += loss\n", 233 | "\n", 234 | " if iter % print_every == 0:\n", 235 | " print('%s (%d %d%%) %.4f' % (timeSince(start), iter, iter / n_iters * 100, loss))\n", 236 | "\n", 237 | " if iter % plot_every == 0:\n", 238 | " all_losses.append(total_loss / plot_every)\n", 239 | " total_loss = 0" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": null, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [ 248 | "plt.plot(all_losses)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [ 257 | "max_length = 100\n", 258 | "\n", 259 | "# Sample from a category and starting letter\n", 260 | "def sample(start_letter='\\n'):\n", 261 | " output_name = \"\"\n", 262 | " input, _, line = randomTrainingExample()\n", 263 | " output_name += line\n", 264 | " \n", 265 | " for i in range(max_length):\n", 266 | " output = lstm(input)\n", 267 | " output = output.detach().cpu().numpy()\n", 268 | " topi = [np.random.choice(n_letters, p=np.exp(l)) for l in output]\n", 269 | " letters = \"\".join([index_to_letter[indx] for indx in topi])\n", 270 | " output_name += letters\n", 271 | " input = inputTensor(output_name[-100:])\n", 272 | "\n", 273 | " return output_name" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": null, 279 | "metadata": {}, 280 | "outputs": [], 281 | "source": [ 282 | "sample()" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [] 291 | } 292 | ], 293 | "metadata": { 294 | "kernelspec": { 295 | "display_name": "Python 3", 296 | "language": "python", 297 | "name": "python3" 298 | }, 299 | "language_info": { 300 | "codemirror_mode": { 301 | "name": "ipython", 302 | "version": 3 303 | }, 304 | "file_extension": ".py", 305 | "mimetype": "text/x-python", 306 | "name": "python", 307 | "nbconvert_exporter": "python", 308 | "pygments_lexer": "ipython3", 309 | "version": "3.5.3" 310 | } 311 | }, 312 | "nbformat": 4, 313 | "nbformat_minor": 2 314 | } 315 | -------------------------------------------------------------------------------- /DQN-Cart-Simple.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import gym\n", 10 | "import math\n", 11 | "import random\n", 12 | "import numpy as np\n", 13 | "import matplotlib\n", 14 | "import matplotlib.pyplot as plt\n", 15 | "from collections import namedtuple\n", 16 | "from itertools import count\n", 17 | "from PIL import Image\n", 18 | "\n", 19 | "import torch\n", 20 | "import torch.nn as nn\n", 21 | "import torch.optim as optim\n", 22 | "import torch.nn.functional as F\n", 23 | "import torchvision.transforms as T\n", 24 | "import models\n", 25 | "from comet_ml import Experiment\n", 26 | "%matplotlib inline" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "experiment = Experiment(api_key=\"dZm2UV8sODS5eDYysEf8TzKNu\", project_name=\"cart\")" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "#env = gym.make(\"Pong-v0\")\n", 45 | "env = gym.make('CartPole-v0')" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "## Replay Memory\n", 53 | "\n", 54 | "Store transitions that the agent observes so can be re-used later. By sampling from this randomly improves stability" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "Transition = namedtuple('Transition',\n", 64 | " ('state', 'action', 'next_state', 'reward'))\n", 65 | "\n", 66 | "class ReplayMemory(object):\n", 67 | " \n", 68 | " def __init__(self, capacity):\n", 69 | " self.capacity = capacity\n", 70 | " self.memory = []\n", 71 | " self.position = 0\n", 72 | " \n", 73 | " def push(self, *args):\n", 74 | " if len(self.memory) < self.capacity:\n", 75 | " self.memory.append(None)\n", 76 | " self.memory[self.position] = Transition(*args)\n", 77 | " self.position = (self.position + 1) % self.capacity\n", 78 | " \n", 79 | " def sample(self, batch_size):\n", 80 | " return random.sample(self.memory, batch_size)\n", 81 | " \n", 82 | " def __len__(self):\n", 83 | " return len(self.memory)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "BATCH_SIZE = 32\n", 93 | "GAMMA = 0.99\n", 94 | "EPS_START = 0.0\n", 95 | "EPS_END = 0.0\n", 96 | "EPS_DECAY = 1000000 + 1000\n", 97 | "TARGET_UPDATE = 10000\n", 98 | "n_frames_min = 2000000\n", 99 | "SAVE_EVERY = 250\n", 100 | "REPLAY_SIZE = 100000\n", 101 | "LR = 1e-4\n", 102 | "BURN_IN = 1000\n", 103 | "RENDER = True\n", 104 | "model_save_name = \"./models/cart_dqn_simple_3.state\"\n", 105 | "start_from_trained = True\n", 106 | "trained_model = \"./models/cart_dqn_simple_2.state\"\n", 107 | "N_ACTIONS = 2\n", 108 | "\n", 109 | "experiment.log_parameter(\"batch size\", BATCH_SIZE)\n", 110 | "experiment.log_parameter(\"gamma\", GAMMA)\n", 111 | "experiment.log_parameter(\"eps start\", EPS_START)\n", 112 | "experiment.log_parameter(\"eps end\", EPS_END)\n", 113 | "experiment.log_parameter(\"eps decay\", EPS_DECAY)\n", 114 | "experiment.log_parameter(\"target update\", TARGET_UPDATE)\n", 115 | "experiment.log_parameter(\"min frames\", n_frames_min)\n", 116 | "experiment.log_parameter(\"start trained\", start_from_trained)\n", 117 | "experiment.log_parameter(\"replay size\", REPLAY_SIZE)\n", 118 | "experiment.log_parameter(\"learning rate\", LR)\n", 119 | "experiment.log_parameter(\"burn in\", BURN_IN)\n", 120 | "\n", 121 | "\n", 122 | "# the policy network is used to play the game - aka actor\n", 123 | "policy_net = models.NNPolicy().cuda()\n", 124 | "if start_from_trained:\n", 125 | " policy_net.load_state_dict(torch.load(trained_model))\n", 126 | "\n", 127 | "# the target net is used to predict Q values for next action\n", 128 | "# we need 2 otherwise we would be using the same network\n", 129 | "# in the actual and predicted values of our loss function\n", 130 | "target_net = models.NNPolicy().cuda()\n", 131 | "target_net.load_state_dict(policy_net.state_dict())\n", 132 | "# sets training to false \n", 133 | "target_net.eval()\n", 134 | "\n", 135 | "optimizer = optim.Adam(policy_net.parameters(), lr=LR)\n", 136 | "memory = ReplayMemory(REPLAY_SIZE)\n", 137 | "\n", 138 | "steps_done = 0\n", 139 | "\n", 140 | "def select_action(state):\n", 141 | " global steps_done\n", 142 | " # gen random number\n", 143 | " sample = random.random()\n", 144 | " # get threshold which decays from start to end \n", 145 | " eps_threshold = EPS_END + (EPS_START - EPS_END) * \\\n", 146 | " math.exp(-1. * steps_done / EPS_DECAY)\n", 147 | " steps_done += 1\n", 148 | " # if exceed, pick best\n", 149 | " if sample > eps_threshold:\n", 150 | " # No gradients b/c not learning, just getting best one\n", 151 | " with torch.no_grad():\n", 152 | " return policy_net(state).max(1)[1].view(1,1), eps_threshold\n", 153 | " # else, random\n", 154 | " else:\n", 155 | " return torch.tensor([[random.randrange(2)]], device='cuda', \n", 156 | " dtype=torch.long), eps_threshold " 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "def optimize_model():\n", 166 | " if len(memory) < BATCH_SIZE or len(memory) < BURN_IN:\n", 167 | " return None\n", 168 | " \n", 169 | " transitions = memory.sample(BATCH_SIZE)\n", 170 | " # transpose the batch...\n", 171 | " batch = Transition(*zip(*transitions))\n", 172 | " # compute mask of transitions which didn't lead to ending game\n", 173 | " non_final_mask = torch.tensor(tuple(map(lambda s: s is not None,\n", 174 | " batch.next_state)), device=\"cuda\", dtype=torch.uint8)\n", 175 | " non_final_next_states = torch.cat([s for s in batch.next_state\n", 176 | " if s is not None])\n", 177 | " state_batch = torch.cat(batch.state)\n", 178 | " action_batch = torch.cat(batch.action)\n", 179 | " reward_batch = torch.cat(batch.reward)\n", 180 | " \n", 181 | " # calculate the Q value of taking the state, action pairs which were taken\n", 182 | " # the gather basically takes the Q value for the action choosen\n", 183 | " # So if my input choose action 3 for the given state, that is what I would gather.\n", 184 | " # Basically, what is the Q value for what actually happened\n", 185 | " # Q value being the total expected value from taking an action given a state.\n", 186 | " # These are basically our predictions for learning\n", 187 | " state_action_values = policy_net(state_batch).gather(1, action_batch)\n", 188 | " \n", 189 | " next_state_values = torch.zeros(BATCH_SIZE).cuda()\n", 190 | " # get the best actions for the next states\n", 191 | " # detach is for speed so don't calc gradients\n", 192 | " target_net_results = target_net(non_final_next_states).max(1)[0].detach()\n", 193 | " next_state_values[non_final_mask] = target_net_results\n", 194 | " # the expected value of the Q(s,a) given from the policy net is the\n", 195 | " # reward given plus the discounted value of the Q value from taking the best\n", 196 | " # action at the next step\n", 197 | " expected_state_action_values = (next_state_values * GAMMA) + reward_batch\n", 198 | " \n", 199 | " loss = F.smooth_l1_loss(state_action_values, \n", 200 | " expected_state_action_values.unsqueeze(1))\n", 201 | " \n", 202 | " # Optimize the policy net to become better and predicting Q values\n", 203 | " optimizer.zero_grad()\n", 204 | " loss.backward()\n", 205 | " optimizer.step()" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": { 212 | "scrolled": true 213 | }, 214 | "outputs": [], 215 | "source": [ 216 | "running_reward = None\n", 217 | "reward_sum = 0\n", 218 | "n_frames = 0\n", 219 | "i_episode = 0\n", 220 | "while n_frames < n_frames_min:\n", 221 | " obs = env.reset()\n", 222 | " state = torch.from_numpy(obs).float().unsqueeze(0).cuda()\n", 223 | " losses = []\n", 224 | " for t in count():\n", 225 | " \n", 226 | " if RENDER:\n", 227 | " env.render()\n", 228 | " action, eps_threshold = select_action(state)\n", 229 | " obs, reward, done, _ = env.step(action.item())\n", 230 | " reward_sum += reward\n", 231 | " reward = torch.tensor([reward])\n", 232 | " reward = reward.cuda()\n", 233 | "\n", 234 | " n_frames += 1\n", 235 | " if n_frames % TARGET_UPDATE == 0:\n", 236 | " print(\"Frame: {}\".format(n_frames))\n", 237 | " target_net.load_state_dict(policy_net.state_dict())\n", 238 | " \n", 239 | " if not done:\n", 240 | " next_state = torch.from_numpy(obs).float().unsqueeze(0).cuda()\n", 241 | " else:\n", 242 | " next_state = None\n", 243 | " \n", 244 | " memory.push(state, action, next_state, reward)\n", 245 | " state = next_state\n", 246 | " optimize_model()\n", 247 | " if done:\n", 248 | " running_reward = reward_sum if running_reward is None else running_reward * 0.99 + reward_sum * 0.01\n", 249 | " print(reward_sum)\n", 250 | " experiment.log_metric(\"reward sum\", reward_sum, step=i_episode)\n", 251 | " reward_sum = 0\n", 252 | " experiment.log_metric(\"reward mean\", running_reward, step=i_episode)\n", 253 | " experiment.log_metric(\"eps\", eps_threshold, step=i_episode)\n", 254 | " i_episode += 1\n", 255 | " break\n", 256 | "\n", 257 | " if i_episode % SAVE_EVERY == 0:\n", 258 | " print(\"Episode: {}\".format(i_episode))\n", 259 | " print(\"Saving Model...\")\n", 260 | " torch.save(policy_net.state_dict(), model_save_name)" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | "torch.save(policy_net.state_dict(), model_save_name)" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [] 278 | } 279 | ], 280 | "metadata": { 281 | "kernelspec": { 282 | "display_name": "Python 3", 283 | "language": "python", 284 | "name": "python3" 285 | }, 286 | "language_info": { 287 | "codemirror_mode": { 288 | "name": "ipython", 289 | "version": 3 290 | }, 291 | "file_extension": ".py", 292 | "mimetype": "text/x-python", 293 | "name": "python", 294 | "nbconvert_exporter": "python", 295 | "pygments_lexer": "ipython3", 296 | "version": "3.5.3" 297 | } 298 | }, 299 | "nbformat": 4, 300 | "nbformat_minor": 2 301 | } 302 | -------------------------------------------------------------------------------- /GAN-Copy1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import os\n", 10 | "import torch\n", 11 | "import torchvision\n", 12 | "import torch.nn as nn\n", 13 | "from torchvision import transforms\n", 14 | "from torchvision.utils import save_image\n", 15 | "import torchvision.datasets as dset\n", 16 | "import torch.optim as optim\n", 17 | "from torch.autograd import Variable\n", 18 | "import torchvision.utils as vutils\n", 19 | "import torch.nn.functional as F\n", 20 | "\n", 21 | "\n", 22 | "# Device configuration\n", 23 | "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "image_size = 64\n", 33 | "num_epochs = 20\n", 34 | "batch_size = 128" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 3, 40 | "metadata": { 41 | "code_folding": [] 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "transform = transforms.Compose([transforms.Resize((image_size, image_size)), \n", 46 | " transforms.ToTensor(), \n", 47 | " transforms.Normalize((0.5, 0.5, 0.5), \n", 48 | " (0.5, 0.5, 0.5)),])\n", 49 | "dataset = dset.ImageFolder(\"/home/tyler/data/image/celebs/\", \n", 50 | " transform)\n", 51 | "#dataset = dset.CIFAR10(root = '/home/tyler/data/image', \n", 52 | "# download = True, \n", 53 | "# transform = transform)\n", 54 | "dataloader = torch.utils.data.DataLoader(dataset, \n", 55 | " batch_size = batch_size, \n", 56 | " shuffle = True, \n", 57 | " num_workers = 2)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 4, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "## weight initalization for network\n", 67 | "\n", 68 | "def weights_init(m):\n", 69 | " classname = m.__class__.__name__\n", 70 | " if classname.find('Conv') != -1:\n", 71 | " m.weight.data.normal_(0.0, 0.02)\n", 72 | " elif classname.find('BatchNorm') != -1:\n", 73 | " m.weight.data.normal_(1.0, 0.02)\n", 74 | " m.bias.data.fill_(0)\n", 75 | " \n", 76 | "def normal_init(m, mean, std):\n", 77 | " if isinstance(m, nn.ConvTranspose2d) or isinstance(m, nn.Conv2d):\n", 78 | " m.weight.data.normal_(mean, std)\n", 79 | " m.bias.data.zero_()" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "### Generator\n", 87 | "\n", 88 | "Produces an image\n", 89 | "\n", 90 | "Q: why the convtranspose2d numbers? A: they are the inverse of discriminator\n", 91 | "\n", 92 | "Q: Why first convtranspose2d in channels is 100? This is just a hyper-parameter. You can choose how many channels you want to randomly generate for the input to your generator." 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 5, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "class Generator(nn.Module):\n", 102 | " # initializers\n", 103 | " def __init__(self, d=128):\n", 104 | " super(Generator, self).__init__()\n", 105 | " self.deconv1 = nn.ConvTranspose2d(100, d*8, 4, 1, 0)\n", 106 | " self.deconv1_bn = nn.BatchNorm2d(d*8)\n", 107 | " self.deconv2 = nn.ConvTranspose2d(d*8, d*4, 4, 2, 1)\n", 108 | " self.deconv2_bn = nn.BatchNorm2d(d*4)\n", 109 | " self.deconv3 = nn.ConvTranspose2d(d*4, d*2, 4, 2, 1)\n", 110 | " self.deconv3_bn = nn.BatchNorm2d(d*2)\n", 111 | " self.deconv4 = nn.ConvTranspose2d(d*2, d, 4, 2, 1)\n", 112 | " self.deconv4_bn = nn.BatchNorm2d(d)\n", 113 | " self.deconv5 = nn.ConvTranspose2d(d, 3, 4, 2, 1)\n", 114 | "\n", 115 | " # weight_init\n", 116 | " def weight_init(self, mean, std):\n", 117 | " for m in self._modules:\n", 118 | " normal_init(self._modules[m], mean, std)\n", 119 | "\n", 120 | " # forward method\n", 121 | " def forward(self, input):\n", 122 | " # x = F.relu(self.deconv1(input))\n", 123 | " x = F.relu(self.deconv1_bn(self.deconv1(input)))\n", 124 | " x = F.relu(self.deconv2_bn(self.deconv2(x)))\n", 125 | " x = F.relu(self.deconv3_bn(self.deconv3(x)))\n", 126 | " x = F.relu(self.deconv4_bn(self.deconv4(x)))\n", 127 | " x = F.tanh(self.deconv5(x))\n", 128 | "\n", 129 | " return x" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "## Discriminator\n", 137 | "\n", 138 | "Just a normal conv net to tell if an image is fake or not." 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 6, 144 | "metadata": { 145 | "code_folding": [] 146 | }, 147 | "outputs": [], 148 | "source": [ 149 | "class Discriminator(nn.Module):\n", 150 | " # initializers\n", 151 | " def __init__(self, d=128):\n", 152 | " super(Discriminator, self).__init__()\n", 153 | " self.conv1 = nn.Conv2d(3, d, 4, 2, 1)\n", 154 | " self.conv2 = nn.Conv2d(d, d*2, 4, 2, 1)\n", 155 | " self.conv2_bn = nn.BatchNorm2d(d*2)\n", 156 | " self.conv3 = nn.Conv2d(d*2, d*4, 4, 2, 1)\n", 157 | " self.conv3_bn = nn.BatchNorm2d(d*4)\n", 158 | " self.conv4 = nn.Conv2d(d*4, d*8, 4, 2, 1)\n", 159 | " self.conv4_bn = nn.BatchNorm2d(d*8)\n", 160 | " self.conv5 = nn.Conv2d(d*8, 1, 4, 1, 0)\n", 161 | "\n", 162 | " # weight_init\n", 163 | " def weight_init(self, mean, std):\n", 164 | " for m in self._modules:\n", 165 | " normal_init(self._modules[m], mean, std)\n", 166 | "\n", 167 | " # forward method\n", 168 | " def forward(self, input):\n", 169 | " x = F.leaky_relu(self.conv1(input), 0.2)\n", 170 | " x = F.leaky_relu(self.conv2_bn(self.conv2(x)), 0.2)\n", 171 | " x = F.leaky_relu(self.conv3_bn(self.conv3(x)), 0.2)\n", 172 | " x = F.leaky_relu(self.conv4_bn(self.conv4(x)), 0.2)\n", 173 | " x = F.sigmoid(self.conv5(x))\n", 174 | "\n", 175 | " return x" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 7, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "G = Generator(128).to(device)\n", 185 | "G.weight_init(0.0, 0.2)\n", 186 | "D = Discriminator(128).to(device)\n", 187 | "D.weight_init(0.0, 0.2)" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "BCE_loss = nn.BCELoss()\n", 197 | "D_optimizer = optim.Adam(D.parameters(), lr = 0.0002, betas = (0.5, 0.999))\n", 198 | "G_optimizer = optim.Adam(G.parameters(), lr = 0.0002, betas = (0.5, 0.999))" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "name": "stdout", 208 | "output_type": "stream", 209 | "text": [ 210 | "Epoch [0/20], Step [200/1583], d_loss: 0.2243, g_loss: 26.2719\n" 211 | ] 212 | } 213 | ], 214 | "source": [ 215 | "total_step = len(dataloader)\n", 216 | "for epoch in range(num_epochs):\n", 217 | " for i, data in enumerate(dataloader):\n", 218 | " \n", 219 | " x_, _ = data\n", 220 | " \n", 221 | " D.zero_grad()\n", 222 | " \n", 223 | " mini_batch = x_.size()[0]\n", 224 | "\n", 225 | " y_real_ = torch.ones(mini_batch)\n", 226 | " y_fake_ = torch.zeros(mini_batch)\n", 227 | "\n", 228 | " x_, y_real_, y_fake_ = Variable(x_.cuda()), Variable(y_real_.cuda()), Variable(y_fake_.cuda())\n", 229 | " D_result = D(x_).squeeze()\n", 230 | " D_real_loss = BCE_loss(D_result, y_real_)\n", 231 | "\n", 232 | " z_ = torch.randn((mini_batch, 100)).view(-1, 100, 1, 1)\n", 233 | " z_ = Variable(z_.cuda())\n", 234 | " G_result = G(z_)\n", 235 | "\n", 236 | " D_result = D(G_result).squeeze()\n", 237 | " D_fake_loss = BCE_loss(D_result, y_fake_)\n", 238 | " D_fake_score = D_result.data.mean()\n", 239 | "\n", 240 | " D_train_loss = D_real_loss + D_fake_loss\n", 241 | "\n", 242 | " D_train_loss.backward()\n", 243 | " D_optimizer.step()\n", 244 | "\n", 245 | " # train generator G\n", 246 | " G.zero_grad()\n", 247 | "\n", 248 | " z_ = torch.randn((mini_batch, 100)).view(-1, 100, 1, 1)\n", 249 | " z_ = Variable(z_.cuda())\n", 250 | "\n", 251 | " G_result = G(z_)\n", 252 | " D_result = D(G_result).squeeze()\n", 253 | " G_train_loss = BCE_loss(D_result, y_real_)\n", 254 | " G_train_loss.backward()\n", 255 | " G_optimizer.step()\n", 256 | "\n", 257 | " \n", 258 | " if (i+1) % 200 == 0:\n", 259 | " print('Epoch [{}/{}], Step [{}/{}], d_loss: {:.4f}, g_loss: {:.4f}' \n", 260 | " .format(epoch, num_epochs, i+1, total_step, D_train_loss.item(), G_train_loss.item()))\n", 261 | " vutils.save_image(x_, '%s/real_samples.png' % \"./results\", normalize = True)\n", 262 | " vutils.save_image(G_result.data, '%s/fake_samples_epoch_%03d.png' % (\"./results\", epoch), normalize = True)" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "metadata": {}, 269 | "outputs": [], 270 | "source": [] 271 | } 272 | ], 273 | "metadata": { 274 | "kernelspec": { 275 | "display_name": "Python 3", 276 | "language": "python", 277 | "name": "python3" 278 | }, 279 | "language_info": { 280 | "codemirror_mode": { 281 | "name": "ipython", 282 | "version": 3 283 | }, 284 | "file_extension": ".py", 285 | "mimetype": "text/x-python", 286 | "name": "python", 287 | "nbconvert_exporter": "python", 288 | "pygments_lexer": "ipython3", 289 | "version": "3.6.1" 290 | } 291 | }, 292 | "nbformat": 4, 293 | "nbformat_minor": 2 294 | } 295 | -------------------------------------------------------------------------------- /Neural_Conversational_Model.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.6.6" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 2 32 | } 33 | -------------------------------------------------------------------------------- /Neural_Style.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from __future__ import print_function\n", 10 | "\n", 11 | "import torch\n", 12 | "import torch.nn as nn\n", 13 | "import torch.nn.functional as F\n", 14 | "import torch.optim as optim\n", 15 | "\n", 16 | "from PIL import Image\n", 17 | "import matplotlib.pyplot as plt\n", 18 | "\n", 19 | "import torchvision.transforms as transforms\n", 20 | "import torchvision.models as models\n", 21 | "\n", 22 | "import copy" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [] 31 | } 32 | ], 33 | "metadata": { 34 | "kernelspec": { 35 | "display_name": "Python 3", 36 | "language": "python", 37 | "name": "python3" 38 | }, 39 | "language_info": { 40 | "codemirror_mode": { 41 | "name": "ipython", 42 | "version": 3 43 | }, 44 | "file_extension": ".py", 45 | "mimetype": "text/x-python", 46 | "name": "python", 47 | "nbconvert_exporter": "python", 48 | "pygments_lexer": "ipython3", 49 | "version": "3.5.3" 50 | } 51 | }, 52 | "nbformat": 4, 53 | "nbformat_minor": 2 54 | } 55 | -------------------------------------------------------------------------------- /Policy_Gradients_Pong.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import gym\n", 10 | "from torch.autograd import Variable\n", 11 | "import torch.nn as nn\n", 12 | "import torch.nn.functional as F\n", 13 | "import torch.optim as optim\n", 14 | "import numpy as np\n", 15 | "import torch\n", 16 | "from torch.distributions import Categorical\n", 17 | "from comet_ml import Experiment\n", 18 | "%matplotlib inline\n", 19 | "import matplotlib.pyplot as plt\n", 20 | "import models\n", 21 | "from itertools import count\n", 22 | "import time" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "hyper_dict = {\"LEARNING_RATE\": 1e-5,\n", 32 | " \"GAMMA\": 0.99,\n", 33 | " \"N_UPDATES\": 100000,\n", 34 | " \"GAMES_PER_UPDATE\": 3,\n", 35 | " \"SAVE_EVERY_UPATE\": 25,\n", 36 | " \"SAVE_FILE\": \"./models/pong_policy_9.state\",\n", 37 | " \"RESUME_FILE\": \"./models/pong_policy_8.state\",\n", 38 | " \"RESUME\": True,\n", 39 | " \"RENDER\": True,\n", 40 | " \"SLOW_DOWN\": 0.02,\n", 41 | " \"PROJECT_NAME\": \"pong-policy\",\n", 42 | " \"ENV\": \"Pong-v0\"}\n", 43 | "\n", 44 | "env = gym.make(hyper_dict['ENV'])\n", 45 | "\n", 46 | "nnpolicy = models.ConvNet(env.action_space.n).cuda()\n", 47 | "if hyper_dict[\"RESUME\"]:\n", 48 | " nnpolicy.load_state_dict(torch.load(hyper_dict[\"RESUME_FILE\"]))\n", 49 | "\n", 50 | "def prepro(I):\n", 51 | " I = I[35:195] # crop\n", 52 | " I = I[::2,::2,0] # downsample by factor of 2\n", 53 | " I[I == 144] = 0 # erase background (background type 1)\n", 54 | " I[I == 109] = 0 # erase background (background type 2)\n", 55 | " I[I != 0] = 1 # everything else (paddles, ball) just set to 1\n", 56 | " return np.expand_dims(I.astype(np.float), axis=0)\n", 57 | "\n", 58 | "\n", 59 | "def get_outputs(obs):\n", 60 | " return nnpolicy(Variable(torch.from_numpy(obs).float().unsqueeze(0).cuda()))" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 3, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stderr", 70 | "output_type": "stream", 71 | "text": [ 72 | "COMET WARNING: We detected that you are running inside a Ipython/Jupyter notebook environment but we cannot save your notebook source code. Please be sure to have installed comet_ml as a notebook server extension by running:\n", 73 | "jupyter comet_ml enable\n", 74 | "COMET INFO: old comet version (1.0.9) detected. current: 1.0.13 please update your comet lib with command: `pip install --no-cache-dir --upgrade comet_ml`\n", 75 | "COMET WARNING: Comet.ml support for Ipython Notebook is limited at the moment, automatic monitoring and stdout capturing is deactivated\n", 76 | "COMET INFO: Experiment is live on comet.ml https://www.comet.ml/syrios/pong-policy/0c6d2a29788e423ba12a52609533f5a0\n", 77 | "\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "experiment = Experiment(api_key=\"dZm2UV8sODS5eDYysEf8TzKNu\", \n", 83 | " project_name=hyper_dict['PROJECT_NAME'])\n", 84 | "experiment.log_multiple_params(hyper_dict)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 4, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "def discount_rewards(rewards, discount_rate):\n", 94 | " \"\"\"\n", 95 | " Discount a list of rewards by GAMMA\n", 96 | " Where now the last value in the list is just the reward for that value\n", 97 | " Since no more moves happened\n", 98 | " And the first gets added upon by the future discounted\n", 99 | " Thus have the discounted reward value for each action during the game\n", 100 | " \"\"\"\n", 101 | " discounted_rewards = np.empty(len(rewards))\n", 102 | " cumulative_rewards = 0\n", 103 | " for step in reversed(range(len(rewards))):\n", 104 | " cumulative_rewards = rewards[step] + discount_rate * cumulative_rewards\n", 105 | " discounted_rewards[step] = cumulative_rewards\n", 106 | " return discounted_rewards\n", 107 | "\n", 108 | "def discount_and_normalize_rewards(rewards, discount_rate):\n", 109 | " \"\"\"\n", 110 | " Discount a list of list of rewards and normalize\n", 111 | " \"\"\"\n", 112 | " all_discounted_rewards = [discount_rewards(reward, discount_rate) \n", 113 | " for reward in rewards]\n", 114 | " flat_rewards = np.concatenate(all_discounted_rewards)\n", 115 | " reward_mean = flat_rewards.mean()\n", 116 | " reward_std = flat_rewards.std()\n", 117 | " return [(discounted_rewards - reward_mean)/(reward_std + np.finfo(np.float32).eps)\n", 118 | " for discounted_rewards in all_discounted_rewards]\n", 119 | "\n", 120 | "def update_model(all_rewards, all_gradients, discount_rate):\n", 121 | " \"\"\"\n", 122 | " Get discounted and normalized rewards for each move from each game\n", 123 | " Loop over every game and take the sum of r * -1 * gradient to understand\n", 124 | " How each gradient should be updated based on that game\n", 125 | " Then average over your batch\n", 126 | " \"\"\"\n", 127 | " # get discounted rewards -> discount rewards which happened \n", 128 | " # later more and normalize\n", 129 | " loss = []\n", 130 | " all_rewards = discount_and_normalize_rewards(all_rewards,discount_rate)\n", 131 | " # for every game played, sum up the total losses where you take the loss \n", 132 | " # (*-1 since log_prob is ASCENT)\n", 133 | " # and multiply by the discounted, normalized reward. Thus, actions that lead \n", 134 | " # to good rewards are applied\n", 135 | " # And actions that lead to be rewards (negative) are inversely applied.\n", 136 | " # Thus, that sum tells you how much to update based on that game\n", 137 | " for step in range(len(all_rewards)):\n", 138 | " r = torch.Tensor(all_rewards[step]).cuda()\n", 139 | " step_loss = []\n", 140 | " for value in range(len(all_rewards[step])):\n", 141 | " step_loss.append(r[value] * all_gradients[step][value] * -1)\n", 142 | " loss.append(sum(step_loss))\n", 143 | " loss = torch.cat(loss)\n", 144 | " optimizer.zero_grad()\n", 145 | " # Take the mean of all games losses to help smooth out the learning (mini-batch)\n", 146 | " policy_loss = loss.mean()\n", 147 | " policy_loss.backward()\n", 148 | " optimizer.step()\n", 149 | " \n", 150 | " \n", 151 | "def getGradients(n_iters, n_games_per_gradient, discount_rate):\n", 152 | " \"\"\"\n", 153 | " For each n_iters:\n", 154 | " Play n_games_per_gradients and use the policy to probabilistically\n", 155 | " Move based output probabilities\n", 156 | " Save gradients and rewards\n", 157 | " After playing the batch of games, update gradients\n", 158 | " \n", 159 | " \"\"\"\n", 160 | " running_reward = None\n", 161 | " reward_sum = 0\n", 162 | " for iteration in range(n_iters):\n", 163 | " all_rewards = []\n", 164 | " all_gradients = []\n", 165 | " for game in range(n_games_per_gradient):\n", 166 | " current_rewards = []\n", 167 | " current_gradients = []\n", 168 | "\n", 169 | " obs = env.reset()\n", 170 | " prev_x = None\n", 171 | " for step in count():\n", 172 | " if hyper_dict['RENDER']:\n", 173 | " env.render()\n", 174 | " # get the predicted action probabilities from our nn policy\n", 175 | " cur_x = prepro(obs)\n", 176 | " x = cur_x - prev_x if prev_x is not None else np.zeros((1,80,80))\n", 177 | " prev_x = cur_x\n", 178 | " outputs = get_outputs(x)\n", 179 | " #select an action with these probabilities\n", 180 | " categorical_distribution = Categorical(outputs)\n", 181 | " seleted_action = categorical_distribution.sample()\n", 182 | " #save the loss function\n", 183 | " current_gradients.append(\n", 184 | " categorical_distribution.log_prob(seleted_action))\n", 185 | " #apply the action\n", 186 | " action = seleted_action.item()\n", 187 | " #save the reward\n", 188 | " obs, reward, done, _ = env.step(action)\n", 189 | " reward_sum += reward\n", 190 | " current_rewards.append(reward)\n", 191 | " if hyper_dict[\"SLOW_DOWN\"] is not None:\n", 192 | " time.sleep(hyper_dict[\"SLOW_DOWN\"]) \n", 193 | " if done:\n", 194 | " running_reward = reward_sum if running_reward is None \\\n", 195 | " else running_reward * 0.99 + reward_sum * 0.01\n", 196 | " experiment.log_metric(\"reward sum\", reward_sum, \n", 197 | " step=game + iteration * n_games_per_gradient)\n", 198 | " reward_sum = 0\n", 199 | " experiment.log_metric(\"reward mean\", \n", 200 | " running_reward,\n", 201 | " step=game + iteration * n_games_per_gradient)\n", 202 | " break\n", 203 | " all_rewards.append(current_rewards)\n", 204 | " all_gradients.append(current_gradients)\n", 205 | " # apply saved loss functions\n", 206 | " update_model(all_rewards, all_gradients, discount_rate)\n", 207 | " if iteration % hyper_dict[\"SAVE_EVERY_UPATE\"] == 0:\n", 208 | " print(\"Iteration: {}\".format(iteration))\n", 209 | " print(\"Saving Model...\")\n", 210 | " torch.save(nnpolicy.state_dict(), hyper_dict[\"SAVE_FILE\"])\n" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": {}, 217 | "outputs": [], 218 | "source": [ 219 | "optimizer = optim.Adam(nnpolicy.parameters(), lr=hyper_dict['LEARNING_RATE'])" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": null, 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "getGradients(n_iters = hyper_dict['N_UPDATES'],\n", 229 | " n_games_per_gradient = hyper_dict['GAMES_PER_UPDATE'],\n", 230 | " discount_rate = hyper_dict['GAMMA'])" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "metadata": {}, 237 | "outputs": [], 238 | "source": [] 239 | } 240 | ], 241 | "metadata": { 242 | "kernelspec": { 243 | "display_name": "Python 3", 244 | "language": "python", 245 | "name": "python3" 246 | }, 247 | "language_info": { 248 | "codemirror_mode": { 249 | "name": "ipython", 250 | "version": 3 251 | }, 252 | "file_extension": ".py", 253 | "mimetype": "text/x-python", 254 | "name": "python", 255 | "nbconvert_exporter": "python", 256 | "pygments_lexer": "ipython3", 257 | "version": "3.6.1" 258 | } 259 | }, 260 | "nbformat": 4, 261 | "nbformat_minor": 2 262 | } 263 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Next Ideas: 2 | 3. Image Captioning 3 | 1. Chat bots 4 | https://arxiv.org/pdf/1510.03055.pdf 5 | https://arxiv.org/pdf/1606.01541.pdf 6 | http://papers.nips.cc/paper/5346-sequence-to-sequence-learning-with-neural-networks.pdf 7 | 8 | -------------------------------------------------------------------------------- /Run_DQN.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import gym\n", 10 | "import math\n", 11 | "import random\n", 12 | "import numpy as np\n", 13 | "import matplotlib\n", 14 | "import matplotlib.pyplot as plt\n", 15 | "from collections import namedtuple\n", 16 | "from itertools import count\n", 17 | "from PIL import Image\n", 18 | "\n", 19 | "import torch\n", 20 | "import torch.nn as nn\n", 21 | "import torch.optim as optim\n", 22 | "import torch.nn.functional as F\n", 23 | "import torchvision.transforms as T\n", 24 | "import models\n", 25 | "%matplotlib inline" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "env = gym.make('CartPole-v0').unwrapped" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "resize = T.Compose([T.ToPILImage(),\n", 44 | " T.Resize(40, interpolation=Image.CUBIC),\n", 45 | " T.ToTensor()])\n", 46 | "\n", 47 | "# This is based on the code from gym.\n", 48 | "screen_width = 600\n", 49 | "\n", 50 | "\n", 51 | "def get_cart_location():\n", 52 | " world_width = env.x_threshold * 2\n", 53 | " scale = screen_width / world_width\n", 54 | " return int(env.state[0] * scale + screen_width / 2.0) # MIDDLE OF CART\n", 55 | "\n", 56 | "\n", 57 | "def get_screen():\n", 58 | " screen = env.render(mode='rgb_array').transpose(\n", 59 | " (2, 0, 1)) # transpose into torch order (CHW)\n", 60 | " # Strip off the top and bottom of the screen\n", 61 | " screen = screen[:, 160:320]\n", 62 | " view_width = 320\n", 63 | " cart_location = get_cart_location()\n", 64 | " if cart_location < view_width // 2:\n", 65 | " slice_range = slice(view_width)\n", 66 | " elif cart_location > (screen_width - view_width // 2):\n", 67 | " slice_range = slice(-view_width, None)\n", 68 | " else:\n", 69 | " slice_range = slice(cart_location - view_width // 2,\n", 70 | " cart_location + view_width // 2)\n", 71 | " # Strip off the edges, so that we have a square image centered on a cart\n", 72 | " screen = screen[:, :, slice_range]\n", 73 | " # Convert to float, rescare, convert to torch tensor\n", 74 | " # (this doesn't require a copy)\n", 75 | " screen = np.ascontiguousarray(screen, dtype=np.float32) / 255\n", 76 | " screen = torch.from_numpy(screen)\n", 77 | " # Resize, and add a batch dimension (BCHW)\n", 78 | " return resize(screen).unsqueeze(0).cuda()\n", 79 | "\n", 80 | "def select_action(state):\n", 81 | " return policy_net(state).max(1)[1].view(1,1)\n" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "N_ACTIONS = 2\n", 91 | "policy_net = models.DQN().cuda()\n", 92 | "policy_net.load_state_dict(torch.load(\"./models/cart_dqn_policy5.state\"))" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "metadata": { 99 | "scrolled": true 100 | }, 101 | "outputs": [], 102 | "source": [ 103 | "num_episodes = 10\n", 104 | "for i_episode in range(num_episodes):\n", 105 | " env.reset()\n", 106 | " last_screen = get_screen()\n", 107 | " current_screen = get_screen()\n", 108 | " state = current_screen - last_screen\n", 109 | " prev_state = None\n", 110 | " \n", 111 | " for t in count():\n", 112 | " action = select_action(state)\n", 113 | " _, reward, done, _ = env.step(action.item())\n", 114 | "\n", 115 | " reward = torch.tensor([reward])\n", 116 | " reward = reward.cuda()\n", 117 | " \n", 118 | " last_screen = current_screen\n", 119 | " current_screen = get_screen()\n", 120 | " \n", 121 | " if not done:\n", 122 | " next_state = current_screen - last_screen\n", 123 | " else:\n", 124 | " next_state = None\n", 125 | " state = next_state\n", 126 | "\n", 127 | " if done:\n", 128 | " break" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [] 137 | } 138 | ], 139 | "metadata": { 140 | "kernelspec": { 141 | "display_name": "Python 3", 142 | "language": "python", 143 | "name": "python3" 144 | }, 145 | "language_info": { 146 | "codemirror_mode": { 147 | "name": "ipython", 148 | "version": 3 149 | }, 150 | "file_extension": ".py", 151 | "mimetype": "text/x-python", 152 | "name": "python", 153 | "nbconvert_exporter": "python", 154 | "pygments_lexer": "ipython3", 155 | "version": "3.6.1" 156 | } 157 | }, 158 | "nbformat": 4, 159 | "nbformat_minor": 2 160 | } 161 | -------------------------------------------------------------------------------- /TF_Conv.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import tensorflow as tf\n", 12 | "from tensorflow.examples.tutorials.mnist import input_data" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 3, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\n", 25 | "Extracting MNIST_data/train-images-idx3-ubyte.gz\n", 26 | "Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\n", 27 | "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n", 28 | "Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\n", 29 | "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n", 30 | "Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\n", 31 | "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "mnist = input_data.read_data_sets('MNIST_data', one_hot=True)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 16, 42 | "metadata": { 43 | "collapsed": true 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "sess = tf.InteractiveSession()" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 17, 53 | "metadata": { 54 | "collapsed": true 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "x = tf.placeholder(tf.float32, [None, 784])\n", 59 | "y_ = tf.placeholder(tf.float32, [None, 10])" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 18, 65 | "metadata": { 66 | "collapsed": true 67 | }, 68 | "outputs": [], 69 | "source": [ 70 | "def weight_variable(shape):\n", 71 | " initial = tf.truncated_normal(shape, stddev=0.1)\n", 72 | " return tf.Variable(initial)\n", 73 | "\n", 74 | "def bias_variable(shape):\n", 75 | " initial = tf.constant(0.1, shape=shape)\n", 76 | " return tf.Variable(initial)\n", 77 | "\n", 78 | "def conv2d(x, W):\n", 79 | " return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')\n", 80 | "\n", 81 | "def maxpool(x):\n", 82 | " return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "### Conv Math\n", 90 | "\n", 91 | "#### Same Padding\n", 92 | "\n", 93 | "out_height = ceil(float(in_height) / float(strides[1]))\n", 94 | "\n", 95 | "out_width = ceil(float(in_width) / float(strides[2]))\n", 96 | "\n", 97 | "#### Valid Padding\n", 98 | "\n", 99 | "out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))\n", 100 | "\n", 101 | "out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))\n", 102 | "\n", 103 | "### Pooling Math\n", 104 | "\n", 105 | "Accepts a volume of size W1×H1×D1\n", 106 | "\n", 107 | "Requires two hyperparameters:\n", 108 | "\n", 109 | "their spatial extent F,\n", 110 | "\n", 111 | "the stride S,\n", 112 | "\n", 113 | "Produces a volume of size W2×H2×D2W2×H2×D2 where:\n", 114 | "\n", 115 | "W2=(W1−F)/S+1\n", 116 | "\n", 117 | "H2=(H1−F)/S+1\n", 118 | "\n", 119 | "D2=D1" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 40, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "# stride x, stride y, # channels, # filters\n", 129 | "W_conv1 = weight_variable([5,5,1,32])\n", 130 | "b_conv1 = bias_variable([32])\n", 131 | "\n", 132 | "# batch, height, width, channels\n", 133 | "x_image = tf.reshape(x, [-1, 28, 28, 1])\n", 134 | "\n", 135 | "###first conv layer\n", 136 | "h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)\n", 137 | "h_maxpool1 = maxpool(h_conv1)\n", 138 | "\n", 139 | "W_conv2 = weight_variable([5, 5, 32, 128])\n", 140 | "b_conv2 = bias_variable([128])\n", 141 | "\n", 142 | "###second conv layer\n", 143 | "h_conv2 = tf.nn.relu(conv2d(h_maxpool1, W_conv2) + b_conv2)\n", 144 | "h_maxpool2 = maxpool(h_conv2)\n", 145 | "\n", 146 | "\n", 147 | "###dense layer\n", 148 | "W_dense1 = weight_variable([7*7*128, 512])\n", 149 | "b_dense1 = bias_variable([512])\n", 150 | "\n", 151 | "h_maxpool2_flat = tf.reshape(h_maxpool2, [-1, 7*7*128])\n", 152 | "h_fc1 = tf.nn.relu(tf.matmul(h_maxpool2_flat, W_dense1) + b_dense1)\n", 153 | "\n", 154 | "###dense layer\n", 155 | "W_dense2 = weight_variable([512, 1024])\n", 156 | "b_dense2 = bias_variable([1024])\n", 157 | "\n", 158 | "h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_dense2) + b_dense2)\n", 159 | "\n", 160 | "###final dense layer\n", 161 | "W_fc3 = weight_variable([1024, 10])\n", 162 | "b_fc3 = bias_variable([10])\n", 163 | "\n", 164 | "y_conv = tf.matmul(h_fc2, W_fc3) + b_fc3" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 41, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "step 0, training accuracy 0.14\n", 177 | "test accuracy 0.0942\n", 178 | "test accuracy 0.0657\n", 179 | "step 2, training accuracy 0.06\n", 180 | "test accuracy 0.0922\n", 181 | "test accuracy 0.1283\n", 182 | "step 4, training accuracy 0.18\n", 183 | "test accuracy 0.1243\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "cross_entropy = tf.reduce_mean(\n", 189 | " tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))\n", 190 | "train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)\n", 191 | "correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))\n", 192 | "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", 193 | "\n", 194 | "with tf.Session() as sess:\n", 195 | " sess.run(tf.global_variables_initializer())\n", 196 | " for i in range(5):\n", 197 | " batch = mnist.train.next_batch(50)\n", 198 | " if i % 2 == 0:\n", 199 | " train_accuracy = accuracy.eval(feed_dict={\n", 200 | " x: batch[0], y_: batch[1]})\n", 201 | " print('step %d, training accuracy %g' % (i, train_accuracy))\n", 202 | " train_step.run(feed_dict={x: batch[0], y_: batch[1]})\n", 203 | "\n", 204 | " print('test accuracy %g' % accuracy.eval(feed_dict={\n", 205 | " x: mnist.test.images, y_: mnist.test.labels}))" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": { 212 | "collapsed": true 213 | }, 214 | "outputs": [], 215 | "source": [] 216 | } 217 | ], 218 | "metadata": { 219 | "kernelspec": { 220 | "display_name": "Python 3", 221 | "language": "python", 222 | "name": "python3" 223 | }, 224 | "language_info": { 225 | "codemirror_mode": { 226 | "name": "ipython", 227 | "version": 3 228 | }, 229 | "file_extension": ".py", 230 | "mimetype": "text/x-python", 231 | "name": "python", 232 | "nbconvert_exporter": "python", 233 | "pygments_lexer": "ipython3", 234 | "version": "3.6.1" 235 | } 236 | }, 237 | "nbformat": 4, 238 | "nbformat_minor": 2 239 | } 240 | -------------------------------------------------------------------------------- /Theano RNN.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 42, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from keras.utils.data_utils import get_file\n", 10 | "import numpy as np\n", 11 | "from theano import shared\n", 12 | "import theano.tensor as T\n", 13 | "import math\n", 14 | "import theano\n", 15 | "from itertools import chain\n", 16 | "from collections import OrderedDict" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 3, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "corpus length: 600893\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "path = get_file('nietzsche.txt', origin=\"https://s3.amazonaws.com/text-datasets/nietzsche.txt\")\n", 34 | "text = open(path).read()\n", 35 | "print('corpus length:', len(text))" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 4, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "total chars: 85\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "chars = sorted(list(set(text)))\n", 53 | "vocab_size = len(chars)+1\n", 54 | "print('total chars:', vocab_size)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 5, 60 | "metadata": { 61 | "collapsed": true 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "chars.insert(0, \"\\0\")\n", 66 | "char_indices = dict((c, i) for i, c in enumerate(chars))\n", 67 | "indices_char = dict((i, c) for i, c in enumerate(chars))" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "## Theano RNN" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 23, 80 | "metadata": { 81 | "collapsed": true 82 | }, 83 | "outputs": [], 84 | "source": [ 85 | "n_hidden = 256\n", 86 | "n_input = vocab_size\n", 87 | "n_output = vocab_size" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 20, 93 | "metadata": { 94 | "collapsed": true 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "def init_wgts(rows, cols): \n", 99 | " #xavier init for weights\n", 100 | " scale = math.sqrt(2/rows)\n", 101 | " ## shared basically means want to pass off to theano and it will use that dat aon GPU\n", 102 | " return shared(np.random.normal(scale=scale, size=(rows, cols)).astype(np.float32))\n", 103 | "def init_bias(rows): \n", 104 | " #zero init for bias\n", 105 | " return shared(np.zeros(rows, dtype=np.float32))\n", 106 | "def wgts_and_bias(n_in, n_out): \n", 107 | " return init_wgts(n_in, n_out), init_bias(n_out)\n", 108 | "def id_and_bias(n): \n", 109 | " #returns identity init for weights\n", 110 | " return shared(np.eye(n, dtype=np.float32)), init_bias(n)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 21, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [ 119 | "## These are our variables\n", 120 | "# input\n", 121 | "t_inp = T.matrix('inp')\n", 122 | "#output\n", 123 | "t_outp = T.matrix('outp')\n", 124 | "#init first hidden state\n", 125 | "t_h0 = T.vector('h0')\n", 126 | "#learning rate\n", 127 | "lr = T.scalar('lr')\n", 128 | "\n", 129 | "#all args give theano\n", 130 | "all_args = [t_h0, t_inp, t_outp, lr]" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 31, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "#hidden weights\n", 140 | "W_h = id_and_bias(n_hidden)\n", 141 | "#input weights\n", 142 | "W_x = wgts_and_bias(n_input, n_hidden)\n", 143 | "#output weights\n", 144 | "W_y = wgts_and_bias(n_hidden, n_output)\n", 145 | "w_all = list(chain.from_iterable([W_h, W_x, W_y]))" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 32, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "def step(x, h, W_h, b_h, W_x, b_x, W_y, b_y):\n", 155 | " # Calculate the hidden activations\n", 156 | " h = T.nnet.relu(T.dot(x, W_x) + b_x + T.dot(h, W_h) + b_h)\n", 157 | " # feed hidden out softmax for y. generalization of softmax to K classes\n", 158 | " y = T.nnet.softmax(T.dot(h, W_y) + b_y)\n", 159 | " return h, T.flatten(y, 1)" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 37, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "## sequences = sequences want to iterate over\n", 169 | "## outputs_info = initial state of necessary things\n", 170 | "[v_h, v_y], _ = theano.scan(step, sequences=t_inp, \n", 171 | " outputs_info=[t_h0, None], non_sequences=w_all)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 40, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "#cross entropy error predictions with actual\n", 181 | "error = T.nnet.categorical_crossentropy(v_y, t_outp).sum()\n", 182 | "#calc grad of error function wrt all weights\n", 183 | "g_all = T.grad(error, w_all)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 43, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "def upd_dict(wgts, grads, lr): \n", 193 | " return OrderedDict({w: w-g*lr for (w,g) in zip(wgts,grads)})\n", 194 | "\n", 195 | "## apply updates\n", 196 | "upd = upd_dict(w_all, g_all, lr)" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": { 203 | "collapsed": true 204 | }, 205 | "outputs": [], 206 | "source": [ 207 | "## combine everything. give it all the args. then what to calc. and pass the function to update every step.\n", 208 | "fn = theano.function(all_args, error, updates=upd, allow_input_downcast=True)" 209 | ] 210 | } 211 | ], 212 | "metadata": { 213 | "kernelspec": { 214 | "display_name": "Python 3", 215 | "language": "python", 216 | "name": "python3" 217 | }, 218 | "language_info": { 219 | "codemirror_mode": { 220 | "name": "ipython", 221 | "version": 3 222 | }, 223 | "file_extension": ".py", 224 | "mimetype": "text/x-python", 225 | "name": "python", 226 | "nbconvert_exporter": "python", 227 | "pygments_lexer": "ipython3", 228 | "version": "3.6.1" 229 | } 230 | }, 231 | "nbformat": 4, 232 | "nbformat_minor": 2 233 | } 234 | -------------------------------------------------------------------------------- /Tweet-to-Text-Copy1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tweepy\n", 10 | "import csv\n", 11 | "import time\n", 12 | "\n", 13 | "# Generate at https://apps.twitter.com/app\n", 14 | "CONSUMER_KEY = 'IVaf7RQIknSTczbMV5kB2onis'\n", 15 | "CONSUMER_SECRET = 'FJAPeipzPlgbBz5vuYxZ83N5YjAC5aySW43sX0djxN7VNiQKft'\n", 16 | "OAUTH_TOKEN = '823299052813090817-3WBWGzjiSEAiSLAFLZxteQSATfGEmsB'\n", 17 | "OAUTH_TOKEN_SECRET = 'I73ZKWPkz3NIHVZMetEGfBvR9Hxq92e3gRELNJjrWUGyt'\n", 18 | "\n", 19 | "auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)\n", 20 | "auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)\n", 21 | "api = tweepy.API(auth,wait_on_rate_limit=True)\n", 22 | "\n", 23 | "tweet_text = []\n", 24 | "\n", 25 | "for i, status in enumerate(tweepy.Cursor(api.search,q=\"#datascience\",count=100,\n", 26 | " lang=\"en\",\n", 27 | " since=\"2018-10-01\",\n", 28 | " tweet_mode='extended').items()):\n", 29 | " if i > 15000:\n", 30 | " break\n", 31 | " \n", 32 | " if hasattr(status, 'retweeted_status'):\n", 33 | " try:\n", 34 | " tweet = status.retweeted_status.full_text\n", 35 | " except:\n", 36 | " tweet = status.retweeted_status.text\n", 37 | " else:\n", 38 | " try:\n", 39 | " tweet = status.full_text\n", 40 | " except AttributeError:\n", 41 | " tweet = status.text\n", 42 | " tweet_text.append(tweet)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "import pandas as pd" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 4, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "df = pd.DataFrame(tweet_text)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 5, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/html": [ 71 | "
\n", 72 | "\n", 85 | "\n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | "
0
0UltraSoC extends tools offering with integrate...
1Lots of Free Open Source Datasets to Make Your...
2UltraSoC announces integrated multi-core debug...
3With $1 billion in funding, @MIT will create a...
4Over 50,000 positions in Data Science and Mach...
\n", 115 | "
" 116 | ], 117 | "text/plain": [ 118 | " 0\n", 119 | "0 UltraSoC extends tools offering with integrate...\n", 120 | "1 Lots of Free Open Source Datasets to Make Your...\n", 121 | "2 UltraSoC announces integrated multi-core debug...\n", 122 | "3 With $1 billion in funding, @MIT will create a...\n", 123 | "4 Over 50,000 positions in Data Science and Mach..." 124 | ] 125 | }, 126 | "execution_count": 5, 127 | "metadata": {}, 128 | "output_type": "execute_result" 129 | } 130 | ], 131 | "source": [ 132 | "df.head()" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 6, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "df.to_csv(\"ds_tweets.csv\", index=False)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.6.6" 169 | } 170 | }, 171 | "nbformat": 4, 172 | "nbformat_minor": 2 173 | } 174 | -------------------------------------------------------------------------------- /Tweet-to-Text.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tweepy\n", 10 | "import csv\n", 11 | "import time\n", 12 | "\n", 13 | "# Generate at https://apps.twitter.com/app\n", 14 | "CONSUMER_KEY = 'IVaf7RQIknSTczbMV5kB2onis'\n", 15 | "CONSUMER_SECRET = 'FJAPeipzPlgbBz5vuYxZ83N5YjAC5aySW43sX0djxN7VNiQKft'\n", 16 | "OAUTH_TOKEN = '823299052813090817-3WBWGzjiSEAiSLAFLZxteQSATfGEmsB'\n", 17 | "OAUTH_TOKEN_SECRET = 'I73ZKWPkz3NIHVZMetEGfBvR9Hxq92e3gRELNJjrWUGyt'\n", 18 | "\n", 19 | "auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)\n", 20 | "auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)\n", 21 | "api = tweepy.API(auth,wait_on_rate_limit=True)\n", 22 | "\n", 23 | "tweet_text = []\n", 24 | "\n", 25 | "for i, status in enumerate(tweepy.Cursor(api.search,q=\"#datascience\",count=100,\n", 26 | " lang=\"en\",\n", 27 | " since=\"2018-10-01\",\n", 28 | " tweet_mode='extended').items()):\n", 29 | " if i > 15000:\n", 30 | " break\n", 31 | " \n", 32 | " if hasattr(status, 'retweeted_status'):\n", 33 | " try:\n", 34 | " tweet = status.retweeted_status.full_text\n", 35 | " except:\n", 36 | " tweet = status.retweeted_status.text\n", 37 | " else:\n", 38 | " try:\n", 39 | " tweet = status.full_text\n", 40 | " except AttributeError:\n", 41 | " tweet = status.text\n", 42 | " tweet_text.append(tweet)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "import pandas as pd" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 4, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "df = pd.DataFrame(tweet_text)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 5, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/html": [ 71 | "
\n", 72 | "\n", 85 | "\n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | "
0
0UltraSoC extends tools offering with integrate...
1Lots of Free Open Source Datasets to Make Your...
2UltraSoC announces integrated multi-core debug...
3With $1 billion in funding, @MIT will create a...
4Over 50,000 positions in Data Science and Mach...
\n", 115 | "
" 116 | ], 117 | "text/plain": [ 118 | " 0\n", 119 | "0 UltraSoC extends tools offering with integrate...\n", 120 | "1 Lots of Free Open Source Datasets to Make Your...\n", 121 | "2 UltraSoC announces integrated multi-core debug...\n", 122 | "3 With $1 billion in funding, @MIT will create a...\n", 123 | "4 Over 50,000 positions in Data Science and Mach..." 124 | ] 125 | }, 126 | "execution_count": 5, 127 | "metadata": {}, 128 | "output_type": "execute_result" 129 | } 130 | ], 131 | "source": [ 132 | "df.head()" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 6, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "df.to_csv(\"ds_tweets.csv\", index=False)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": null, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.6.6" 169 | } 170 | }, 171 | "nbformat": 4, 172 | "nbformat_minor": 2 173 | } 174 | -------------------------------------------------------------------------------- /Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import argparse\n", 10 | "import gym\n", 11 | "import numpy as np\n", 12 | "from itertools import count\n", 13 | "\n", 14 | "import torch\n", 15 | "import torch.nn as nn\n", 16 | "import torch.nn.functional as F\n", 17 | "import torch.optim as optim\n", 18 | "from torch.autograd import Variable\n", 19 | "from torch.distributions import Categorical" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 4, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stderr", 29 | "output_type": "stream", 30 | "text": [ 31 | "[2018-04-21 22:18:12,262] Making new env: CartPole-v0\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "env = gym.make('CartPole-v0')\n", 37 | "render = False\n", 38 | "gamma = 0.95\n", 39 | "log_interval= 100\n", 40 | "\n", 41 | "\n", 42 | "\n", 43 | "class Policy(nn.Module):\n", 44 | " def __init__(self):\n", 45 | " super(Policy, self).__init__()\n", 46 | " self.affine1 = nn.Linear(4, 128)\n", 47 | " self.affine2 = nn.Linear(128, 2)\n", 48 | "\n", 49 | " self.saved_log_probs = []\n", 50 | " self.rewards = []\n", 51 | "\n", 52 | " def forward(self, x):\n", 53 | " x = F.relu(self.affine1(x))\n", 54 | " action_scores = self.affine2(x)\n", 55 | " return F.softmax(action_scores, dim=1)\n", 56 | "\n", 57 | "\n", 58 | "policy = Policy()\n", 59 | "optimizer = optim.Adam(policy.parameters(), lr=1e-2)\n", 60 | "\n", 61 | "\n", 62 | "def select_action(state):\n", 63 | " state = torch.from_numpy(state).float().unsqueeze(0)\n", 64 | " probs = policy(Variable(state))\n", 65 | " m = Categorical(probs)\n", 66 | " action = m.sample()\n", 67 | " policy.saved_log_probs.append(m.log_prob(action))\n", 68 | " return action.data[0]\n", 69 | "\n", 70 | "\n", 71 | "def finish_episode():\n", 72 | " R = 0\n", 73 | " policy_loss = []\n", 74 | " rewards = []\n", 75 | " for r in policy.rewards[::-1]:\n", 76 | " R = r + gamma * R\n", 77 | " rewards.insert(0, R)\n", 78 | " rewards = torch.Tensor(rewards)\n", 79 | " rewards = (rewards - rewards.mean()) / (rewards.std() + np.finfo(np.float32).eps)\n", 80 | " for log_prob, reward in zip(policy.saved_log_probs, rewards):\n", 81 | " policy_loss.append(-log_prob * reward)\n", 82 | " optimizer.zero_grad()\n", 83 | " policy_loss = torch.cat(policy_loss).sum()\n", 84 | " policy_loss.backward()\n", 85 | " optimizer.step()\n", 86 | " del policy.rewards[:]\n", 87 | " del policy.saved_log_probs[:]\n", 88 | "\n", 89 | "\n", 90 | "def main():\n", 91 | " running_reward = 10\n", 92 | " for i_episode in range(2000):\n", 93 | " state = env.reset()\n", 94 | " for t in range(10000): # Don't infinite loop while learning\n", 95 | " action = select_action(state)\n", 96 | " state, reward, done, _ = env.step(action)\n", 97 | " if render:\n", 98 | " env.render()\n", 99 | " policy.rewards.append(reward)\n", 100 | " if done:\n", 101 | " break\n", 102 | "\n", 103 | " running_reward = running_reward * 0.99 + t * 0.01\n", 104 | " finish_episode()\n", 105 | " if i_episode % log_interval == 0:\n", 106 | " print('Episode {}\\tLast length: {:5d}\\tAverage length: {:.2f}'.format(\n", 107 | " i_episode, t, running_reward))\n", 108 | "# if running_reward > env.spec.reward_threshold:\n", 109 | "# print(\"Solved! Running reward is now {} and \"\n", 110 | "# \"the last episode runs to {} time steps!\".format(running_reward, t))\n", 111 | "# break" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 5, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "name": "stdout", 121 | "output_type": "stream", 122 | "text": [ 123 | "Episode 0\tLast length: 14\tAverage length: 10.04\n", 124 | "Episode 100\tLast length: 104\tAverage length: 75.05\n", 125 | "Episode 200\tLast length: 111\tAverage length: 82.62\n", 126 | "Episode 300\tLast length: 9999\tAverage length: 1850.78\n" 127 | ] 128 | }, 129 | { 130 | "ename": "KeyboardInterrupt", 131 | "evalue": "", 132 | "output_type": "error", 133 | "traceback": [ 134 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 135 | "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 136 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'__main__'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 137 | "\u001b[0;32m\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m 67\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 68\u001b[0m \u001b[0mrunning_reward\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrunning_reward\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m0.99\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mt\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m0.01\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 69\u001b[0;31m \u001b[0mfinish_episode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 70\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mi_episode\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mlog_interval\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 71\u001b[0m print('Episode {}\\tLast length: {:5d}\\tAverage length: {:.2f}'.format(\n", 138 | "\u001b[0;32m\u001b[0m in \u001b[0;36mfinish_episode\u001b[0;34m()\u001b[0m\n\u001b[1;32m 47\u001b[0m \u001b[0moptimizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzero_grad\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0mpolicy_loss\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpolicy_loss\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 49\u001b[0;31m \u001b[0mpolicy_loss\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 50\u001b[0m \u001b[0moptimizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mpolicy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrewards\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 139 | "\u001b[0;32m/Users/tfolkman/anaconda3/lib/python3.5/site-packages/torch/autograd/variable.py\u001b[0m in \u001b[0;36mbackward\u001b[0;34m(self, gradient, retain_graph, create_graph, retain_variables)\u001b[0m\n\u001b[1;32m 165\u001b[0m \u001b[0mVariable\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 166\u001b[0m \"\"\"\n\u001b[0;32m--> 167\u001b[0;31m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mautograd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgradient\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mretain_graph\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcreate_graph\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mretain_variables\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 168\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mregister_hook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhook\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 140 | "\u001b[0;32m/Users/tfolkman/anaconda3/lib/python3.5/site-packages/torch/autograd/__init__.py\u001b[0m in \u001b[0;36mbackward\u001b[0;34m(variables, grad_variables, retain_graph, create_graph, retain_variables)\u001b[0m\n\u001b[1;32m 97\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 98\u001b[0m Variable._execution_engine.run_backward(\n\u001b[0;32m---> 99\u001b[0;31m variables, grad_variables, retain_graph)\n\u001b[0m\u001b[1;32m 100\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 101\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 141 | "\u001b[0;31mKeyboardInterrupt\u001b[0m: " 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "if __name__ == '__main__':\n", 147 | " main()" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [] 156 | } 157 | ], 158 | "metadata": { 159 | "kernelspec": { 160 | "display_name": "Python 3", 161 | "language": "python", 162 | "name": "python3" 163 | }, 164 | "language_info": { 165 | "codemirror_mode": { 166 | "name": "ipython", 167 | "version": 3 168 | }, 169 | "file_extension": ".py", 170 | "mimetype": "text/x-python", 171 | "name": "python", 172 | "nbconvert_exporter": "python", 173 | "pygments_lexer": "ipython3", 174 | "version": "3.5.3" 175 | } 176 | }, 177 | "nbformat": 4, 178 | "nbformat_minor": 2 179 | } 180 | -------------------------------------------------------------------------------- /common/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tfolkman/deep-learning-experiments/b570f5bf32cdc0ba8247ddb937ce815544f5a5d0/common/__init__.py -------------------------------------------------------------------------------- /common/multiprocessing_env.py: -------------------------------------------------------------------------------- 1 | #This code is from openai baseline 2 | #https://github.com/openai/baselines/tree/master/baselines/common/vec_env 3 | 4 | import numpy as np 5 | from multiprocessing import Process, Pipe 6 | 7 | def worker(remote, parent_remote, env_fn_wrapper): 8 | parent_remote.close() 9 | env = env_fn_wrapper.x() 10 | while True: 11 | cmd, data = remote.recv() 12 | if cmd == 'step': 13 | ob, reward, done, info = env.step(data) 14 | if done: 15 | ob = env.reset() 16 | remote.send((ob, reward, done, info)) 17 | elif cmd == 'reset': 18 | ob = env.reset() 19 | remote.send(ob) 20 | elif cmd == 'reset_task': 21 | ob = env.reset_task() 22 | remote.send(ob) 23 | elif cmd == 'close': 24 | remote.close() 25 | break 26 | elif cmd == 'get_spaces': 27 | remote.send((env.observation_space, env.action_space)) 28 | else: 29 | raise NotImplementedError 30 | 31 | class VecEnv(object): 32 | """ 33 | An abstract asynchronous, vectorized environment. 34 | """ 35 | def __init__(self, num_envs, observation_space, action_space): 36 | self.num_envs = num_envs 37 | self.observation_space = observation_space 38 | self.action_space = action_space 39 | 40 | def reset(self): 41 | """ 42 | Reset all the environments and return an array of 43 | observations, or a tuple of observation arrays. 44 | If step_async is still doing work, that work will 45 | be cancelled and step_wait() should not be called 46 | until step_async() is invoked again. 47 | """ 48 | pass 49 | 50 | def step_async(self, actions): 51 | """ 52 | Tell all the environments to start taking a step 53 | with the given actions. 54 | Call step_wait() to get the results of the step. 55 | You should not call this if a step_async run is 56 | already pending. 57 | """ 58 | pass 59 | 60 | def step_wait(self): 61 | """ 62 | Wait for the step taken with step_async(). 63 | Returns (obs, rews, dones, infos): 64 | - obs: an array of observations, or a tuple of 65 | arrays of observations. 66 | - rews: an array of rewards 67 | - dones: an array of "episode done" booleans 68 | - infos: a sequence of info objects 69 | """ 70 | pass 71 | 72 | def close(self): 73 | """ 74 | Clean up the environments' resources. 75 | """ 76 | pass 77 | 78 | def step(self, actions): 79 | self.step_async(actions) 80 | return self.step_wait() 81 | 82 | 83 | class CloudpickleWrapper(object): 84 | """ 85 | Uses cloudpickle to serialize contents (otherwise multiprocessing tries to use pickle) 86 | """ 87 | def __init__(self, x): 88 | self.x = x 89 | def __getstate__(self): 90 | import cloudpickle 91 | return cloudpickle.dumps(self.x) 92 | def __setstate__(self, ob): 93 | import pickle 94 | self.x = pickle.loads(ob) 95 | 96 | 97 | class SubprocVecEnv(VecEnv): 98 | def __init__(self, env_fns, spaces=None): 99 | """ 100 | envs: list of gym environments to run in subprocesses 101 | """ 102 | self.waiting = False 103 | self.closed = False 104 | nenvs = len(env_fns) 105 | self.nenvs = nenvs 106 | self.remotes, self.work_remotes = zip(*[Pipe() for _ in range(nenvs)]) 107 | self.ps = [Process(target=worker, args=(work_remote, remote, CloudpickleWrapper(env_fn))) 108 | for (work_remote, remote, env_fn) in zip(self.work_remotes, self.remotes, env_fns)] 109 | for p in self.ps: 110 | p.daemon = True # if the main process crashes, we should not cause things to hang 111 | p.start() 112 | for remote in self.work_remotes: 113 | remote.close() 114 | 115 | self.remotes[0].send(('get_spaces', None)) 116 | observation_space, action_space = self.remotes[0].recv() 117 | VecEnv.__init__(self, len(env_fns), observation_space, action_space) 118 | 119 | def step_async(self, actions): 120 | for remote, action in zip(self.remotes, actions): 121 | remote.send(('step', action)) 122 | self.waiting = True 123 | 124 | def step_wait(self): 125 | results = [remote.recv() for remote in self.remotes] 126 | self.waiting = False 127 | obs, rews, dones, infos = zip(*results) 128 | return np.stack(obs), np.stack(rews), np.stack(dones), infos 129 | 130 | def reset(self): 131 | for remote in self.remotes: 132 | remote.send(('reset', None)) 133 | return np.stack([remote.recv() for remote in self.remotes]) 134 | 135 | def reset_task(self): 136 | for remote in self.remotes: 137 | remote.send(('reset_task', None)) 138 | return np.stack([remote.recv() for remote in self.remotes]) 139 | 140 | def close(self): 141 | if self.closed: 142 | return 143 | if self.waiting: 144 | for remote in self.remotes: 145 | remote.recv() 146 | for remote in self.remotes: 147 | remote.send(('close', None)) 148 | for p in self.ps: 149 | p.join() 150 | self.closed = True 151 | 152 | def __len__(self): 153 | return self.nenvs 154 | -------------------------------------------------------------------------------- /coursera-deep-learning/cnns/resnets_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import tensorflow as tf 4 | import h5py 5 | import math 6 | 7 | def load_dataset(): 8 | train_dataset = h5py.File('/home/tyler/data/image/signs/train_signs.h5', "r") 9 | train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features 10 | train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels 11 | 12 | test_dataset = h5py.File('/home/tyler/data/image/signs/test_signs.h5', "r") 13 | test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features 14 | test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels 15 | 16 | classes = np.array(test_dataset["list_classes"][:]) # the list of classes 17 | 18 | train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0])) 19 | test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0])) 20 | 21 | return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes 22 | 23 | 24 | def random_mini_batches(X, Y, mini_batch_size = 64, seed = 0): 25 | """ 26 | Creates a list of random minibatches from (X, Y) 27 | 28 | Arguments: 29 | X -- input data, of shape (input size, number of examples) (m, Hi, Wi, Ci) 30 | Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples) (m, n_y) 31 | mini_batch_size - size of the mini-batches, integer 32 | seed -- this is only for the purpose of grading, so that you're "random minibatches are the same as ours. 33 | 34 | Returns: 35 | mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y) 36 | """ 37 | 38 | m = X.shape[0] # number of training examples 39 | mini_batches = [] 40 | np.random.seed(seed) 41 | 42 | # Step 1: Shuffle (X, Y) 43 | permutation = list(np.random.permutation(m)) 44 | shuffled_X = X[permutation,:,:,:] 45 | shuffled_Y = Y[permutation,:] 46 | 47 | # Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case. 48 | num_complete_minibatches = math.floor(m/mini_batch_size) # number of mini batches of size mini_batch_size in your partitionning 49 | for k in range(0, num_complete_minibatches): 50 | mini_batch_X = shuffled_X[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:,:,:] 51 | mini_batch_Y = shuffled_Y[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:] 52 | mini_batch = (mini_batch_X, mini_batch_Y) 53 | mini_batches.append(mini_batch) 54 | 55 | # Handling the end case (last mini-batch < mini_batch_size) 56 | if m % mini_batch_size != 0: 57 | mini_batch_X = shuffled_X[num_complete_minibatches * mini_batch_size : m,:,:,:] 58 | mini_batch_Y = shuffled_Y[num_complete_minibatches * mini_batch_size : m,:] 59 | mini_batch = (mini_batch_X, mini_batch_Y) 60 | mini_batches.append(mini_batch) 61 | 62 | return mini_batches 63 | 64 | 65 | def convert_to_one_hot(Y, C): 66 | Y = np.eye(C)[Y.reshape(-1)].T 67 | return Y 68 | 69 | 70 | def forward_propagation_for_predict(X, parameters): 71 | """ 72 | Implements the forward propagation for the model: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAX 73 | 74 | Arguments: 75 | X -- input dataset placeholder, of shape (input size, number of examples) 76 | parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3" 77 | the shapes are given in initialize_parameters 78 | 79 | Returns: 80 | Z3 -- the output of the last LINEAR unit 81 | """ 82 | 83 | # Retrieve the parameters from the dictionary "parameters" 84 | W1 = parameters['W1'] 85 | b1 = parameters['b1'] 86 | W2 = parameters['W2'] 87 | b2 = parameters['b2'] 88 | W3 = parameters['W3'] 89 | b3 = parameters['b3'] 90 | # Numpy Equivalents: 91 | Z1 = tf.add(tf.matmul(W1, X), b1) # Z1 = np.dot(W1, X) + b1 92 | A1 = tf.nn.relu(Z1) # A1 = relu(Z1) 93 | Z2 = tf.add(tf.matmul(W2, A1), b2) # Z2 = np.dot(W2, a1) + b2 94 | A2 = tf.nn.relu(Z2) # A2 = relu(Z2) 95 | Z3 = tf.add(tf.matmul(W3, A2), b3) # Z3 = np.dot(W3,Z2) + b3 96 | 97 | return Z3 98 | 99 | def predict(X, parameters): 100 | 101 | W1 = tf.convert_to_tensor(parameters["W1"]) 102 | b1 = tf.convert_to_tensor(parameters["b1"]) 103 | W2 = tf.convert_to_tensor(parameters["W2"]) 104 | b2 = tf.convert_to_tensor(parameters["b2"]) 105 | W3 = tf.convert_to_tensor(parameters["W3"]) 106 | b3 = tf.convert_to_tensor(parameters["b3"]) 107 | 108 | params = {"W1": W1, 109 | "b1": b1, 110 | "W2": W2, 111 | "b2": b2, 112 | "W3": W3, 113 | "b3": b3} 114 | 115 | x = tf.placeholder("float", [12288, 1]) 116 | 117 | z3 = forward_propagation_for_predict(x, params) 118 | p = tf.argmax(z3) 119 | 120 | sess = tf.Session() 121 | prediction = sess.run(p, feed_dict = {x: X}) 122 | 123 | return prediction -------------------------------------------------------------------------------- /coursera-deep-learning/cnns/yolo_utils.py: -------------------------------------------------------------------------------- 1 | import colorsys 2 | import imghdr 3 | import os 4 | import random 5 | from keras import backend as K 6 | 7 | import numpy as np 8 | from PIL import Image, ImageDraw, ImageFont 9 | 10 | def read_classes(classes_path): 11 | with open(classes_path) as f: 12 | class_names = f.readlines() 13 | class_names = [c.strip() for c in class_names] 14 | return class_names 15 | 16 | def read_anchors(anchors_path): 17 | with open(anchors_path) as f: 18 | anchors = f.readline() 19 | anchors = [float(x) for x in anchors.split(',')] 20 | anchors = np.array(anchors).reshape(-1, 2) 21 | return anchors 22 | 23 | def generate_colors(class_names): 24 | hsv_tuples = [(x / len(class_names), 1., 1.) for x in range(len(class_names))] 25 | colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) 26 | colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors)) 27 | random.seed(10101) # Fixed seed for consistent colors across runs. 28 | random.shuffle(colors) # Shuffle colors to decorrelate adjacent classes. 29 | random.seed(None) # Reset seed to default. 30 | return colors 31 | 32 | def scale_boxes(boxes, image_shape): 33 | """ Scales the predicted boxes in order to be drawable on the image""" 34 | height = image_shape[0] 35 | width = image_shape[1] 36 | image_dims = K.stack([height, width, height, width]) 37 | image_dims = K.reshape(image_dims, [1, 4]) 38 | boxes = boxes * image_dims 39 | return boxes 40 | 41 | def preprocess_image(img_path, model_image_size): 42 | image_type = imghdr.what(img_path) 43 | image = Image.open(img_path) 44 | resized_image = image.resize(tuple(reversed(model_image_size)), Image.BICUBIC) 45 | image_data = np.array(resized_image, dtype='float32') 46 | image_data /= 255. 47 | image_data = np.expand_dims(image_data, 0) # Add batch dimension. 48 | return image, image_data 49 | 50 | def draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors): 51 | 52 | font = ImageFont.truetype(font='font/FiraMono-Medium.otf',size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) 53 | thickness = (image.size[0] + image.size[1]) // 300 54 | 55 | for i, c in reversed(list(enumerate(out_classes))): 56 | predicted_class = class_names[c] 57 | box = out_boxes[i] 58 | score = out_scores[i] 59 | 60 | label = '{} {:.2f}'.format(predicted_class, score) 61 | 62 | draw = ImageDraw.Draw(image) 63 | label_size = draw.textsize(label, font) 64 | 65 | top, left, bottom, right = box 66 | top = max(0, np.floor(top + 0.5).astype('int32')) 67 | left = max(0, np.floor(left + 0.5).astype('int32')) 68 | bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32')) 69 | right = min(image.size[0], np.floor(right + 0.5).astype('int32')) 70 | print(label, (left, top), (right, bottom)) 71 | 72 | if top - label_size[1] >= 0: 73 | text_origin = np.array([left, top - label_size[1]]) 74 | else: 75 | text_origin = np.array([left, top + 1]) 76 | 77 | # My kingdom for a good redistributable image drawing library. 78 | for i in range(thickness): 79 | draw.rectangle([left + i, top + i, right - i, bottom - i], outline=colors[c]) 80 | draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=colors[c]) 81 | draw.text(text_origin, label, fill=(0, 0, 0), font=font) 82 | del draw -------------------------------------------------------------------------------- /coursera-deep-learning/sequence-modeling/data_utils.py: -------------------------------------------------------------------------------- 1 | from music_utils import * 2 | from preprocess import * 3 | from keras.utils import to_categorical 4 | 5 | chords, abstract_grammars = get_musical_data('data/original_metheny.mid') 6 | corpus, tones, tones_indices, indices_tones = get_corpus_data(abstract_grammars) 7 | N_tones = len(set(corpus)) 8 | n_a = 64 9 | x_initializer = np.zeros((1, 1, 78)) 10 | a_initializer = np.zeros((1, n_a)) 11 | c_initializer = np.zeros((1, n_a)) 12 | 13 | def load_music_utils(): 14 | chords, abstract_grammars = get_musical_data('data/original_metheny.mid') 15 | corpus, tones, tones_indices, indices_tones = get_corpus_data(abstract_grammars) 16 | N_tones = len(set(corpus)) 17 | X, Y, N_tones = data_processing(corpus, tones_indices, 60, 30) 18 | return (X, Y, N_tones, indices_tones) 19 | 20 | 21 | def generate_music(inference_model, corpus = corpus, abstract_grammars = abstract_grammars, tones = tones, tones_indices = tones_indices, indices_tones = indices_tones, T_y = 10, max_tries = 1000, diversity = 0.5): 22 | """ 23 | Generates music using a model trained to learn musical patterns of a jazz soloist. Creates an audio stream 24 | to save the music and play it. 25 | 26 | Arguments: 27 | model -- Keras model Instance, output of djmodel() 28 | corpus -- musical corpus, list of 193 tones as strings (ex: 'C,0.333,') 29 | abstract_grammars -- list of grammars, on element can be: 'S,0.250, C,0.250, A,0.250,' 30 | tones -- set of unique tones, ex: 'A,0.250,' is one element of the set. 31 | tones_indices -- a python dictionary mapping unique tone (ex: A,0.250,< m2,P-4 >) into their corresponding indices (0-77) 32 | indices_tones -- a python dictionary mapping indices (0-77) into their corresponding unique tone (ex: A,0.250,< m2,P-4 >) 33 | Tx -- integer, number of time-steps used at training time 34 | temperature -- scalar value, defines how conservative/creative the model is when generating music 35 | 36 | Returns: 37 | predicted_tones -- python list containing predicted tones 38 | """ 39 | 40 | # set up audio stream 41 | out_stream = stream.Stream() 42 | 43 | # Initialize chord variables 44 | curr_offset = 0.0 # variable used to write sounds to the Stream. 45 | num_chords = int(len(chords) / 3) # number of different set of chords 46 | 47 | print("Predicting new values for different set of chords.") 48 | # Loop over all 18 set of chords. At each iteration generate a sequence of tones 49 | # and use the current chords to convert it into actual sounds 50 | for i in range(1, num_chords): 51 | 52 | # Retrieve current chord from stream 53 | curr_chords = stream.Voice() 54 | 55 | # Loop over the chords of the current set of chords 56 | for j in chords[i]: 57 | # Add chord to the current chords with the adequate offset, no need to understand this 58 | curr_chords.insert((j.offset % 4), j) 59 | 60 | # Generate a sequence of tones using the model 61 | _, indices = predict_and_sample(inference_model) 62 | indices = list(indices.squeeze()) 63 | pred = [indices_tones[p] for p in indices] 64 | 65 | predicted_tones = 'C,0.25 ' 66 | for k in range(len(pred) - 1): 67 | predicted_tones += pred[k] + ' ' 68 | 69 | predicted_tones += pred[-1] 70 | 71 | #### POST PROCESSING OF THE PREDICTED TONES #### 72 | # We will consider "A" and "X" as "C" tones. It is a common choice. 73 | predicted_tones = predicted_tones.replace(' A',' C').replace(' X',' C') 74 | 75 | # Pruning #1: smoothing measure 76 | predicted_tones = prune_grammar(predicted_tones) 77 | 78 | # Use predicted tones and current chords to generate sounds 79 | sounds = unparse_grammar(predicted_tones, curr_chords) 80 | 81 | # Pruning #2: removing repeated and too close together sounds 82 | sounds = prune_notes(sounds) 83 | 84 | # Quality assurance: clean up sounds 85 | sounds = clean_up_notes(sounds) 86 | 87 | # Print number of tones/notes in sounds 88 | print('Generated %s sounds using the predicted values for the set of chords ("%s") and after pruning' % (len([k for k in sounds if isinstance(k, note.Note)]), i)) 89 | 90 | # Insert sounds into the output stream 91 | for m in sounds: 92 | out_stream.insert(curr_offset + m.offset, m) 93 | for mc in curr_chords: 94 | out_stream.insert(curr_offset + mc.offset, mc) 95 | 96 | curr_offset += 4.0 97 | 98 | # Initialize tempo of the output stream with 130 bit per minute 99 | out_stream.insert(0.0, tempo.MetronomeMark(number=130)) 100 | 101 | # Save audio stream to fine 102 | mf = midi.translate.streamToMidiFile(out_stream) 103 | mf.open("output/my_music.midi", 'wb') 104 | mf.write() 105 | print("Your generated music is saved in output/my_music.midi") 106 | mf.close() 107 | 108 | # Play the final stream through output (see 'play' lambda function above) 109 | # play = lambda x: midi.realtime.StreamPlayer(x).play() 110 | # play(out_stream) 111 | 112 | return out_stream 113 | 114 | 115 | def predict_and_sample(inference_model, x_initializer = x_initializer, a_initializer = a_initializer, 116 | c_initializer = c_initializer): 117 | """ 118 | Predicts the next value of values using the inference model. 119 | 120 | Arguments: 121 | inference_model -- Keras model instance for inference time 122 | x_initializer -- numpy array of shape (1, 1, 78), one-hot vector initializing the values generation 123 | a_initializer -- numpy array of shape (1, n_a), initializing the hidden state of the LSTM_cell 124 | c_initializer -- numpy array of shape (1, n_a), initializing the cell state of the LSTM_cel 125 | Ty -- length of the sequence you'd like to generate. 126 | 127 | Returns: 128 | results -- numpy-array of shape (Ty, 78), matrix of one-hot vectors representing the values generated 129 | indices -- numpy-array of shape (Ty, 1), matrix of indices representing the values generated 130 | """ 131 | 132 | ### START CODE HERE ### 133 | pred = inference_model.predict([x_initializer, a_initializer, c_initializer]) 134 | indices = np.argmax(pred, axis = -1) 135 | results = to_categorical(indices, num_classes=78) 136 | ### END CODE HERE ### 137 | 138 | return results, indices -------------------------------------------------------------------------------- /coursera-deep-learning/sequence-modeling/inference_code.py: -------------------------------------------------------------------------------- 1 | def inference_model(LSTM_cell, densor, n_x = 78, n_a = 64, Ty = 100): 2 | """ 3 | Uses the trained "LSTM_cell" and "densor" from model() to generate a sequence of values. 4 | 5 | Arguments: 6 | LSTM_cell -- the trained "LSTM_cell" from model(), Keras layer object 7 | densor -- the trained "densor" from model(), Keras layer object 8 | n_x -- number of unique values 9 | n_a -- number of units in the LSTM_cell 10 | Ty -- number of time steps to generate 11 | 12 | Returns: 13 | inference_model -- Keras model instance 14 | """ 15 | 16 | # Define the input of your model with a shape 17 | x0 = Input(shape=(1, n_x)) 18 | 19 | # Define s0, initial hidden state for the decoder LSTM 20 | a0 = Input(shape=(n_a,), name='a0') 21 | c0 = Input(shape=(n_a,), name='c0') 22 | a = a0 23 | c = c0 24 | x = x0 25 | 26 | ### START CODE HERE ### 27 | # Step 1: Create an empty list of "outputs" to later store your predicted values (≈1 line) 28 | outputs = [] 29 | 30 | # Step 2: Loop over Ty and generate a value at every time step 31 | for t in range(Ty): 32 | 33 | # Step 2.A: Perform one step of LSTM_cell (≈1 line) 34 | a, _, c = LSTM_cell(x, initial_state=[a, c]) 35 | 36 | # Step 2.B: Apply Dense layer to the hidden state output of the LSTM_cell (≈1 line) 37 | out = densor(a) 38 | 39 | # Step 2.C: Append the prediction "out" to "outputs" (≈1 line) 40 | outputs.append(out) 41 | 42 | # Step 2.D: Set the prediction "out" to be the next input "x". You will need to use RepeatVector(1). (≈1 line) 43 | x = RepeatVector(1)(out) 44 | 45 | # Step 3: Create model instance with the correct "inputs" and "outputs" (≈1 line) 46 | inference_model = Model(inputs=[x0, a0, c0], outputs=outputs) 47 | ### END CODE HERE ### 48 | 49 | return inference_model 50 | 51 | 52 | inference_model = inference_model(LSTM_cell, densor) 53 | 54 | 55 | x1 = np.zeros((1, 1, 78)) 56 | x1[:,:,35] = 1 57 | a1 = np.zeros((1, n_a)) 58 | c1 = np.zeros((1, n_a)) 59 | predicting = inference_model.predict([x1, a1, c1]) 60 | 61 | 62 | indices = np.argmax(predicting, axis = -1) 63 | results = to_categorical(indices, num_classes=78) -------------------------------------------------------------------------------- /coursera-deep-learning/sequence-modeling/insert_test.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tfolkman/deep-learning-experiments/b570f5bf32cdc0ba8247ddb937ce815544f5a5d0/coursera-deep-learning/sequence-modeling/insert_test.wav -------------------------------------------------------------------------------- /coursera-deep-learning/sequence-modeling/midi.py: -------------------------------------------------------------------------------- 1 | """ 2 | File: midi.py 3 | Author: Addy771 4 | Description: 5 | A script which converts MIDI files to WAV and optionally to MP3 using ffmpeg. 6 | Works by playing each file and using the stereo mix device to record at the same time 7 | """ 8 | 9 | 10 | import pyaudio # audio recording 11 | import wave # file saving 12 | import pygame # midi playback 13 | import fnmatch # name matching 14 | import os # file listing 15 | 16 | 17 | #### CONFIGURATION #### 18 | 19 | do_ffmpeg_convert = True # Uses FFmpeg to convert WAV files to MP3. Requires ffmpeg.exe in the script folder or PATH 20 | do_wav_cleanup = True # Deletes WAV files after conversion to MP3 21 | sample_rate = 44100 # Sample rate used for WAV/MP3 22 | channels = 2 # Audio channels (1 = mono, 2 = stereo) 23 | buffer = 1024 # Audio buffer size 24 | mp3_bitrate = 128 # Bitrate to save MP3 with in kbps (CBR) 25 | input_device = 1 # Which recording device to use. On my system Stereo Mix = 1 26 | 27 | 28 | 29 | # Begins playback of a MIDI file 30 | def play_music(music_file): 31 | 32 | try: 33 | pygame.mixer.music.load(music_file) 34 | 35 | except pygame.error: 36 | print ("Couldn't play %s! (%s)" % (music_file, pygame.get_error())) 37 | return 38 | 39 | pygame.mixer.music.play() 40 | 41 | 42 | 43 | # Init pygame playback 44 | bitsize = -16 # unsigned 16 bit 45 | pygame.mixer.init(sample_rate, bitsize, channels, buffer) 46 | 47 | # optional volume 0 to 1.0 48 | pygame.mixer.music.set_volume(1.0) 49 | 50 | # Init pyAudio 51 | format = pyaudio.paInt16 52 | audio = pyaudio.PyAudio() 53 | 54 | 55 | 56 | try: 57 | 58 | # Make a list of .mid files in the current directory and all subdirectories 59 | matches = [] 60 | for root, dirnames, filenames in os.walk("./"): 61 | for filename in fnmatch.filter(filenames, '*.mid'): 62 | matches.append(os.path.join(root, filename)) 63 | 64 | # Play each song in the list 65 | for song in matches: 66 | 67 | # Create a filename with a .wav extension 68 | file_name = os.path.splitext(os.path.basename(song))[0] 69 | new_file = file_name + '.wav' 70 | 71 | # Open the stream and start recording 72 | stream = audio.open(format=format, channels=channels, rate=sample_rate, input=True, input_device_index=input_device, frames_per_buffer=buffer) 73 | 74 | # Playback the song 75 | print("Playing " + file_name + ".mid\n") 76 | play_music(song) 77 | 78 | frames = [] 79 | 80 | # Record frames while the song is playing 81 | while pygame.mixer.music.get_busy(): 82 | frames.append(stream.read(buffer)) 83 | 84 | # Stop recording 85 | stream.stop_stream() 86 | stream.close() 87 | 88 | 89 | # Configure wave file settings 90 | wave_file = wave.open(new_file, 'wb') 91 | wave_file.setnchannels(channels) 92 | wave_file.setsampwidth(audio.get_sample_size(format)) 93 | wave_file.setframerate(sample_rate) 94 | 95 | print("Saving " + new_file) 96 | 97 | # Write the frames to the wave file 98 | wave_file.writeframes(b''.join(frames)) 99 | wave_file.close() 100 | 101 | # Call FFmpeg to handle the MP3 conversion if desired 102 | if do_ffmpeg_convert: 103 | os.system('ffmpeg -i ' + new_file + ' -y -f mp3 -ab ' + str(mp3_bitrate) + 'k -ac ' + str(channels) + ' -ar ' + str(sample_rate) + ' -vn ' + file_name + '.mp3') 104 | 105 | # Delete the WAV file if desired 106 | if do_wav_cleanup: 107 | os.remove(new_file) 108 | 109 | # End PyAudio 110 | audio.terminate() 111 | 112 | except KeyboardInterrupt: 113 | # if user hits Ctrl/C then exit 114 | # (works only in console mode) 115 | pygame.mixer.music.fadeout(1000) 116 | pygame.mixer.music.stop() 117 | raise SystemExit -------------------------------------------------------------------------------- /coursera-deep-learning/sequence-modeling/music_utils.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import tensorflow as tf 3 | import keras.backend as K 4 | from keras.layers import RepeatVector 5 | import sys 6 | from music21 import * 7 | import numpy as np 8 | from grammar import * 9 | from preprocess import * 10 | from qa import * 11 | 12 | 13 | def data_processing(corpus, values_indices, m = 60, Tx = 30): 14 | # cut the corpus into semi-redundant sequences of Tx values 15 | Tx = Tx 16 | N_values = len(set(corpus)) 17 | np.random.seed(0) 18 | X = np.zeros((m, Tx, N_values), dtype=np.bool) 19 | Y = np.zeros((m, Tx, N_values), dtype=np.bool) 20 | for i in range(m): 21 | # for t in range(1, Tx): 22 | random_idx = np.random.choice(len(corpus) - Tx) 23 | corp_data = corpus[random_idx:(random_idx + Tx)] 24 | for j in range(Tx): 25 | idx = values_indices[corp_data[j]] 26 | if j != 0: 27 | X[i, j, idx] = 1 28 | Y[i, j-1, idx] = 1 29 | 30 | Y = np.swapaxes(Y,0,1) 31 | Y = Y.tolist() 32 | return np.asarray(X), np.asarray(Y), N_values 33 | 34 | def next_value_processing(model, next_value, x, predict_and_sample, indices_values, abstract_grammars, duration, max_tries = 1000, temperature = 0.5): 35 | """ 36 | Helper function to fix the first value. 37 | 38 | Arguments: 39 | next_value -- predicted and sampled value, index between 0 and 77 40 | x -- numpy-array, one-hot encoding of next_value 41 | predict_and_sample -- predict function 42 | indices_values -- a python dictionary mapping indices (0-77) into their corresponding unique value (ex: A,0.250,< m2,P-4 >) 43 | abstract_grammars -- list of grammars, on element can be: 'S,0.250, C,0.250, A,0.250,' 44 | duration -- scalar, index of the loop in the parent function 45 | max_tries -- Maximum numbers of time trying to fix the value 46 | 47 | Returns: 48 | next_value -- process predicted value 49 | """ 50 | 51 | # fix first note: must not have < > and not be a rest 52 | if (duration < 0.00001): 53 | tries = 0 54 | while (next_value.split(',')[0] == 'R' or 55 | len(next_value.split(',')) != 2): 56 | # give up after 1000 tries; random from input's first notes 57 | if tries >= max_tries: 58 | #print('Gave up on first note generation after', max_tries, 'tries') 59 | # np.random is exclusive to high 60 | rand = np.random.randint(0, len(abstract_grammars)) 61 | next_value = abstract_grammars[rand].split(' ')[0] 62 | else: 63 | next_value = predict_and_sample(model, x, indices_values, temperature) 64 | 65 | tries += 1 66 | 67 | return next_value 68 | 69 | 70 | def sequence_to_matrix(sequence, values_indices): 71 | """ 72 | Convert a sequence (slice of the corpus) into a matrix (numpy) of one-hot vectors corresponding 73 | to indices in values_indices 74 | 75 | Arguments: 76 | sequence -- python list 77 | 78 | Returns: 79 | x -- numpy-array of one-hot vectors 80 | """ 81 | sequence_len = len(sequence) 82 | x = np.zeros((1, sequence_len, len(values_indices))) 83 | for t, value in enumerate(sequence): 84 | if (not value in values_indices): print(value) 85 | x[0, t, values_indices[value]] = 1. 86 | return x 87 | 88 | def one_hot(x): 89 | x = K.argmax(x) 90 | x = tf.one_hot(x, 78) 91 | x = RepeatVector(1)(x) 92 | return x -------------------------------------------------------------------------------- /coursera-deep-learning/sequence-modeling/nmt_utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from faker import Faker 3 | import random 4 | from tqdm import tqdm 5 | from babel.dates import format_date 6 | import matplotlib.pyplot as plt 7 | 8 | fake = Faker() 9 | fake.seed(12345) 10 | random.seed(12345) 11 | 12 | # Define format of the data we would like to generate 13 | FORMATS = ['short', 14 | 'medium', 15 | 'long', 16 | 'full', 17 | 'full', 18 | 'full', 19 | 'full', 20 | 'full', 21 | 'full', 22 | 'full', 23 | 'full', 24 | 'full', 25 | 'full', 26 | 'd MMM YYY', 27 | 'd MMMM YYY', 28 | 'dd MMM YYY', 29 | 'd MMM, YYY', 30 | 'd MMMM, YYY', 31 | 'dd, MMM YYY', 32 | 'd MM YY', 33 | 'd MMMM YYY', 34 | 'MMMM d YYY', 35 | 'MMMM d, YYY', 36 | 'dd.MM.YY'] 37 | 38 | # change this if you want it to work with another language 39 | LOCALES = ['en_US'] 40 | 41 | def load_date(): 42 | """ 43 | Loads some fake dates 44 | :returns: tuple containing human readable string, machine readable string, and date object 45 | """ 46 | dt = fake.date_object() 47 | 48 | try: 49 | human_readable = format_date(dt, format=random.choice(FORMATS), locale='en_US') # locale=random.choice(LOCALES)) 50 | human_readable = human_readable.lower() 51 | human_readable = human_readable.replace(',','') 52 | machine_readable = dt.isoformat() 53 | 54 | except AttributeError as e: 55 | return None, None, None 56 | 57 | return human_readable, machine_readable, dt 58 | 59 | def load_dataset(m): 60 | """ 61 | Loads a dataset with m examples and vocabularies 62 | :m: the number of examples to generate 63 | """ 64 | 65 | human_vocab = set() 66 | machine_vocab = set() 67 | dataset = [] 68 | Tx = 30 69 | 70 | 71 | for i in tqdm(range(m)): 72 | h, m, _ = load_date() 73 | if h is not None: 74 | dataset.append((h, m)) 75 | human_vocab.update(tuple(h)) 76 | machine_vocab.update(tuple(m)) 77 | 78 | human = dict(zip(sorted(human_vocab) + ['', ''], 79 | list(range(len(human_vocab) + 2)))) 80 | inv_machine = dict(enumerate(sorted(machine_vocab))) 81 | machine = {v:k for k,v in inv_machine.items()} 82 | 83 | return dataset, human, machine, inv_machine 84 | 85 | def preprocess_data(dataset, human_vocab, machine_vocab, Tx, Ty): 86 | 87 | X, Y = zip(*dataset) 88 | 89 | X = np.array([string_to_int(i, Tx, human_vocab) for i in X]) 90 | Y = [string_to_int(t, Ty, machine_vocab) for t in Y] 91 | 92 | return X, np.array(Y) 93 | 94 | def string_to_int(string, length, vocab): 95 | """ 96 | Converts all strings in the vocabulary into a list of integers representing the positions of the 97 | input string's characters in the "vocab" 98 | 99 | Arguments: 100 | string -- input string, e.g. 'Wed 10 Jul 2007' 101 | length -- the number of time steps you'd like, determines if the output will be padded or cut 102 | vocab -- vocabulary, dictionary used to index every character of your "string" 103 | 104 | Returns: 105 | rep -- list of integers (or '') (size = length) representing the position of the string's character in the vocabulary 106 | """ 107 | 108 | #make lower to standardize 109 | string = string.lower() 110 | string = string.replace(',','') 111 | 112 | if len(string) > length: 113 | string = string[:length] 114 | 115 | rep = list(map(lambda x: vocab.get(x, ''), string)) 116 | 117 | if len(string) < length: 118 | rep += [vocab['']] * (length - len(string)) 119 | 120 | #print (rep) 121 | return rep 122 | 123 | 124 | def int_to_string(ints, inv_vocab): 125 | """ 126 | Output a machine readable list of characters based on a list of indexes in the machine's vocabulary 127 | 128 | Arguments: 129 | ints -- list of integers representing indexes in the machine's vocabulary 130 | inv_vocab -- dictionary mapping machine readable indexes to machine readable characters 131 | 132 | Returns: 133 | l -- list of characters corresponding to the indexes of ints thanks to the inv_vocab mapping 134 | """ 135 | 136 | l = [inv_vocab[i] for i in ints] 137 | return l 138 | 139 | 140 | EXAMPLES = ['3 May 1979', '5 Apr 09', '20th February 2016', 'Wed 10 Jul 2007'] 141 | 142 | def run_example(model, input_vocabulary, inv_output_vocabulary, text): 143 | encoded = string_to_int(text, TIME_STEPS, input_vocabulary) 144 | prediction = model.predict(np.array([encoded])) 145 | prediction = np.argmax(prediction[0], axis=-1) 146 | return int_to_string(prediction, inv_output_vocabulary) 147 | 148 | def run_examples(model, input_vocabulary, inv_output_vocabulary, examples=EXAMPLES): 149 | predicted = [] 150 | for example in examples: 151 | predicted.append(''.join(run_example(model, input_vocabulary, inv_output_vocabulary, example))) 152 | print('input:', example) 153 | print('output:', predicted[-1]) 154 | return predicted 155 | 156 | 157 | def softmax(x, axis=1): 158 | """Softmax activation function. 159 | # Arguments 160 | x : Tensor. 161 | axis: Integer, axis along which the softmax normalization is applied. 162 | # Returns 163 | Tensor, output of softmax transformation. 164 | # Raises 165 | ValueError: In case `dim(x) == 1`. 166 | """ 167 | ndim = K.ndim(x) 168 | if ndim == 2: 169 | return K.softmax(x) 170 | elif ndim > 2: 171 | e = K.exp(x - K.max(x, axis=axis, keepdims=True)) 172 | s = K.sum(e, axis=axis, keepdims=True) 173 | return e / s 174 | else: 175 | raise ValueError('Cannot apply softmax to a tensor that is 1D') 176 | 177 | 178 | def plot_attention_map(model, input_vocabulary, inv_output_vocabulary, text, n_s = 128, num = 6, Tx = 30, Ty = 10): 179 | """ 180 | Plot the attention map. 181 | 182 | """ 183 | attention_map = np.zeros((10, 30)) 184 | Ty, Tx = attention_map.shape 185 | 186 | s0 = np.zeros((1, n_s)) 187 | c0 = np.zeros((1, n_s)) 188 | layer = model.layers[num] 189 | 190 | encoded = np.array(string_to_int(text, Tx, input_vocabulary)).reshape((1, 30)) 191 | encoded = np.array(list(map(lambda x: to_categorical(x, num_classes=len(input_vocabulary)), encoded))) 192 | 193 | f = K.function(model.inputs, [layer.get_output_at(t) for t in range(Ty)]) 194 | r = f([encoded, s0, c0]) 195 | 196 | for t in range(Ty): 197 | for t_prime in range(Tx): 198 | attention_map[t][t_prime] = r[t][0,t_prime,0] 199 | 200 | # Normalize attention map 201 | # row_max = attention_map.max(axis=1) 202 | # attention_map = attention_map / row_max[:, None] 203 | 204 | prediction = model.predict([encoded, s0, c0]) 205 | 206 | predicted_text = [] 207 | for i in range(len(prediction)): 208 | predicted_text.append(int(np.argmax(prediction[i], axis=1))) 209 | 210 | predicted_text = list(predicted_text) 211 | predicted_text = int_to_string(predicted_text, inv_output_vocabulary) 212 | text_ = list(text) 213 | 214 | # get the lengths of the string 215 | input_length = len(text) 216 | output_length = Ty 217 | 218 | # Plot the attention_map 219 | plt.clf() 220 | f = plt.figure(figsize=(8, 8.5)) 221 | ax = f.add_subplot(1, 1, 1) 222 | 223 | # add image 224 | i = ax.imshow(attention_map, interpolation='nearest', cmap='Blues') 225 | 226 | # add colorbar 227 | cbaxes = f.add_axes([0.2, 0, 0.6, 0.03]) 228 | cbar = f.colorbar(i, cax=cbaxes, orientation='horizontal') 229 | cbar.ax.set_xlabel('Alpha value (Probability output of the "softmax")', labelpad=2) 230 | 231 | # add labels 232 | ax.set_yticks(range(output_length)) 233 | ax.set_yticklabels(predicted_text[:output_length]) 234 | 235 | ax.set_xticks(range(input_length)) 236 | ax.set_xticklabels(text_[:input_length], rotation=45) 237 | 238 | ax.set_xlabel('Input Sequence') 239 | ax.set_ylabel('Output Sequence') 240 | 241 | # add grid and legend 242 | ax.grid() 243 | 244 | #f.show() 245 | 246 | return attention_map -------------------------------------------------------------------------------- /coursera-deep-learning/sequence-modeling/preprocess.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author: Ji-Sung Kim 3 | Project: deepjazz 4 | Purpose: Parse, cleanup and process data. 5 | 6 | Code adapted from Evan Chow's jazzml, https://github.com/evancchow/jazzml with 7 | express permission. 8 | ''' 9 | 10 | from __future__ import print_function 11 | 12 | from music21 import * 13 | from collections import defaultdict, OrderedDict 14 | from itertools import groupby, zip_longest 15 | 16 | from grammar import * 17 | 18 | from grammar import parse_melody 19 | from music_utils import * 20 | 21 | #----------------------------HELPER FUNCTIONS----------------------------------# 22 | 23 | ''' Helper function to parse a MIDI file into its measures and chords ''' 24 | def __parse_midi(data_fn): 25 | # Parse the MIDI data for separate melody and accompaniment parts. 26 | midi_data = converter.parse(data_fn) 27 | # Get melody part, compress into single voice. 28 | melody_stream = midi_data[5] # For Metheny piece, Melody is Part #5. 29 | melody1, melody2 = melody_stream.getElementsByClass(stream.Voice) 30 | for j in melody2: 31 | melody1.insert(j.offset, j) 32 | melody_voice = melody1 33 | 34 | for i in melody_voice: 35 | if i.quarterLength == 0.0: 36 | i.quarterLength = 0.25 37 | 38 | # Change key signature to adhere to comp_stream (1 sharp, mode = major). 39 | # Also add Electric Guitar. 40 | melody_voice.insert(0, instrument.ElectricGuitar()) 41 | melody_voice.insert(0, key.KeySignature(sharps=1)) 42 | 43 | # The accompaniment parts. Take only the best subset of parts from 44 | # the original data. Maybe add more parts, hand-add valid instruments. 45 | # Should add least add a string part (for sparse solos). 46 | # Verified are good parts: 0, 1, 6, 7 ''' 47 | partIndices = [0, 1, 6, 7] 48 | comp_stream = stream.Voice() 49 | comp_stream.append([j.flat for i, j in enumerate(midi_data) 50 | if i in partIndices]) 51 | 52 | # Full stream containing both the melody and the accompaniment. 53 | # All parts are flattened. 54 | full_stream = stream.Voice() 55 | for i in range(len(comp_stream)): 56 | full_stream.append(comp_stream[i]) 57 | full_stream.append(melody_voice) 58 | 59 | # Extract solo stream, assuming you know the positions ..ByOffset(i, j). 60 | # Note that for different instruments (with stream.flat), you NEED to use 61 | # stream.Part(), not stream.Voice(). 62 | # Accompanied solo is in range [478, 548) 63 | solo_stream = stream.Voice() 64 | for part in full_stream: 65 | curr_part = stream.Part() 66 | curr_part.append(part.getElementsByClass(instrument.Instrument)) 67 | curr_part.append(part.getElementsByClass(tempo.MetronomeMark)) 68 | curr_part.append(part.getElementsByClass(key.KeySignature)) 69 | curr_part.append(part.getElementsByClass(meter.TimeSignature)) 70 | curr_part.append(part.getElementsByOffset(476, 548, 71 | includeEndBoundary=True)) 72 | cp = curr_part.flat 73 | solo_stream.insert(cp) 74 | 75 | # Group by measure so you can classify. 76 | # Note that measure 0 is for the time signature, metronome, etc. which have 77 | # an offset of 0.0. 78 | melody_stream = solo_stream[-1] 79 | measures = OrderedDict() 80 | offsetTuples = [(int(n.offset / 4), n) for n in melody_stream] 81 | measureNum = 0 # for now, don't use real m. nums (119, 120) 82 | for key_x, group in groupby(offsetTuples, lambda x: x[0]): 83 | measures[measureNum] = [n[1] for n in group] 84 | measureNum += 1 85 | 86 | # Get the stream of chords. 87 | # offsetTuples_chords: group chords by measure number. 88 | chordStream = solo_stream[0] 89 | chordStream.removeByClass(note.Rest) 90 | chordStream.removeByClass(note.Note) 91 | offsetTuples_chords = [(int(n.offset / 4), n) for n in chordStream] 92 | 93 | # Generate the chord structure. Use just track 1 (piano) since it is 94 | # the only instrument that has chords. 95 | # Group into 4s, just like before. 96 | chords = OrderedDict() 97 | measureNum = 0 98 | for key_x, group in groupby(offsetTuples_chords, lambda x: x[0]): 99 | chords[measureNum] = [n[1] for n in group] 100 | measureNum += 1 101 | 102 | # Fix for the below problem. 103 | # 1) Find out why len(measures) != len(chords). 104 | # ANSWER: resolves at end but melody ends 1/16 before last measure so doesn't 105 | # actually show up, while the accompaniment's beat 1 right after does. 106 | # Actually on second thought: melody/comp start on Ab, and resolve to 107 | # the same key (Ab) so could actually just cut out last measure to loop. 108 | # Decided: just cut out the last measure. 109 | del chords[len(chords) - 1] 110 | assert len(chords) == len(measures) 111 | 112 | return measures, chords 113 | 114 | ''' Helper function to get the grammatical data from given musical data. ''' 115 | def __get_abstract_grammars(measures, chords): 116 | # extract grammars 117 | abstract_grammars = [] 118 | for ix in range(1, len(measures)): 119 | m = stream.Voice() 120 | for i in measures[ix]: 121 | m.insert(i.offset, i) 122 | c = stream.Voice() 123 | for j in chords[ix]: 124 | c.insert(j.offset, j) 125 | parsed = parse_melody(m, c) 126 | abstract_grammars.append(parsed) 127 | 128 | return abstract_grammars 129 | 130 | #----------------------------PUBLIC FUNCTIONS----------------------------------# 131 | 132 | ''' Get musical data from a MIDI file ''' 133 | def get_musical_data(data_fn): 134 | 135 | measures, chords = __parse_midi(data_fn) 136 | abstract_grammars = __get_abstract_grammars(measures, chords) 137 | 138 | return chords, abstract_grammars 139 | 140 | ''' Get corpus data from grammatical data ''' 141 | def get_corpus_data(abstract_grammars): 142 | corpus = [x for sublist in abstract_grammars for x in sublist.split(' ')] 143 | values = set(corpus) 144 | val_indices = dict((v, i) for i, v in enumerate(values)) 145 | indices_val = dict((i, v) for i, v in enumerate(values)) 146 | 147 | return corpus, values, val_indices, indices_val 148 | 149 | ''' 150 | def load_music_utils(): 151 | chord_data, raw_music_data = get_musical_data('data/original_metheny.mid') 152 | music_data, values, values_indices, indices_values = get_corpus_data(raw_music_data) 153 | 154 | X, Y = data_processing(music_data, values_indices, Tx = 20, step = 3) 155 | return (X, Y) 156 | ''' 157 | -------------------------------------------------------------------------------- /coursera-deep-learning/sequence-modeling/qa.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Author: Ji-Sung Kim, Evan Chow 3 | Project: deepjazz 4 | Purpose: Provide pruning and cleanup functions. 5 | 6 | Code adapted from Evan Chow's jazzml, https://github.com/evancchow/jazzml 7 | with express permission. 8 | ''' 9 | from itertools import zip_longest 10 | import random 11 | 12 | from music21 import * 13 | 14 | #----------------------------HELPER FUNCTIONS----------------------------------# 15 | 16 | ''' Helper function to down num to the nearest multiple of mult. ''' 17 | def __roundDown(num, mult): 18 | return (float(num) - (float(num) % mult)) 19 | 20 | ''' Helper function to round up num to nearest multiple of mult. ''' 21 | def __roundUp(num, mult): 22 | return __roundDown(num, mult) + mult 23 | 24 | ''' Helper function that, based on if upDown < 0 or upDown >= 0, rounds number 25 | down or up respectively to nearest multiple of mult. ''' 26 | def __roundUpDown(num, mult, upDown): 27 | if upDown < 0: 28 | return __roundDown(num, mult) 29 | else: 30 | return __roundUp(num, mult) 31 | 32 | ''' Helper function, from recipes, to iterate over list in chunks of n 33 | length. ''' 34 | def __grouper(iterable, n, fillvalue=None): 35 | args = [iter(iterable)] * n 36 | return zip_longest(*args, fillvalue=fillvalue) 37 | 38 | #----------------------------PUBLIC FUNCTIONS----------------------------------# 39 | 40 | ''' Smooth the measure, ensuring that everything is in standard note lengths 41 | (e.g., 0.125, 0.250, 0.333 ... ). ''' 42 | def prune_grammar(curr_grammar): 43 | pruned_grammar = curr_grammar.split(' ') 44 | 45 | for ix, gram in enumerate(pruned_grammar): 46 | terms = gram.split(',') 47 | terms[1] = str(__roundUpDown(float(terms[1]), 0.250, 48 | random.choice([-1, 1]))) 49 | pruned_grammar[ix] = ','.join(terms) 50 | pruned_grammar = ' '.join(pruned_grammar) 51 | 52 | return pruned_grammar 53 | 54 | ''' Remove repeated notes, and notes that are too close together. ''' 55 | def prune_notes(curr_notes): 56 | for n1, n2 in __grouper(curr_notes, n=2): 57 | if n2 == None: # corner case: odd-length list 58 | continue 59 | if isinstance(n1, note.Note) and isinstance(n2, note.Note): 60 | if n1.nameWithOctave == n2.nameWithOctave: 61 | curr_notes.remove(n2) 62 | 63 | return curr_notes 64 | 65 | ''' Perform quality assurance on notes ''' 66 | def clean_up_notes(curr_notes): 67 | removeIxs = [] 68 | for ix, m in enumerate(curr_notes): 69 | # QA1: ensure nothing is of 0 quarter note len, if so changes its len 70 | if (m.quarterLength == 0.0): 71 | m.quarterLength = 0.250 72 | # QA2: ensure no two melody notes have same offset, i.e. form a chord. 73 | # Sorted, so same offset would be consecutive notes. 74 | if (ix < (len(curr_notes) - 1)): 75 | if (m.offset == curr_notes[ix + 1].offset and 76 | isinstance(curr_notes[ix + 1], note.Note)): 77 | removeIxs.append((ix + 1)) 78 | curr_notes = [i for ix, i in enumerate(curr_notes) if ix not in removeIxs] 79 | 80 | return curr_notes -------------------------------------------------------------------------------- /coursera-deep-learning/sequence-modeling/td_utils.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | from scipy.io import wavfile 3 | import os 4 | from pydub import AudioSegment 5 | 6 | # Calculate and plot spectrogram for a wav audio file 7 | def graph_spectrogram(wav_file): 8 | rate, data = get_wav_info(wav_file) 9 | nfft = 200 # Length of each window segment 10 | fs = 8000 # Sampling frequencies 11 | noverlap = 120 # Overlap between windows 12 | nchannels = data.ndim 13 | if nchannels == 1: 14 | pxx, freqs, bins, im = plt.specgram(data, nfft, fs, noverlap = noverlap) 15 | elif nchannels == 2: 16 | pxx, freqs, bins, im = plt.specgram(data[:,0], nfft, fs, noverlap = noverlap) 17 | return pxx 18 | 19 | # Load a wav file 20 | def get_wav_info(wav_file): 21 | rate, data = wavfile.read(wav_file) 22 | return rate, data 23 | 24 | # Used to standardize volume of audio clip 25 | def match_target_amplitude(sound, target_dBFS): 26 | change_in_dBFS = target_dBFS - sound.dBFS 27 | return sound.apply_gain(change_in_dBFS) 28 | 29 | # Load raw audio files for speech synthesis 30 | def load_raw_audio(): 31 | activates = [] 32 | backgrounds = [] 33 | negatives = [] 34 | for filename in os.listdir("./raw_data/activates"): 35 | if filename.endswith("wav"): 36 | activate = AudioSegment.from_wav("./raw_data/activates/"+filename) 37 | activates.append(activate) 38 | for filename in os.listdir("./raw_data/backgrounds"): 39 | if filename.endswith("wav"): 40 | background = AudioSegment.from_wav("./raw_data/backgrounds/"+filename) 41 | backgrounds.append(background) 42 | for filename in os.listdir("./raw_data/negatives"): 43 | if filename.endswith("wav"): 44 | negative = AudioSegment.from_wav("./raw_data/negatives/"+filename) 45 | negatives.append(negative) 46 | return activates, negatives, backgrounds -------------------------------------------------------------------------------- /coursera-deep-learning/sequence-modeling/train.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tfolkman/deep-learning-experiments/b570f5bf32cdc0ba8247ddb937ce815544f5a5d0/coursera-deep-learning/sequence-modeling/train.wav -------------------------------------------------------------------------------- /deep_rl_course/.ipynb_checkpoints/Q_LEARNING-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import gym\n", 11 | "import random\n", 12 | "from tqdm import tqdm" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 3, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stderr", 22 | "output_type": "stream", 23 | "text": [ 24 | "[2018-11-13 00:27:47,448] Making new env: Breakout-v0\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "env = gym.make(\"Breakout-v0\")" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "## Create and Init Q Table" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 36, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "action_size = env.action_space.n\n", 46 | "state_size = env.observation_space.n\n", 47 | "qtable = np.zeros((state_size, action_size))" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## Hyperparameters" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 37, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "total_episodes = 50000\n", 64 | "total_test_episodes = 100\n", 65 | "max_steps = 99\n", 66 | "\n", 67 | "learning_rate = 0.7\n", 68 | "discount_rate = 0.618\n", 69 | "\n", 70 | "min_epsilon = 0.01\n", 71 | "decay_rate = 0.9999\n", 72 | "epsilon = 1.0" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "## Run Q-Learning Algo\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 38, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stderr", 89 | "output_type": "stream", 90 | "text": [ 91 | "100%|██████████| 50000/50000 [00:17<00:00, 2923.27it/s]\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "for episode in tqdm(range(total_episodes)):\n", 97 | " \n", 98 | " state = env.reset()\n", 99 | " step = 0\n", 100 | " done = False\n", 101 | " \n", 102 | " for step in range(max_steps):\n", 103 | " \n", 104 | " greedy_probabiliy = random.uniform(0, 1)\n", 105 | " \n", 106 | " if greedy_probabiliy > epsilon:\n", 107 | " # choose action with highest Q value for that state\n", 108 | " action = np.argmax(qtable[state,:])\n", 109 | " else:\n", 110 | " # randomly pick\n", 111 | " action = env.action_space.sample()\n", 112 | " \n", 113 | " new_state, reward, done, _ = env.step(action)\n", 114 | " \n", 115 | " #update q table\n", 116 | " qtable[state, action] = qtable[state, action] + \\\n", 117 | " learning_rate * (reward + discount_rate * np.max(qtable[new_state,:]) - qtable[state, action]) \n", 118 | " \n", 119 | " state = new_state\n", 120 | " \n", 121 | " if done:\n", 122 | " break\n", 123 | " \n", 124 | " epsilon = max(min_epsilon, epsilon*decay_rate)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "## See Q Table" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 39, 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "data": { 141 | "text/plain": [ 142 | "array([[ 0. , 0. , 0. , 0. ,\n", 143 | " 0. , 0. ],\n", 144 | " [ -1.89494435, -1.44813002, -1.89494435, -1.44813002,\n", 145 | " -0.72512948, -10.44813002],\n", 146 | " [ -0.72512948, 0.4447743 , -0.72512948, 0.4447743 ,\n", 147 | " 2.33782249, -8.5552257 ],\n", 148 | " ...,\n", 149 | " [ 2.33782249, 5.40100727, 2.33782249, 0.4447743 ,\n", 150 | " -6.66217751, -6.66217751],\n", 151 | " [ -1.89494435, -1.44813002, -1.89494435, -1.44813002,\n", 152 | " -10.89494435, -10.89494435],\n", 153 | " [ 18.37802094, 10.35761694, 18.37802094, 31.35602094,\n", 154 | " 9.37802094, 9.37802094]])" 155 | ] 156 | }, 157 | "execution_count": 39, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "qtable" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "## Play" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 40, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "name": "stderr", 180 | "output_type": "stream", 181 | "text": [ 182 | "100%|██████████| 3/3 [00:00<00:00, 4484.29it/s]" 183 | ] 184 | }, 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "Score over time: 0.33\n" 190 | ] 191 | }, 192 | { 193 | "name": "stderr", 194 | "output_type": "stream", 195 | "text": [ 196 | "\n" 197 | ] 198 | } 199 | ], 200 | "source": [ 201 | "env.reset()\n", 202 | "rewards = []\n", 203 | "\n", 204 | "for episode in tqdm(range(3)):\n", 205 | " state = env.reset()\n", 206 | " step = 0\n", 207 | " done = False\n", 208 | " total_rewards = 0\n", 209 | " \n", 210 | " for step in range(max_steps):\n", 211 | " action = np.argmax(qtable[state,:])\n", 212 | " new_state, reward, done, _ = env.step(action)\n", 213 | " total_rewards += reward\n", 214 | " \n", 215 | " if done:\n", 216 | " rewards.append(total_rewards)\n", 217 | " break\n", 218 | " state = new_state\n", 219 | " \n", 220 | "env.close()\n", 221 | "print (\"Score over time: \" + str(sum(rewards)/total_test_episodes))" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "metadata": {}, 228 | "outputs": [], 229 | "source": [] 230 | } 231 | ], 232 | "metadata": { 233 | "kernelspec": { 234 | "display_name": "Python 3", 235 | "language": "python", 236 | "name": "python3" 237 | }, 238 | "language_info": { 239 | "codemirror_mode": { 240 | "name": "ipython", 241 | "version": 3 242 | }, 243 | "file_extension": ".py", 244 | "mimetype": "text/x-python", 245 | "name": "python", 246 | "nbconvert_exporter": "python", 247 | "pygments_lexer": "ipython3", 248 | "version": "3.5.3" 249 | } 250 | }, 251 | "nbformat": 4, 252 | "nbformat_minor": 2 253 | } 254 | -------------------------------------------------------------------------------- /deep_rl_course/Q_LEARNING.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import gym\n", 11 | "import random\n", 12 | "from tqdm import tqdm" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 3, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stderr", 22 | "output_type": "stream", 23 | "text": [ 24 | "[2018-11-13 00:27:47,448] Making new env: Breakout-v0\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "env = gym.make(\"Breakout-v0\")" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "## Create and Init Q Table" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 36, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "action_size = env.action_space.n\n", 46 | "state_size = env.observation_space.n\n", 47 | "qtable = np.zeros((state_size, action_size))" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## Hyperparameters" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 37, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "total_episodes = 50000\n", 64 | "total_test_episodes = 100\n", 65 | "max_steps = 99\n", 66 | "\n", 67 | "learning_rate = 0.7\n", 68 | "discount_rate = 0.618\n", 69 | "\n", 70 | "min_epsilon = 0.01\n", 71 | "decay_rate = 0.9999\n", 72 | "epsilon = 1.0" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "## Run Q-Learning Algo\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 38, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stderr", 89 | "output_type": "stream", 90 | "text": [ 91 | "100%|██████████| 50000/50000 [00:17<00:00, 2923.27it/s]\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "for episode in tqdm(range(total_episodes)):\n", 97 | " \n", 98 | " state = env.reset()\n", 99 | " step = 0\n", 100 | " done = False\n", 101 | " \n", 102 | " for step in range(max_steps):\n", 103 | " \n", 104 | " greedy_probabiliy = random.uniform(0, 1)\n", 105 | " \n", 106 | " if greedy_probabiliy > epsilon:\n", 107 | " # choose action with highest Q value for that state\n", 108 | " action = np.argmax(qtable[state,:])\n", 109 | " else:\n", 110 | " # randomly pick\n", 111 | " action = env.action_space.sample()\n", 112 | " \n", 113 | " new_state, reward, done, _ = env.step(action)\n", 114 | " \n", 115 | " #update q table\n", 116 | " qtable[state, action] = qtable[state, action] + \\\n", 117 | " learning_rate * (reward + discount_rate * np.max(qtable[new_state,:]) - qtable[state, action]) \n", 118 | " \n", 119 | " state = new_state\n", 120 | " \n", 121 | " if done:\n", 122 | " break\n", 123 | " \n", 124 | " epsilon = max(min_epsilon, epsilon*decay_rate)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "## See Q Table" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 39, 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "data": { 141 | "text/plain": [ 142 | "array([[ 0. , 0. , 0. , 0. ,\n", 143 | " 0. , 0. ],\n", 144 | " [ -1.89494435, -1.44813002, -1.89494435, -1.44813002,\n", 145 | " -0.72512948, -10.44813002],\n", 146 | " [ -0.72512948, 0.4447743 , -0.72512948, 0.4447743 ,\n", 147 | " 2.33782249, -8.5552257 ],\n", 148 | " ...,\n", 149 | " [ 2.33782249, 5.40100727, 2.33782249, 0.4447743 ,\n", 150 | " -6.66217751, -6.66217751],\n", 151 | " [ -1.89494435, -1.44813002, -1.89494435, -1.44813002,\n", 152 | " -10.89494435, -10.89494435],\n", 153 | " [ 18.37802094, 10.35761694, 18.37802094, 31.35602094,\n", 154 | " 9.37802094, 9.37802094]])" 155 | ] 156 | }, 157 | "execution_count": 39, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "qtable" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "## Play" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 40, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "name": "stderr", 180 | "output_type": "stream", 181 | "text": [ 182 | "100%|██████████| 3/3 [00:00<00:00, 4484.29it/s]" 183 | ] 184 | }, 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "Score over time: 0.33\n" 190 | ] 191 | }, 192 | { 193 | "name": "stderr", 194 | "output_type": "stream", 195 | "text": [ 196 | "\n" 197 | ] 198 | } 199 | ], 200 | "source": [ 201 | "env.reset()\n", 202 | "rewards = []\n", 203 | "\n", 204 | "for episode in tqdm(range(3)):\n", 205 | " state = env.reset()\n", 206 | " step = 0\n", 207 | " done = False\n", 208 | " total_rewards = 0\n", 209 | " \n", 210 | " for step in range(max_steps):\n", 211 | " action = np.argmax(qtable[state,:])\n", 212 | " new_state, reward, done, _ = env.step(action)\n", 213 | " total_rewards += reward\n", 214 | " \n", 215 | " if done:\n", 216 | " rewards.append(total_rewards)\n", 217 | " break\n", 218 | " state = new_state\n", 219 | " \n", 220 | "env.close()\n", 221 | "print (\"Score over time: \" + str(sum(rewards)/total_test_episodes))" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "metadata": {}, 228 | "outputs": [], 229 | "source": [] 230 | } 231 | ], 232 | "metadata": { 233 | "kernelspec": { 234 | "display_name": "Python 3", 235 | "language": "python", 236 | "name": "python3" 237 | }, 238 | "language_info": { 239 | "codemirror_mode": { 240 | "name": "ipython", 241 | "version": 3 242 | }, 243 | "file_extension": ".py", 244 | "mimetype": "text/x-python", 245 | "name": "python", 246 | "nbconvert_exporter": "python", 247 | "pygments_lexer": "ipython3", 248 | "version": "3.5.3" 249 | } 250 | }, 251 | "nbformat": 4, 252 | "nbformat_minor": 2 253 | } 254 | -------------------------------------------------------------------------------- /deep_rl_course/README.md: -------------------------------------------------------------------------------- 1 | https://simoninithomas.github.io/Deep_reinforcement_learning_Course/ -------------------------------------------------------------------------------- /deep_rl_course/basic.cfg: -------------------------------------------------------------------------------- 1 | # Lines starting with # are treated as comments (or with whitespaces+#). 2 | # It doesn't matter if you use capital letters or not. 3 | # It doesn't matter if you use underscore or camel notation for keys, e.g. episode_timeout is the same as episodeTimeout. 4 | 5 | doom_scenario_path = basic.wad 6 | doom_map = map01 7 | 8 | # Rewards 9 | living_reward = -1 10 | 11 | # Rendering options 12 | screen_resolution = RES_160X120 13 | screen_format = GRAY8 14 | render_hud = True 15 | render_crosshair = false 16 | render_weapon = true 17 | render_decals = true 18 | render_particles = true 19 | window_visible = false 20 | 21 | # make episodes start after 20 tics (after unholstering the gun) 22 | episode_start_time = 14 23 | 24 | # make episodes finish after 300 actions (tics) 25 | episode_timeout = 300 26 | 27 | # Available buttons 28 | available_buttons = 29 | { 30 | MOVE_LEFT 31 | MOVE_RIGHT 32 | ATTACK 33 | } 34 | 35 | # Game variables that will be in the state 36 | available_game_variables = { AMMO2} 37 | 38 | mode = PLAYER 39 | doom_skill = 5 -------------------------------------------------------------------------------- /deep_rl_course/basic.wad: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tfolkman/deep-learning-experiments/b570f5bf32cdc0ba8247ddb937ce815544f5a5d0/deep_rl_course/basic.wad -------------------------------------------------------------------------------- /fastai_course2/vgg16_avg.py: -------------------------------------------------------------------------------- 1 | import warnings 2 | 3 | from keras.models import Model 4 | from keras.layers import Flatten, Dense, Input 5 | from keras.layers import Convolution2D, AveragePooling2D 6 | from keras.engine.topology import get_source_inputs 7 | from keras.utils.layer_utils import convert_all_kernels_in_model 8 | from keras.utils.data_utils import get_file 9 | from keras import backend as K 10 | from keras.applications.imagenet_utils import decode_predictions, preprocess_input, _obtain_input_shape 11 | 12 | 13 | TH_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels.h5' 14 | TF_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5' 15 | TH_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels_notop.h5' 16 | TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' 17 | 18 | 19 | def VGG16_Avg(include_top=True, weights='imagenet', 20 | input_tensor=None, input_shape=None, 21 | classes=1000): 22 | if weights not in {'imagenet', None}: 23 | raise ValueError('The `weights` argument should be either ' 24 | '`None` (random initialization) or `imagenet` ' 25 | '(pre-training on ImageNet).') 26 | 27 | if weights == 'imagenet' and include_top and classes != 1000: 28 | raise ValueError('If using `weights` as imagenet with `include_top`' 29 | ' as true, `classes` should be 1000') 30 | # Determine proper input shape 31 | input_shape = _obtain_input_shape(input_shape, 32 | default_size=224, 33 | min_size=48, 34 | data_format=K.image_dim_ordering(), 35 | include_top=include_top) 36 | 37 | if input_tensor is None: 38 | img_input = Input(shape=input_shape) 39 | else: 40 | if not K.is_keras_tensor(input_tensor): 41 | img_input = Input(tensor=input_tensor, shape=input_shape) 42 | else: 43 | img_input = input_tensor 44 | # Block 1 45 | x = Convolution2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) 46 | x = Convolution2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) 47 | x = AveragePooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) 48 | 49 | # Block 2 50 | x = Convolution2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) 51 | x = Convolution2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) 52 | x = AveragePooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) 53 | 54 | # Block 3 55 | x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) 56 | x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) 57 | x = Convolution2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) 58 | x = AveragePooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) 59 | 60 | # Block 4 61 | x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) 62 | x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) 63 | x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) 64 | x = AveragePooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) 65 | 66 | # Block 5 67 | x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) 68 | x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) 69 | x = Convolution2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) 70 | x = AveragePooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) 71 | 72 | if include_top: 73 | # Classification block 74 | x = Flatten(name='flatten')(x) 75 | x = Dense(4096, activation='relu', name='fc1')(x) 76 | x = Dense(4096, activation='relu', name='fc2')(x) 77 | x = Dense(classes, activation='softmax', name='predictions')(x) 78 | 79 | # Ensure that the model takes into account 80 | # any potential predecessors of `input_tensor`. 81 | if input_tensor is not None: 82 | inputs = get_source_inputs(input_tensor) 83 | else: 84 | inputs = img_input 85 | # Create model. 86 | model = Model(inputs, x, name='vgg16') 87 | 88 | # load weights 89 | if weights == 'imagenet': 90 | if K.image_dim_ordering() == 'th': 91 | if include_top: 92 | weights_path = get_file('vgg16_weights_th_dim_ordering_th_kernels.h5', 93 | TH_WEIGHTS_PATH, 94 | cache_subdir='models') 95 | else: 96 | weights_path = get_file('vgg16_weights_th_dim_ordering_th_kernels_notop.h5', 97 | TH_WEIGHTS_PATH_NO_TOP, 98 | cache_subdir='models') 99 | model.load_weights(weights_path) 100 | if K.backend() == 'tensorflow': 101 | warnings.warn('You are using the TensorFlow backend, yet you ' 102 | 'are using the Theano ' 103 | 'image dimension ordering convention ' 104 | '(`image_dim_ordering="th"`). ' 105 | 'For best performance, set ' 106 | '`image_dim_ordering="tf"` in ' 107 | 'your Keras config ' 108 | 'at ~/.keras/keras.json.') 109 | convert_all_kernels_in_model(model) 110 | else: 111 | if include_top: 112 | weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels.h5', 113 | TF_WEIGHTS_PATH, 114 | cache_subdir='models') 115 | else: 116 | weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', 117 | TF_WEIGHTS_PATH_NO_TOP, 118 | cache_subdir='models') 119 | model.load_weights(weights_path) 120 | if K.backend() == 'theano': 121 | convert_all_kernels_in_model(model) 122 | return model -------------------------------------------------------------------------------- /inference_kids.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torchvision import transforms 3 | import PIL 4 | 5 | image_path = "/home/tyler/Downloads/tyler.jpg" 6 | model_path = "./models/kids.pkl" 7 | 8 | classes = ['aiden', 'cheryl', 'clara', 'emery', 'tyler'] 9 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 10 | 11 | trans = transforms.Compose([ 12 | transforms.Resize((224,224)), 13 | transforms.ToTensor(), 14 | transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) 15 | 16 | model = torch.load(model_path).to(device) 17 | predictions = model(trans(PIL.Image.open(image_path)).unsqueeze(0).to(device)) 18 | print(predictions) 19 | most_likely = classes[torch.argmax(predictions)] 20 | 21 | print(most_likely) -------------------------------------------------------------------------------- /models.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch.nn.functional as F 3 | 4 | class DQN(nn.Module): 5 | 6 | def __init__(self): 7 | super(DQN, self).__init__() 8 | self.conv1 = nn.Conv2d(3, 16, kernel_size=5, stride=2) 9 | self.bn1 = nn.BatchNorm2d(16) 10 | self.conv2 = nn.Conv2d(16, 32, kernel_size=5, stride=2) 11 | self.bn2 = nn.BatchNorm2d(32) 12 | self.conv3 = nn.Conv2d(32, 32, kernel_size=5, stride=2) 13 | self.bn3 = nn.BatchNorm2d(32) 14 | self.head = nn.Linear(448, 2) 15 | 16 | def forward(self, x): 17 | x = F.relu(self.bn1(self.conv1(x))) 18 | x = F.relu(self.bn2(self.conv2(x))) 19 | x = F.relu(self.bn3(self.conv3(x))) 20 | return self.head(x.view(x.size(0), -1)) 21 | 22 | 23 | class ConvNet(nn.Module): 24 | 25 | def __init__(self, n_actions): 26 | super(ConvNet, self).__init__() 27 | self.conv1 = nn.Conv2d(1, 16, kernel_size=5, stride=2) 28 | self.bn1 = nn.BatchNorm2d(16) 29 | self.conv2 = nn.Conv2d(16, 32, kernel_size=5, stride=2) 30 | self.bn2 = nn.BatchNorm2d(32) 31 | self.conv3 = nn.Conv2d(32, 32, kernel_size=5, stride=2) 32 | self.bn3 = nn.BatchNorm2d(32) 33 | self.head = nn.Linear(1568, n_actions) 34 | 35 | def forward(self, x): 36 | x = F.relu(self.bn1(self.conv1(x))) 37 | x = F.relu(self.bn2(self.conv2(x))) 38 | x = F.relu(self.bn3(self.conv3(x))) 39 | return F.sigmoid(self.head(x.view(x.size(0), -1))) 40 | 41 | 42 | class CartPolicy(nn.Module): 43 | def __init__(self, n_inputs=4, n_hidden=4, n_outputs=2): 44 | super(CartPolicy, self).__init__() 45 | self.n_inputs = n_inputs 46 | self.n_hidden = n_hidden 47 | self.n_outputs = n_outputs 48 | 49 | self.hidden1 = nn.Linear(n_inputs, n_hidden) 50 | self.out = nn.Linear(n_hidden, n_outputs) 51 | 52 | def forward(self, x): 53 | output = F.elu(self.hidden1(x)) 54 | output = self.out(output) 55 | return F.softmax(output, dim=1) 56 | 57 | 58 | class NNPolicy(nn.Module): 59 | def __init__(self, n_inputs=4, n_hidden=4, n_outputs=2): 60 | super(NNPolicy, self).__init__() 61 | self.n_inputs = n_inputs 62 | self.n_hidden = n_hidden 63 | self.n_outputs = n_outputs 64 | 65 | self.hidden1 = nn.Linear(n_inputs, n_hidden) 66 | self.out = nn.Linear(n_hidden, n_outputs) 67 | 68 | def forward(self, x): 69 | output = F.elu(self.hidden1(x)) 70 | output = self.out(output) 71 | return output.view(x.size(0),-1) -------------------------------------------------------------------------------- /untitled.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tfolkman/deep-learning-experiments/b570f5bf32cdc0ba8247ddb937ce815544f5a5d0/untitled.txt --------------------------------------------------------------------------------