使用tensorflow的多项式回归的例子(二)

发布于:2025-07-11 ⋅ 阅读:(21) ⋅ 点赞:(0)

例2

import tensorflow as tf

import numpy as np

import matplotlib.pyplot as plt

plt.style.use('default')

#import tensorflow.contrib.eager as tfe

#from google.colab import files

#tf.enable_eager_execution()

x = np.arange(0, 5, 0.1)

y = x**3 - 4*x**2 - 2*x + 2

y_noise = y + np.random.normal(0, 1.5, len(x))

fig = plt.figure (figsize = (10,6))

plt.plot(x,y, label = "Ground truth", color = 'black')

plt.scatter(x, y_noise, label = "Ground truth + noise", color = 'black')

plt.legend(fontsize = 16, loc = 'upper left')

plt.xlabel ('x', fontsize = 16)

plt.ylabel ('y', fontsize = 16)

plt.tick_params(axis='both', which='major', labelsize=16)

# Save the file for publication

fig.savefig('ch2-michelucci-fig1.png', bbox_inches='tight', dpi = 300)

#files.download('ch2-michelucci-fig1.png')

def model(x,w):

    pred=w[0] * x ** 3 + w[1] * x ** 2 + w[2] * x + w[3]

    return pred

def loss(x, y,w):

    err = model(x,w) - y

    return tf.reduce_mean(tf.square(err))

def grad(x,y,w):

    with tf.GradientTape() as tape:

        loss_ = loss(x,y,w)

return tape.gradient(loss_,[w])

optimizer = tf.optimizers.Adam()

W = tf.Variable(np.random.randn(4),dtype=tf.float32)

loss_list_train = []

#model = PolyModel()

#with tf.GradientTape() as tape:

#grad = tf.implicit_gradients(loss)

#dL_dW,dL_db = tape.gradient(Loss_train,[W,b])

iters = 20001

for i in range(iters):

  #optimizer.apply_gradients(grad(model, x, y))

  grads=grad(x,y,W)

  optimizer.apply_gradients(zip(grads,[W]))

  loss_train =loss(x,y,W).numpy()

  loss_list_train.append(loss_train)

  if i % 2000 == 0:

        print("Iteration {}, loss: {}".format(i, loss_train))

loss_hist=loss_list_train

fig = plt.figure (figsize = (10,6))

plt.plot(np.arange(0, iters),loss_hist, label = "Loss function (MSE)", color = 'black')

plt.legend(fontsize = 16, loc = 'upper right')

plt.xlabel ('Iterations', fontsize = 16)

plt.ylabel ('MSE', fontsize = 16)

plt.tick_params(axis='both', which='major', labelsize=16)

plt.xlim(0, 20000)

# Save the file for publication

fig.savefig('ch2-michelucci-fig2.png', bbox_inches='tight', dpi = 300)

#files.download('ch2-michelucci-fig2.png')

fig = plt.figure (figsize = (10,6))

plt.plot(x,y, label = "Ground truth", color = 'black')

plt.scatter(x, y_noise, label = "Ground truth + noise", color = 'black')

plt.plot(x, model(x,W).numpy(), color = 'red', ls = '--', lw = 3,

         label = 'Fitted function')

plt.legend(fontsize = 16, loc = 'upper left')

plt.xlabel ('x', fontsize = 16)

plt.ylabel ('y', fontsize = 16)

plt.tick_params(axis='both', which='major', labelsize=16)

# Save the file for publication

fig.savefig('ch2-michelucci-fig3.png', bbox_inches='tight', dpi = 300)

#files.download('ch2-michelucci-fig3.png')

 


网站公告

今日签到

点亮在社区的每一天
去签到