Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Nome: Gabriel Gomes Cruz da Rocha
- Matricula: 201407708
- Atividade_-_Adaline
- '''
- import random
- import numpy as np
- import matplotlib.pyplot as plt
- import math
- def ufunction(X, w, b, i):
- U = 0
- for j in range(0,2):
- U = U + (w[j] * X[i][j])
- U = U + b
- return U
- def activation(U):
- return math.tanh(U)
- def updatew(w, a, e, X, i):
- for j in range(0,2):
- w[j] = w[j] + (a * e[i] * X[i][j])
- def erro( X, w, b, d):
- aux = 0
- Y1 = np.zeros(4)
- e1 = np.zeros(4)
- for i in range(4):
- Y1[i] = activation(ufunction(X, w, b, i))
- e1[i] = d[i] - Y1[i]
- aux = aux + pow(e1[i], 2)
- return aux/4
- def plotar(w1, w2, bias, title):
- xvals = np.arange(-1, 3, 0.01)
- newyvals = (((xvals * w2) * - 1) - bias) / w1
- plt.plot(xvals, newyvals, 'r-')
- plt.title(title)
- plt.xlabel('X1')
- plt.ylabel('X2')
- plt.axis([-1,2,-1,2])
- plt.plot([0,1,0],[0,0,1], 'b^')
- plt.plot([1],[1], 'go')
- plt.xticks([0,1])
- plt.yticks([0,1])
- plt.show()
- def perceptron(max_int, E, a, X, d):
- w = [random.random(), random.random()]
- b = random.random()
- t = 1
- print("Valores Iniciais: W1 = %.4f" %w[0] ,"W2 = %.4f" %w[1] ,"bias = %.4f" %b)
- Y = np.zeros(4)
- e = np.zeros(4)
- deltaMSE = 1
- while(t < max_int and deltaMSE > E):
- MSEinicial = erro( X, w, b, d)
- for i in range(0,4):
- Y[i] = activation(ufunction(X, w, b, i))
- e[i] = d[i] - Y[i]
- updatew(w, a, e, X, i)
- b = b + (a * e[i])
- MSEfinal = erro(X, w, b, d)
- deltaMSE = MSEfinal - MSEinicial
- t = t + 1
- print("Valores Finais: W1 = %.4f" %w[0] ,"W2 = %.4f" %w[1] ,"bias = %.4f" %b)
- return(w, b)
- def main():
- max_int = 100
- E = 0.0000001
- alpha = 0.1
- X = [[0,0],[0,1],[1,0],[1,1]]
- d = [-1, -1, -1, 1]
- w, b = perceptron(max_int, E, alpha, X, d)
- plotar(w[0], w[1], b, "Porta lógica AND com Adaline")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement