And:
from typing import Anyimport numpy as npclass Perception:def __init__(self,input_length,weights=None,bias=None):if weights is None:self.weights =np.ones(input_length)*1else:self.weights=weightsif bias is None:self.bias = -1else:self.bias=bias@staticmethoddef activation_function(x):if x > 0:return 1return 0def __call__(self,input_data):weighted_input=self.weights*input_dataweight_sum=weighted_input.sum()+self.biasreturn Perception.activation_function(weight_sum)weights =np.array([1,1])bias =-1AND_Gate =Perception(2,weights,bias)input_data =[np.array([0,0]) , np.array([0,1]), np.array([1,0]),np.array([1,1]) ]for x in input_data:out =AND_Gate(np.array(x))print(x,out)
OR:
from typing import Anyimport numpy as np
class Perception: def __init__(self,input_length,weights=None,bias=None): if weights is None: self.weights =np.ones(input_length)*1 else: self.weights=weights if bias is None: self.bias = -1 else: self.bias=bias
@staticmethod def activation_function(x): if x > 0: return 1 return 0 def __call__(self,input_data): weighted_input=self.weights*input_data weight_sum=weighted_input.sum()+self.bias return Perception.activation_function(weight_sum) weights =np.array([1,1])bias =-0.5OR_Gate =Perception(2,weights,bias) input_data =[np.array([0,0]) , np.array([0,1]), np.array([1,0]),np.array([1,1]) ]
for x in input_data: out =OR_Gate(np.array(x)) print(x,out)
沒有留言:
張貼留言