Skip to content

Instantly share code, notes, and snippets.

@hsaito
Last active January 9, 2017 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsaito/5ba613f65b2b6f6987e379ca61f0c048 to your computer and use it in GitHub Desktop.
Save hsaito/5ba613f65b2b6f6987e379ca61f0c048 to your computer and use it in GitHub Desktop.
Element 1 Element 2 Against Effect
Ghost Fairy Dragon None
Bug Grass Fly Super Effective
Rock Fly Fighting Normal
Bug Grass Poison Super Effective
Ghost Ground Ghost Super Effective
Water Grass Ice Normal
Bug Steel Fire Super Effective
Bug Fly Steel Normal
Ghost Fire Fairy Not Very Effective
Fire None Water Super Effective
Ghost Fairy Fighting None
Bug None Fighting Not Very Effective
Water Psychic Bug Super Effective
Ghost Fire Normal None
Ghost Fairy Ghost Super Effective
Bug None Fly Super Effective
Electric Steel Steel Not Very Effective
Bug Grass Fire Super Effective
Water None Electric Super Effective
Bug Fly Fire Super Effective
Grass Psychic Electric Not Very Effective
Ghost Fire Fire Not Very Effective
Ghost Grass Poison Normal
Ghost Fire Psychic Normal
Electric Fly Ground None
Grass None Fire Super Effective
Fighting None Normal Normal
Water Psychic Fire Not Very Effective
Ghost Fairy Fire Normal
Electric Fly Ground None
Water Bug Fire Normal
Poison Fly Fighting Not Very Effective
Bug Fly Ghost Normal
Ghost Fairy Ghost Super Effective
Rock Fly Water Super Effective
Dark Steel Fighting Super Effective
Poison Dark Fly Normal
Water Grass Fire Normal
Water Electric Grass Super Effective
Water Fly Bug Not Very Effective
Rock Ground Water Super Effective
Bug Grass Dragon Normal
Rock Fly Normal Not Very Effective
Fighting Steel Poison None
Grass Psychic Fire Super Effective
Fighting Steel Fly Normal
Grass Psychic Fire Super Effective
Bug Poison Fire Super Effective
Poison Dark Ice Normal
Poison Dark Fighting Normal
Normal Fighting Normal Normal
Rock Ground Water Super Effective
Dark Steel Water Normal
Psychic Fairy Fairy Normal
Grass Fly Dark Normal
Bug Fly Fly Super Effective
Psychic Fighting Poison Normal
Fighting Dark Ground Normal
Poison Ice Poison Normal
Dark Steel Psychic None
Electric Steel Water Normal
Water Psychic Ice Not Very Effective
Bug Fly Fairy Normal
Poison Dark Ice Normal
Water Fly Fire Not Very Effective
Poison Dark Normal Normal
Bug Fly Normal Normal
Bug Fire Steel Not Very Effective
Rock Bug Ground Normal
Water Poison Fire Not Very Effective
Water Fly Electric Super Effective
Grass Ground Dark Normal
Water Dragon Ground Normal
Fairy None Ground Normal
Electronic Steel Electric Not Very Effective
Psychic Fairy Fire Normal
Water Fly Fly Normal
Bug Grass Fire Super Effective
Bug Fly Dark Normal
Ghost Ground Fighting None
Bug Poison Fairy Not Very Effective
Electric None Steel Not Very Effective
Bug Grass Steel Normal
Water Bug Dragon Normal
Water Psychic Fighting Not Very Effective
Dark Ghost Fairy Super Effective
Bug Grass Grass Not Very Effective
Ghost Fire Ice Not Very Effective
Steel Fairy Dragon None
Water Poison Ghost Normal
Water Fly Ground None
Electric None Poison Normal
Normal Fairy Bug Not Very Effective
Rock Fly Psychic Normal
Water Psychic Steel Not Very Effective
Electric Steel Steel Not Very Effective
Ghost Grass Water Not Very Effective
Grass Fly Normal Normal
Fairy None Fighting Not Very Effective
Bug Grass Ground Not Very Effective
Ghost Fire Fairy Not Very Effective
Water Fly Dragon Normal
Dragon None Dark Normal
Psychic Fighting Steel Normal
Water Psychic Fire Not Very Effective
Ghost Ground Ground Normal
Water Rock Fly Not Very Effective
Water Fly Ghost Normal
Normal Fighting Steel Normal
Grass Psychic Rock Normal
Ghost Ground Rock Not Very Effective
Ghost Fairy Dragon None
Water Rock Dark Normal
Ghost None Ghost Super Effective
Bug Steel Electric Normal
Electric Steel Grass Not Very Effective
Grass Psychic Water Not Very Effective
Water Electric Ground Super Effective
Water Fly Fairy Normal
Bug Grass Dragon Normal
Rock Grass Water Super Effective
Fire None Fire Not Very Effective
Water None Ground Normal
Bug Fly Poison Normal
Grass Psychic Ice Super Effective
Water Fly Steel Not Very Effective
Water Psychic Dragon Normal
Rock Fly Grass Normal
Poison Dark Normal Normal
Ghost Fire Dragon Normal
Bug Grass Poison Super Effective
Bug Steel Fighting Normal
Dragon None Ice Super Effective
Rock Fly Dark Normal
Bug Steel Dragon Not Very Effective
Rock Ground Steel Super Effective
Poison None Fire Normal
Bug Fly Fire Super Effective
Psychic None Rock Normal
Water Electric Poison Normal
Bug Grass Fighting Not Very Effective
Ghost Fire Fighting None
Poison Dark Ground Super Effective
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pokemon Elements Learning with TensorFlow\n",
"\n",
"Most of code segments are adopted from from [the effort of predicting Titanic survivors](http://mtitg.com/tensorflow-titanic/) by [mtitg](http://mtitg.com/tensorflow-titanic/)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import random\n",
"import numpy as np\n",
"import pandas as pd\n",
"import tensorflow as tf\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.feature_extraction import DictVectorizer\n",
"from sklearn import preprocessing\n",
"\n",
"import os\n",
"cwd = os.getcwd()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Parameters"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"learning_rate = 0.001 # Learning rate -- higher the quicker it converges to cost; but might miss local min\n",
"training_epochs = 5 # Divide the whole set with this epoch, and calculate cost per segment\n",
"batch_size = 15 # How many training data to use per learning (per sess.run())\n",
"display_step = 1 # If 1, display cost per epoch\n",
"train_size = 120 # How many within data set to use for training\n",
"step_size = 1000 # How many learning steps"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Network Parameters"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"n_hidden_1 = 64 # Number of units for hidden layer 1\n",
"n_hidden_2 = 64 # Number of units for hidden layer 2\n",
"n_input = 3 # Parameters given (Element 1, Element 2, (Element Against))\n",
"n_classes = 4 # Class of classifications (In our case, it's None, Not Very Effective, Normal, and Super Effective)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Loading Test Data\n",
"\n",
"*ID* is only used for internal sorting purpose and we are not using it within the computations here."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"df = pd.read_csv(\"pokemon_matrix.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Element 1</th>\n",
" <th>Element 2</th>\n",
" <th>Against</th>\n",
" <th>Effect</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Ghost</td>\n",
" <td>Fairy</td>\n",
" <td>Dragon</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Bug</td>\n",
" <td>Grass</td>\n",
" <td>Fly</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Rock</td>\n",
" <td>Fly</td>\n",
" <td>Fighting</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Bug</td>\n",
" <td>Grass</td>\n",
" <td>Poison</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Ghost</td>\n",
" <td>Ground</td>\n",
" <td>Ghost</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Water</td>\n",
" <td>Grass</td>\n",
" <td>Ice</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Bug</td>\n",
" <td>Steel</td>\n",
" <td>Fire</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Bug</td>\n",
" <td>Fly</td>\n",
" <td>Steel</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Ghost</td>\n",
" <td>Fire</td>\n",
" <td>Fairy</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Fire</td>\n",
" <td>None</td>\n",
" <td>Water</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>Ghost</td>\n",
" <td>Fairy</td>\n",
" <td>Fighting</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>Bug</td>\n",
" <td>None</td>\n",
" <td>Fighting</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>Water</td>\n",
" <td>Psychic</td>\n",
" <td>Bug</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>Ghost</td>\n",
" <td>Fire</td>\n",
" <td>Normal</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>Ghost</td>\n",
" <td>Fairy</td>\n",
" <td>Ghost</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>Bug</td>\n",
" <td>None</td>\n",
" <td>Fly</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>Electric</td>\n",
" <td>Steel</td>\n",
" <td>Steel</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>Bug</td>\n",
" <td>Grass</td>\n",
" <td>Fire</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>Water</td>\n",
" <td>None</td>\n",
" <td>Electric</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>Bug</td>\n",
" <td>Fly</td>\n",
" <td>Fire</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>Grass</td>\n",
" <td>Psychic</td>\n",
" <td>Electric</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>Ghost</td>\n",
" <td>Fire</td>\n",
" <td>Fire</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>Ghost</td>\n",
" <td>Grass</td>\n",
" <td>Poison</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>Ghost</td>\n",
" <td>Fire</td>\n",
" <td>Psychic</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>Electric</td>\n",
" <td>Fly</td>\n",
" <td>Ground</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>Grass</td>\n",
" <td>None</td>\n",
" <td>Fire</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>Fighting</td>\n",
" <td>None</td>\n",
" <td>Normal</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>Water</td>\n",
" <td>Psychic</td>\n",
" <td>Fire</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>Ghost</td>\n",
" <td>Fairy</td>\n",
" <td>Fire</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>Electric</td>\n",
" <td>Fly</td>\n",
" <td>Ground</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>113</th>\n",
" <td>Ghost</td>\n",
" <td>None</td>\n",
" <td>Ghost</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>114</th>\n",
" <td>Bug</td>\n",
" <td>Steel</td>\n",
" <td>Electric</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>115</th>\n",
" <td>Electric</td>\n",
" <td>Steel</td>\n",
" <td>Grass</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>116</th>\n",
" <td>Grass</td>\n",
" <td>Psychic</td>\n",
" <td>Water</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>117</th>\n",
" <td>Water</td>\n",
" <td>Electric</td>\n",
" <td>Ground</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>118</th>\n",
" <td>Water</td>\n",
" <td>Fly</td>\n",
" <td>Fairy</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>119</th>\n",
" <td>Bug</td>\n",
" <td>Grass</td>\n",
" <td>Dragon</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>120</th>\n",
" <td>Rock</td>\n",
" <td>Grass</td>\n",
" <td>Water</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>121</th>\n",
" <td>Fire</td>\n",
" <td>None</td>\n",
" <td>Fire</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>122</th>\n",
" <td>Water</td>\n",
" <td>None</td>\n",
" <td>Ground</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>123</th>\n",
" <td>Bug</td>\n",
" <td>Fly</td>\n",
" <td>Poison</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>124</th>\n",
" <td>Grass</td>\n",
" <td>Psychic</td>\n",
" <td>Ice</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>125</th>\n",
" <td>Water</td>\n",
" <td>Fly</td>\n",
" <td>Steel</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>126</th>\n",
" <td>Water</td>\n",
" <td>Psychic</td>\n",
" <td>Dragon</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>127</th>\n",
" <td>Rock</td>\n",
" <td>Fly</td>\n",
" <td>Grass</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>128</th>\n",
" <td>Poison</td>\n",
" <td>Dark</td>\n",
" <td>Normal</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>129</th>\n",
" <td>Ghost</td>\n",
" <td>Fire</td>\n",
" <td>Dragon</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>130</th>\n",
" <td>Bug</td>\n",
" <td>Grass</td>\n",
" <td>Poison</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>131</th>\n",
" <td>Bug</td>\n",
" <td>Steel</td>\n",
" <td>Fighting</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>132</th>\n",
" <td>Dragon</td>\n",
" <td>None</td>\n",
" <td>Ice</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>133</th>\n",
" <td>Rock</td>\n",
" <td>Fly</td>\n",
" <td>Dark</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>134</th>\n",
" <td>Bug</td>\n",
" <td>Steel</td>\n",
" <td>Dragon</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>135</th>\n",
" <td>Rock</td>\n",
" <td>Ground</td>\n",
" <td>Steel</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>136</th>\n",
" <td>Poison</td>\n",
" <td>None</td>\n",
" <td>Fire</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>137</th>\n",
" <td>Bug</td>\n",
" <td>Fly</td>\n",
" <td>Fire</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>138</th>\n",
" <td>Psychic</td>\n",
" <td>None</td>\n",
" <td>Rock</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>139</th>\n",
" <td>Water</td>\n",
" <td>Electric</td>\n",
" <td>Poison</td>\n",
" <td>Normal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>140</th>\n",
" <td>Bug</td>\n",
" <td>Grass</td>\n",
" <td>Fighting</td>\n",
" <td>Not Very Effective</td>\n",
" </tr>\n",
" <tr>\n",
" <th>141</th>\n",
" <td>Ghost</td>\n",
" <td>Fire</td>\n",
" <td>Fighting</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>142</th>\n",
" <td>Poison</td>\n",
" <td>Dark</td>\n",
" <td>Ground</td>\n",
" <td>Super Effective</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>143 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" Element 1 Element 2 Against Effect\n",
"0 Ghost Fairy Dragon None\n",
"1 Bug Grass Fly Super Effective\n",
"2 Rock Fly Fighting Normal\n",
"3 Bug Grass Poison Super Effective\n",
"4 Ghost Ground Ghost Super Effective\n",
"5 Water Grass Ice Normal\n",
"6 Bug Steel Fire Super Effective\n",
"7 Bug Fly Steel Normal\n",
"8 Ghost Fire Fairy Not Very Effective\n",
"9 Fire None Water Super Effective\n",
"10 Ghost Fairy Fighting None\n",
"11 Bug None Fighting Not Very Effective\n",
"12 Water Psychic Bug Super Effective\n",
"13 Ghost Fire Normal None\n",
"14 Ghost Fairy Ghost Super Effective\n",
"15 Bug None Fly Super Effective\n",
"16 Electric Steel Steel Not Very Effective\n",
"17 Bug Grass Fire Super Effective\n",
"18 Water None Electric Super Effective\n",
"19 Bug Fly Fire Super Effective\n",
"20 Grass Psychic Electric Not Very Effective\n",
"21 Ghost Fire Fire Not Very Effective\n",
"22 Ghost Grass Poison Normal\n",
"23 Ghost Fire Psychic Normal\n",
"24 Electric Fly Ground None\n",
"25 Grass None Fire Super Effective\n",
"26 Fighting None Normal Normal\n",
"27 Water Psychic Fire Not Very Effective\n",
"28 Ghost Fairy Fire Normal\n",
"29 Electric Fly Ground None\n",
".. ... ... ... ...\n",
"113 Ghost None Ghost Super Effective\n",
"114 Bug Steel Electric Normal\n",
"115 Electric Steel Grass Not Very Effective\n",
"116 Grass Psychic Water Not Very Effective\n",
"117 Water Electric Ground Super Effective\n",
"118 Water Fly Fairy Normal\n",
"119 Bug Grass Dragon Normal\n",
"120 Rock Grass Water Super Effective\n",
"121 Fire None Fire Not Very Effective\n",
"122 Water None Ground Normal\n",
"123 Bug Fly Poison Normal\n",
"124 Grass Psychic Ice Super Effective\n",
"125 Water Fly Steel Not Very Effective\n",
"126 Water Psychic Dragon Normal\n",
"127 Rock Fly Grass Normal\n",
"128 Poison Dark Normal Normal\n",
"129 Ghost Fire Dragon Normal\n",
"130 Bug Grass Poison Super Effective\n",
"131 Bug Steel Fighting Normal\n",
"132 Dragon None Ice Super Effective\n",
"133 Rock Fly Dark Normal\n",
"134 Bug Steel Dragon Not Very Effective\n",
"135 Rock Ground Steel Super Effective\n",
"136 Poison None Fire Normal\n",
"137 Bug Fly Fire Super Effective\n",
"138 Psychic None Rock Normal\n",
"139 Water Electric Poison Normal\n",
"140 Bug Grass Fighting Not Very Effective\n",
"141 Ghost Fire Fighting None\n",
"142 Poison Dark Ground Super Effective\n",
"\n",
"[143 rows x 4 columns]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Encode the data so TensorFlow can take it"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"labelEncoder = preprocessing.LabelEncoder()\n",
"df['Element 1'] = labelEncoder.fit_transform(df['Element 1']) \n",
"df['Element 2'] = labelEncoder.fit_transform(df['Element 2']) \n",
"df['Against'] = labelEncoder.fit_transform(df['Against']) \n",
"\n",
"x_np = np.array(df[['Element 1', 'Element 2', 'Against']].fillna(0))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"d = df[['Effect']].to_dict('record')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"vectorizer = DictVectorizer(sparse=False)\n",
"y_np = vectorizer.fit_transform(d)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"[x_train, x_test] = np.vsplit(x_np, [train_size]) # Split input into training and test\n",
"[y_train, y_test] = np.vsplit(y_np, [train_size]) # Split labels into training and test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tensorflow Graph input"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = tf.placeholder(\"float\", [None, n_input])\n",
"y = tf.placeholder(\"float\", [None, n_classes])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create model"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def multilayer_perceptron(x, weights, biases):\n",
" # Hidden layer with RELU activation\n",
" layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])\n",
" layer_1 = tf.nn.relu(layer_1)\n",
" # Hidden layer with RELU activation\n",
" layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])\n",
" layer_2 = tf.nn.relu(layer_2)\n",
" # Output layer with linear activation\n",
" out_layer = tf.matmul(layer_2, weights['out']) + biases['out']\n",
" return out_layer"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Store layers weight & bias"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"weights = {\n",
" 'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),\n",
" 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),\n",
" 'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))\n",
"}\n",
"biases = {\n",
" 'b1': tf.Variable(tf.random_normal([n_hidden_1])),\n",
" 'b2': tf.Variable(tf.random_normal([n_hidden_2])),\n",
" 'out': tf.Variable(tf.random_normal([n_classes]))\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Construct model"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pred = multilayer_perceptron(x, weights, biases)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define loss and optimizer"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))\n",
"optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initializing the variables"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"init = tf.global_variables_initializer()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Start computation"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch: 0001 cost= 18.820287877\n",
"Epoch: 0002 cost= 0.176757155\n",
"Epoch: 0003 cost= 0.003131402\n",
"Epoch: 0004 cost= 0.001445520\n",
"Epoch: 0005 cost= 0.000698007\n",
"Optimization Finished!\n",
"Accuracy: 0.478261\n"
]
}
],
"source": [
"# Launch the graph\n",
"with tf.Session() as sess:\n",
" sess.run(init)\n",
" train_writer = tf.summary.FileWriter(cwd+'/train',\n",
" sess.graph)\n",
"\n",
" # Training cycle\n",
" for epoch in range(training_epochs):\n",
" avg_cost = 0.\n",
"\n",
" # Loop over step_size\n",
" for i in range(step_size):\n",
" # From the training data, get random data for numbers specified in batch_size\n",
" ind = np.random.choice(batch_size, batch_size)\n",
" x_train_batch = x_train[ind]\n",
" y_train_batch = y_train[ind]\n",
" # Run optimization op (backprop) and cost op (to get loss value)\n",
" _, c = sess.run([optimizer, cost], feed_dict={x: x_train_batch,\n",
" y: y_train_batch})\n",
" # Compute average loss\n",
" avg_cost += c / step_size\n",
" # Display logs per epoch step\n",
" if epoch % display_step == 0:\n",
" print(\"Epoch:\", '%04d' % (epoch+1), \"cost=\", \\\n",
" \"{:.9f}\".format(avg_cost))\n",
" print (\"Optimization Finished!\")\n",
"\n",
" # Test model\n",
" correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n",
" # Calculate accuracy\n",
" accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n",
" print(\"Accuracy:\", accuracy.eval({x: x_test, y: y_test}))"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:lab]",
"language": "python",
"name": "conda-env-lab-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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment