|
3 | 3 | def linear_activation(z): |
4 | 4 | return z |
5 | 5 |
|
6 | | -def parametric_activation(a,z): |
7 | | - if z<=0: |
8 | | - return a*z |
9 | | - else: |
10 | | - return z |
11 | 6 |
|
12 | 7 | def tanh_activation(z): |
13 | 8 | return np.tanh(z) |
14 | | - |
| 9 | + |
15 | 10 | # 2 layer NN for implementation of OR gate |
16 | | -def orgate(x): |
17 | | - weights = np.array([2,2]) |
| 11 | +def orgate(input1, input2): |
18 | 12 | bias = -1 |
19 | | - weighted_input = np.matmul(weights,x) + bias |
| 13 | + weighted_input = 2*input1 + 2*input2 + bias |
20 | 14 | y = linear_activation(weighted_input) |
21 | | - return y |
22 | | - |
23 | | -x = np.array([0,0]) |
24 | | -print(orgate(x)) |
| 15 | + if y<0: |
| 16 | + return False |
| 17 | + else: |
| 18 | + return True |
25 | 19 |
|
26 | | -# 4 layer NN for computing whether absolute difference is between 1 and 3 |
27 | | -# if between 1 and 3 outputs >0 else output <=0 |
28 | | -def multilayer(x): |
29 | | - w1 = np.array([1,-1]) |
30 | | - b1 = 0 |
31 | | - weighted_input1 = np.matmul(w1,x) + b1 |
32 | | - output1 = parametric_activation(-1,weighted_input1) |
33 | | - w2 = np.array([1]) |
34 | | - b2 = -2 |
35 | | - weighted_input2 = np.matmul(w2,[output1]) + b2 |
36 | | - output2 = parametric_activation(-1,weighted_input2) |
37 | | - w3 = np.array([-1]) |
38 | | - b3 = 1 |
39 | | - weighted_input3 = np.matmul(w3,[output2]) + b3 |
40 | | - y = tanh_activation(weighted_input3) |
41 | | - return y |
| 20 | +def boolToBinary(bool1,bool2): |
| 21 | + binary = [] |
| 22 | + if bool1: |
| 23 | + binary.append(1) |
| 24 | + else: |
| 25 | + binary.append(0) |
| 26 | + if bool2: |
| 27 | + binary.append(1) |
| 28 | + else: |
| 29 | + binary.append(0) |
| 30 | + return binary[0], binary[1] |
42 | 31 |
|
43 | | -x = np.array([4,5.5]) |
44 | | -print(multilayer(x)) |
| 32 | +input1, input2 = boolToBinary(True,True) |
| 33 | +print(orgate(input1,input2)) |
0 commit comments