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.
TFLearn is just five lines but captures the essence of TensorFlow and clearly makes the development on top of TF more enjoyable.


This approach is rather verbose but you have control over every detail. It feels like taking the derivative of a function by invoking deltas and limits.

%matplotlib inline
import tensorflow as tf
import numpy
import matplotlib.pyplot as plt
rng = numpy.random
# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50
# Training Data
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]
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]
train_X = numpy.asarray(X)
train_Y = numpy.asarray(Y)
n_samples = train_X.shape[0]

# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")

# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")

# Construct a linear model
pred = tf.add(tf.mul(X, W), b)

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.initialize_all_variables()
# Launch the graph
with tf.Session() as sess:

    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
  , feed_dict={X: x, Y: y})

        #Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c =, feed_dict={X: train_X, Y:train_Y})
            print( "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c),"W=",, "b=",

    print("Optimization Finished!")
    training_cost =, feed_dict={X: train_X, Y: train_Y})
    print("Training cost=", training_cost, "W=",, "b=",, 'n')

    #Graphic display
    plt.plot(train_X, train_Y, 'ro', label='Original data')
    plt.plot(train_X, * train_X +, label='Fitted line')



This is in essence just five lines and, referring to the derivative analogy above, it’s like using reliable rules of thumb.

from __future__ import absolute_import, division, print_function
import tflearn
input_ = tflearn.input_data(shape=[None])
linear = tflearn.single_unit(input_)
regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square',
                                metric='R2', learning_rate=0.01)
m = tflearn.DNN(regression), Y, n_epoch=1000, show_metric=True, snapshot_epoch=False)

print("nRegression result:")
print("Y = " + str(m.get_weights(linear.W)) +
      "*X + " + str(m.get_weights(linear.b)))

print("nTest prediction for x = 3.2, 3.3, 3.4:")
print(m.predict([3.2, 3.3, 3.4]))



Here the philosophy is completely different and feels like doing classical mechanics while the neural nets are quantum mechanics.

So, the conclusion is that no approach is better but rather only depends on the question you ask and what you’re after.

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model

X = np.array(X).reshape(-1, 1)
# Create linear regression object
regr = linear_model.LinearRegression()

# Train the model using the training sets,Y)

# The coefficients
print('Coefficients: n', regr.coef_)
# The mean squared error
print("Mean squared error: %.2f"
      % np.mean((regr.predict(X) - Y) ** 2))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(X, Y))

# Plot outputs
plt.scatter(X, Y,  color='black')
plt.plot(X, regr.predict(X), color='blue',