Created
October 26, 2015 02:49
-
-
Save jgc128/0bad81b40787f15f6ced to your computer and use it in GitHub Desktop.
Test Keras library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"from sklearn import datasets\n", | |
"from sklearn.cross_validation import train_test_split\n", | |
"\n", | |
"import theano.tensor as T\n", | |
"import theano\n", | |
"\n", | |
"from keras.models import Sequential\n", | |
"from keras.layers.core import Dense, Activation\n", | |
"from keras.optimizers import SGD\n", | |
"from keras.regularizers import l2, activity_l2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 226, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"input_size = 4\n", | |
"hidden_size = 16\n", | |
"output_size = 3" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Create dataset" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 212, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# load iris dataset\n", | |
"iris = datasets.load_iris()\n", | |
"X = iris.data\n", | |
"Y = iris.target" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 227, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# X1 = X[:,0:2]\n", | |
"X1 = X" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 214, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# create one-hot representation for Y\n", | |
"Y1 = np.zeros((len(Y), output_size), dtype=np.int)\n", | |
"Y1[np.arange(len(Y)), np.array(Y)] = 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 215, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# # create classifier for Y = 0\n", | |
"# Y1 = np.copy(Y)\n", | |
"# Y1[Y==0] = 1\n", | |
"# Y1[Y!=0] = 0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 216, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1]])" | |
] | |
}, | |
"execution_count": 216, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Y1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 217, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"X_train, X_test, Y_train, Y_test = train_test_split(X1, Y1, test_size=0.2, random_state=42)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Create Neural Network\n", | |
"===========" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 218, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def class_activation_func(x):\n", | |
" return T.switch(T.lt(x, 0.5), 0.0, 1.0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 228, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"model = Sequential()\n", | |
"\n", | |
"# input\n", | |
"model.add(Dense(output_dim=hidden_size, input_dim=input_size, activation='sigmoid'))\n", | |
"\n", | |
"# hidden layers\n", | |
"model.add(Dense(output_dim=hidden_size, activation='sigmoid'))\n", | |
"\n", | |
"# output\n", | |
"model.add(Dense(output_dim=output_size, activation='sigmoid'))\n", | |
"# model.add(Activation(class_activation_func))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 229, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"model.compile(class_mode='categorical', loss='categorical_crossentropy', optimizer='sgd')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 230, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Epoch 1/10\n", | |
"120/120 [==============================] - 0s - loss: 1.1130 - acc: 0.3333\n", | |
"Epoch 2/10\n", | |
"120/120 [==============================] - 0s - loss: 1.1129 - acc: 0.3333\n", | |
"Epoch 3/10\n", | |
"120/120 [==============================] - 0s - loss: 1.1128 - acc: 0.3333\n", | |
"Epoch 4/10\n", | |
"120/120 [==============================] - 0s - loss: 1.1127 - acc: 0.3333\n", | |
"Epoch 5/10\n", | |
"120/120 [==============================] - 0s - loss: 1.1126 - acc: 0.3333\n", | |
"Epoch 6/10\n", | |
"120/120 [==============================] - 0s - loss: 1.1126 - acc: 0.3333\n", | |
"Epoch 7/10\n", | |
"120/120 [==============================] - 0s - loss: 1.1125 - acc: 0.3333\n", | |
"Epoch 8/10\n", | |
"120/120 [==============================] - 0s - loss: 1.1124 - acc: 0.3333\n", | |
"Epoch 9/10\n", | |
"120/120 [==============================] - 0s - loss: 1.1123 - acc: 0.3333\n", | |
"Epoch 10/10\n", | |
"120/120 [==============================] - 0s - loss: 1.1122 - acc: 0.3333\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7f9c12b27f28>" | |
] | |
}, | |
"execution_count": 230, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"model.fit(X_train, Y_train, nb_epoch=10, show_accuracy = True)\n", | |
"# model.fit(X, Y, nb_epoch=10, validation_split = 0.1, show_accuracy = True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 231, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"30/30 [==============================] - 0s\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", | |
" 0, 0, 0, 0, 0, 0, 0])" | |
] | |
}, | |
"execution_count": 231, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"model.predict_classes(X_test)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 232, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0, 1, 0],\n", | |
" [1, 0, 0],\n", | |
" [0, 0, 1],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [1, 0, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 0, 1],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 0, 1],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 0, 1],\n", | |
" [0, 1, 0],\n", | |
" [0, 1, 0],\n", | |
" [0, 0, 1],\n", | |
" [1, 0, 0],\n", | |
" [0, 0, 1],\n", | |
" [1, 0, 0],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [0, 0, 1],\n", | |
" [1, 0, 0],\n", | |
" [1, 0, 0]])" | |
] | |
}, | |
"execution_count": 232, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Y_test" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment