Skip to content

Instantly share code, notes, and snippets.

@2hands10fingers
Last active June 28, 2020 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2hands10fingers/ba2fb3b21788c5436241a99d110b0ffb to your computer and use it in GitHub Desktop.
Save 2hands10fingers/ba2fb3b21788c5436241a99d110b0ffb to your computer and use it in GitHub Desktop.
import numpy as np
"""
Special Notes:
- A model is your set weights and biases, and potentially an activation function
and potentially your type of network? Not sure. Need clarification
"""
# Inputs / variables from your dataset
np.random.seed(0)
class Layer_Dense:
# Not first layer. First layer is inputs.
# This could be as many layers as you want theoretically.
# acitvation_function(inputs * weight + bias) // activation_function(y = mx + b)
def __init__(self, n_inputs, n_neurons):
self.weights = self.rando_weights(n_inputs, n_neurons)
self.biases = self.bias_set(n_neurons)
def forward(self, inputs):
self.inputs = inputs
self.output = self.dot_prod(self.inputs, self.weights) + self.biases
# print(self.output)
if (self.activation):
self.activate()
return self
def bias_set(self, n_neurons):
return np.zeros((1, n_neurons))
def rando_weights(self, n_inputs, n_neurons):
return 0.10 * np.random.randn(n_inputs, n_neurons)
def dot_prod(self, inputs, weights):
return np.dot(inputs, weights)
def activation_fn(self, acti_type):
activations = Activations()
self.activavtion_abrrev = acti_type
self.activation = activations.fn(acti_type)
return self
def activate(self):
if(self.output.any() and self.activation):
data = self.activation()
data.forward(self.output)
self.output = data.output
return self
def next(self, new=None):
neuro_length = len(self.output[0])
next_handler = neuro_length if new == None else new
self.weights = self.rando_weights(neuro_length, next_handler)
self.bias = self.bias_set(neuro_length)
self.forward(self.output)
return self
def iterNext(self, layer_count):
for i in range(0,layer_count):
self.next()
return self
class Activations:
def __init__(self):
self.activations = {
"relu": Activation_relu
"sigmoid" Avtivation_sigmoid
}
def fn(self, act_type):
activation = act_type.lower()
self.active_activation = activation
return self.activations[activation]
class Activation_relu:
def forward(self, inputs):
self.output = np.maximum(0, inputs)
class Avtivation_sigmoid:
def forward(self, inputs):
# 1 / 1 + e ** -x
self.output = 1.0 / (1 + (2.718281828459045 * -x))
class DataGenerator:
def spiral_data(self, points, classes):
X = np.zeros((points*classes, 2))
y = np.zeros(points*classes, dtype='uint8')
for class_number in range(classes):
ix = range(points*class_number, points*(class_number+1))
r = np.linspace(0.0, 1, points) # radius
t = np.linspace(class_number*4, (class_number+1)*4, points) + np.random.randn(points)*0.2
X[ix] = np.c_[r*np.sin(t*2.5), r*np.cos(t*2.5)]
y[ix] = class_number
return X, y
# X = [[1,2,3,2.5],[2.0,5.0,-1.0,2.0],[-1.5,2.7,3.3,-0.8]]
gen = DataGenerator()
X, y = gen.spiral_data(100, 3)
layer_one = Layer_Dense(2,5) #(size, node/neuron count)
results = layer_one.activation_fn("relu").forward(X).iterNext(82)
print(results.output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment