Skip to content

Instantly share code, notes, and snippets.

@ifindev
Created May 1, 2020 01:20
Show Gist options
  • Save ifindev/9ffc4fd1f26528a69c190eb715fb5d75 to your computer and use it in GitHub Desktop.
Save ifindev/9ffc4fd1f26528a69c190eb715fb5d75 to your computer and use it in GitHub Desktop.
"""
Author : Muhammad Arifin
Date : 1 May 2020
Description : Rewrite the implementation on this repo https://github.com/arnandprabaswara/simple_hebb_neuron/blob/master/Hebb_Rule.ipynb
"""
import numpy as np
import pandas as pd
# Inisiasi data
df = pd.DataFrame({ 'x1':[0, 0, 0], 'x2':[0, 0, 0], 'x3':[1, 1, 1],
'x4':[1, 1, 1], 'x5':[0, 0, 0], 'x6':[0, 0, 0],
'x7':[0, 0, 0], 'x8':[1, 1, 1], 'x9':[0, 1, 1],
'x10':[0, 1, 0],'x11':[1, 0, 0],'x12':[0, 0, 0],
'x13':[0, 0, 0],'x14':[1, 1, 1],'x15':[0, 0, 0],
'x16':[0, 0, 0],'x17':[1, 1, 1],'x18':[0, 0, 0],
'x19':[0, 0, 0],'x20':[1, 1, 1],'x21':[1, 1, 1],
'x22':[1, 1, 1],'x23':[1, 1, 1],'x24':[0, 0, 0],
'x25':[0, 0, 0],'x26':[1, 1, 1],'x27':[0, 0, 0],
'x28':[0, 0, 0],'x29':[1, 1, 1],'x30':[0, 0, 0],
'x31':[0, 0, 0],'x32':[1, 1, 1],'x33':[0, 0, 0],
'x34':[0, 0, 0],'x35':[1, 1, 1],'x36':[0, 0, 0],})
# Weights and bias initialization
# Weights values stored in a dictionary for easy retrieval in
# later processing steps.
weights = { 'w1':1, 'w2':1, 'w3':0, 'w4':0, 'w5':1, 'w6':1,
'w7':1, 'w8':0, 'w9':1,'w10':1,'w11':0,'w12':1,
'w13':1,'w14':0,'w15':1,'w16':1,'w17':0,'w18':1,
'w19':1,'w20':0,'w21':0,'w22':0,'w23':0,'w24':1,
'w25':1,'w26':0,'w27':1,'w28':1,'w29':0,'w30':1,
'w31':1,'w32':0,'w33':1,'w34':1,'w35':0,'w36':1,}
# Bias Value
b = -1
# Creating the Neuron and do calculations
yin1 = 0
yin1 += b
# Adding yin1 function with multiplication of feature vector and
# their corresponding weigth
for i in range(1,37):
yin1 += df.iloc[ : , i-1] * weights["w%d" %i]
# # Sigmoid function
# # https://en.wikipedia.org/wiki/Sigmoid_function
y = 1 / (1 + np.e**(-yin1))
# Updating the weights
new_weights = {} # new weights Dictionary object
for i in range(1,37):
# Updating the weight
wbuffer = 0 # temporary variable buffer
listbuffer = [] # temporary list buffer
wbuffer += weights["w%d" %i] + ( df.iloc[ : , i - 1] * y)
listbuffer = [w for w in wbuffer]
new_weights["w%d" %i] = listbuffer
# updating the bias value
new_b = b + y
#Turning weights data and new_weights data into pandas dataframe
weights_for_df = {}
for key,val in weights.items():
weights_for_df[key] = [val]
df_weights = pd.DataFrame(weights_for_df)
df_new_weights = pd.DataFrame.from_dict(new_weights)
# Print the old and updated weight values
print("==================== Initial Weights ====================\n")
print(df_weights,"\n")
print("==================== Updated Weights ====================\n")
print(df_new_weights)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment