In this article we are going to present a simple Logistic Regression model build using Google Machine Learning Frame call TensorFlow.
We will use some Python Libraries like:
We will use some Python Libraries like:
- numpy
- for numeric data manipulation
- pandas
- for data frame manipulation
- matplotlib
- for graph
import tensorflow as tf import numpy as np import pandas as pd import matplotlib.pyplot as plt # We generate linearly separated data xData = np.linspace(0.0, 10.0, 100000)
# We generate some noise to add to our data noise = np.random.randn(len(xData)) # Visualize data generated print(xData)
# We checkout ou noise shape
print(noise.shape)
# From our visualisation we can see that this line best feet our
# data yTrue = (0.5)*xData + 5 + noise # Creating DataFrame from our data xDf = pd.DataFrame(data=xData, columns=['x data']) yDf = pd.DataFrame(data=yTrue, columns=['y']) # Visualizing the first 5 values of our frames print(xDf.head()) print(yDf.head()) # Here we concatanate our data frames myData = pd.concat([xDf, yDf], axis=1) print(myData.head()) # We can visualize 250 randomly choosing sample of our data myData.sample(n=250).plot(kind='scatter', x='x data', y='y') plt.show() # Have a large dataset we will work in batches.
# We define our batch size batchSize = 1000 # We set randomly our 2 value to learn m = tf.Variable(0.1) b = tf.Variable(0.14) # We define placeholder: Values that are going to be feet
# in order to learn m and b xph = tf.placeholder(tf.float32, [batchSize]) yph = tf.placeholder(tf.float32, [batchSize]) # This is our our model will look like y_model = m*xph + b # Our cost function that we will minimize error = tf.reduce_sum(tf.square(yph - y_model)) # We define here our learning rate using gradien descent optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.00001) train = optimizer.minimize(error) # This will be use to initialize our variables init = tf.global_variables_initializer()
# We create ou tensorFlow session in order to compute our
# variables with tf.Session() as sess:
# We initialize our variables sess.run(init)
# We define the number of batches we are going to use batches = 1000 for i in range(batches):
# We choose random 250 values of our data randIndex = np.random.randint(len(xData), size=batchSize)
# We create ou feed dictionary feed = {xph:xData[randIndex], yph:yTrue[randIndex]} #print(feed)
# run our train sess.run(train, feed_dict=feed) model = sess.run([m, b]) # We view our learned values print(model[0], model[1]) # We plot the result plt.plot(xData, model[0]*xData + model[1], 'r') index = np.random.randint(len(xData), size=250) print(index) plt.scatter(xData[index], yTrue[index]) plt.show()