Last active
January 20, 2019 13:21
-
-
Save conquistadorjd/64ca946395f4cf746cc2ada4a7fa9e38 to your computer and use it in GitHub Desktop.
TensorFlow
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
TensorFlow |
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
################################################################################################ | |
# name: tensorflow_basics_01.py | |
# desc: Gettig started with tensorflow with simple multiplication | |
# date: 2019-01-19 | |
# Author: conquistadorjd | |
################################################################################################ | |
import tensorflow as tf | |
print("*** Program Started ***") | |
# Initialize two constants | |
x1 = tf.constant(2) | |
x2 = tf.constant(7) | |
# Multiply | |
result = tf.multiply(x1, x2) | |
# Print the result | |
print(result) | |
# Intialize the Session | |
sess = tf.Session() | |
# Print the result | |
print(sess.run(result)) | |
# Close the session | |
sess.close() | |
print("*** Program Ended ***") |
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
################################################################################################ | |
# name: tensorflow_basics_02.py | |
# desc: Gettig started with tensorflow with simple multiplication of matrices | |
# date: 2019-01-19 | |
# Author: conquistadorjd | |
################################################################################################ | |
import tensorflow as tf | |
print("*** Program Started ***") | |
# Initialize two constants | |
x1 = tf.constant([1,2,3,4]) | |
x2 = tf.constant([5,6,7,8]) | |
# Multiply | |
result = tf.multiply(x1, x2) | |
# Print the result | |
print(result) | |
with tf.Session() as sess: | |
output = sess.run(result) | |
print(output) | |
print("*** Program Ended ***") |
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
################################################################################################ | |
# name: tensorflow_regression_linear_01.py | |
# desc: Gettig started with tensorflow with linear regression | |
# date: 2019-01-19 | |
# Author: conquistadorjd | |
################################################################################################ | |
import numpy as np | |
import seaborn | |
from matplotlib import pyplot as plt | |
import tensorflow as tf | |
from sklearn import datasets, linear_model | |
from sklearn.metrics import mean_squared_error, r2_score | |
print("*** Program Started ***") | |
######################################################################################## input data | |
##### Input Data #1 | |
# y_data=[1,3,5,3,8,12] | |
# x_data=[1,2,3,4,5,6] | |
##### Input Data #2 | |
# x_data = np.arange(100, step=20) | |
# y_data = x_data + 20 * np.sin(x_data/10) | |
##### Input Data #3 | |
# x_data = [1, 2, 3, 4] | |
# y_data = [2, 4, 6, 8] | |
##### Input Data #4 | |
x_data = [6, 9, 12, 15,20,25,31,37,44,54] | |
y_data = [10,15, 14, 18,25,35,50,65,80,95] | |
######################################################################################## regression using sklearn | |
#### change specific to sklearn | |
x = np.array(x_data).reshape(-1, 1) | |
y = y_data | |
# print(len(x_data), len(y_data)) | |
regr = linear_model.LinearRegression() | |
regr.fit(x, y) | |
m=regr.coef_[0] | |
b=regr.intercept_ | |
print("****Using sklearn \n slope=",m, "\nintercept=",b,"\n***") | |
# xx= x | |
# yy = regr.predict(xx) | |
# plt.scatter(x,y,s=None, marker='o',color='g',edgecolors='g',alpha=0.9,label="Jagur") | |
# plt.plot(xx,yy) | |
# plt.show() | |
######################################################################################## regression using TensorFlow | |
# Define input data | |
# variable for parameter slope (W) | |
W = tf.Variable([1.0], tf.float32) | |
#variable for parameter bias (b) | |
b = tf.Variable([1.0], tf.float32) | |
# placeholders for independent variable, denoted by x | |
x = tf.placeholder(tf.float32) | |
# Equation of Linear Regression | |
linear_model = W * x + b | |
# Initializing the session | |
sess = tf.Session() | |
init = tf.global_variables_initializer() | |
sess.run(init) | |
# running regression model | |
print(sess.run(linear_model, {x: x_data})) | |
# placefolder for dependent variable | |
y = tf.placeholder(tf.float32) | |
#Loss function | |
loss = tf.reduce_sum(tf.square(linear_model - y)) | |
print(sess.run(loss, {x:x_data, y:y_data})) | |
# an instance of gradient descent optimizer | |
optimizer = tf.train.GradientDescentOptimizer(0.01) | |
train = optimizer.minimize(loss) | |
for i in range(1000): | |
sess.run(train, {x:x_data, y:y_data}) | |
print("*** Using TensorFlow") | |
print(sess.run([W, b])) | |
print("*** Program Ended ***") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment