Computes the number of input and output units for a weight shape. # Arguments shape: Integer shape tuple. data_format: Image data format to use for convolution kernels. Note that all kernels in Keras are standardized on the `channels_last` ordering (even w
(shape, data_format='channels_first')
| 246 | |
| 247 | |
| 248 | def _compute_fans(shape, data_format='channels_first'): |
| 249 | """Computes the number of input and output units for a weight shape. |
| 250 | # Arguments |
| 251 | shape: Integer shape tuple. |
| 252 | data_format: Image data format to use for convolution kernels. |
| 253 | Note that all kernels in Keras are standardized on the |
| 254 | `channels_last` ordering (even when inputs are set |
| 255 | to `channels_first`). |
| 256 | # Returns |
| 257 | A tuple of scalars, `(fan_in, fan_out)`. |
| 258 | # Raises |
| 259 | ValueError: in case of invalid `data_format` argument. |
| 260 | """ |
| 261 | if len(shape) == 2: |
| 262 | fan_in = shape[0] |
| 263 | fan_out = shape[1] |
| 264 | elif len(shape) in {3, 4, 5}: |
| 265 | # Assuming convolution kernels (1D, 2D or 3D). |
| 266 | # TH kernel shape: (depth, input_depth, ...) |
| 267 | # TF kernel shape: (..., input_depth, depth) |
| 268 | if data_format == 'channels_first': |
| 269 | receptive_field_size = np.prod(shape[2:]) |
| 270 | fan_in = shape[1] * receptive_field_size |
| 271 | fan_out = shape[0] * receptive_field_size |
| 272 | elif data_format == 'channels_last': |
| 273 | receptive_field_size = np.prod(shape[:-2]) |
| 274 | fan_in = shape[-2] * receptive_field_size |
| 275 | fan_out = shape[-1] * receptive_field_size |
| 276 | else: |
| 277 | raise ValueError('Invalid data_format: ' + data_format) |
| 278 | else: |
| 279 | # No specific assumptions. |
| 280 | fan_in = np.sqrt(np.prod(shape)) |
| 281 | fan_out = np.sqrt(np.prod(shape)) |
| 282 | return fan_in, fan_out |
| 283 | |
| 284 | |
| 285 | def _random_fill(t, scale, mode, distribution): |