2019.12.29(pm): Cost function and Python class

https://github.com/SeongjooHong/jupyter-notebook/blob/master/cost%20function%20and%20graduent%20descent%20and%20python%20class%20study.ipynb

import matplotlib.pyplot as plt
from sklearn.datasets import load_diabetes
diabetes = load_diabetes()

diabetes.target[:3]
plt.scatter(diabetes.data[:, 2], diabetes.target)
plt.xlabel('x')
plt.xlabel('y')
plt.show()

x = diabetes.data[:, 2]
y = diabetes.target

w = 1.0
b = 1.0

y_hat = x[0] * w + b
w_inc = w + 0.1
y_hat_inc = x[0] * w_inc + b
w_rate = (y_hat_inc - y_hat) / (w_inc - w)
w_new = w + w_rate
b_inc = b + 0.1
y_hat_inc = x[0] * w + b_inc
b_rate = (y_hat_inc - y_hat) / (b_inc - b)
err = y[0] - y_hat
w_new = w + w_rate * err
b_new = b + 1 * err
y_hat = x[1] * w_new + b_new
err = y[1] - y_hat
w_rate = x[1]
w_new = w_new + w_rate * err
b_new = b_new + 1 * err

for i in range(1,100):
    for x_i, y_i in zip(x, y):
        y_hat = x_i * w + b
        err = y_i - y_hat
        w_rate = x_i
        w = w + w_rate * err
        b = b + 1 * err
print(w, b)

y_hat = x_i * w + b
err = y_i - y_hat
w_rate = x_i
w = w + w_rate * err

err = y_i - y_hat
b = b + 1 * err

class Neuron:
    def __init__(self):
        self.w =1.0
        self.b =1.0
    
    def forpass(self, x):
        y_hat = x * self.w + self.b
        return y_hat
    
    def backprop(self, x, err):
        w_grad = x * err
        b_grad = 1 * err
        return w_grad, b_grad
    def fit(self, x, y, epochs = 100):
        for i in range(epochs):
            for x_i, y_i in zip(x, y):
                y_hat = self.forpass(x_i)
                err = -(y_i - y_hat)
                w_grad, b_grad = self.backprop(x_i, err)
                self.w -= w_grad
                self.b -= b_grad

neuron = Neuron()
neuron.fit(x, y)

plt.scatter(x, y)
pt1 = (-0.1, -0.1 * neuron.w + neuron.b)
pt2 = (0.15, 0.15 * neuron.w + neuron.b)
plt.plot([pt1[0], pt2[0]], [pt1[1], pt2[1]])
plt.xlabel('x')
plt.xlabel('y')
plt.show()

6 Replies to “2019.12.29(pm): Cost function and Python class”

  1. Good post. I learn something totally new and challenging on sites I
    stumbleupon everyday. It will always be helpful to read articles from other writers
    and use something from their websites.

  2. Do you mind if I quote a couple of your posts as long as I
    provide credit and sources back to your blog? My blog is in the exact same niche as yours and my visitors
    would genuinely benefit from a lot of the information you provide here.
    Please let me know if this alright with you.

    Appreciate it!

  3. Hi! I know this is kind of off topic but I was
    wondering if you knew where I could find a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having trouble
    finding one? Thanks a lot!

  4. Hey There. I found your blog using msn. This is a very well written article.
    I will be sure to bookmark it and come back to read more of your useful info.
    Thanks for the post. I’ll definitely return.

Leave a Reply