@@ -507,3 +507,30 @@ def forward(self, inputs):
507507 output = self .add (output , residual )
508508 output = self .norm (output )
509509 return output
510+
511+ class LayerNorm (layer .Layer ):
512+ def __init__ (self , n_features , eps = 1e-6 ):
513+ super (LayerNorm , self ).__init__ ()
514+ self .n_features = n_features
515+ self .eps = eps
516+
517+ def initialize (self , x ):
518+ shape = (self .n_features ,)
519+ self .Gamma = Tensor (shape = shape , dtype = x .dtype , requires_grad = False , stores_grad = False )
520+ self .Beta = Tensor (shape = shape , dtype = x .dtype , requires_grad = False , stores_grad = False )
521+ self .Gamma .set_value (1.0 )
522+ self .Beta .set_value (0.0 )
523+
524+ def forward (self , x ):
525+ # x: input tensor with shape [batch_size, n_features]
526+ # x_normalized = (x - tensor.from_numpy(self.mean)) / tensor.from_numpy(np.sqrt(self.var + self.eps))
527+ # y = self.gamma * x_normalized + self.beta
528+ mean = np .mean (tensor .to_numpy (x ), axis = - 1 , keepdims = True )
529+ var = np .var (tensor .to_numpy (x ), axis = - 1 , keepdims = True )
530+
531+ sub1 = tensor .from_numpy (mean )
532+ div1 = tensor .from_numpy (np .sqrt (var + self .eps ))
533+ x_normalized = autograd .div (autograd .sub (x , sub1 ), div1 )
534+ y = autograd .mul (self .Gamma , x_normalized )
535+ y = autograd .add (y , self .Beta )
536+ return y
0 commit comments