import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt


x_data = tf.random.normal(shape=(1000,), dtype= tf.float32)
y_data = 3*x_data + 1

w = tf.Variable(-1.)
b = tf.Variable(-1.)

learning_rate = 0.01 #보편화된 수치 0.01

w_trace, b_trace = [], []
for x, y in zip(x_data, y_data) :
    with tf.GradientTape() as tape :
        prediction = (w*x + b)
        loss = (prediction - y)**2

    gradients = tape.gradient(loss, [w,b])

    w_trace.append(w.numpy())
    b_trace.append(b.numpy())

    w = tf.Variable(w - learning_rate * gradients[0])
    b = tf.Variable(b - learning_rate * gradients[1])
    break

fig, ax = plt.subplots(figsize = (20, 10))
ax.plot(w_trace, label = "weight")
ax.plot(b_trace, label = "bias")

ax.tick_params(labelsize = 20)
ax.legend(fontsize =30)

카테고리:

업데이트:

댓글남기기