MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / bias_add

Function bias_add

tensorflow/python/keras/backend.py:5377–5444  ·  view source on GitHub ↗

Adds a bias vector to a tensor. Arguments: x: Tensor or variable. bias: Bias tensor to add. data_format: string, `"channels_last"` or `"channels_first"`. Returns: Output tensor. Raises: ValueError: In one of the two cases below: 1. invalid `da

(x, bias, data_format=None)

Source from the content-addressed store, hash-verified

5375
5376@keras_export('keras.backend.bias_add')
5377def bias_add(x, bias, data_format=None):
5378 """Adds a bias vector to a tensor.
5379
5380 Arguments:
5381 x: Tensor or variable.
5382 bias: Bias tensor to add.
5383 data_format: string, `"channels_last"` or `"channels_first"`.
5384
5385 Returns:
5386 Output tensor.
5387
5388 Raises:
5389 ValueError: In one of the two cases below:
5390 1. invalid `data_format` argument.
5391 2. invalid bias shape.
5392 the bias should be either a vector or
5393 a tensor with ndim(x) - 1 dimension
5394 """
5395 if data_format is None:
5396 data_format = image_data_format()
5397 if data_format not in {'channels_first', 'channels_last'}:
5398 raise ValueError('Unknown data_format: ' + str(data_format))
5399 bias_shape = int_shape(bias)
5400 if len(bias_shape) != 1 and len(bias_shape) != ndim(x) - 1:
5401 raise ValueError(
5402 'Unexpected bias dimensions %d, expect to be 1 or %d dimensions' %
5403 (len(bias_shape), ndim(x)))
5404 # pylint: disable=g-no-augmented-assignment
5405 if ndim(x) == 5:
5406 if data_format == 'channels_first':
5407 if len(bias_shape) == 1:
5408 x = x + reshape(bias, (1, bias_shape[0], 1, 1, 1))
5409 else:
5410 x = x + reshape(bias, (1, bias_shape[3]) + bias_shape[:3])
5411 elif data_format == 'channels_last':
5412 if len(bias_shape) == 1:
5413 x = x + reshape(bias, (1, 1, 1, bias_shape[0]))
5414 else:
5415 x = x + reshape(bias, (1,) + bias_shape)
5416 elif ndim(x) == 4:
5417 if data_format == 'channels_first':
5418 if len(bias_shape) == 1:
5419 if _has_nchw_support():
5420 x = nn.bias_add(x, bias, data_format='NCHW')
5421 else:
5422 x = x + reshape(bias, (1, bias_shape[0], 1, 1))
5423 else:
5424 x = x + reshape(bias, (1, bias_shape[2]) + bias_shape[:2])
5425 elif data_format == 'channels_last':
5426 if len(bias_shape) == 1:
5427 x = nn.bias_add(x, bias, data_format='NHWC')
5428 else:
5429 x = x + reshape(bias, (1,) + bias_shape)
5430 elif ndim(x) == 3:
5431 if data_format == 'channels_first':
5432 if len(bias_shape) == 1:
5433 x = x + reshape(bias, (1, bias_shape[0], 1))
5434 else:

Callers

nothing calls this directly

Calls 5

image_data_formatFunction · 0.85
int_shapeFunction · 0.85
ndimFunction · 0.85
_has_nchw_supportFunction · 0.85
reshapeFunction · 0.70

Tested by

no test coverage detected