{:title ["3 Polynomial Line Fitting (Video)"], :check ["true"]}
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as pl
Generate some data
x_data = np.linspace(0, 1, 10)
y_data = 3 * x_data + np.sin(6*x_data) + 1.
pl.plot(x_data, y_data, '--o');
Compute the power series of x
K = 5
def series(x):
powers = np.zeros((len(x), K+1))
for i in range(K+1):
powers[:, i] = np.power(x, i)
return powers
Model and parameters
f(x)=k∑i=0aixiThe model parameters will be:
θ=[a0,a1,…,ak]theta = [
tf.Variable(np.random.randn(K+1, 1), dtype=tf.float64)
]
def f(x):
w = theta[0]
return tf.squeeze(series(x) @ w)
We will use a TensorFlow built-in loss function.
loss(y_true, y_pred)
loss = tf.keras.losses.MeanSquaredError()
We can use a built-in optimizer.
All TensorFlow optimizers have a method:
optimizer.apply_gradients([
(grad_1, var_1),
(grad_2, var_2),
...
(grad_n, var_n),
])
optimizer = tf.keras.optimizers.Adam()
## To illustrate now an optimizer works
L0 = loss(y_data, f(x_data))
print("L0 =", L0)
with tf.GradientTape() as tape:
L = loss(y_data, f(x_data))
grads = tape.gradient(L, theta)
optimizer.apply_gradients(zip(grads, theta))
L = loss(y_data, f(x_data))
print("L =", L)
def train(epochs):
# initialize the model parameters
theta[0].assign(np.random.randn(K+1, 1))
# Training loop
for i in range(epochs):
with tf.GradientTape() as tape:
L = loss(y_data, f(x_data))
grads = tape.gradient(L, theta)
optimizer.apply_gradients(zip(grads,theta))
if(i % (epochs // 10)) == 0:
L = loss(y_data, f(x_data))
print("[%.2d] %.2f" % (i, L))
train(20000)
pl.plot(x_data, y_data, 'o')
pl.plot(x_data, f(x_data), '--', color='red');