mirror of
https://github.com/yunjey/pytorch-tutorial.git
synced 2025-07-23 17:33:36 +08:00
tutorials are added
This commit is contained in:
397
tutorials/00 - PyTorch Basics/basics.ipynb
Normal file
397
tutorials/00 - PyTorch Basics/basics.ipynb
Normal file
@ -0,0 +1,397 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 19,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import torch \n",
|
||||||
|
"import torchvision\n",
|
||||||
|
"import torch.nn as nn\n",
|
||||||
|
"import torch.utils.data as data\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import torchvision.transforms as transforms\n",
|
||||||
|
"import torchvision.datasets as dsets\n",
|
||||||
|
"from torch.autograd import Variable"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Simple Example"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"-1.2532 -1.1120 0.9717\n",
|
||||||
|
"-2.3617 0.1516 1.1280\n",
|
||||||
|
"-2.1599 0.0828 -1.4305\n",
|
||||||
|
" 0.5265 0.5020 -2.1852\n",
|
||||||
|
"-0.9197 0.1772 -1.1378\n",
|
||||||
|
"[torch.FloatTensor of size 5x3]\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# random normal\n",
|
||||||
|
"x = torch.randn(5, 3)\n",
|
||||||
|
"print (x)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# build a layer\n",
|
||||||
|
"linear = nn.Linear(3, 2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Parameter containing:\n",
|
||||||
|
" 0.3884 -0.3335 -0.5146\n",
|
||||||
|
"-0.3692 0.1977 -0.4081\n",
|
||||||
|
"[torch.FloatTensor of size 2x3]\n",
|
||||||
|
"\n",
|
||||||
|
"Parameter containing:\n",
|
||||||
|
"-0.4826\n",
|
||||||
|
"-0.0038\n",
|
||||||
|
"[torch.FloatTensor of size 2]\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# Sess weight and bias\n",
|
||||||
|
"print (linear.weight)\n",
|
||||||
|
"print (linear.bias)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Variable containing:\n",
|
||||||
|
"-1.0986 -0.1575\n",
|
||||||
|
"-2.0311 0.4378\n",
|
||||||
|
"-0.6131 1.3938\n",
|
||||||
|
" 0.6790 0.7929\n",
|
||||||
|
"-0.3134 0.8351\n",
|
||||||
|
"[torch.FloatTensor of size 5x2]\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# forward propagate\n",
|
||||||
|
"y = linear(Variable(x))\n",
|
||||||
|
"print (y)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Convert numpy array to torch tensor"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# convert numpy array to tensor\n",
|
||||||
|
"a = np.array([[1,2], [3,4]])\n",
|
||||||
|
"b = torch.from_numpy(a)\n",
|
||||||
|
"print (b)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Input pipeline"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"#### (1) Preprocessing"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 18,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Image Preprocessing \n",
|
||||||
|
"transform = transforms.Compose([\n",
|
||||||
|
" transforms.Scale(40),\n",
|
||||||
|
" transforms.RandomHorizontalFlip(),\n",
|
||||||
|
" transforms.RandomCrop(32),\n",
|
||||||
|
" transforms.ToTensor()])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### (2) Define Dataset"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Files already downloaded and verified\n",
|
||||||
|
"torch.Size([3, 32, 32])\n",
|
||||||
|
"6\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# download and loading dataset f\n",
|
||||||
|
"train_dataset = dsets.CIFAR10(root='./data/',\n",
|
||||||
|
" train=True, \n",
|
||||||
|
" transform=transform,\n",
|
||||||
|
" download=True)\n",
|
||||||
|
"\n",
|
||||||
|
"image, label = train_dataset[0]\n",
|
||||||
|
"print (image.size())\n",
|
||||||
|
"print (label)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### (3) Data Loader"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 16,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# data loader provides queue and thread in a very simple way\n",
|
||||||
|
"train_loader = data.DataLoader(dataset=train_dataset,\n",
|
||||||
|
" batch_size=100, \n",
|
||||||
|
" shuffle=True,\n",
|
||||||
|
" num_workers=2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# iteration start then queue and thread start\n",
|
||||||
|
"data_iter = iter(train_loader)\n",
|
||||||
|
"\n",
|
||||||
|
"# mini-batch images and labels\n",
|
||||||
|
"images, labels = data_iter.next()\n",
|
||||||
|
"\n",
|
||||||
|
"for images, labels in train_loader:\n",
|
||||||
|
" # your training code will be written here\n",
|
||||||
|
" pass"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### (4) What about custom dataset not cifar10?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 25,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"class CustomDataset(data.Dataset):\n",
|
||||||
|
" def __init__(self):\n",
|
||||||
|
" pass\n",
|
||||||
|
" def __getitem__(self, index):\n",
|
||||||
|
" # You should build this function to return one data for given index\n",
|
||||||
|
" pass\n",
|
||||||
|
" def __len__(self):\n",
|
||||||
|
" pass"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 26,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"ename": "TypeError",
|
||||||
|
"evalue": "'NoneType' object cannot be interpreted as an integer",
|
||||||
|
"output_type": "error",
|
||||||
|
"traceback": [
|
||||||
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||||
|
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||||
|
"\u001b[0;32m<ipython-input-26-a76c7b5c92c3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mshuffle\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m num_workers=2)\n\u001b[0m",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/torch/utils/data/dataloader.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, dataset, batch_size, shuffle, sampler, num_workers, collate_fn, pin_memory)\u001b[0m\n\u001b[1;32m 250\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msampler\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msampler\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 251\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mshuffle\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 252\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msampler\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mRandomSampler\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdataset\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 253\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mshuffle\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 254\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msampler\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSequentialSampler\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdataset\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/torch/utils/data/sampler.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, data_source)\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 46\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata_source\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 47\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnum_samples\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_source\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 48\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__iter__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;31mTypeError\u001b[0m: 'NoneType' object cannot be interpreted as an integer"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"custom_dataset = CustomDataset()\n",
|
||||||
|
"data.DataLoader(dataset=custom_dataset,\n",
|
||||||
|
" batch_size=100, \n",
|
||||||
|
" shuffle=True,\n",
|
||||||
|
" num_workers=2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Using Pretrained Model"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 17,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Downloading: \"https://s3.amazonaws.com/pytorch/models/resnet18-5c106cde.pth\" to /home/yunjey/.torch/models/resnet18-5c106cde.pth\n",
|
||||||
|
"100%|██████████| 46827520/46827520 [07:48<00:00, 99907.53it/s] \n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# Download and load pretrained model\n",
|
||||||
|
"resnet = torchvision.models.resnet18(pretrained=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# delete top layer for finetuning\n",
|
||||||
|
"sub_model = nn.Sequentialtial(*list(resnet.children()[:-1]))\n",
|
||||||
|
"\n",
|
||||||
|
"# for test\n",
|
||||||
|
"images = Variable(torch.randn(10, 3, 256, 256))\n",
|
||||||
|
"print (resnet(images).size())\n",
|
||||||
|
"print (sub_model(images).size())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Save and Load Model"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Save and load the trained model\n",
|
||||||
|
"torch.save(sub_model, 'model.pkl')\n",
|
||||||
|
"\n",
|
||||||
|
"model = torch.load('model.pkl')"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python [conda root]",
|
||||||
|
"language": "python",
|
||||||
|
"name": "conda-root-py"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.5.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
89
tutorials/00 - PyTorch Basics/main.py
Normal file
89
tutorials/00 - PyTorch Basics/main.py
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
import torch
|
||||||
|
import torchvision
|
||||||
|
import torch.nn as nn
|
||||||
|
import numpy as np
|
||||||
|
import torch.utils.data as data
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Create a torch tensor with random normal.
|
||||||
|
x = torch.randn(5, 3)
|
||||||
|
print (x)
|
||||||
|
|
||||||
|
# Build a layer.
|
||||||
|
linear = nn.Linear(3, 2)
|
||||||
|
print (linear.weight)
|
||||||
|
print (linear.bias)
|
||||||
|
|
||||||
|
# Forward propagate.
|
||||||
|
y = linear(Variable(x))
|
||||||
|
print (y)
|
||||||
|
|
||||||
|
# Convert numpy array to torch tensor.
|
||||||
|
a = np.array([[1,2], [3,4]])
|
||||||
|
b = torch.from_numpy(a)
|
||||||
|
print (b)
|
||||||
|
|
||||||
|
# Download and load cifar10 dataset .
|
||||||
|
train_dataset = dsets.CIFAR10(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transforms.ToTensor(),
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
# Select one data pair.
|
||||||
|
image, label = train_dataset[0]
|
||||||
|
print (image.size())
|
||||||
|
print (label)
|
||||||
|
|
||||||
|
# Input pipeline (this provides queue and thread in a very simple way).
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=100,
|
||||||
|
shuffle=True,
|
||||||
|
num_workers=2)
|
||||||
|
|
||||||
|
# When iteration starts, queue and thread start to load dataset.
|
||||||
|
data_iter = iter(train_loader)
|
||||||
|
|
||||||
|
# Mini-batch images and labels.
|
||||||
|
images, labels = data_iter.next()
|
||||||
|
|
||||||
|
# Actual usage of data loader is as below.
|
||||||
|
for images, labels in train_loader:
|
||||||
|
# Your training code will be written here
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Build custom dataset.
|
||||||
|
class CustomDataset(data.Dataset):
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
def __getitem__(self, index):
|
||||||
|
# TODO
|
||||||
|
# 1. Read one data from file (e.g. using np.fromfile, PIL.Image.open).
|
||||||
|
# 2. Return a data pair (e.g. image and label).
|
||||||
|
pass
|
||||||
|
def __len__(self):
|
||||||
|
# You should change 0 to the total size of your dataset.
|
||||||
|
return 0
|
||||||
|
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=100,
|
||||||
|
shuffle=True,
|
||||||
|
num_workers=2)
|
||||||
|
|
||||||
|
|
||||||
|
# Download and load pretrained model.
|
||||||
|
resnet = torchvision.models.resnet18(pretrained=True)
|
||||||
|
|
||||||
|
# Detach top layer for finetuning.
|
||||||
|
sub_model = nn.Sequential(*list(resnet.children())[:-1])
|
||||||
|
|
||||||
|
# For test
|
||||||
|
images = Variable(torch.randn(10, 3, 256, 256))
|
||||||
|
print (resnet(images).size())
|
||||||
|
print (sub_model(images).size())
|
||||||
|
|
||||||
|
# Save and load the model.
|
||||||
|
torch.save(sub_model, 'model.pkl')
|
||||||
|
model = torch.load('model.pkl')
|
61
tutorials/01 - Linear Regression/main.py
Normal file
61
tutorials/01 - Linear Regression/main.py
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
input_size = 1
|
||||||
|
output_size = 1
|
||||||
|
num_epochs = 60
|
||||||
|
learning_rate = 0.001
|
||||||
|
|
||||||
|
# Toy Dataset
|
||||||
|
x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168],
|
||||||
|
[9.779], [6.182], [7.59], [2.167], [7.042],
|
||||||
|
[10.791], [5.313], [7.997], [3.1]], dtype=np.float32)
|
||||||
|
|
||||||
|
y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573],
|
||||||
|
[3.366], [2.596], [2.53], [1.221], [2.827],
|
||||||
|
[3.465], [1.65], [2.904], [1.3]], dtype=np.float32)
|
||||||
|
|
||||||
|
# Linear Regression Model
|
||||||
|
class LinearRegression(nn.Module):
|
||||||
|
def __init__(self, input_size, output_size):
|
||||||
|
super(LinearRegression, self).__init__()
|
||||||
|
self.linear = nn.Linear(input_size, output_size)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.linear(x)
|
||||||
|
return out
|
||||||
|
|
||||||
|
model = LinearRegression(input_size, output_size)
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.MSELoss()
|
||||||
|
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Train the Model
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
# Convert numpy array to torch Variable
|
||||||
|
inputs = Variable(torch.from_numpy(x_train))
|
||||||
|
targets = Variable(torch.from_numpy(y_train))
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad()
|
||||||
|
outputs = model(inputs)
|
||||||
|
loss = criterion(outputs, targets)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (epoch+1) % 5 == 0:
|
||||||
|
print ('Epoch [%d/%d], Loss: %.4f'
|
||||||
|
%(epoch+1, num_epochs, loss.data[0]))
|
||||||
|
|
||||||
|
# Plot the graph
|
||||||
|
predicted = model(Variable(torch.from_numpy(x_train))).data.numpy()
|
||||||
|
plt.plot(x_train, y_train, 'ro', label='Original data')
|
||||||
|
plt.plot(x_train, predicted, label='Fitted line')
|
||||||
|
plt.legend()
|
||||||
|
plt.show()
|
79
tutorials/02 - Logistic Regression/main.py
Normal file
79
tutorials/02 - Logistic Regression/main.py
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
input_size = 784
|
||||||
|
num_classes = 10
|
||||||
|
num_epochs = 5
|
||||||
|
batch_size = 100
|
||||||
|
learning_rate = 0.001
|
||||||
|
|
||||||
|
# MNIST Dataset (Images and Labels)
|
||||||
|
train_dataset = dsets.MNIST(root='./data',
|
||||||
|
train=True,
|
||||||
|
transform=transforms.ToTensor(),
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.MNIST(root='./data',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Dataset Loader (Input Pipline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# Model
|
||||||
|
class LogisticRegression(nn.Module):
|
||||||
|
def __init__(self, input_size, num_classes):
|
||||||
|
super(LogisticRegression, self).__init__()
|
||||||
|
self.linear = nn.Linear(input_size, num_classes)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.linear(x)
|
||||||
|
return out
|
||||||
|
|
||||||
|
model = LogisticRegression(input_size, num_classes)
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
# Softmax is internally computed.
|
||||||
|
# Set parameters to be updated.
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Training the Model
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
images = Variable(images.view(-1, 28*28))
|
||||||
|
labels = Variable(labels)
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad()
|
||||||
|
outputs = model(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ('Epoch: [%d/%d], Step: [%d/%d], Loss: %.4f'
|
||||||
|
% (epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
|
||||||
|
|
||||||
|
# Test the Model
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images.view(-1, 28*28))
|
||||||
|
outputs = model(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted == labels).sum()
|
||||||
|
|
||||||
|
print('Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
|
84
tutorials/03 - Feedforward Neural Network/main-gpu.py
Normal file
84
tutorials/03 - Feedforward Neural Network/main-gpu.py
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
input_size = 784
|
||||||
|
hidden_size = 500
|
||||||
|
num_classes = 10
|
||||||
|
num_epochs = 5
|
||||||
|
batch_size = 100
|
||||||
|
learning_rate = 0.001
|
||||||
|
|
||||||
|
# MNIST Dataset
|
||||||
|
train_dataset = dsets.MNIST(root='./data',
|
||||||
|
train=True,
|
||||||
|
transform=transforms.ToTensor(),
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.MNIST(root='./data',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# Neural Network Model (1 hidden layer)
|
||||||
|
class Net(nn.Module):
|
||||||
|
def __init__(self, input_size, hidden_size, num_classes):
|
||||||
|
super(Net, self).__init__()
|
||||||
|
self.fc1 = nn.Linear(input_size, hidden_size)
|
||||||
|
self.relu = nn.ReLU()
|
||||||
|
self.fc2 = nn.Linear(hidden_size, num_classes)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.fc1(x)
|
||||||
|
out = self.relu(out)
|
||||||
|
out = self.fc2(out)
|
||||||
|
return out
|
||||||
|
|
||||||
|
net = Net(input_size, hidden_size, num_classes)
|
||||||
|
net.cuda()
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Train the Model
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
# Convert torch tensor to Variable
|
||||||
|
images = Variable(images.view(-1, 28*28)).cuda()
|
||||||
|
labels = Variable(labels).cuda()
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad() # zero the gradient buffer
|
||||||
|
outputs = net(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f'
|
||||||
|
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
|
||||||
|
|
||||||
|
# Test the Model
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images.view(-1, 28*28)).cuda()
|
||||||
|
outputs = net(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted.cpu() == labels).sum()
|
||||||
|
|
||||||
|
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
|
84
tutorials/03 - Feedforward Neural Network/main.py
Normal file
84
tutorials/03 - Feedforward Neural Network/main.py
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
input_size = 784
|
||||||
|
hidden_size = 500
|
||||||
|
num_classes = 10
|
||||||
|
num_epochs = 5
|
||||||
|
batch_size = 100
|
||||||
|
learning_rate = 0.001
|
||||||
|
|
||||||
|
# MNIST Dataset
|
||||||
|
train_dataset = dsets.MNIST(root='./data',
|
||||||
|
train=True,
|
||||||
|
transform=transforms.ToTensor(),
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.MNIST(root='./data',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# Neural Network Model (1 hidden layer)
|
||||||
|
class Net(nn.Module):
|
||||||
|
def __init__(self, input_size, hidden_size, num_classes):
|
||||||
|
super(Net, self).__init__()
|
||||||
|
self.fc1 = nn.Linear(input_size, hidden_size)
|
||||||
|
self.relu = nn.ReLU()
|
||||||
|
self.fc2 = nn.Linear(hidden_size, num_classes)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.fc1(x)
|
||||||
|
out = self.relu(out)
|
||||||
|
out = self.fc2(out)
|
||||||
|
return out
|
||||||
|
|
||||||
|
net = Net(input_size, hidden_size, num_classes)
|
||||||
|
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Train the Model
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
# Convert torch tensor to Variable
|
||||||
|
images = Variable(images.view(-1, 28*28))
|
||||||
|
labels = Variable(labels)
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad() # zero the gradient buffer
|
||||||
|
outputs = net(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f'
|
||||||
|
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
|
||||||
|
|
||||||
|
# Test the Model
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images.view(-1, 28*28))
|
||||||
|
outputs = net(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted == labels).sum()
|
||||||
|
|
||||||
|
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
|
90
tutorials/04 - Convolutional Neural Network/main-gpu.py
Normal file
90
tutorials/04 - Convolutional Neural Network/main-gpu.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
num_epochs = 5
|
||||||
|
batch_size = 100
|
||||||
|
learning_rate = 0.001
|
||||||
|
|
||||||
|
# MNIST Dataset
|
||||||
|
train_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transforms.ToTensor(),
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# CNN Model (2 conv layer)
|
||||||
|
class CNN(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(CNN, self).__init__()
|
||||||
|
self.layer1 = nn.Sequential(
|
||||||
|
nn.Conv2d(1, 16, kernel_size=5, padding=2),
|
||||||
|
nn.BatchNorm2d(16),
|
||||||
|
nn.ReLU(),
|
||||||
|
nn.MaxPool2d(2))
|
||||||
|
self.layer2 = nn.Sequential(
|
||||||
|
nn.Conv2d(16, 32, kernel_size=5, padding=2),
|
||||||
|
nn.BatchNorm2d(32),
|
||||||
|
nn.ReLU(),
|
||||||
|
nn.MaxPool2d(2))
|
||||||
|
self.fc = nn.Linear(7*7*32, 10)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.layer1(x)
|
||||||
|
out = self.layer2(out)
|
||||||
|
out = out.view(out.size(0), -1)
|
||||||
|
out = self.fc(out)
|
||||||
|
return out
|
||||||
|
|
||||||
|
cnn = CNN()
|
||||||
|
cnn.cuda()
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Train the Model
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
images = Variable(images).cuda()
|
||||||
|
labels = Variable(labels).cuda()
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad()
|
||||||
|
outputs = cnn(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f'
|
||||||
|
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
|
||||||
|
|
||||||
|
# Test the Model
|
||||||
|
cnn.eval()
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images).cuda()
|
||||||
|
outputs = cnn(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted == labels).sum()
|
||||||
|
|
||||||
|
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
|
90
tutorials/04 - Convolutional Neural Network/main.py
Normal file
90
tutorials/04 - Convolutional Neural Network/main.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
num_epochs = 5
|
||||||
|
batch_size = 100
|
||||||
|
learning_rate = 0.001
|
||||||
|
|
||||||
|
# MNIST Dataset
|
||||||
|
train_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transforms.ToTensor(),
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# CNN Model (2 conv layer)
|
||||||
|
class CNN(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(CNN, self).__init__()
|
||||||
|
self.layer1 = nn.Sequential(
|
||||||
|
nn.Conv2d(1, 16, kernel_size=5, padding=2),
|
||||||
|
nn.BatchNorm2d(16),
|
||||||
|
nn.ReLU(),
|
||||||
|
nn.MaxPool2d(2))
|
||||||
|
self.layer2 = nn.Sequential(
|
||||||
|
nn.Conv2d(16, 32, kernel_size=5, padding=2),
|
||||||
|
nn.BatchNorm2d(32),
|
||||||
|
nn.ReLU(),
|
||||||
|
nn.MaxPool2d(2))
|
||||||
|
self.fc = nn.Linear(7*7*32, 10)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.layer1(x)
|
||||||
|
out = self.layer2(out)
|
||||||
|
out = out.view(out.size(0), -1)
|
||||||
|
out = self.fc(out)
|
||||||
|
return out
|
||||||
|
|
||||||
|
cnn = CNN()
|
||||||
|
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Train the Model
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
images = Variable(images)
|
||||||
|
labels = Variable(labels)
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad()
|
||||||
|
outputs = cnn(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f'
|
||||||
|
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
|
||||||
|
|
||||||
|
# Test the Model
|
||||||
|
cnn.eval()
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images)
|
||||||
|
outputs = cnn(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted == labels).sum()
|
||||||
|
|
||||||
|
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
|
142
tutorials/05 - Deep Residual Network/main-gpu.py
Normal file
142
tutorials/05 - Deep Residual Network/main-gpu.py
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
# Implementation of https://arxiv.org/pdf/1512.03385.pdf
|
||||||
|
# See section 4.2 for model architecture on CIFAR-10
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
# Image Preprocessing
|
||||||
|
transform = transforms.Compose([
|
||||||
|
transforms.Scale(40),
|
||||||
|
transforms.RandomHorizontalFlip(),
|
||||||
|
transforms.RandomCrop(32),
|
||||||
|
transforms.ToTensor()])
|
||||||
|
|
||||||
|
# CIFAR-10 Dataset
|
||||||
|
train_dataset = dsets.CIFAR10(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transform,
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.CIFAR10(root='./data/',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=100,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=100,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# 3x3 Convolution
|
||||||
|
def conv3x3(in_channels, out_channels, stride=1):
|
||||||
|
return nn.Conv2d(in_channels, out_channels, kernel_size=3,
|
||||||
|
stride=stride, padding=1, bias=False)
|
||||||
|
|
||||||
|
# Residual Block
|
||||||
|
class ResidualBlock(nn.Module):
|
||||||
|
def __init__(self, in_channels, out_channels, stride=1, downsample=None):
|
||||||
|
super(ResidualBlock, self).__init__()
|
||||||
|
self.conv1 = conv3x3(in_channels, out_channels, stride)
|
||||||
|
self.bn1 = nn.BatchNorm2d(out_channels)
|
||||||
|
self.relu = nn.ReLU(inplace=True)
|
||||||
|
self.conv2 = conv3x3(out_channels, out_channels)
|
||||||
|
self.bn2 = nn.BatchNorm2d(out_channels)
|
||||||
|
self.downsample = downsample
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
residual = x
|
||||||
|
out = self.conv1(x)
|
||||||
|
out = self.bn1(out)
|
||||||
|
out = self.relu(out)
|
||||||
|
out = self.conv2(out)
|
||||||
|
out = self.bn2(out)
|
||||||
|
if self.downsample:
|
||||||
|
residual = self.downsample(x)
|
||||||
|
out += residual
|
||||||
|
out = self.relu(out)
|
||||||
|
return out
|
||||||
|
|
||||||
|
# ResNet Module
|
||||||
|
class ResNet(nn.Module):
|
||||||
|
def __init__(self, block, layers, num_classes=10):
|
||||||
|
super(ResNet, self).__init__()
|
||||||
|
self.in_channels = 16
|
||||||
|
self.conv = conv3x3(3, 16)
|
||||||
|
self.bn = nn.BatchNorm2d(16)
|
||||||
|
self.relu = nn.ReLU(inplace=True)
|
||||||
|
self.layer1 = self.make_layer(block, 16, layers[0])
|
||||||
|
self.layer2 = self.make_layer(block, 32, layers[0], 2)
|
||||||
|
self.layer3 = self.make_layer(block, 64, layers[1], 2)
|
||||||
|
self.avg_pool = nn.AvgPool2d(8)
|
||||||
|
self.fc = nn.Linear(64, num_classes)
|
||||||
|
|
||||||
|
def make_layer(self, block, out_channels, blocks, stride=1):
|
||||||
|
downsample = None
|
||||||
|
if (stride != 1) or (self.in_channels != out_channels):
|
||||||
|
downsample = nn.Sequential(
|
||||||
|
conv3x3(self.in_channels, out_channels, stride=stride),
|
||||||
|
nn.BatchNorm2d(out_channels))
|
||||||
|
layers = []
|
||||||
|
layers.append(block(self.in_channels, out_channels, stride, downsample))
|
||||||
|
self.in_channels = out_channels
|
||||||
|
for i in range(1, blocks):
|
||||||
|
layers.append(block(out_channels, out_channels))
|
||||||
|
return nn.Sequential(*layers)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.conv(x)
|
||||||
|
out = self.bn(out)
|
||||||
|
out = self.relu(out)
|
||||||
|
out = self.layer1(out)
|
||||||
|
out = self.layer2(out)
|
||||||
|
out = self.layer3(out)
|
||||||
|
out = self.avg_pool(out)
|
||||||
|
out = out.view(out.size(0), -1)
|
||||||
|
out = self.fc(out)
|
||||||
|
return out
|
||||||
|
|
||||||
|
resnet = ResNet(ResidualBlock, [3, 3, 3])
|
||||||
|
resnet.cuda()
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
lr = 0.001
|
||||||
|
optimizer = torch.optim.Adam(resnet.parameters(), lr=lr)
|
||||||
|
|
||||||
|
# Training
|
||||||
|
for epoch in range(40):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
images = Variable(images.cuda())
|
||||||
|
labels = Variable(labels.cuda())
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad()
|
||||||
|
outputs = resnet(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ("Epoch [%d/%d], Iter [%d/%d] Loss: %.4f" %(epoch+1, 40, i+1, 500, loss.data[0]))
|
||||||
|
|
||||||
|
# Decaying Learning Rate
|
||||||
|
if (epoch+1) % 20 == 0:
|
||||||
|
lr /= 3
|
||||||
|
optimizer = torch.optim.Adam(resnet.parameters(), lr=lr)
|
||||||
|
|
||||||
|
# Test
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images.cuda())
|
||||||
|
outputs = resnet(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted.cpu() == labels).sum()
|
||||||
|
|
||||||
|
print('Accuracy of the model on the test images: %d %%' % (100 * correct / total))
|
142
tutorials/05 - Deep Residual Network/main.py
Normal file
142
tutorials/05 - Deep Residual Network/main.py
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
# Implementation of https://arxiv.org/pdf/1512.03385.pdf
|
||||||
|
# See section 4.2 for model architecture on CIFAR-10
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
# Image Preprocessing
|
||||||
|
transform = transforms.Compose([
|
||||||
|
transforms.Scale(40),
|
||||||
|
transforms.RandomHorizontalFlip(),
|
||||||
|
transforms.RandomCrop(32),
|
||||||
|
transforms.ToTensor()])
|
||||||
|
|
||||||
|
# CIFAR-10 Dataset
|
||||||
|
train_dataset = dsets.CIFAR10(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transform,
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.CIFAR10(root='./data/',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=100,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=100,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# 3x3 Convolution
|
||||||
|
def conv3x3(in_channels, out_channels, stride=1):
|
||||||
|
return nn.Conv2d(in_channels, out_channels, kernel_size=3,
|
||||||
|
stride=stride, padding=1, bias=False)
|
||||||
|
|
||||||
|
# Residual Block
|
||||||
|
class ResidualBlock(nn.Module):
|
||||||
|
def __init__(self, in_channels, out_channels, stride=1, downsample=None):
|
||||||
|
super(ResidualBlock, self).__init__()
|
||||||
|
self.conv1 = conv3x3(in_channels, out_channels, stride)
|
||||||
|
self.bn1 = nn.BatchNorm2d(out_channels)
|
||||||
|
self.relu = nn.ReLU(inplace=True)
|
||||||
|
self.conv2 = conv3x3(out_channels, out_channels)
|
||||||
|
self.bn2 = nn.BatchNorm2d(out_channels)
|
||||||
|
self.downsample = downsample
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
residual = x
|
||||||
|
out = self.conv1(x)
|
||||||
|
out = self.bn1(out)
|
||||||
|
out = self.relu(out)
|
||||||
|
out = self.conv2(out)
|
||||||
|
out = self.bn2(out)
|
||||||
|
if self.downsample:
|
||||||
|
residual = self.downsample(x)
|
||||||
|
out += residual
|
||||||
|
out = self.relu(out)
|
||||||
|
return out
|
||||||
|
|
||||||
|
# ResNet Module
|
||||||
|
class ResNet(nn.Module):
|
||||||
|
def __init__(self, block, layers, num_classes=10):
|
||||||
|
super(ResNet, self).__init__()
|
||||||
|
self.in_channels = 16
|
||||||
|
self.conv = conv3x3(3, 16)
|
||||||
|
self.bn = nn.BatchNorm2d(16)
|
||||||
|
self.relu = nn.ReLU(inplace=True)
|
||||||
|
self.layer1 = self.make_layer(block, 16, layers[0])
|
||||||
|
self.layer2 = self.make_layer(block, 32, layers[0], 2)
|
||||||
|
self.layer3 = self.make_layer(block, 64, layers[1], 2)
|
||||||
|
self.avg_pool = nn.AvgPool2d(8)
|
||||||
|
self.fc = nn.Linear(64, num_classes)
|
||||||
|
|
||||||
|
def make_layer(self, block, out_channels, blocks, stride=1):
|
||||||
|
downsample = None
|
||||||
|
if (stride != 1) or (self.in_channels != out_channels):
|
||||||
|
downsample = nn.Sequential(
|
||||||
|
conv3x3(self.in_channels, out_channels, stride=stride),
|
||||||
|
nn.BatchNorm2d(out_channels))
|
||||||
|
layers = []
|
||||||
|
layers.append(block(self.in_channels, out_channels, stride, downsample))
|
||||||
|
self.in_channels = out_channels
|
||||||
|
for i in range(1, blocks):
|
||||||
|
layers.append(block(out_channels, out_channels))
|
||||||
|
return nn.Sequential(*layers)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.conv(x)
|
||||||
|
out = self.bn(out)
|
||||||
|
out = self.relu(out)
|
||||||
|
out = self.layer1(out)
|
||||||
|
out = self.layer2(out)
|
||||||
|
out = self.layer3(out)
|
||||||
|
out = self.avg_pool(out)
|
||||||
|
out = out.view(out.size(0), -1)
|
||||||
|
out = self.fc(out)
|
||||||
|
return out
|
||||||
|
|
||||||
|
resnet = ResNet(ResidualBlock, [2, 2, 2, 2])
|
||||||
|
resnet
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
lr = 0.001
|
||||||
|
optimizer = torch.optim.Adam(resnet.parameters(), lr=lr)
|
||||||
|
|
||||||
|
# Training
|
||||||
|
for epoch in range(80):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
images = Variable(images)
|
||||||
|
labels = Variable(labels)
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad()
|
||||||
|
outputs = resnet(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ("Epoch [%d/%d], Iter [%d/%d] Loss: %.4f" %(epoch+1, 80, i+1, 500, loss.data[0]))
|
||||||
|
|
||||||
|
# Decaying Learning Rate
|
||||||
|
if (epoch+1) % 30 == 0:
|
||||||
|
lr /= 3
|
||||||
|
optimizer = torch.optim.Adam(resnet.parameters(), lr=lr)
|
||||||
|
|
||||||
|
# Test
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images)
|
||||||
|
outputs = resnet(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted == labels).sum()
|
||||||
|
|
||||||
|
print('Accuracy of the model on the test images: %d %%' % (100 * correct / total))
|
90
tutorials/06 - Recurrent Neural Network/main-gpu.py
Normal file
90
tutorials/06 - Recurrent Neural Network/main-gpu.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
sequence_length = 28
|
||||||
|
input_size = 28
|
||||||
|
hidden_size = 128
|
||||||
|
num_layers = 2
|
||||||
|
num_classes = 10
|
||||||
|
batch_size = 100
|
||||||
|
num_epochs = 2
|
||||||
|
learning_rate = 0.01
|
||||||
|
|
||||||
|
# MNIST Dataset
|
||||||
|
train_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transforms.ToTensor(),
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# RNN Model (Many-to-One)
|
||||||
|
class RNN(nn.Module):
|
||||||
|
def __init__(self, input_size, hidden_size, num_layers, num_classes):
|
||||||
|
super(RNN, self).__init__()
|
||||||
|
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
|
||||||
|
self.fc = nn.Linear(hidden_size, num_classes)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
# Set initial states
|
||||||
|
h0 = Variable(torch.zeros(num_layers, x.size(0), hidden_size).cuda())
|
||||||
|
c0 = Variable(torch.zeros(num_layers, x.size(0), hidden_size).cuda())
|
||||||
|
|
||||||
|
# Forward propagate RNN
|
||||||
|
out, _ = self.lstm(x, (h0, c0))
|
||||||
|
|
||||||
|
# Decode hidden state of last time step
|
||||||
|
out = self.fc(out[:, -1, :])
|
||||||
|
return out
|
||||||
|
|
||||||
|
rnn = RNN(input_size, hidden_size, num_layers, num_classes)
|
||||||
|
rnn.cuda()
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.Adam(rnn.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Train the Model
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
images = Variable(images.view(-1, sequence_length, input_size)).cuda()
|
||||||
|
labels = Variable(labels).cuda()
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad()
|
||||||
|
outputs = rnn(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f'
|
||||||
|
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
|
||||||
|
|
||||||
|
# Test the Model
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images.view(-1, sequence_length, input_size)).cuda()
|
||||||
|
outputs = rnn(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted.cpu() == labels).sum()
|
||||||
|
|
||||||
|
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
|
90
tutorials/06 - Recurrent Neural Network/main.py
Normal file
90
tutorials/06 - Recurrent Neural Network/main.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
sequence_length = 28
|
||||||
|
input_size = 28
|
||||||
|
hidden_size = 128
|
||||||
|
num_layers = 2
|
||||||
|
num_classes = 10
|
||||||
|
batch_size = 100
|
||||||
|
num_epochs = 2
|
||||||
|
learning_rate = 0.01
|
||||||
|
|
||||||
|
# MNIST Dataset
|
||||||
|
train_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transforms.ToTensor(),
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# RNN Model (Many-to-One)
|
||||||
|
class RNN(nn.Module):
|
||||||
|
def __init__(self, input_size, hidden_size, num_layers, num_classes):
|
||||||
|
super(RNN, self).__init__()
|
||||||
|
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
|
||||||
|
self.fc = nn.Linear(hidden_size, num_classes)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
# Set initial states
|
||||||
|
h0 = Variable(torch.zeros(num_layers, x.size(0), hidden_size))
|
||||||
|
c0 = Variable(torch.zeros(num_layers, x.size(0), hidden_size))
|
||||||
|
|
||||||
|
# Forward propagate RNN
|
||||||
|
out, _ = self.lstm(x, (h0, c0))
|
||||||
|
|
||||||
|
# Decode hidden state of last time step
|
||||||
|
out = self.fc(out[:, -1, :])
|
||||||
|
return out
|
||||||
|
|
||||||
|
rnn = RNN(input_size, hidden_size, num_layers, num_classes)
|
||||||
|
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.Adam(rnn.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Train the Model
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
images = Variable(images.view(-1, sequence_length, input_size))
|
||||||
|
labels = Variable(labels).cuda()
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad()
|
||||||
|
outputs = rnn(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f'
|
||||||
|
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
|
||||||
|
|
||||||
|
# Test the Model
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images.view(-1, sequence_length, input_size))
|
||||||
|
outputs = rnn(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted == labels).sum()
|
||||||
|
|
||||||
|
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
|
@ -0,0 +1,91 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
sequence_length = 28
|
||||||
|
input_size = 28
|
||||||
|
hidden_size = 128
|
||||||
|
num_layers = 2
|
||||||
|
num_classes = 10
|
||||||
|
batch_size = 100
|
||||||
|
num_epochs = 2
|
||||||
|
learning_rate = 0.003
|
||||||
|
|
||||||
|
# MNIST Dataset
|
||||||
|
train_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transforms.ToTensor(),
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# BiRNN Model (Many-to-One)
|
||||||
|
class BiRNN(nn.Module):
|
||||||
|
def __init__(self, input_size, hidden_size, num_layers, num_classes):
|
||||||
|
super(BiRNN, self).__init__()
|
||||||
|
self.lstm = nn.LSTM(input_size, hidden_size, num_layers,
|
||||||
|
batch_first=True, bidirectional=True)
|
||||||
|
self.fc = nn.Linear(hidden_size*2, num_classes) # 2 for bidirection
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
# Set initial states
|
||||||
|
h0 = Variable(torch.zeros(num_layers*2, x.size(0), hidden_size)).cuda() # 2 for bidirection
|
||||||
|
c0 = Variable(torch.zeros(num_layers*2, x.size(0), hidden_size)).cuda()
|
||||||
|
|
||||||
|
# Forward propagate RNN
|
||||||
|
out, _ = self.lstm(x, (h0, c0))
|
||||||
|
|
||||||
|
# Decode hidden state of last time step
|
||||||
|
out = self.fc(out[:, -1, :])
|
||||||
|
return out
|
||||||
|
|
||||||
|
rnn = BiRNN(input_size, hidden_size, num_layers, num_classes)
|
||||||
|
rnn.cuda()
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.Adam(rnn.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Train the Model
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
images = Variable(images.view(-1, sequence_length, input_size)).cuda()
|
||||||
|
labels = Variable(labels).cuda()
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad()
|
||||||
|
outputs = rnn(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f'
|
||||||
|
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
|
||||||
|
|
||||||
|
# Test the Model
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images.view(-1, sequence_length, input_size)).cuda()
|
||||||
|
outputs = rnn(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted.cpu() == labels).sum()
|
||||||
|
|
||||||
|
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
|
@ -0,0 +1,91 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
sequence_length = 28
|
||||||
|
input_size = 28
|
||||||
|
hidden_size = 128
|
||||||
|
num_layers = 2
|
||||||
|
num_classes = 10
|
||||||
|
batch_size = 100
|
||||||
|
num_epochs = 2
|
||||||
|
learning_rate = 0.003
|
||||||
|
|
||||||
|
# MNIST Dataset
|
||||||
|
train_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transforms.ToTensor(),
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
test_dataset = dsets.MNIST(root='./data/',
|
||||||
|
train=False,
|
||||||
|
transform=transforms.ToTensor())
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
|
||||||
|
batch_size=batch_size,
|
||||||
|
shuffle=False)
|
||||||
|
|
||||||
|
# BiRNN Model (Many-to-One)
|
||||||
|
class BiRNN(nn.Module):
|
||||||
|
def __init__(self, input_size, hidden_size, num_layers, num_classes):
|
||||||
|
super(BiRNN, self).__init__()
|
||||||
|
self.lstm = nn.LSTM(input_size, hidden_size, num_layers,
|
||||||
|
batch_first=True, bidirectional=True)
|
||||||
|
self.fc = nn.Linear(hidden_size*2, num_classes) # 2 for bidirection
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
# Set initial states
|
||||||
|
h0 = Variable(torch.zeros(num_layers*2, x.size(0), hidden_size)) # 2 for bidirection
|
||||||
|
c0 = Variable(torch.zeros(num_layers*2, x.size(0), hidden_size))
|
||||||
|
|
||||||
|
# Forward propagate RNN
|
||||||
|
out, _ = self.lstm(x, (h0, c0))
|
||||||
|
|
||||||
|
# Decode hidden state of last time step
|
||||||
|
out = self.fc(out[:, -1, :])
|
||||||
|
return out
|
||||||
|
|
||||||
|
rnn = BiRNN(input_size, hidden_size, num_layers, num_classes)
|
||||||
|
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.Adam(rnn.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Train the Model
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
for i, (images, labels) in enumerate(train_loader):
|
||||||
|
images = Variable(images.view(-1, sequence_length, input_size))
|
||||||
|
labels = Variable(labels)
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
optimizer.zero_grad()
|
||||||
|
outputs = rnn(images)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f'
|
||||||
|
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
|
||||||
|
|
||||||
|
# Test the Model
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
for images, labels in test_loader:
|
||||||
|
images = Variable(images.view(-1, sequence_length, input_size))
|
||||||
|
outputs = rnn(images)
|
||||||
|
_, predicted = torch.max(outputs.data, 1)
|
||||||
|
total += labels.size(0)
|
||||||
|
correct += (predicted == labels).sum()
|
||||||
|
|
||||||
|
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
|
134
tutorials/08 - Generative Adversarial Network/main-gpu.py
Normal file
134
tutorials/08 - Generative Adversarial Network/main-gpu.py
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
import torch
|
||||||
|
import torchvision
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
# Image Preprocessing
|
||||||
|
transform = transforms.Compose([
|
||||||
|
transforms.Scale(36),
|
||||||
|
transforms.RandomCrop(32),
|
||||||
|
transforms.ToTensor(),
|
||||||
|
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])
|
||||||
|
|
||||||
|
# CIFAR-10 Dataset
|
||||||
|
train_dataset = dsets.CIFAR10(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transform,
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=100,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
# 5x5 Convolution
|
||||||
|
def conv5x5(in_channels, out_channels, stride):
|
||||||
|
return nn.Conv2d(in_channels, out_channels, kernel_size=4,
|
||||||
|
stride=stride, padding=1, bias=False)
|
||||||
|
|
||||||
|
# Discriminator Model
|
||||||
|
class Discriminator(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Discriminator, self).__init__()
|
||||||
|
self.model = nn.Sequential(
|
||||||
|
conv5x5(3, 16, 2),
|
||||||
|
nn.LeakyReLU(0.2, inplace=True),
|
||||||
|
conv5x5(16, 32, 2),
|
||||||
|
nn.BatchNorm2d(32),
|
||||||
|
nn.LeakyReLU(0.2, inplace=True),
|
||||||
|
conv5x5(32, 64, 2),
|
||||||
|
nn.BatchNorm2d(64),
|
||||||
|
nn.LeakyReLU(0.2, inplace=True),
|
||||||
|
nn.Conv2d(64, 1, kernel_size=4),
|
||||||
|
nn.Sigmoid())
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.model(x)
|
||||||
|
out = out.view(out.size(0), -1)
|
||||||
|
return out
|
||||||
|
|
||||||
|
# 4x4 Transpose convolution
|
||||||
|
def conv_transpose4x4(in_channels, out_channels, stride=1, padding=1, bias=False):
|
||||||
|
return nn.ConvTranspose2d(in_channels, out_channels, kernel_size=4,
|
||||||
|
stride=stride, padding=padding, bias=bias)
|
||||||
|
|
||||||
|
# Generator Model
|
||||||
|
class Generator(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Generator, self).__init__()
|
||||||
|
self.model = nn.Sequential(
|
||||||
|
conv_transpose4x4(128, 64, padding=0),
|
||||||
|
nn.BatchNorm2d(64),
|
||||||
|
nn.ReLU(inplace=True),
|
||||||
|
conv_transpose4x4(64, 32, 2),
|
||||||
|
nn.BatchNorm2d(32),
|
||||||
|
nn.ReLU(inplace=True),
|
||||||
|
conv_transpose4x4(32, 16, 2),
|
||||||
|
nn.BatchNorm2d(16),
|
||||||
|
nn.ReLU(inplace=True),
|
||||||
|
conv_transpose4x4(16, 3, 2, bias=True),
|
||||||
|
nn.Tanh())
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = x.view(x.size(0), 128, 1, 1)
|
||||||
|
out = self.model(x)
|
||||||
|
return out
|
||||||
|
|
||||||
|
discriminator = Discriminator()
|
||||||
|
generator = Generator()
|
||||||
|
discriminator.cuda()
|
||||||
|
generator.cuda()
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.BCELoss()
|
||||||
|
lr = 0.002
|
||||||
|
d_optimizer = torch.optim.Adam(discriminator.parameters(), lr=lr)
|
||||||
|
g_optimizer = torch.optim.Adam(generator.parameters(), lr=lr)
|
||||||
|
|
||||||
|
# Training
|
||||||
|
for epoch in range(50):
|
||||||
|
for i, (images, _) in enumerate(train_loader):
|
||||||
|
images = Variable(images.cuda())
|
||||||
|
real_labels = Variable(torch.ones(images.size(0)).cuda())
|
||||||
|
fake_labels = Variable(torch.zeros(images.size(0)).cuda())
|
||||||
|
|
||||||
|
# Train the discriminator
|
||||||
|
discriminator.zero_grad()
|
||||||
|
outputs = discriminator(images)
|
||||||
|
real_loss = criterion(outputs, real_labels)
|
||||||
|
real_score = outputs
|
||||||
|
|
||||||
|
noise = Variable(torch.randn(images.size(0), 128).cuda())
|
||||||
|
fake_images = generator(noise)
|
||||||
|
outputs = discriminator(fake_images)
|
||||||
|
fake_loss = criterion(outputs, fake_labels)
|
||||||
|
fake_score = outputs
|
||||||
|
|
||||||
|
d_loss = real_loss + fake_loss
|
||||||
|
d_loss.backward()
|
||||||
|
d_optimizer.step()
|
||||||
|
|
||||||
|
# Train the generator
|
||||||
|
generator.zero_grad()
|
||||||
|
noise = Variable(torch.randn(images.size(0), 128).cuda())
|
||||||
|
fake_images = generator(noise)
|
||||||
|
outputs = discriminator(fake_images)
|
||||||
|
g_loss = criterion(outputs, real_labels)
|
||||||
|
g_loss.backward()
|
||||||
|
g_optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print('Epoch [%d/%d], Step[%d/%d], d_loss: %.4f, g_loss: %.4f, '
|
||||||
|
'D(x): %.2f, D(G(z)): %.2f'
|
||||||
|
%(epoch, 50, i+1, 500, d_loss.data[0], g_loss.data[0],
|
||||||
|
real_score.cpu().data.mean(), fake_score.cpu().data.mean()))
|
||||||
|
|
||||||
|
# Save the sampled images
|
||||||
|
torchvision.utils.save_image(fake_images.data,
|
||||||
|
'./data/fake_samples_%d_%d.png' %(epoch+1, i+1))
|
||||||
|
|
||||||
|
# Save the Models
|
||||||
|
torch.save(generator, './generator.pkl')
|
||||||
|
torch.save(discriminator, './discriminator.pkl')
|
134
tutorials/08 - Generative Adversarial Network/main.py
Normal file
134
tutorials/08 - Generative Adversarial Network/main.py
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
import torch
|
||||||
|
import torchvision
|
||||||
|
import torch.nn as nn
|
||||||
|
import torchvision.datasets as dsets
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
from torch.autograd import Variable
|
||||||
|
|
||||||
|
# Image Preprocessing
|
||||||
|
transform = transforms.Compose([
|
||||||
|
transforms.Scale(36),
|
||||||
|
transforms.RandomCrop(32),
|
||||||
|
transforms.ToTensor(),
|
||||||
|
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])
|
||||||
|
|
||||||
|
# CIFAR-10 Dataset
|
||||||
|
train_dataset = dsets.CIFAR10(root='./data/',
|
||||||
|
train=True,
|
||||||
|
transform=transform,
|
||||||
|
download=True)
|
||||||
|
|
||||||
|
# Data Loader (Input Pipeline)
|
||||||
|
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
|
||||||
|
batch_size=100,
|
||||||
|
shuffle=True)
|
||||||
|
|
||||||
|
# 5x5 Convolution
|
||||||
|
def conv5x5(in_channels, out_channels, stride):
|
||||||
|
return nn.Conv2d(in_channels, out_channels, kernel_size=4,
|
||||||
|
stride=stride, padding=1, bias=False)
|
||||||
|
|
||||||
|
# Discriminator Model
|
||||||
|
class Discriminator(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Discriminator, self).__init__()
|
||||||
|
self.model = nn.Sequential(
|
||||||
|
conv5x5(3, 16, 2),
|
||||||
|
nn.LeakyReLU(0.2, inplace=True),
|
||||||
|
conv5x5(16, 32, 2),
|
||||||
|
nn.BatchNorm2d(32),
|
||||||
|
nn.LeakyReLU(0.2, inplace=True),
|
||||||
|
conv5x5(32, 64, 2),
|
||||||
|
nn.BatchNorm2d(64),
|
||||||
|
nn.LeakyReLU(0.2, inplace=True),
|
||||||
|
nn.Conv2d(64, 1, kernel_size=4),
|
||||||
|
nn.Sigmoid())
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.model(x)
|
||||||
|
out = out.view(out.size(0), -1)
|
||||||
|
return out
|
||||||
|
|
||||||
|
# 4x4 Transpose convolution
|
||||||
|
def conv_transpose4x4(in_channels, out_channels, stride=1, padding=1, bias=False):
|
||||||
|
return nn.ConvTranspose2d(in_channels, out_channels, kernel_size=4,
|
||||||
|
stride=stride, padding=padding, bias=bias)
|
||||||
|
|
||||||
|
# Generator Model
|
||||||
|
class Generator(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Generator, self).__init__()
|
||||||
|
self.model = nn.Sequential(
|
||||||
|
conv_transpose4x4(128, 64, padding=0),
|
||||||
|
nn.BatchNorm2d(64),
|
||||||
|
nn.ReLU(inplace=True),
|
||||||
|
conv_transpose4x4(64, 32, 2),
|
||||||
|
nn.BatchNorm2d(32),
|
||||||
|
nn.ReLU(inplace=True),
|
||||||
|
conv_transpose4x4(32, 16, 2),
|
||||||
|
nn.BatchNorm2d(16),
|
||||||
|
nn.ReLU(inplace=True),
|
||||||
|
conv_transpose4x4(16, 3, 2, bias=True),
|
||||||
|
nn.Tanh())
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = x.view(x.size(0), 128, 1, 1)
|
||||||
|
out = self.model(x)
|
||||||
|
return out
|
||||||
|
|
||||||
|
discriminator = Discriminator()
|
||||||
|
generator = Generator()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.BCELoss()
|
||||||
|
lr = 0.0002
|
||||||
|
d_optimizer = torch.optim.Adam(discriminator.parameters(), lr=lr)
|
||||||
|
g_optimizer = torch.optim.Adam(generator.parameters(), lr=lr)
|
||||||
|
|
||||||
|
# Training
|
||||||
|
for epoch in range(50):
|
||||||
|
for i, (images, _) in enumerate(train_loader):
|
||||||
|
images = Variable(images.cuda())
|
||||||
|
real_labels = Variable(torch.ones(images.size(0)))
|
||||||
|
fake_labels = Variable(torch.zeros(images.size(0)))
|
||||||
|
|
||||||
|
# Train the discriminator
|
||||||
|
discriminator.zero_grad()
|
||||||
|
outputs = discriminator(images)
|
||||||
|
real_loss = criterion(outputs, real_labels)
|
||||||
|
real_score = outputs
|
||||||
|
|
||||||
|
noise = Variable(torch.randn(images.size(0), 128))
|
||||||
|
fake_images = generator(noise)
|
||||||
|
outputs = discriminator(fake_images)
|
||||||
|
fake_loss = criterion(outputs, fake_labels)
|
||||||
|
fake_score = outputs
|
||||||
|
|
||||||
|
d_loss = real_loss + fake_loss
|
||||||
|
d_loss.backward()
|
||||||
|
d_optimizer.step()
|
||||||
|
|
||||||
|
# Train the generator
|
||||||
|
generator.zero_grad()
|
||||||
|
noise = Variable(torch.randn(images.size(0), 128))
|
||||||
|
fake_images = generator(noise)
|
||||||
|
outputs = discriminator(fake_images)
|
||||||
|
g_loss = criterion(outputs, real_labels)
|
||||||
|
g_loss.backward()
|
||||||
|
g_optimizer.step()
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print('Epoch [%d/%d], Step[%d/%d], d_loss: %.4f, g_loss: %.4f, '
|
||||||
|
'D(x): %.2f, D(G(z)): %.2f'
|
||||||
|
%(epoch, 50, i+1, 500, d_loss.data[0], g_loss.data[0],
|
||||||
|
real_score.data.mean(), fake_score.data.mean()))
|
||||||
|
|
||||||
|
# Save the sampled images
|
||||||
|
torchvision.utils.save_image(fake_images.data,
|
||||||
|
'./data/fake_samples_%d_%d.png' %(epoch+1, i+1))
|
||||||
|
|
||||||
|
# Save checkpoint files
|
||||||
|
torch.save(generator.state_dict(), './generator.pth')
|
||||||
|
torch.save(discriminator.state_dict(), './discriminator.pth')
|
42068
tutorials/09 - Language Model/data/train.txt
Normal file
42068
tutorials/09 - Language Model/data/train.txt
Normal file
File diff suppressed because it is too large
Load Diff
46
tutorials/09 - Language Model/data_utils.py
Normal file
46
tutorials/09 - Language Model/data_utils.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import torch
|
||||||
|
import os
|
||||||
|
|
||||||
|
class Dictionary(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.word2idx = {}
|
||||||
|
self.idx2word = {}
|
||||||
|
self.idx = 0
|
||||||
|
|
||||||
|
def add_word(self, word):
|
||||||
|
if not word in self.word2idx:
|
||||||
|
self.word2idx[word] = self.idx
|
||||||
|
self.idx2word[self.idx] = word
|
||||||
|
self.idx += 1
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.word2idx)
|
||||||
|
|
||||||
|
class Corpus(object):
|
||||||
|
def __init__(self, path='./data'):
|
||||||
|
self.dictionary = Dictionary()
|
||||||
|
self.train = os.path.join(path, 'train.txt')
|
||||||
|
self.test = os.path.join(path, 'test.txt')
|
||||||
|
|
||||||
|
def get_data(self, path, batch_size=20):
|
||||||
|
# Add words to the dictionary
|
||||||
|
with open(path, 'r') as f:
|
||||||
|
tokens = 0
|
||||||
|
for line in f:
|
||||||
|
words = line.split() + ['<eos>']
|
||||||
|
tokens += len(words)
|
||||||
|
for word in words:
|
||||||
|
self.dictionary.add_word(word)
|
||||||
|
|
||||||
|
# Tokenize the file content
|
||||||
|
ids = torch.LongTensor(tokens)
|
||||||
|
token = 0
|
||||||
|
with open(path, 'r') as f:
|
||||||
|
for line in f:
|
||||||
|
words = line.split() + ['<eos>']
|
||||||
|
for word in words:
|
||||||
|
ids[token] = self.dictionary.word2idx[word]
|
||||||
|
token += 1
|
||||||
|
num_batches = ids.size(0) // batch_size
|
||||||
|
ids = ids[:num_batches*batch_size]
|
||||||
|
return ids.view(batch_size, -1)
|
123
tutorials/09 - Language Model/main-gpu.py
Normal file
123
tutorials/09 - Language Model/main-gpu.py
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
# Some part of the code was referenced from below.
|
||||||
|
# https://github.com/pytorch/examples/tree/master/word_language_model
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import numpy as np
|
||||||
|
from torch.autograd import Variable
|
||||||
|
from data_utils import Dictionary, Corpus
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
embed_size = 128
|
||||||
|
hidden_size = 1024
|
||||||
|
num_layers = 1
|
||||||
|
num_epochs = 5
|
||||||
|
num_samples = 1000 # number of words to be sampled
|
||||||
|
batch_size = 20
|
||||||
|
seq_length = 30
|
||||||
|
learning_rate = 0.002
|
||||||
|
|
||||||
|
# Load Penn Treebank Dataset
|
||||||
|
train_path = './data/train.txt'
|
||||||
|
sample_path = './sample.txt'
|
||||||
|
corpus = Corpus()
|
||||||
|
ids = corpus.get_data(train_path, batch_size)
|
||||||
|
vocab_size = len(corpus.dictionary)
|
||||||
|
num_batches = ids.size(1) // seq_length
|
||||||
|
|
||||||
|
# RNN Based Language Model
|
||||||
|
class RNNLM(nn.Module):
|
||||||
|
def __init__(self, vocab_size, embed_size, hidden_size, num_layers):
|
||||||
|
super(RNNLM, self).__init__()
|
||||||
|
self.embed = nn.Embedding(vocab_size, embed_size)
|
||||||
|
self.lstm = nn.LSTM(embed_size, hidden_size, num_layers, batch_first=True)
|
||||||
|
self.linear = nn.Linear(hidden_size, vocab_size)
|
||||||
|
|
||||||
|
self.init_weights()
|
||||||
|
|
||||||
|
def init_weights(self):
|
||||||
|
self.embed.weight.data.uniform_(-0.1, 0.1)
|
||||||
|
self.linear.bias.data.fill_(0)
|
||||||
|
self.linear.weight.data.uniform_(-0.1, 0.1)
|
||||||
|
|
||||||
|
def forward(self, x, h):
|
||||||
|
# Embed word ids to vectors
|
||||||
|
x = self.embed(x)
|
||||||
|
|
||||||
|
# Forward propagate RNN
|
||||||
|
out, h = self.lstm(x, h)
|
||||||
|
|
||||||
|
# Reshape output to (batch_size*sequence_length, hidden_size)
|
||||||
|
out = out.contiguous().view(out.size(0)*out.size(1), out.size(2))
|
||||||
|
|
||||||
|
# Decode hidden states of all time step
|
||||||
|
out = self.linear(out)
|
||||||
|
return out, h
|
||||||
|
|
||||||
|
model = RNNLM(vocab_size, embed_size, hidden_size, num_layers)
|
||||||
|
model.cuda()
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Truncated Backpropagation
|
||||||
|
def detach(states):
|
||||||
|
return [Variable(state.data) for state in states]
|
||||||
|
|
||||||
|
# Training
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
# Initial hidden and memory states
|
||||||
|
states = (Variable(torch.zeros(num_layers, batch_size, hidden_size)).cuda(),
|
||||||
|
Variable(torch.zeros(num_layers, batch_size, hidden_size)).cuda())
|
||||||
|
|
||||||
|
for i in range(0, ids.size(1) - seq_length, seq_length):
|
||||||
|
# Get batch inputs and targets
|
||||||
|
inputs = Variable(ids[:, i:i+seq_length]).cuda()
|
||||||
|
targets = Variable(ids[:, (i+1):(i+1)+seq_length].contiguous()).cuda()
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
model.zero_grad()
|
||||||
|
states = detach(states)
|
||||||
|
outputs, states = model(inputs, states)
|
||||||
|
loss = criterion(outputs, targets.view(-1))
|
||||||
|
loss.backward()
|
||||||
|
torch.nn.utils.clip_grad_norm(model.parameters(), 0.5)
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
step = (i+1) // seq_length
|
||||||
|
if step % 100 == 0:
|
||||||
|
print ('Epoch [%d/%d], Step[%d/%d], Loss: %.3f, Perplexity: %5.2f' %
|
||||||
|
(epoch+1, num_epochs, step, num_batches, loss.data[0], np.exp(loss.data[0])))
|
||||||
|
|
||||||
|
# Sampling
|
||||||
|
with open(sample_path, 'w') as f:
|
||||||
|
# Set intial hidden ane memory states
|
||||||
|
state = (Variable(torch.zeros(num_layers, 1, hidden_size)).cuda(),
|
||||||
|
Variable(torch.zeros(num_layers, 1, hidden_size)).cuda())
|
||||||
|
|
||||||
|
# Select one word id randomly
|
||||||
|
prob = torch.ones(vocab_size)
|
||||||
|
input = Variable(torch.multinomial(prob, num_samples=1).unsqueeze(1),
|
||||||
|
volatile=True).cuda()
|
||||||
|
|
||||||
|
for i in range(num_samples):
|
||||||
|
# Forward propagate rnn
|
||||||
|
output, state = model(input, state)
|
||||||
|
|
||||||
|
# Sample a word id
|
||||||
|
prob = output.squeeze().data.exp().cpu()
|
||||||
|
word_id = torch.multinomial(prob, 1)[0]
|
||||||
|
|
||||||
|
# Feed sampled word id to next time step
|
||||||
|
input.data.fill_(word_id)
|
||||||
|
|
||||||
|
# File write
|
||||||
|
word = corpus.dictionary.idx2word[word_id]
|
||||||
|
word = '\n' if word == '<eos>' else word + ' '
|
||||||
|
f.write(word)
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print('Sampled [%d/%d] words and save to %s'%(i+1, num_samples, sample_path))
|
||||||
|
|
||||||
|
# Save the Trained Model
|
||||||
|
torch.save(model, 'model.pkl')
|
123
tutorials/09 - Language Model/main.py
Normal file
123
tutorials/09 - Language Model/main.py
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
# Some part of the code was referenced from below.
|
||||||
|
# https://github.com/pytorch/examples/tree/master/word_language_model
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import numpy as np
|
||||||
|
from torch.autograd import Variable
|
||||||
|
from data_utils import Dictionary, Corpus
|
||||||
|
|
||||||
|
# Hyper Parameters
|
||||||
|
embed_size = 128
|
||||||
|
hidden_size = 1024
|
||||||
|
num_layers = 1
|
||||||
|
num_epochs = 5
|
||||||
|
num_samples = 1000 # number of words to be sampled
|
||||||
|
batch_size = 20
|
||||||
|
seq_length = 30
|
||||||
|
learning_rate = 0.002
|
||||||
|
|
||||||
|
# Load Penn Treebank Dataset
|
||||||
|
train_path = './data/train.txt'
|
||||||
|
sample_path = './sample.txt'
|
||||||
|
corpus = Corpus()
|
||||||
|
ids = corpus.get_data(train_path, batch_size)
|
||||||
|
vocab_size = len(corpus.dictionary)
|
||||||
|
num_batches = ids.size(1) // seq_length
|
||||||
|
|
||||||
|
# RNN Based Language Model
|
||||||
|
class RNNLM(nn.Module):
|
||||||
|
def __init__(self, vocab_size, embed_size, hidden_size, num_layers):
|
||||||
|
super(RNNLM, self).__init__()
|
||||||
|
self.embed = nn.Embedding(vocab_size, embed_size)
|
||||||
|
self.lstm = nn.LSTM(embed_size, hidden_size, num_layers, batch_first=True)
|
||||||
|
self.linear = nn.Linear(hidden_size, vocab_size)
|
||||||
|
|
||||||
|
self.init_weights()
|
||||||
|
|
||||||
|
def init_weights(self):
|
||||||
|
self.embed.weight.data.uniform_(-0.1, 0.1)
|
||||||
|
self.linear.bias.data.fill_(0)
|
||||||
|
self.linear.weight.data.uniform_(-0.1, 0.1)
|
||||||
|
|
||||||
|
def forward(self, x, h):
|
||||||
|
# Embed word ids to vectors
|
||||||
|
x = self.embed(x)
|
||||||
|
|
||||||
|
# Forward propagate RNN
|
||||||
|
out, h = self.lstm(x, h)
|
||||||
|
|
||||||
|
# Reshape output to (batch_size*sequence_length, hidden_size)
|
||||||
|
out = out.contiguous().view(out.size(0)*out.size(1), out.size(2))
|
||||||
|
|
||||||
|
# Decode hidden states of all time step
|
||||||
|
out = self.linear(out)
|
||||||
|
return out, h
|
||||||
|
|
||||||
|
model = RNNLM(vocab_size, embed_size, hidden_size, num_layers)
|
||||||
|
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.CrossEntropyLoss()
|
||||||
|
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Truncated Backpropagation
|
||||||
|
def detach(states):
|
||||||
|
return [Variable(state.data) for state in states]
|
||||||
|
|
||||||
|
# Training
|
||||||
|
for epoch in range(num_epochs):
|
||||||
|
# Initial hidden and memory states
|
||||||
|
states = (Variable(torch.zeros(num_layers, batch_size, hidden_size)),
|
||||||
|
Variable(torch.zeros(num_layers, batch_size, hidden_size)))
|
||||||
|
|
||||||
|
for i in range(0, ids.size(1) - seq_length, seq_length):
|
||||||
|
# Get batch inputs and targets
|
||||||
|
inputs = Variable(ids[:, i:i+seq_length])
|
||||||
|
targets = Variable(ids[:, (i+1):(i+1)+seq_length].contiguous())
|
||||||
|
|
||||||
|
# Forward + Backward + Optimize
|
||||||
|
model.zero_grad()
|
||||||
|
states = detach(states)
|
||||||
|
outputs, states = model(inputs, states)
|
||||||
|
loss = criterion(outputs, targets.view(-1))
|
||||||
|
loss.backward()
|
||||||
|
torch.nn.utils.clip_grad_norm(model.parameters(), 0.5)
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
step = (i+1) // seq_length
|
||||||
|
if step % 100 == 0:
|
||||||
|
print ('Epoch [%d/%d], Step[%d/%d], Loss: %.3f, Perplexity: %5.2f' %
|
||||||
|
(epoch+1, num_epochs, step, num_batches, loss.data[0], np.exp(loss.data[0])))
|
||||||
|
|
||||||
|
# Sampling
|
||||||
|
with open(sample_path, 'w') as f:
|
||||||
|
# Set intial hidden ane memory states
|
||||||
|
state = (Variable(torch.zeros(num_layers, 1, hidden_size)),
|
||||||
|
Variable(torch.zeros(num_layers, 1, hidden_size)))
|
||||||
|
|
||||||
|
# Select one word id randomly
|
||||||
|
prob = torch.ones(vocab_size)
|
||||||
|
input = Variable(torch.multinomial(prob, num_samples=1).unsqueeze(1),
|
||||||
|
volatile=True)
|
||||||
|
|
||||||
|
for i in range(num_samples):
|
||||||
|
# Forward propagate rnn
|
||||||
|
output, state = model(input, state)
|
||||||
|
|
||||||
|
# Sample a word id
|
||||||
|
prob = output.squeeze().data.exp()
|
||||||
|
word_id = torch.multinomial(prob, 1)[0]
|
||||||
|
|
||||||
|
# Feed sampled word id to next time step
|
||||||
|
input.data.fill_(word_id)
|
||||||
|
|
||||||
|
# File write
|
||||||
|
word = corpus.dictionary.idx2word[word_id]
|
||||||
|
word = '\n' if word == '<eos>' else word + ' '
|
||||||
|
f.write(word)
|
||||||
|
|
||||||
|
if (i+1) % 100 == 0:
|
||||||
|
print('Sampled [%d/%d] words and save to %s'%(i+1, num_samples, sample_path))
|
||||||
|
|
||||||
|
# Save the Trained Model
|
||||||
|
torch.save(model, 'model.pkl')
|
359
tutorials/10 - Deep Q Network/ReplayMemory.ipynb
Normal file
359
tutorials/10 - Deep Q Network/ReplayMemory.ipynb
Normal file
@ -0,0 +1,359 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# PyTorch DQN Implemenation\n",
|
||||||
|
"\n",
|
||||||
|
"<br/>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import torch\n",
|
||||||
|
"import torch.nn as nn\n",
|
||||||
|
"import gym\n",
|
||||||
|
"import random\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import torchvision.transforms as transforms\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"from torch.autograd import Variable\n",
|
||||||
|
"from collections import deque, namedtuple"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[2017-03-09 21:31:48,174] Making new env: CartPole-v0\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"env = gym.envs.make(\"CartPole-v0\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"class Net(nn.Module):\n",
|
||||||
|
" def __init__(self):\n",
|
||||||
|
" super(Net, self).__init__()\n",
|
||||||
|
" self.fc1 = nn.Linear(4, 128)\n",
|
||||||
|
" self.tanh = nn.Tanh()\n",
|
||||||
|
" self.fc2 = nn.Linear(128, 2)\n",
|
||||||
|
" self.init_weights()\n",
|
||||||
|
" \n",
|
||||||
|
" def init_weights(self):\n",
|
||||||
|
" self.fc1.weight.data.uniform_(-0.1, 0.1)\n",
|
||||||
|
" self.fc2.weight.data.uniform_(-0.1, 0.1)\n",
|
||||||
|
" \n",
|
||||||
|
" def forward(self, x):\n",
|
||||||
|
" out = self.fc1(x)\n",
|
||||||
|
" out = self.tanh(out)\n",
|
||||||
|
" out = self.fc2(out)\n",
|
||||||
|
" return out"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def make_epsilon_greedy_policy(network, epsilon, nA):\n",
|
||||||
|
" def policy(state):\n",
|
||||||
|
" sample = random.random()\n",
|
||||||
|
" if sample < (1-epsilon) + (epsilon/nA):\n",
|
||||||
|
" q_values = network(state.view(1, -1))\n",
|
||||||
|
" action = q_values.data.max(1)[1][0, 0]\n",
|
||||||
|
" else:\n",
|
||||||
|
" action = random.randrange(nA)\n",
|
||||||
|
" return action\n",
|
||||||
|
" return policy"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"class ReplayMemory(object):\n",
|
||||||
|
" \n",
|
||||||
|
" def __init__(self, capacity):\n",
|
||||||
|
" self.memory = deque()\n",
|
||||||
|
" self.capacity = capacity\n",
|
||||||
|
" \n",
|
||||||
|
" def push(self, transition):\n",
|
||||||
|
" if len(self.memory) > self.capacity:\n",
|
||||||
|
" self.memory.popleft()\n",
|
||||||
|
" self.memory.append(transition)\n",
|
||||||
|
" \n",
|
||||||
|
" def sample(self, batch_size):\n",
|
||||||
|
" return random.sample(self.memory, batch_size)\n",
|
||||||
|
" \n",
|
||||||
|
" def __len__(self):\n",
|
||||||
|
" return len(self.memory)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def to_tensor(ndarray, volatile=False):\n",
|
||||||
|
" return Variable(torch.from_numpy(ndarray), volatile=volatile).float()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def deep_q_learning(num_episodes=10, batch_size=100, \n",
|
||||||
|
" discount_factor=0.95, epsilon=0.1, epsilon_decay=0.95):\n",
|
||||||
|
"\n",
|
||||||
|
" # Q-Network and memory \n",
|
||||||
|
" net = Net()\n",
|
||||||
|
" memory = ReplayMemory(10000)\n",
|
||||||
|
" \n",
|
||||||
|
" # Loss and Optimizer\n",
|
||||||
|
" criterion = nn.MSELoss()\n",
|
||||||
|
" optimizer = torch.optim.Adam(net.parameters(), lr=0.001)\n",
|
||||||
|
" \n",
|
||||||
|
" for i_episode in range(num_episodes):\n",
|
||||||
|
" \n",
|
||||||
|
" # Set policy (TODO: decaying epsilon)\n",
|
||||||
|
" #if (i_episode+1) % 100 == 0:\n",
|
||||||
|
" # epsilon *= 0.9\n",
|
||||||
|
" \n",
|
||||||
|
" policy = make_epsilon_greedy_policy(\n",
|
||||||
|
" net, epsilon, env.action_space.n)\n",
|
||||||
|
" \n",
|
||||||
|
" # Start an episode\n",
|
||||||
|
" state = env.reset()\n",
|
||||||
|
" \n",
|
||||||
|
" for t in range(10000):\n",
|
||||||
|
" \n",
|
||||||
|
" # Sample action from epsilon greed policy\n",
|
||||||
|
" action = policy(to_tensor(state)) \n",
|
||||||
|
" next_state, reward, done, _ = env.step(action)\n",
|
||||||
|
" \n",
|
||||||
|
" \n",
|
||||||
|
" # Restore transition in memory\n",
|
||||||
|
" memory.push([state, action, reward, next_state])\n",
|
||||||
|
" \n",
|
||||||
|
" \n",
|
||||||
|
" if len(memory) >= batch_size:\n",
|
||||||
|
" # Sample mini-batch transitions from memory\n",
|
||||||
|
" batch = memory.sample(batch_size)\n",
|
||||||
|
" state_batch = np.vstack([trans[0] for trans in batch])\n",
|
||||||
|
" action_batch =np.vstack([trans[1] for trans in batch]) \n",
|
||||||
|
" reward_batch = np.vstack([trans[2] for trans in batch])\n",
|
||||||
|
" next_state_batch = np.vstack([trans[3] for trans in batch])\n",
|
||||||
|
" \n",
|
||||||
|
" # Forward + Backward + Opimize\n",
|
||||||
|
" net.zero_grad()\n",
|
||||||
|
" q_values = net(to_tensor(state_batch))\n",
|
||||||
|
" next_q_values = net(to_tensor(next_state_batch, volatile=True))\n",
|
||||||
|
" next_q_values.volatile = False\n",
|
||||||
|
" \n",
|
||||||
|
" td_target = to_tensor(reward_batch) + discount_factor * (next_q_values).max(1)[0]\n",
|
||||||
|
" loss = criterion(q_values.gather(1, \n",
|
||||||
|
" to_tensor(action_batch).long().view(-1, 1)), td_target)\n",
|
||||||
|
" loss.backward()\n",
|
||||||
|
" optimizer.step()\n",
|
||||||
|
" \n",
|
||||||
|
" if done:\n",
|
||||||
|
" break\n",
|
||||||
|
" \n",
|
||||||
|
" state = next_state\n",
|
||||||
|
" \n",
|
||||||
|
" if len(memory) >= batch_size and (i_episode+1) % 10 == 0:\n",
|
||||||
|
" print ('episode: %d, time: %d, loss: %.4f' %(i_episode, t, loss.data[0]))\n",
|
||||||
|
" "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"episode: 9, time: 9, loss: 0.9945\n",
|
||||||
|
"episode: 19, time: 9, loss: 1.8221\n",
|
||||||
|
"episode: 29, time: 9, loss: 4.3124\n",
|
||||||
|
"episode: 39, time: 8, loss: 6.9764\n",
|
||||||
|
"episode: 49, time: 9, loss: 6.8300\n",
|
||||||
|
"episode: 59, time: 8, loss: 5.5186\n",
|
||||||
|
"episode: 69, time: 9, loss: 4.1160\n",
|
||||||
|
"episode: 79, time: 9, loss: 2.4802\n",
|
||||||
|
"episode: 89, time: 13, loss: 0.7890\n",
|
||||||
|
"episode: 99, time: 10, loss: 0.2805\n",
|
||||||
|
"episode: 109, time: 12, loss: 0.1323\n",
|
||||||
|
"episode: 119, time: 13, loss: 0.0519\n",
|
||||||
|
"episode: 129, time: 18, loss: 0.0176\n",
|
||||||
|
"episode: 139, time: 22, loss: 0.0067\n",
|
||||||
|
"episode: 149, time: 17, loss: 0.0114\n",
|
||||||
|
"episode: 159, time: 26, loss: 0.0017\n",
|
||||||
|
"episode: 169, time: 23, loss: 0.0018\n",
|
||||||
|
"episode: 179, time: 21, loss: 0.0023\n",
|
||||||
|
"episode: 189, time: 11, loss: 0.0024\n",
|
||||||
|
"episode: 199, time: 7, loss: 0.0040\n",
|
||||||
|
"episode: 209, time: 8, loss: 0.0030\n",
|
||||||
|
"episode: 219, time: 7, loss: 0.0070\n",
|
||||||
|
"episode: 229, time: 9, loss: 0.0031\n",
|
||||||
|
"episode: 239, time: 9, loss: 0.0029\n",
|
||||||
|
"episode: 249, time: 8, loss: 0.0046\n",
|
||||||
|
"episode: 259, time: 8, loss: 0.0009\n",
|
||||||
|
"episode: 269, time: 10, loss: 0.0020\n",
|
||||||
|
"episode: 279, time: 9, loss: 0.0025\n",
|
||||||
|
"episode: 289, time: 8, loss: 0.0015\n",
|
||||||
|
"episode: 299, time: 10, loss: 0.0009\n",
|
||||||
|
"episode: 309, time: 8, loss: 0.0012\n",
|
||||||
|
"episode: 319, time: 8, loss: 0.0034\n",
|
||||||
|
"episode: 329, time: 8, loss: 0.0008\n",
|
||||||
|
"episode: 339, time: 9, loss: 0.0021\n",
|
||||||
|
"episode: 349, time: 8, loss: 0.0018\n",
|
||||||
|
"episode: 359, time: 9, loss: 0.0017\n",
|
||||||
|
"episode: 369, time: 9, loss: 0.0006\n",
|
||||||
|
"episode: 379, time: 9, loss: 0.0023\n",
|
||||||
|
"episode: 389, time: 10, loss: 0.0017\n",
|
||||||
|
"episode: 399, time: 8, loss: 0.0018\n",
|
||||||
|
"episode: 409, time: 8, loss: 0.0023\n",
|
||||||
|
"episode: 419, time: 9, loss: 0.0020\n",
|
||||||
|
"episode: 429, time: 9, loss: 0.0006\n",
|
||||||
|
"episode: 439, time: 10, loss: 0.0006\n",
|
||||||
|
"episode: 449, time: 10, loss: 0.0025\n",
|
||||||
|
"episode: 459, time: 9, loss: 0.0013\n",
|
||||||
|
"episode: 469, time: 8, loss: 0.0011\n",
|
||||||
|
"episode: 479, time: 8, loss: 0.0005\n",
|
||||||
|
"episode: 489, time: 8, loss: 0.0004\n",
|
||||||
|
"episode: 499, time: 7, loss: 0.0017\n",
|
||||||
|
"episode: 509, time: 7, loss: 0.0004\n",
|
||||||
|
"episode: 519, time: 10, loss: 0.0008\n",
|
||||||
|
"episode: 529, time: 11, loss: 0.0006\n",
|
||||||
|
"episode: 539, time: 9, loss: 0.0010\n",
|
||||||
|
"episode: 549, time: 8, loss: 0.0006\n",
|
||||||
|
"episode: 559, time: 8, loss: 0.0012\n",
|
||||||
|
"episode: 569, time: 9, loss: 0.0011\n",
|
||||||
|
"episode: 579, time: 8, loss: 0.0010\n",
|
||||||
|
"episode: 589, time: 8, loss: 0.0008\n",
|
||||||
|
"episode: 599, time: 10, loss: 0.0010\n",
|
||||||
|
"episode: 609, time: 8, loss: 0.0005\n",
|
||||||
|
"episode: 619, time: 9, loss: 0.0004\n",
|
||||||
|
"episode: 629, time: 8, loss: 0.0007\n",
|
||||||
|
"episode: 639, time: 10, loss: 0.0014\n",
|
||||||
|
"episode: 649, time: 10, loss: 0.0004\n",
|
||||||
|
"episode: 659, time: 9, loss: 0.0008\n",
|
||||||
|
"episode: 669, time: 8, loss: 0.0005\n",
|
||||||
|
"episode: 679, time: 8, loss: 0.0003\n",
|
||||||
|
"episode: 689, time: 9, loss: 0.0009\n",
|
||||||
|
"episode: 699, time: 8, loss: 0.0004\n",
|
||||||
|
"episode: 709, time: 8, loss: 0.0013\n",
|
||||||
|
"episode: 719, time: 8, loss: 0.0006\n",
|
||||||
|
"episode: 729, time: 7, loss: 0.0021\n",
|
||||||
|
"episode: 739, time: 9, loss: 0.0023\n",
|
||||||
|
"episode: 749, time: 9, loss: 0.0039\n",
|
||||||
|
"episode: 759, time: 8, loss: 0.0030\n",
|
||||||
|
"episode: 769, time: 9, loss: 0.0016\n",
|
||||||
|
"episode: 779, time: 7, loss: 0.0041\n",
|
||||||
|
"episode: 789, time: 8, loss: 0.0050\n",
|
||||||
|
"episode: 799, time: 8, loss: 0.0041\n",
|
||||||
|
"episode: 809, time: 11, loss: 0.0053\n",
|
||||||
|
"episode: 819, time: 7, loss: 0.0018\n",
|
||||||
|
"episode: 829, time: 9, loss: 0.0019\n",
|
||||||
|
"episode: 839, time: 11, loss: 0.0017\n",
|
||||||
|
"episode: 849, time: 8, loss: 0.0029\n",
|
||||||
|
"episode: 859, time: 9, loss: 0.0012\n",
|
||||||
|
"episode: 869, time: 9, loss: 0.0036\n",
|
||||||
|
"episode: 879, time: 7, loss: 0.0017\n",
|
||||||
|
"episode: 889, time: 9, loss: 0.0016\n",
|
||||||
|
"episode: 899, time: 10, loss: 0.0023\n",
|
||||||
|
"episode: 909, time: 8, loss: 0.0032\n",
|
||||||
|
"episode: 919, time: 8, loss: 0.0015\n",
|
||||||
|
"episode: 929, time: 9, loss: 0.0021\n",
|
||||||
|
"episode: 939, time: 9, loss: 0.0015\n",
|
||||||
|
"episode: 949, time: 9, loss: 0.0016\n",
|
||||||
|
"episode: 959, time: 9, loss: 0.0013\n",
|
||||||
|
"episode: 969, time: 12, loss: 0.0029\n",
|
||||||
|
"episode: 979, time: 7, loss: 0.0016\n",
|
||||||
|
"episode: 989, time: 7, loss: 0.0012\n",
|
||||||
|
"episode: 999, time: 9, loss: 0.0013\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"deep_q_learning(1000)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 2",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python2"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 2
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython2",
|
||||||
|
"version": "2.7.13"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
154
tutorials/10 - Deep Q Network/Untitled.ipynb
Normal file
154
tutorials/10 - Deep Q Network/Untitled.ipynb
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import gym\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from matplotlib import pyplot as plt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[2017-03-08 21:13:15,268] Making new env: Breakout-v0\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ename": "DependencyNotInstalled",
|
||||||
|
"evalue": "No module named 'atari_py'. (HINT: you can install Atari dependencies by running 'pip install gym[atari]'.)",
|
||||||
|
"output_type": "error",
|
||||||
|
"traceback": [
|
||||||
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||||
|
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/envs/atari/atari_env.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0matari_py\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mImportError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;31mImportError\u001b[0m: No module named 'atari_py'",
|
||||||
|
"\nDuring handling of the above exception, another exception occurred:\n",
|
||||||
|
"\u001b[0;31mDependencyNotInstalled\u001b[0m Traceback (most recent call last)",
|
||||||
|
"\u001b[0;32m<ipython-input-6-fd0311e5e366>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0menv\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgym\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menvs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmake\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Breakout-v0\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Action space size: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0menv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maction_space\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0menv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_action_meanings\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\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/envs/registration.py\u001b[0m in \u001b[0;36mmake\u001b[0;34m(id)\u001b[0m\n\u001b[1;32m 159\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 160\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmake\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 161\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mregistry\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmake\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 162\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mspec\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/envs/registration.py\u001b[0m in \u001b[0;36mmake\u001b[0;34m(self, id)\u001b[0m\n\u001b[1;32m 117\u001b[0m \u001b[0mlogger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minfo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Making new env: %s'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 118\u001b[0m \u001b[0mspec\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mspec\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 119\u001b[0;31m \u001b[0menv\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mspec\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmake\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 120\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0menv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mspec\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimestep_limit\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mspec\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtags\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'vnc'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 121\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mgym\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrappers\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime_limit\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mTimeLimit\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/envs/registration.py\u001b[0m in \u001b[0;36mmake\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 83\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0merror\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Attempting to make deprecated env {}. (HINT: is there a newer registered version of this env?)'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 84\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 85\u001b[0;31m \u001b[0mcls\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_entry_point\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 86\u001b[0m \u001b[0menv\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 87\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/envs/registration.py\u001b[0m in \u001b[0;36mload\u001b[0;34m(name)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mentry_point\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpkg_resources\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mEntryPoint\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparse\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'x={}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mentry_point\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg/pkg_resources/__init__.py\u001b[0m in \u001b[0;36mload\u001b[0;34m(self, require, *args, **kwargs)\u001b[0m\n\u001b[1;32m 2256\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mrequire\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2257\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrequire\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2258\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresolve\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 2259\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2260\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mresolve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg/pkg_resources/__init__.py\u001b[0m in \u001b[0;36mresolve\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 2262\u001b[0m \u001b[0mResolve\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mentry\u001b[0m \u001b[0mpoint\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mits\u001b[0m \u001b[0mmodule\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mattrs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2263\u001b[0m \"\"\"\n\u001b[0;32m-> 2264\u001b[0;31m \u001b[0mmodule\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m__import__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodule_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfromlist\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'__name__'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevel\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[0m\u001b[1;32m 2265\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2266\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mfunctools\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreduce\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgetattr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mattrs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/envs/atari/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mgym\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menvs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0matari\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0matari_env\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mAtariEnv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/envs/atari/atari_env.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0matari_py\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mImportError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0merror\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDependencyNotInstalled\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"{}. (HINT: you can install Atari dependencies by running 'pip install gym[atari]'.)\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\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 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mlogging\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;31mDependencyNotInstalled\u001b[0m: No module named 'atari_py'. (HINT: you can install Atari dependencies by running 'pip install gym[atari]'.)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"env = gym.envs.make(\"Breakout-v0\")\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"Action space size: {}\".format(env.action_space.n))\n",
|
||||||
|
"print(env.get_action_meanings())\n",
|
||||||
|
"\n",
|
||||||
|
"observation = env.reset()\n",
|
||||||
|
"print(\"Observation space shape: {}\".format(observation.shape))\n",
|
||||||
|
"\n",
|
||||||
|
"plt.figure()\n",
|
||||||
|
"plt.imshow(env.render(mode='rgb_array'))\n",
|
||||||
|
"\n",
|
||||||
|
"[env.step(2) for x in range(1)]\n",
|
||||||
|
"plt.figure()\n",
|
||||||
|
"plt.imshow(env.render(mode='rgb_array'))\n",
|
||||||
|
"\n",
|
||||||
|
"env.render(close=True)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[2017-03-08 21:12:44,474] Making new env: CartPole-v0\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ename": "NameError",
|
||||||
|
"evalue": "name 'base' is not defined",
|
||||||
|
"output_type": "error",
|
||||||
|
"traceback": [
|
||||||
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||||
|
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
|
||||||
|
"\u001b[0;32m<ipython-input-5-3093026983cb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mobservation\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0menv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mt\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0menv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrender\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 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobservation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0maction\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0menv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maction_space\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msample\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/core.py\u001b[0m in \u001b[0;36mrender\u001b[0;34m(self, mode, close)\u001b[0m\n\u001b[1;32m 155\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mmode\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmodes\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 156\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0merror\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mUnsupportedMode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Unsupported rendering mode: {}. (Supported modes for {}: {})'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 157\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_render\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mclose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 158\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 159\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/core.py\u001b[0m in \u001b[0;36m_render\u001b[0;34m(self, mode, close)\u001b[0m\n\u001b[1;32m 285\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 286\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_render\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'human'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mclose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 287\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrender\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mclose\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 288\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 289\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_close\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/core.py\u001b[0m in \u001b[0;36mrender\u001b[0;34m(self, mode, close)\u001b[0m\n\u001b[1;32m 155\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mmode\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmodes\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 156\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0merror\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mUnsupportedMode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Unsupported rendering mode: {}. (Supported modes for {}: {})'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 157\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_render\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mclose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 158\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 159\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/envs/classic_control/cartpole.py\u001b[0m in \u001b[0;36m_render\u001b[0;34m(self, mode, close)\u001b[0m\n\u001b[1;32m 112\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 113\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mviewer\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 114\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mgym\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menvs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclassic_control\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mrendering\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 115\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mviewer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrendering\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mViewer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mscreen_width\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mscreen_height\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 116\u001b[0m \u001b[0ml\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0mcartwidth\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcartwidth\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcartheight\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0mcartheight\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/gym/envs/classic_control/rendering.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mpyglet\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgl\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 24\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mImportError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0mreraise\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprefix\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"Error occured while running `from pyglet.gl import *`\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0msuffix\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \\\"-screen 0 1400x900x24\\\" python <your_script.py>'\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;32m/home/yunjey/anaconda3/lib/python3.5/site-packages/pyglet/gl/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 222\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 223\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mcarbon\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mCarbonConfig\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mConfig\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 224\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mbase\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 225\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 226\u001b[0m \u001b[0;31m# XXX remove\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||||
|
"\u001b[0;31mNameError\u001b[0m: name 'base' is not defined"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"import gym\n",
|
||||||
|
"env = gym.make('CartPole-v0')\n",
|
||||||
|
"for i_episode in range(20):\n",
|
||||||
|
" observation = env.reset()\n",
|
||||||
|
" for t in range(100):\n",
|
||||||
|
" env.render()\n",
|
||||||
|
" print(observation)\n",
|
||||||
|
" action = env.action_space.sample()\n",
|
||||||
|
" observation, reward, done, info = env.step(action)\n",
|
||||||
|
" if done:\n",
|
||||||
|
" print(\"Episode finished after {} timesteps\".format(t+1))\n",
|
||||||
|
" break"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python [conda root]",
|
||||||
|
"language": "python",
|
||||||
|
"name": "conda-root-py"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.5.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 1
|
||||||
|
}
|
124
tutorials/10 - Deep Q Network/dqn13.py
Normal file
124
tutorials/10 - Deep Q Network/dqn13.py
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
%matplotlib inline
|
||||||
|
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import gym
|
||||||
|
import random
|
||||||
|
import numpy as np
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from torch.autograd import Variable
|
||||||
|
from collections import deque, namedtuple
|
||||||
|
|
||||||
|
env = gym.envs.make("CartPole-v0")
|
||||||
|
|
||||||
|
class Net(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Net, self).__init__()
|
||||||
|
self.fc1 = nn.Linear(4, 128)
|
||||||
|
self.tanh = nn.Tanh()
|
||||||
|
self.fc2 = nn.Linear(128, 2)
|
||||||
|
self.init_weights()
|
||||||
|
|
||||||
|
def init_weights(self):
|
||||||
|
self.fc1.weight.data.uniform_(-0.1, 0.1)
|
||||||
|
self.fc2.weight.data.uniform_(-0.1, 0.1)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
out = self.fc1(x)
|
||||||
|
out = self.tanh(out)
|
||||||
|
out = self.fc2(out)
|
||||||
|
return out
|
||||||
|
|
||||||
|
def make_epsilon_greedy_policy(network, epsilon, nA):
|
||||||
|
def policy(state):
|
||||||
|
sample = random.random()
|
||||||
|
if sample < (1-epsilon) + (epsilon/nA):
|
||||||
|
q_values = network(state.view(1, -1))
|
||||||
|
action = q_values.data.max(1)[1][0, 0]
|
||||||
|
else:
|
||||||
|
action = random.randrange(nA)
|
||||||
|
return action
|
||||||
|
return policy
|
||||||
|
|
||||||
|
class ReplayMemory(object):
|
||||||
|
|
||||||
|
def __init__(self, capacity):
|
||||||
|
self.memory = deque()
|
||||||
|
self.capacity = capacity
|
||||||
|
|
||||||
|
def push(self, transition):
|
||||||
|
if len(self.memory) > self.capacity:
|
||||||
|
self.memory.popleft()
|
||||||
|
self.memory.append(transition)
|
||||||
|
|
||||||
|
def sample(self, batch_size):
|
||||||
|
return random.sample(self.memory, batch_size)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.memory)
|
||||||
|
|
||||||
|
def to_tensor(ndarray, volatile=False):
|
||||||
|
return Variable(torch.from_numpy(ndarray), volatile=volatile).float()
|
||||||
|
|
||||||
|
def deep_q_learning(num_episodes=10, batch_size=100,
|
||||||
|
discount_factor=0.95, epsilon=0.1, epsilon_decay=0.95):
|
||||||
|
|
||||||
|
# Q-Network and memory
|
||||||
|
net = Net()
|
||||||
|
memory = ReplayMemory(10000)
|
||||||
|
|
||||||
|
# Loss and Optimizer
|
||||||
|
criterion = nn.MSELoss()
|
||||||
|
optimizer = torch.optim.Adam(net.parameters(), lr=0.001)
|
||||||
|
|
||||||
|
for i_episode in range(num_episodes):
|
||||||
|
|
||||||
|
# Set policy (TODO: decaying epsilon)
|
||||||
|
#if (i_episode+1) % 100 == 0:
|
||||||
|
# epsilon *= 0.9
|
||||||
|
|
||||||
|
policy = make_epsilon_greedy_policy(
|
||||||
|
net, epsilon, env.action_space.n)
|
||||||
|
|
||||||
|
# Start an episode
|
||||||
|
state = env.reset()
|
||||||
|
|
||||||
|
for t in range(10000):
|
||||||
|
|
||||||
|
# Sample action from epsilon greed policy
|
||||||
|
action = policy(to_tensor(state))
|
||||||
|
next_state, reward, done, _ = env.step(action)
|
||||||
|
|
||||||
|
|
||||||
|
# Restore transition in memory
|
||||||
|
memory.push([state, action, reward, next_state])
|
||||||
|
|
||||||
|
|
||||||
|
if len(memory) >= batch_size:
|
||||||
|
# Sample mini-batch transitions from memory
|
||||||
|
batch = memory.sample(batch_size)
|
||||||
|
state_batch = np.vstack([trans[0] for trans in batch])
|
||||||
|
action_batch =np.vstack([trans[1] for trans in batch])
|
||||||
|
reward_batch = np.vstack([trans[2] for trans in batch])
|
||||||
|
next_state_batch = np.vstack([trans[3] for trans in batch])
|
||||||
|
|
||||||
|
# Forward + Backward + Opimize
|
||||||
|
net.zero_grad()
|
||||||
|
q_values = net(to_tensor(state_batch))
|
||||||
|
next_q_values = net(to_tensor(next_state_batch, volatile=True))
|
||||||
|
next_q_values.volatile = False
|
||||||
|
|
||||||
|
td_target = to_tensor(reward_batch) + discount_factor * (next_q_values).max(1)[0]
|
||||||
|
loss = criterion(q_values.gather(1,
|
||||||
|
to_tensor(action_batch).long().view(-1, 1)), td_target)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
if done:
|
||||||
|
break
|
||||||
|
|
||||||
|
state = next_state
|
||||||
|
|
||||||
|
if len(memory) >= batch_size and (i_episode+1) % 10 == 0:
|
||||||
|
print ('episode: %d, time: %d, loss: %.4f' %(i_episode, t, loss.data[0]))
|
Reference in New Issue
Block a user