@@ -82,10 +82,10 @@ def __init__(
8282 self .b_init = b_init
8383 self .in_channels = in_channels
8484
85- # Attention: To build, we need not only the in_channels!
86- # if self.in_channels:
87- # self.build(None)
88- # self._built = True
85+ # Attention: To build, we need not only the in_channels! Solved.
86+ if self .in_channels is not None :
87+ self .build (None )
88+ self ._built = True
8989
9090 logging .info (
9191 "DeConv2d {}: n_filters: {} strides: {} padding: {} act: {} dilation: {}" .format (
@@ -132,10 +132,13 @@ def build(self, inputs_shape):
132132 # dtype=tf.float32,
133133 name = self .name ,
134134 )
135- if self .data_format == "channels_first" :
136- self .in_channels = inputs_shape [1 ]
135+ if inputs_shape is not None :
136+ self .in_channels = inputs_shape [1 if self .data_format == "channels_first" else - 1 ]
137+ elif self .in_channels is not None :
138+ inputs_shape = [1 , self .in_channels , 1 , 1
139+ ] if self .data_format == "channels_first" else [1 , 1 , 1 , self .in_channels ]
137140 else :
138- self . in_channels = inputs_shape [ - 1 ]
141+ raise ValueError ( "Either inputs_shape or in_channels must be specified for build." )
139142 _out = self .layer (
140143 tf .convert_to_tensor (np .random .uniform (size = inputs_shape ), dtype = np .float32 )
141144 ) #np.random.uniform([1] + list(inputs_shape))) # initialize weights
@@ -206,12 +209,12 @@ def __init__(
206209 self .data_format = data_format
207210 self .W_init = W_init
208211 self .b_init = b_init
209- self .in_channels = in_channels ,
212+ self .in_channels = in_channels
210213
211- # Attention: To build, we need not only the in_channels!
212- # if self.in_channels:
213- # self.build(None)
214- # self._built = True
214+ # Attention: To build, we need not only the in_channels! Solved.
215+ if self .in_channels is not None :
216+ self .build (None )
217+ self ._built = True
215218
216219 logging .info (
217220 "DeConv3d %s: n_filters: %s strides: %s pad: %s act: %s" % (
@@ -252,16 +255,17 @@ def build(self, inputs_shape):
252255 bias_initializer = self .b_init ,
253256 name = self .name ,
254257 )
255- if self .data_format == "channels_first" :
256- self .in_channels = inputs_shape [1 ]
258+ if inputs_shape is not None :
259+ self .in_channels = inputs_shape [1 if self .data_format == "channels_first" else - 1 ]
260+ elif self .in_channels is not None :
261+ inputs_shape = [1 , self .in_channels , 1 , 1 , 1
262+ ] if self .data_format == "channels_first" else [1 , 1 , 1 , 1 , self .in_channels ]
257263 else :
258- self .in_channels = inputs_shape [- 1 ]
259-
264+ raise ValueError ("Either inputs_shape or in_channels must be specified for build." )
260265 _out = self .layer (
261266 tf .convert_to_tensor (np .random .uniform (size = inputs_shape ), dtype = np .float32 )
262267 ) #self.layer(np.random.uniform([1] + list(inputs_shape))) # initialize weights
263268 outputs_shape = _out .shape
264- # self._add_weights(self.layer.weights)
265269 self ._trainable_weights = self .layer .weights
266270
267271 def forward (self , inputs ):
0 commit comments