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

Conv2D.py 4.1 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  1. import numpy as np
  2. from core.leras import nn
  3. tf = nn.tf
  4. class Conv2D(nn.LayerBase):
  5. """
  6. default kernel_initializer - CA
  7. use_wscale bool enables equalized learning rate, if kernel_initializer is None, it will be forced to random_normal
  8. """
  9. def __init__(self, in_ch, out_ch, kernel_size, strides=1, padding='SAME', dilations=1, use_bias=True, use_wscale=False, kernel_initializer=None, bias_initializer=None, trainable=True, dtype=None, **kwargs ):
  10. if not isinstance(strides, int):
  11. raise ValueError ("strides must be an int type")
  12. if not isinstance(dilations, int):
  13. raise ValueError ("dilations must be an int type")
  14. kernel_size = int(kernel_size)
  15. if dtype is None:
  16. dtype = nn.floatx
  17. if isinstance(padding, str):
  18. if padding == "SAME":
  19. padding = ( (kernel_size - 1) * dilations + 1 ) // 2
  20. elif padding == "VALID":
  21. padding = None
  22. else:
  23. raise ValueError ("Wrong padding type. Should be VALID SAME or INT or 4x INTs")
  24. else:
  25. padding = int(padding)
  26. self.in_ch = in_ch
  27. self.out_ch = out_ch
  28. self.kernel_size = kernel_size
  29. self.strides = strides
  30. self.padding = padding
  31. self.dilations = dilations
  32. self.use_bias = use_bias
  33. self.use_wscale = use_wscale
  34. self.kernel_initializer = kernel_initializer
  35. self.bias_initializer = bias_initializer
  36. self.trainable = trainable
  37. self.dtype = dtype
  38. super().__init__(**kwargs)
  39. def build_weights(self):
  40. kernel_initializer = self.kernel_initializer
  41. if self.use_wscale:
  42. gain = 1.0 if self.kernel_size == 1 else np.sqrt(2)
  43. fan_in = self.kernel_size*self.kernel_size*self.in_ch
  44. he_std = gain / np.sqrt(fan_in)
  45. self.wscale = tf.constant(he_std, dtype=self.dtype )
  46. if kernel_initializer is None:
  47. kernel_initializer = tf.initializers.random_normal(0, 1.0, dtype=self.dtype)
  48. #if kernel_initializer is None:
  49. # kernel_initializer = nn.initializers.ca()
  50. self.weight = tf.get_variable("weight", (self.kernel_size,self.kernel_size,self.in_ch,self.out_ch), dtype=self.dtype, initializer=kernel_initializer, trainable=self.trainable )
  51. if self.use_bias:
  52. bias_initializer = self.bias_initializer
  53. if bias_initializer is None:
  54. bias_initializer = tf.initializers.zeros(dtype=self.dtype)
  55. self.bias = tf.get_variable("bias", (self.out_ch,), dtype=self.dtype, initializer=bias_initializer, trainable=self.trainable )
  56. def get_weights(self):
  57. weights = [self.weight]
  58. if self.use_bias:
  59. weights += [self.bias]
  60. return weights
  61. def forward(self, x):
  62. weight = self.weight
  63. if self.use_wscale:
  64. weight = weight * self.wscale
  65. padding = self.padding
  66. if padding is not None:
  67. if nn.data_format == "NHWC":
  68. padding = [ [0,0], [padding,padding], [padding,padding], [0,0] ]
  69. else:
  70. padding = [ [0,0], [0,0], [padding,padding], [padding,padding] ]
  71. x = tf.pad (x, padding, mode='CONSTANT')
  72. strides = self.strides
  73. if nn.data_format == "NHWC":
  74. strides = [1,strides,strides,1]
  75. else:
  76. strides = [1,1,strides,strides]
  77. dilations = self.dilations
  78. if nn.data_format == "NHWC":
  79. dilations = [1,dilations,dilations,1]
  80. else:
  81. dilations = [1,1,dilations,dilations]
  82. x = tf.nn.conv2d(x, weight, strides, 'VALID', dilations=dilations, data_format=nn.data_format)
  83. if self.use_bias:
  84. if nn.data_format == "NHWC":
  85. bias = tf.reshape (self.bias, (1,1,1,self.out_ch) )
  86. else:
  87. bias = tf.reshape (self.bias, (1,self.out_ch,1,1) )
  88. x = tf.add(x, bias)
  89. return x
  90. def __str__(self):
  91. r = f"{self.__class__.__name__} : in_ch:{self.in_ch} out_ch:{self.out_ch} "
  92. return r
  93. nn.Conv2D = Conv2D
Tip!

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

Comments

Loading...