megadanyx / Python-VN-HW

Python HW VANAR
1 stars 0 forks source link

HW1 | ML #4

Open megadanyx opened 2 years ago

megadanyx commented 2 years ago

image

# Input: room temperature [0 ... 50 ] Celsius degrees
# Outpus: [0 ... 1] / ok - hot
# Correct solution
# T >= 30C -> Then hot
from random import randint
import matplotlib.pylab as plt
# X -----> (NEURON) -------> Y
# print(" X   >  Y")
# print("---------")

# Testing date
x1 = [t for t in range(0,51)]
x2 = [t for t in range(0,51)]
y = []

# Training date
xt1 = [0,   5, 10, 15, 20, 25, 30, 35, 40]
xt2 = [5,  10, 15, 20, 25, 30, 35, 40, 45]
yt =  [0,   0,  0,  1,  1,  1,  1,  1,  1]
#     i
# learning rate
lr = 0.0001
epochs = 0
accuracy = [0 for a in range(len(xt1))]
B = 0
W1 = 0
W2 = 0

while epochs<= 1000:
    # NEURON TRAINING
    # print(f"set:       X       Yp      Yt      e      dW")
    for i in range(len(xt1)):
    # pick one trining sample
        Xt1 = xt1[i]
        Xt2 = xt2[i]
        Yt = yt[i]
        # runing one prediction
        ##################### NEURON #######################
        # B = 0
        # W = 0
        # some of inputs
        _Y = Xt1 * W1 + Xt2 * W2 + B

        # Step activation function
        Yp = 1 if _Y >= 1 else 0
        ##################### NEURON #######################
        # estimate error
        e = Yt - Yp

        # W - parametru optimization
        dw1 = lr * e * Xt1
        W1 += dw1
        dw2 = lr * e * Xt2
        W2 += dw2
        # print(f"{i:3}:{Xt:8}{Yp:8}{Yt:8}{e:8}{dw:8.3}{W:8.3}")
        accuracy[i] = 1 - e 
    epochs += 1
    # print(f"{epochs:3} {dw} {W}")
print("Training Done!")

accuracy_avg = sum(accuracy) / len(accuracy)
# print(f"ACCURACY: {accuracy_avg}")

    # EXPLICIT NEURON   

print("\n\nTesting...")
print(f"set:      X1      X2      Yp")
for i in range(len(x1)):
    # pick one trining sample
    Xt1 = x1[i]
    Xt2 = x1[i]

    # runing one prediction
    ##################### NEURON #######################
    # some of inputs
    _Y = Xt1 * W1 + Xt2 * W2 + B
    # Step activation function
    Yp = 1 if _Y >= 1 else 0
    ##################### NEURON #######################
    y.append(Yp)
    print(f"{i:3}{Xt1:8}{Xt2:8}{Yp:8}")
print("Testing Done!")
print(f"ACCURACY: {accuracy_avg}")
plt.plot(x1,y)

image

image

megadanyx commented 2 years ago

fixed accuracy

image image