我们使用Iris数据,Sepal length为y值而Petal width为x值。
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn import datasets
from tensorflow.python.framework import ops
ops.reset_default_graph()
# Load the data
# iris.data = [(Sepal Length, Sepal Width, Petal Length, Petal Width)]
iris = datasets.load_iris()
x_vals = np.array([x[3] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])
def model(x,w,b):
# Declare model operations
model_output = tf.add(tf.matmul(x, w), b)
return model_output
def loss(x, y,w,b):
# Declare loss function (L2 loss)
loss = tf.reduce_mean(tf.square(y - model(x,w,b)))
return loss
def grad(x,y,w,b):
with tf.GradientTape() as tape:
loss_ = loss(x,y,w,b)
return tape.gradient(loss_,[w,b])
# Declare batch size
batch_size = 25
# Create variables for linear regression
w = tf.Variable(tf.random.normal(shape=[1,1]),tf.float32)
b = tf.Variable(tf.random.normal(shape=[1,1]),tf.float32)
optimizer = tf.optimizers.Adam()
# Training loop
loss_vec = []
for i in range(5000):
rand_index = np.random.choice(len(x_vals), size=batch_size)
rand_x = np.transpose([x_vals[rand_index]])
rand_y = np.transpose([y_vals[rand_index]])
x=tf.cast(rand_x,tf.float32)
y=tf.cast(rand_y,tf.float32)
grads=grad(x,y,w,b)
optimizer.apply_gradients(zip(grads,[w,b]))
#sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
temp_loss = loss(x, y,w,b).numpy()
#sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
loss_vec.append(temp_loss)
if (i+1)%25==0:
print('Step #' + str(i+1) + ' A = ' + str(w.numpy()) + ' b = ' + str(b.numpy()))
print('Loss = ' + str(temp_loss))
# Get the optimal coefficients
[slope] = w.numpy()
[y_intercept] = b.numpy()
# Get best fit line
best_fit = []
for i in x_vals:
best_fit.append(slope*i+y_intercept)
# Plot the result
plt.plot(x_vals, y_vals, 'o', label='Data Points')
plt.plot(x_vals, best_fit, 'r-', label='Best fit line', linewidth=3)
plt.legend(loc='upper left')
plt.title('Sepal Length vs Petal Width')
plt.xlabel('Petal Width')
plt.ylabel('Sepal Length')
plt.show()
# Plot loss over time
plt.plot(loss_vec, 'k-')
plt.title('L2 Loss per Generation')
plt.xlabel('Generation')
plt.ylabel('L2 Loss')
plt.show()