@@ -109,3 +109,55 @@ def linear(x, n_units, scope=None, stddev=0.02,
109109 matrix = tf .get_variable ("Matrix" , [shape [1 ], n_units ], tf .float32 ,
110110 tf .random_normal_initializer (stddev = stddev ))
111111 return activation (tf .matmul (x , matrix ))
112+
113+
114+ def conv2d (x , n_filters ,
115+ k_h = 5 , k_w = 5 ,
116+ stride_h = 2 , stride_w = 2 ,
117+ stddev = 0.02 ,
118+ activation = lambda x : x ,
119+ bias = True ,
120+ padding = 'SAME' ,
121+ name = "Conv2D" ):
122+ """2D Convolution with options for kernel size, stride, and init deviation.
123+
124+ Parameters
125+ ----------
126+ x : Tensor
127+ Input tensor to convolve.
128+ n_filters : int
129+ Number of filters to apply.
130+ k_h : int, optional
131+ Kernel height.
132+ k_w : int, optional
133+ Kernel width.
134+ stride_h : int, optional
135+ Stride in rows.
136+ stride_w : int, optional
137+ Stride in cols.
138+ stddev : float, optional
139+ Initialization's standard deviation.
140+ activation : arguments, optional
141+ Function which applies a nonlinearity
142+ padding : str, optional
143+ 'SAME' or 'VALID'
144+ name : str, optional
145+ Variable scope to use.
146+
147+ Returns
148+ -------
149+ x : Tensor
150+ Convolved input.
151+ """
152+ with tf .variable_scope (name ):
153+ w = tf .get_variable (
154+ 'w' , [k_h , k_w , x .get_shape ()[- 1 ], n_filters ],
155+ initializer = tf .truncated_normal_initializer (stddev = stddev ))
156+ conv = tf .nn .conv2d (
157+ x , w , strides = [1 , stride_h , stride_w , 1 ], padding = padding )
158+ if bias :
159+ b = tf .get_variable (
160+ 'b' , [n_filters ],
161+ initializer = tf .truncated_normal_initializer (stddev = stddev ))
162+ conv = conv + b
163+ return conv
0 commit comments