Skip to content

Instantly share code, notes, and snippets.

@Orbifold
Created November 4, 2016 06:12
Show Gist options
  • Save Orbifold/c6ede989b41e847700f0e0ea2724f066 to your computer and use it in GitHub Desktop.
Save Orbifold/c6ede989b41e847700f0e0ea2724f066 to your computer and use it in GitHub Desktop.
Standard linear regression using TensorFlow, TFLearn and sklearn.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Three ways linear regression\n",
"\n",
"The snippets below all perform a standard linear regresson on the same data. It's a comparison between TensorFlow, TFLearn and sklearn. It takes obviously more code in TensorFlow but one should rather compare the flexibility of the approaches.\n",
"TFLearn is just five lines but captures the essence of TensorFlow and clearly makes the development on top of TF more enjoyable."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TensorFlow"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import tensorflow as tf\n",
"import numpy\n",
"import matplotlib.pyplot as plt\n",
"rng = numpy.random\n",
"# Parameters\n",
"learning_rate = 0.01\n",
"training_epochs = 1000\n",
"display_step = 50\n",
"# Training Data\n",
"X = [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,5.654,9.27,3.1]\n",
"Y = [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,2.42,2.94,1.3]\n",
"train_X = numpy.asarray(X)\n",
"train_Y = numpy.asarray(Y)\n",
"n_samples = train_X.shape[0]\n",
"\n",
"# tf Graph Input\n",
"X = tf.placeholder(\"float\")\n",
"Y = tf.placeholder(\"float\")\n",
"\n",
"# Set model weights\n",
"W = tf.Variable(rng.randn(), name=\"weight\")\n",
"b = tf.Variable(rng.randn(), name=\"bias\")\n",
"\n",
"# Construct a linear model\n",
"pred = tf.add(tf.mul(X, W), b)\n",
"\n",
"# Mean squared error\n",
"cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)\n",
"# Gradient descent\n",
"optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)\n",
"\n",
"# Initializing the variables\n",
"init = tf.initialize_all_variables()\n",
"# Launch the graph\n",
"with tf.Session() as sess:\n",
" sess.run(init)\n",
"\n",
" # Fit all training data\n",
" for epoch in range(training_epochs):\n",
" for (x, y) in zip(train_X, train_Y):\n",
" sess.run(optimizer, feed_dict={X: x, Y: y})\n",
"\n",
" #Display logs per epoch step\n",
" if (epoch+1) % display_step == 0:\n",
" c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})\n",
" print( \"Epoch:\", '%04d' % (epoch+1), \"cost=\", \"{:.9f}\".format(c),\"W=\", sess.run(W), \"b=\", sess.run(b))\n",
"\n",
" print(\"Optimization Finished!\")\n",
" training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})\n",
" print(\"Training cost=\", training_cost, \"W=\", sess.run(W), \"b=\", sess.run(b), '\\n')\n",
"\n",
" #Graphic display\n",
" plt.plot(train_X, train_Y, 'ro', label='Original data')\n",
" plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')\n",
" plt.legend()\n",
" plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TFLearn"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import absolute_import, division, print_function\n",
"import tflearn\n",
"input_ = tflearn.input_data(shape=[None])\n",
"linear = tflearn.single_unit(input_)\n",
"regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square',\n",
" metric='R2', learning_rate=0.01)\n",
"m = tflearn.DNN(regression)\n",
"m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False)\n",
"\n",
"print(\"\\nRegression result:\")\n",
"print(\"Y = \" + str(m.get_weights(linear.W)) +\n",
" \"*X + \" + str(m.get_weights(linear.b)))\n",
"\n",
"print(\"\\nTest prediction for x = 3.2, 3.3, 3.4:\")\n",
"print(m.predict([3.2, 3.3, 3.4]))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sklearn"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from sklearn import datasets, linear_model\n",
"\n",
"X = np.array(X).reshape(-1, 1)\n",
"# Create linear regression object\n",
"regr = linear_model.LinearRegression()\n",
"\n",
"# Train the model using the training sets\n",
"regr.fit(X,Y)\n",
"\n",
"# The coefficients\n",
"print('Coefficients: \\n', regr.coef_)\n",
"# The mean squared error\n",
"print(\"Mean squared error: %.2f\"\n",
" % np.mean((regr.predict(X) - Y) ** 2))\n",
"# Explained variance score: 1 is perfect prediction\n",
"print('Variance score: %.2f' % regr.score(X, Y))\n",
"\n",
"# Plot outputs\n",
"plt.scatter(X, Y, color='black')\n",
"plt.plot(X, regr.predict(X), color='blue',\n",
" linewidth=3)\n",
"\n",
"plt.xticks(())\n",
"plt.yticks(())\n",
"\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"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": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment