단일 뉴런 (Single Neuron) - 단일 입력

Very Simple Neuron

In [4]:
import numpy as np
import random
import math
In [22]:
class SimpleNeuron:
    def __init__(self):
        self.w = np.array([0.0])   # weight of one input
        self.b = np.array([0.0])   # bias
        print("Initial w: {0}, b: {1}".format(self.w, self.b))

    def u(self, x):
        return np.dot(self.w, x) + self.b

    def f(self, u):
        return max(0.0, u)

    def z(self, x):
        u = self.u(x)
        return self.f(u)

    def squared_error(self, x, z_target):
        return 1.0 / 2.0 * math.pow(self.z(x) - z_target, 2)

    def numerical_f_derivative(self, u):
        delta = 1e-6 # 0.000001
        return (self.f(u + delta) - self.f(u - delta)) / (2 * delta)

    def d_E_over_d_w(self, x, z_target):
        u = self.u(x)
        z = self.f(u)
        error = z - z_target
        return error * self.numerical_f_derivative(u) * x

    def d_E_over_d_b(self, x, z_target):
        u = self.u(x)
        z = self.f(u)
        error = z - z_target
        return error * self.numerical_f_derivative(u)

    def learning(self, alpha, maxEpoch, data):
        for i in range(maxEpoch):
            for idx in range(data.numTrainData):
                x = data.training_input_value[idx]
                z_target = data.training_z_target[idx]
                self.w = self.w - alpha * self.d_E_over_d_w(x, z_target)
                self.b = self.b - alpha * self.d_E_over_d_b(x, z_target)
                
            sum = 0.0
            for idx in range(data.numTrainData):
                sum = sum + self.squared_error(data.training_input_value[idx], data.training_z_target[idx])
            print("Epoch {0}: Error: {1}, w: {2}, b: {3}".format(i, sum / data.numTrainData, self.w, self.b))

Function Estimation (or Generation)

  • $f(x) = 10 \cdot x + 4$
In [24]:
class Data:
    def __init__(self):
        self.training_input_value = np.array([1.0, 2.0, 3.0])
        self.training_z_target = np.array([14.0, 24.0, 34.0])
        self.numTrainData = len(self.training_input_value)

if __name__ == '__main__':
    n = SimpleNeuron()
    d = Data()
    for idx in range(d.numTrainData):
        x = d.training_input_value[idx]
        z = n.z(x)
        z_target = d.training_z_target[idx]
        error = n.squared_error(x, z_target)
        print("x: {0}, z: {1}, z_target: {2}, error: {3}".format(x, n.z(x), z_target, error))

    n.learning(0.01, 100, d)

    for idx in range(d.numTrainData):
        x = d.training_input_value[idx]
        z = n.z(x)
        z_target = d.training_z_target[idx]
        error = n.squared_error(x, z_target)
        print("x: {0}, z: {1}, z_target: {2}, error: {3}".format(x, n.z(x), z_target, error))
Initial w: [ 0.], b: [ 0.]
x: 1.0, z: 0.0, z_target: 14.0, error: 98.0
x: 2.0, z: 0.0, z_target: 24.0, error: 288.0
x: 3.0, z: 0.0, z_target: 34.0, error: 578.0
Epoch 0: Error: 230.77323231138317, w: [ 1.50440618], b: [ 0.65821635]
Epoch 1: Error: 163.75834269128222, w: [ 2.83133202], b: [ 1.2125931]
Epoch 2: Error: 116.20313889204839, w: [ 3.9491839], b: [ 1.67949548]
Epoch 3: Error: 82.45712804823386, w: [ 4.89091112], b: [ 2.07270962]
Epoch 4: Error: 58.51051668790867, w: [ 5.68427023], b: [ 2.40384897]
Epoch 5: Error: 41.5178155898545, w: [ 6.35264347], b: [ 2.68269676]
Epoch 6: Error: 29.45977033479816, w: [ 6.91572808], b: [ 2.91749437]
Epoch 7: Error: 20.903453747406118, w: [ 7.39011706], b: [ 3.11518441]
Epoch 8: Error: 14.832017935918415, w: [ 7.78978844], b: [ 3.28161531]
Epoch 9: Error: 10.523879631718419, w: [ 8.12651731], b: [ 3.42171382]
Epoch 10: Error: 7.46698545584297, w: [ 8.41022308], b: [ 3.53963025]
Epoch 11: Error: 5.297971293759992, w: [ 8.64926185], b: [ 3.63886082]
Epoch 12: Error: 3.75898753044318, w: [ 8.85067286], b: [ 3.72235077]
Epoch 13: Error: 2.6670606782590753, w: [ 9.02038595], b: [ 3.79258116]
Epoch 14: Error: 1.8923513178649813, w: [ 9.16339648], b: [ 3.85164206]
Epoch 15: Error: 1.3427252038075776, w: [ 9.2839125], b: [ 3.90129413]
Epoch 16: Error: 0.9528044289981762, w: [ 9.38547894], b: [ 3.94302056]
Epoch 17: Error: 0.6761978031631971, w: [ 9.47108202], b: [ 3.9780708]
Epoch 18: Error: 0.47998731311477644, w: [ 9.54323736], b: [ 4.00749738]
Epoch 19: Error: 0.34081558030499076, w: [ 9.60406412], b: [ 4.03218695]
Epoch 20: Error: 0.24210943346499122, w: [ 9.65534748], b: [ 4.05288644]
Epoch 21: Error: 0.17210970666062195, w: [ 9.6985913], b: [ 4.07022505]
Epoch 22: Error: 0.12247315754785983, w: [ 9.73506242], b: [ 4.08473282]
Epoch 23: Error: 0.08728037723585795, w: [ 9.76582804], b: [ 4.09685626]
Epoch 24: Error: 0.06233180132443816, w: [ 9.79178716], b: [ 4.10697151]
Epoch 25: Error: 0.044648163294133465, w: [ 9.81369712], b: [ 4.11539543]
Epoch 26: Error: 0.03211602343447189, w: [ 9.83219589], b: [ 4.12239494]
Epoch 27: Error: 0.02323624497021504, w: [ 9.84782091], b: [ 4.1281949]
Epoch 28: Error: 0.016945522058852847, w: [ 9.8610249], b: [ 4.13298471]
Epoch 29: Error: 0.01248974528121452, w: [ 9.87218927], b: [ 4.13692395]
Epoch 30: Error: 0.009334150518167307, w: [ 9.88163528], b: [ 4.14014702]
Epoch 31: Error: 0.007099568613483231, w: [ 9.8896336], b: [ 4.14276716]
Epoch 32: Error: 0.005517195360493611, w: [ 9.89641221], b: [ 4.14487976]
Epoch 33: Error: 0.004396502700165672, w: [ 9.90216318], b: [ 4.14656516]
Epoch 34: Error: 0.0036024741406858025, w: [ 9.90704831], b: [ 4.14789104]
Epoch 35: Error: 0.003039455915767942, w: [ 9.9112039], b: [ 4.14891444]
Epoch 36: Error: 0.002639702008117589, w: [ 9.91474477], b: [ 4.14968337]
Epoch 37: Error: 0.0023552493466718525, w: [ 9.91776764], b: [ 4.15023829]
Epoch 38: Error: 0.0021521555716886077, w: [ 9.92035399], b: [ 4.1506133]
Epoch 39: Error: 0.0020064128203034046, w: [ 9.92257247], b: [ 4.15083709]
Epoch 40: Error: 0.0019010504191091647, w: [ 9.9244809], b: [ 4.15093385]
Epoch 41: Error: 0.0018240808826519295, w: [ 9.92612798], b: [ 4.15092395]
Epoch 42: Error: 0.0017670440278193377, w: [ 9.92755477], b: [ 4.15082456]
Epoch 43: Error: 0.0017239752598930222, w: [ 9.92879581], b: [ 4.15065012]
Epoch 44: Error: 0.0016906746365645787, w: [ 9.92988025], b: [ 4.15041281]
Epoch 45: Error: 0.0016641891818495406, w: [ 9.9308326], b: [ 4.15012288]
Epoch 46: Error: 0.0016424463676120696, w: [ 9.93167354], b: [ 4.14978897]
Epoch 47: Error: 0.0016239947327804894, w: [ 9.93242047], b: [ 4.14941836]
Epoch 48: Error: 0.0016078204168201767, w: [ 9.93308807], b: [ 4.14901717]
Epoch 49: Error: 0.0015932174685211456, w: [ 9.93368869], b: [ 4.14859055]
Epoch 50: Error: 0.0015796962349081845, w: [ 9.93423273], b: [ 4.14814287]
Epoch 51: Error: 0.0015669187053456654, w: [ 9.93472898], b: [ 4.14767777]
Epoch 52: Error: 0.0015546529270639377, w: [ 9.93518481], b: [ 4.14719834]
Epoch 53: Error: 0.0015427409066349842, w: [ 9.93560645], b: [ 4.14670717]
Epoch 54: Error: 0.0015310760414176123, w: [ 9.93599915], b: [ 4.14620645]
Epoch 55: Error: 0.0015195872801138759, w: [ 9.93636732], b: [ 4.14569801]
Epoch 56: Error: 0.0015082280302528203, w: [ 9.93671469], b: [ 4.1451834]
Epoch 57: Error: 0.0014969684105239492, w: [ 9.93704438], b: [ 4.14466393]
Epoch 58: Error: 0.0014857898568032075, w: [ 9.93735905], b: [ 4.14414069]
Epoch 59: Error: 0.0014746813817159805, w: [ 9.93766093], b: [ 4.1436146]
Epoch 60: Error: 0.001463636993571124, w: [ 9.93795187], b: [ 4.14308644]
Epoch 61: Error: 0.001452653926240501, w: [ 9.93823348], b: [ 4.14255686]
Epoch 62: Error: 0.0014417314346280073, w: [ 9.93850708], b: [ 4.14202641]
Epoch 63: Error: 0.0014308699831989075, w: [ 9.93877379], b: [ 4.14149555]
Epoch 64: Error: 0.0014200707064747186, w: [ 9.93903456], b: [ 4.14096466]
Epoch 65: Error: 0.001409335056679278, w: [ 9.93929019], b: [ 4.14043407]
Epoch 66: Error: 0.0013986645792876987, w: [ 9.93954135], b: [ 4.13990406]
Epoch 67: Error: 0.0013880607752218461, w: [ 9.93978861], b: [ 4.13937484]
Epoch 68: Error: 0.0013775250210812523, w: [ 9.94003244], b: [ 4.13884661]
Epoch 69: Error: 0.001367058527654942, w: [ 9.94027325], b: [ 4.13831954]
Epoch 70: Error: 0.0013566623231684352, w: [ 9.94051138], b: [ 4.13779374]
Epoch 71: Error: 0.0013463372520405373, w: [ 9.94074712], b: [ 4.13726934]
Epoch 72: Error: 0.0013360839829327145, w: [ 9.94098071], b: [ 4.13674643]
Epoch 73: Error: 0.0013259030219552033, w: [ 9.94121236], b: [ 4.13622508]
Epoch 74: Error: 0.0013157947283265158, w: [ 9.94144223], b: [ 4.13570535]
Epoch 75: Error: 0.0013057593307595596, w: [ 9.94167047], b: [ 4.13518731]
Epoch 76: Error: 0.0012957969435151908, w: [ 9.94189722], b: [ 4.13467098]
Epoch 77: Error: 0.0012859075815059608, w: [ 9.94212256], b: [ 4.13415641]
Epoch 78: Error: 0.0012760911741270268, w: [ 9.9423466], b: [ 4.13364363]
Epoch 79: Error: 0.0012663475776832826, w: [ 9.9425694], b: [ 4.13313265]
Epoch 80: Error: 0.0012566765863959975, w: [ 9.94279104], b: [ 4.1326235]
Epoch 81: Error: 0.0012470779420509302, w: [ 9.94301156], b: [ 4.13211618]
Epoch 82: Error: 0.0012375513423836962, w: [ 9.94323101], b: [ 4.13161072]
Epoch 83: Error: 0.0012280964483207821, w: [ 9.94344943], b: [ 4.13110711]
Epoch 84: Error: 0.001218712890198719, w: [ 9.94366685], b: [ 4.13060536]
Epoch 85: Error: 0.0012094002730839533, w: [ 9.94388331], b: [ 4.13010548]
Epoch 86: Error: 0.0012001581813072667, w: [ 9.94409882], b: [ 4.12960746]
Epoch 87: Error: 0.0011909861823153644, w: [ 9.94431342], b: [ 4.12911132]
Epoch 88: Error: 0.0011818838299376881, w: [ 9.94452711], b: [ 4.12861704]
Epoch 89: Error: 0.0011728506671480996, w: [ 9.94473992], b: [ 4.12812462]
Epoch 90: Error: 0.0011638862283978536, w: [ 9.94495185], b: [ 4.12763407]
Epoch 91: Error: 0.001154990041580486, w: [ 9.94516293], b: [ 4.12714537]
Epoch 92: Error: 0.0011461616296867816, w: [ 9.94537316], b: [ 4.12665853]
Epoch 93: Error: 0.0011374005121949325, w: [ 9.94558254], b: [ 4.12617354]
Epoch 94: Error: 0.001128706206237044, w: [ 9.9457911], b: [ 4.12569039]
Epoch 95: Error: 0.0011200782275775218, w: [ 9.94599883], b: [ 4.12520909]
Epoch 96: Error: 0.0011115160914310052, w: [ 9.94620575], b: [ 4.12472962]
Epoch 97: Error: 0.001103019313146341, w: [ 9.94641186], b: [ 4.12425198]
Epoch 98: Error: 0.0010945874087772338, w: [ 9.94661717], b: [ 4.12377616]
Epoch 99: Error: 0.0010862198955570532, w: [ 9.94682167], b: [ 4.12330216]
x: 1.0, z: [ 14.07012383], z_target: 14.0, error: 0.0024586758850689804
x: 2.0, z: [ 24.0169455], z_target: 24.0, error: 0.00014357505516145237
x: 3.0, z: [ 33.96376718], z_target: 34.0, error: 0.0006564087464407272
In [ ]: