In [1]:
import numpy as np
import tensorflow as tf
In [2]:
np.random.seed(101)
tf.set_random_seed(101)
In [3]:
rand_a = np.random.uniform(0, 100, (5, 5))
In [4]:
rand_a
Out[4]:
In [5]:
rand_b = np.random.uniform(0, 100, (5, 1))
In [6]:
rand_b
Out[6]:
In [7]:
a = tf.placeholder(tf.float32)
In [8]:
b = tf.placeholder(tf.float32)
In [9]:
add_op = a + b
In [10]:
mul_op = a * b
In [12]:
with tf.Session() as sess:
add_result = sess.run(add_op, feed_dict={a: rand_a, b: rand_b})
mult_result = sess.run(mul_op, feed_dict={a: rand_a, b: rand_b})
print(add_result)
print("\n")
print(mult_result)
In [13]:
n_features = 10
n_dense_neurons = 3
In [14]:
x = tf.placeholder(tf.float32, (None, n_features))
In [17]:
W = tf.Variable(tf.random_normal([n_features, n_dense_neurons]))
b = tf.Variable(tf.ones([n_dense_neurons]))
In [20]:
xW = tf.matmul(x, W)
In [21]:
z = tf.add(xW, b)
In [22]:
a = tf.sigmoid(z)
In [23]:
init = tf.global_variables_initializer()
In [26]:
with tf.Session() as sess:
sess.run(init)
layer_out = sess.run(a, feed_dict={x: np.random.random([1, n_features])})
In [27]:
print(layer_out)
In [29]:
x_data = np.linspace(0, 10, 10) + np.random.uniform(-1.5, 1.5, 10)
In [33]:
import matplotlib.pyplot as plt
%matplotlib inline
In [34]:
y_label = np.linspace(0, 10, 10) + np.random.uniform(-1.5, 1.5, 10)
In [36]:
plt.plot(x_data, y_label, "*")
Out[36]:
y = mx + b
In [38]:
np.random.rand(2)
Out[38]:
In [47]:
m = tf.Variable(0.78452185)
b = tf.Variable(0.68724204)
In [48]:
error = 0
for x, y in zip(x_data, y_label):
y_hat = m*x + b
error += (y - y_hat)**2 # squared error
In [49]:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001) # gradient descent optimizer
In [50]:
train = optimizer.minimize(error)
In [53]:
init = tf.global_variables_initializer()
In [54]:
with tf.Session() as sess:
sess.run(init)
training_steps = 1
for i in range(training_steps):
sess.run(train)
final_slope, final_intercept = sess.run([m, b])
In [55]:
x_test = np.linspace(-1, 11, 10)
y_pred_plot = final_slope * x_test + final_intercept
In [57]:
plt.plot(x_test, y_pred_plot, "r")
plt.plot(x_data, y_label, "*")
Out[57]:
In [ ]: