ANN P1

mail@pastecode.io avatar
unknown
python
3 years ago
1.4 kB
245
Indexable
Never
#!/usr/bin/env python
# coding: utf-8

# ### Import Libraries

# In[1]:


try:
    import matplotlib.pyplot as plt
    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
except Exception as e:
    print("Failed to load libraries... ",e)


# ### Initialize Weights (w1,w2)

# In[2]:


w0 = -1 # Weight for bias
w1 = np.arange(-5,5,.25)
w2 = np.arange(-5,5,.25)


# ### Create a perceptron for hanlding OR Function

# In[3]:


output = np.zeros((len(w1),len(w1)))
for i in range(len(w1)):
    for j in range(len(w2)):
        # Evaluate aggregation function
        # Check for x1=0,x2=0 then f(g(x))<0
        if -1 * 1 + 0*w1[i] + 0*w2[j] < 0:
            output[i][j] += 1
        # Check for x1=0,x2=0 then f(g(x))>=0
        if -1 * 1 + 0*w1[i] + 1*w2[j] >= 0:
            output[i][j] += 1
        # Check for x1=0,x2=0 then f(g(x))>=0
        if -1 * 1 + 1*w1[i] + 0*w2[j] >= 0:
            output[i][j] += 1
        # Check for x1=0,x2=0 then f(g(x))>=0
        if -1 * 1 + 1*w1[i] + 1*w2[j] >= 0:
            output[i][j] += 1
        # Print aggeragation function value for corresponding w1,w2
        print(f"agg({w1[i]},{w2[j]}) = {output[i][j]}")


# ### Plot Error Surface

# In[4]:


fig = plt.figure(figsize=(19,8))
ax = Axes3D(fig)
X,Y = np.meshgrid(w1,w2)
ax.plot_surface(X,Y,output,rstride=5,cstride=5,cmap=cm.Blues)
plt.show()