Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

InstanceNorm2D.py 1.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  1. from core.leras import nn
  2. tf = nn.tf
  3. class InstanceNorm2D(nn.LayerBase):
  4. def __init__(self, in_ch, dtype=None, **kwargs):
  5. self.in_ch = in_ch
  6. if dtype is None:
  7. dtype = nn.floatx
  8. self.dtype = dtype
  9. super().__init__(**kwargs)
  10. def build_weights(self):
  11. kernel_initializer = tf.initializers.glorot_uniform(dtype=self.dtype)
  12. self.weight = tf.get_variable("weight", (self.in_ch,), dtype=self.dtype, initializer=kernel_initializer )
  13. self.bias = tf.get_variable("bias", (self.in_ch,), dtype=self.dtype, initializer=tf.initializers.zeros() )
  14. def get_weights(self):
  15. return [self.weight, self.bias]
  16. def forward(self, x):
  17. if nn.data_format == "NHWC":
  18. shape = (1,1,1,self.in_ch)
  19. else:
  20. shape = (1,self.in_ch,1,1)
  21. weight = tf.reshape ( self.weight , shape )
  22. bias = tf.reshape ( self.bias , shape )
  23. x_mean = tf.reduce_mean(x, axis=nn.conv2d_spatial_axes, keepdims=True )
  24. x_std = tf.math.reduce_std(x, axis=nn.conv2d_spatial_axes, keepdims=True ) + 1e-5
  25. x = (x - x_mean) / x_std
  26. x *= weight
  27. x += bias
  28. return x
  29. nn.InstanceNorm2D = InstanceNorm2D
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...