# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/cxbxmxcx/GenReality/blob/master/GEN_1_perceptron_class.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="SEZMaMLfrG9M"
import numpy as np
import matplotlib.pyplot as plt

# + [markdown] id="gApgZsEoiSU3"
# Regression

# + id="71rIK6EniPOA" outputId="385921a1-fae4-425f-b378-ea145e914f2a" colab={"base_uri": "https://localhost:8080/", "height": 50}
X = np.array([[1,2,3],[3,4,5],[5,6,7],[7,8,9],[9,8,7]])
Y = np.array([1,2,3,4,5])
             
print(X.shape)
print(Y.shape)

# + id="OGR8Z5t1n0bS" outputId="925fd64d-96ad-498d-c6aa-be35b1f179f6" colab={"base_uri": "https://localhost:8080/", "height": 265}
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:,0], X[:,1], X[:,2], c='r', marker='o')

# + [markdown] id="VViIopAmsCnY"
# Hyperparameters & Parameters

# + id="EbxVDzvLsBzz" outputId="4c8c4912-9893-434a-9bb6-c24b1520e70c" colab={"base_uri": "https://localhost:8080/", "height": 34}
no_of_inputs = X.shape[1]
epochs = 50
learning_rate = .01
weights = np.random.rand(no_of_inputs + 1)
print(weights.shape)


# + [markdown] id="zB23JptVszqe"
# Activation Function

# + id="4Cermj4Fst_M"
def relu_activation(sum):
  if sum > 0: return sum
  else: return 0


# + [markdown] id="62CgnF7SjeKD"
# Step Activation
#

# + [markdown] id="Lp8_pfqew8Y9"
# The Preceptron Class

# + id="-i15fphnw-xO"
class Perceptron(object):
  def __init__(self, no_of_inputs, activation):
    self.learning_rate = learning_rate
    self.weights = np.zeros(no_of_inputs + 1)
    self.activation = activation
           
  def predict(self, inputs):
    summation = np.dot(inputs, self.weights[1:]) + self.weights[0]
    return self.activation(summation)

  def train(self, training_inputs, training_labels, epochs=100, learning_rate=0.01):
    history = []
    for _ in range(epochs):
      for inputs, label in zip(training_inputs, training_labels):
        prediction = self.predict(inputs)
        loss = (label - prediction) 
        loss2 = loss*loss
        history.append(loss2)
        print(f"loss = {loss2}")
        self.weights[1:] += self.learning_rate * loss * inputs
        self.weights[0] += self.learning_rate * loss
    return history


# + [markdown] id="QBhQ6O12u0A6"
# Regression Perceptron & Training
#

# + id="IHY5n0MxxbDM" outputId="f1fc2219-598e-4503-f918-6a0d8978ccf9" colab={"base_uri": "https://localhost:8080/", "height": 1000}
perceptron = Perceptron(no_of_inputs, relu_activation)
history = perceptron.train(X,Y, epochs=epochs)

# + [markdown] id="WIyIMYX_jGY3"
# Regression Prediction

# + id="DkfR58uEyfTJ" outputId="c8cfe0a5-bf4e-4316-9585-36db5d9f987b" colab={"base_uri": "https://localhost:8080/", "height": 34}
perceptron.predict(np.array([3,3,3]))

# + id="dy9cIYdvMBHN" outputId="fcd8e492-bd29-43f6-c863-4f10234f0b77" colab={"base_uri": "https://localhost:8080/", "height": 282}
plt.plot(history)

StarcoderdataJupyterScriptsDedupFiltered