Created
February 19, 2016 11:35
-
-
Save scturtle/94694fb40fd96e1ee0c2 to your computer and use it in GitHub Desktop.
multiclass classification using tensorflow
This file contains hidden or 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": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import tensorflow as tf\n", | |
"from sklearn import datasets" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"iris = datasets.load_iris()\n", | |
"num_labels = len(set(iris.target))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(150, 4) (150, 3)\n" | |
] | |
} | |
], | |
"source": [ | |
"data = iris.data.astype(np.float32)\n", | |
"labels = (np.arange(num_labels) == np.array(iris.target)[:,None]).astype(np.float32)\n", | |
"print(data.shape, labels.shape)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def accuracy(predictions, labels):\n", | |
" return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))\n", | |
" / predictions.shape[0])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"softmax regression:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"feature_size = data.shape[1]\n", | |
"\n", | |
"graph = tf.Graph()\n", | |
"\n", | |
"with graph.as_default():\n", | |
" tf_train_dataset = tf.constant(data)\n", | |
" tf_train_labels = tf.constant(labels)\n", | |
"\n", | |
" weights = tf.Variable(tf.truncated_normal([feature_size, num_labels]))\n", | |
" biases = tf.Variable(tf.zeros([num_labels]))\n", | |
"\n", | |
" logits = tf.matmul(tf_train_dataset, weights) + biases\n", | |
" loss = tf.reduce_mean(\n", | |
" tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))\n", | |
"\n", | |
" optimizer = tf.train.AdamOptimizer(0.1).minimize(loss)\n", | |
" train_prediction = tf.nn.softmax(logits)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"step:0 loss:4.024884 accuracy: 66.67\n", | |
"step:500 loss:0.072255 accuracy: 98.00\n", | |
"step:1000 loss:0.054472 accuracy: 98.00\n", | |
"step:1500 loss:0.048384 accuracy: 98.00\n", | |
"step:2000 loss:0.045301 accuracy: 98.00\n", | |
"step:2500 loss:0.043418 accuracy: 98.00\n", | |
"step:3000 loss:0.042158 accuracy: 98.67\n", | |
"step:3500 loss:0.041285 accuracy: 98.67\n", | |
"step:4000 loss:0.040682 accuracy: 98.67\n", | |
"step:4500 loss:0.040273 accuracy: 98.67\n", | |
"step:5000 loss:0.040007 accuracy: 98.67\n", | |
"step:5500 loss:0.039843 accuracy: 98.67\n", | |
"step:6000 loss:0.039750 accuracy: 98.67\n", | |
"step:6500 loss:0.039702 accuracy: 98.67\n", | |
"step:7000 loss:0.039680 accuracy: 98.67\n", | |
"step:7500 loss:0.039674 accuracy: 98.67\n", | |
"step:8000 loss:0.039672 accuracy: 98.67\n", | |
"step:8500 loss:0.039667 accuracy: 98.67\n", | |
"step:9000 loss:0.039665 accuracy: 98.67\n", | |
"step:9500 loss:0.039664 accuracy: 98.67\n", | |
"step:10000 loss:0.039664 accuracy: 98.67\n" | |
] | |
} | |
], | |
"source": [ | |
"with tf.Session(graph=graph) as session:\n", | |
" tf.initialize_all_variables().run()\n", | |
" for step in range(10001):\n", | |
" _, l, predictions = session.run([optimizer, loss, train_prediction])\n", | |
" if step % 500 == 0:\n", | |
" print('step:{} loss:{:.6f} accuracy: {:.2f}'.format(\n", | |
" step, l, accuracy(predictions, labels)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"linear multiclass SVM:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"feature_size = data.shape[1]\n", | |
"delta = 1.0\n", | |
"regulation_rate = 5e-4\n", | |
"graph = tf.Graph()\n", | |
"\n", | |
"with graph.as_default():\n", | |
" tf_train_dataset = tf.constant(data)\n", | |
" tf_train_labels = tf.constant(labels)\n", | |
" \n", | |
" weights = tf.Variable(tf.truncated_normal([feature_size, num_labels]))\n", | |
" biases = tf.Variable(tf.zeros([num_labels]))\n", | |
"\n", | |
" logits = tf.matmul(tf_train_dataset, weights) + biases\n", | |
" # TODO better way as numpy's: np.choose(data.target, logits.T)\n", | |
" y = tf.reduce_sum(logits * tf_train_labels, 1, keep_dims=True)\n", | |
" loss = tf.reduce_mean(tf.reduce_sum(tf.maximum(0.0, logits - y + delta), 1)) - delta\n", | |
" loss += regulation_rate * tf.nn.l2_loss(weights)\n", | |
"\n", | |
" optimizer = tf.train.AdamOptimizer(0.1).minimize(loss)\n", | |
" train_prediction = tf.nn.softmax(logits)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"step:0 loss:7.959514 accuracy: 33.33\n", | |
"step:500 loss:0.065034 accuracy: 97.33\n", | |
"step:1000 loss:0.060172 accuracy: 98.00\n", | |
"step:1500 loss:0.058802 accuracy: 98.00\n", | |
"step:2000 loss:0.057681 accuracy: 98.00\n", | |
"step:2500 loss:0.061486 accuracy: 98.00\n", | |
"step:3000 loss:0.057971 accuracy: 98.00\n", | |
"step:3500 loss:0.058048 accuracy: 98.00\n", | |
"step:4000 loss:0.055821 accuracy: 98.00\n", | |
"step:4500 loss:0.058429 accuracy: 98.00\n", | |
"step:5000 loss:0.056925 accuracy: 98.00\n", | |
"step:5500 loss:0.056828 accuracy: 98.00\n", | |
"step:6000 loss:0.060888 accuracy: 98.00\n", | |
"step:6500 loss:0.058099 accuracy: 98.00\n", | |
"step:7000 loss:0.055918 accuracy: 98.00\n", | |
"step:7500 loss:0.056860 accuracy: 98.67\n", | |
"step:8000 loss:0.056827 accuracy: 98.00\n", | |
"step:8500 loss:0.055365 accuracy: 98.00\n", | |
"step:9000 loss:0.055880 accuracy: 98.00\n", | |
"step:9500 loss:0.056300 accuracy: 98.67\n", | |
"step:10000 loss:0.058815 accuracy: 98.00\n" | |
] | |
} | |
], | |
"source": [ | |
"with tf.Session(graph=graph) as session:\n", | |
" tf.initialize_all_variables().run()\n", | |
" for step in range(10001):\n", | |
" _, l, predictions = session.run([optimizer, loss, train_prediction])\n", | |
" if step % 500 == 0:\n", | |
" print('step:{} loss:{:.6f} accuracy: {:.2f}'.format(\n", | |
" step, l, accuracy(predictions, labels)))" | |
] | |
}, | |
{ | |
"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.5.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Can you show how to generate prediction with this trained model?
Please show how to generate prediction with the trained model.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think
softmax_cross_entropy_with_logits
is not supposed for multi-class, it's just for non-one-hot label. Maybesigmoid_cross_entropy_with_logits
is the one.