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

Dense.py 2.7 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  1. import numpy as np
  2. from core.leras import nn
  3. tf = nn.tf
  4. class Dense(nn.LayerBase):
  5. def __init__(self, in_ch, out_ch, use_bias=True, use_wscale=False, maxout_ch=0, kernel_initializer=None, bias_initializer=None, trainable=True, dtype=None, **kwargs ):
  6. """
  7. use_wscale enables weight scale (equalized learning rate)
  8. if kernel_initializer is None, it will be forced to random_normal
  9. maxout_ch https://link.springer.com/article/10.1186/s40537-019-0233-0
  10. typical 2-4 if you want to enable DenseMaxout behaviour
  11. """
  12. self.in_ch = in_ch
  13. self.out_ch = out_ch
  14. self.use_bias = use_bias
  15. self.use_wscale = use_wscale
  16. self.maxout_ch = maxout_ch
  17. self.kernel_initializer = kernel_initializer
  18. self.bias_initializer = bias_initializer
  19. self.trainable = trainable
  20. if dtype is None:
  21. dtype = nn.floatx
  22. self.dtype = dtype
  23. super().__init__(**kwargs)
  24. def build_weights(self):
  25. if self.maxout_ch > 1:
  26. weight_shape = (self.in_ch,self.out_ch*self.maxout_ch)
  27. else:
  28. weight_shape = (self.in_ch,self.out_ch)
  29. kernel_initializer = self.kernel_initializer
  30. if self.use_wscale:
  31. gain = 1.0
  32. fan_in = np.prod( weight_shape[:-1] )
  33. he_std = gain / np.sqrt(fan_in) # He init
  34. self.wscale = tf.constant(he_std, dtype=self.dtype )
  35. if kernel_initializer is None:
  36. kernel_initializer = tf.initializers.random_normal(0, 1.0, dtype=self.dtype)
  37. if kernel_initializer is None:
  38. kernel_initializer = tf.initializers.glorot_uniform(dtype=self.dtype)
  39. self.weight = tf.get_variable("weight", weight_shape, dtype=self.dtype, initializer=kernel_initializer, trainable=self.trainable )
  40. if self.use_bias:
  41. bias_initializer = self.bias_initializer
  42. if bias_initializer is None:
  43. bias_initializer = tf.initializers.zeros(dtype=self.dtype)
  44. self.bias = tf.get_variable("bias", (self.out_ch,), dtype=self.dtype, initializer=bias_initializer, trainable=self.trainable )
  45. def get_weights(self):
  46. weights = [self.weight]
  47. if self.use_bias:
  48. weights += [self.bias]
  49. return weights
  50. def forward(self, x):
  51. weight = self.weight
  52. if self.use_wscale:
  53. weight = weight * self.wscale
  54. x = tf.matmul(x, weight)
  55. if self.maxout_ch > 1:
  56. x = tf.reshape (x, (-1, self.out_ch, self.maxout_ch) )
  57. x = tf.reduce_max(x, axis=-1)
  58. if self.use_bias:
  59. x = tf.add(x, tf.reshape(self.bias, (1,self.out_ch) ) )
  60. return x
  61. nn.Dense = Dense
Tip!

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

Comments

Loading...