本文整理汇总了Python中tensorflow.keras.backend.bias_add方法的典型用法代码示例。如果您正苦于以下问题:Python backend.bias_add方法的具体用法?Python backend.bias_add怎么用?Python backend.bias_add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.backend
的用法示例。
在下文中一共展示了backend.bias_add方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def call(self, inputs):
features = inputs[0]
fltr = inputs[1]
# Enforce sparse representation
if not K.is_sparse(fltr):
fltr = ops.dense_to_sparse(fltr)
# Propagation
indices = fltr.indices
N = tf.shape(features, out_type=indices.dtype)[0]
indices = ops.sparse_add_self_loops(indices, N)
targets, sources = indices[:, -2], indices[:, -1]
messages = tf.gather(features, sources)
aggregated = self.aggregate_op(messages, targets, N)
output = K.concatenate([features, aggregated])
output = ops.dot(output, self.kernel)
if self.use_bias:
output = K.bias_add(output, self.bias)
output = K.l2_normalize(output, axis=-1)
if self.activation is not None:
output = self.activation(output)
return output
示例2: call
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def call(self, inputs):
features = inputs[0]
fltr = inputs[1]
# Convolution
output = K.dot(features, self.kernel_1)
output = ops.filter_dot(fltr, output)
# Skip connection
skip = K.dot(features, self.kernel_2)
output += skip
if self.use_bias:
output = K.bias_add(output, self.bias)
if self.activation is not None:
output = self.activation(output)
return output
示例3: call
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def call(self, inputs, training=None):
outputs = depthwise_conv2d(
inputs,
self.depthwise_kernel,
strides=self.strides,
padding=self.padding,
dilation_rate=self.dilation_rate,
data_format=self.data_format)
if self.bias:
outputs = K.bias_add(
outputs,
self.bias,
data_format=self.data_format)
if self.activation is not None:
return self.activation(outputs)
return outputs
示例4: call
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def call(self, inputs):
features = inputs[0]
fltr = inputs[1]
# Convolution
output = ops.dot(features, self.kernel)
output = ops.filter_dot(fltr, output)
if self.use_bias:
output = K.bias_add(output, self.bias)
if self.activation is not None:
output = self.activation(output)
return output
示例5: call
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def call(self, inputs):
X = inputs[0] # (batch_size, N, F)
A = inputs[1] # (batch_size, N, N)
E = inputs[2] # (n_edges, S) or (batch_size, N, N, S)
mode = ops.autodetect_mode(A, X)
if mode == modes.SINGLE:
return self._call_single(inputs)
# Parameters
N = K.shape(X)[-2]
F = K.int_shape(X)[-1]
F_ = self.channels
# Filter network
kernel_network = E
for l in self.kernel_network_layers:
kernel_network = l(kernel_network)
# Convolution
target_shape = (-1, N, N, F_, F) if mode == modes.BATCH else (N, N, F_, F)
kernel = K.reshape(kernel_network, target_shape)
output = kernel * A[..., None, None]
output = tf.einsum('abicf,aif->abc', output, X)
if self.root:
output += ops.dot(X, self.root_kernel)
if self.use_bias:
output = K.bias_add(output, self.bias)
if self.activation is not None:
output = self.activation(output)
return output
示例6: _call_single
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def _call_single(self, inputs):
X = inputs[0] # (N, F)
A = inputs[1] # (N, N)
E = inputs[2] # (n_edges, S)
assert K.ndim(E) == 2, 'In single mode, E must have shape (n_edges, S).'
# Enforce sparse representation
if not K.is_sparse(A):
A = ops.dense_to_sparse(A)
# Parameters
N = tf.shape(X)[-2]
F = K.int_shape(X)[-1]
F_ = self.channels
# Filter network
kernel_network = E
for l in self.kernel_network_layers:
kernel_network = l(kernel_network) # (n_edges, F * F_)
target_shape = (-1, F, F_)
kernel = tf.reshape(kernel_network, target_shape)
# Propagation
index_i = A.indices[:, -2]
index_j = A.indices[:, -1]
messages = tf.gather(X, index_j)
messages = ops.dot(messages[:, None, :], kernel)[:, 0, :]
aggregated = ops.scatter_sum(messages, index_i, N)
# Update
output = aggregated
if self.root:
output += ops.dot(X, self.root_kernel)
if self.use_bias:
output = K.bias_add(output, self.bias)
if self.activation is not None:
output = self.activation(output)
return output
示例7: gcs
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def gcs(self, inputs, stack, iteration):
"""
Creates a graph convolutional layer with a skip connection.
:param inputs: list of input Tensors, namely
- input node features
- input node features for the skip connection
- normalized adjacency matrix;
:param stack: int, current stack (used to retrieve kernels);
:param iteration: int, current iteration (used to retrieve kernels);
:return: output node features.
"""
X = inputs[0]
X_skip = inputs[1]
fltr = inputs[2]
if self.share_weights and iteration >= 1:
iter = 1
else:
iter = iteration
kernel_1, kernel_2, bias = self.kernels[stack][iter]
# Convolution
output = K.dot(X, kernel_1)
output = ops.filter_dot(fltr, output)
# Skip connection
skip = K.dot(X_skip, kernel_2)
skip = Dropout(self.dropout_rate)(skip)
output += skip
if self.use_bias:
output = K.bias_add(output, bias)
output = self.gcn_activation(output)
return output
示例8: call
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def call(self, inputs):
# Implement Eq.(9)
perturbed_kernel = self.kernel + \
self.sigma_kernel * K.random_uniform(shape=self.kernel_shape)
outputs = K.dot(inputs, perturbed_kernel)
if self.use_bias:
perturbed_bias = self.bias + \
self.sigma_bias * K.random_uniform(shape=self.bias_shape)
outputs = K.bias_add(outputs, perturbed_bias)
if self.activation is not None:
outputs = self.activation(outputs)
return outputs
示例9: call
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def call(self, inputs):
output = self.local_conv3d(inputs,
self.kernel,
self.kernel_size,
self.strides,
(self.output_row, self.output_col, self.output_z),
self.data_format)
if self.use_bias:
output = K.bias_add(output, self.bias,
data_format=self.data_format)
output = self.activation(output)
return output
示例10: call
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def call(self, inputs, **kwargs):
gate = K.dot(inputs, self.gate_kernel)
gate = K.bias_add(gate, self.gate_bias, data_format="channels_last")
gate = self.activation(gate)
new_value = K.dot(inputs, self.dense_kernel)
new_value = K.bias_add(new_value, self.dense_bias, data_format="channels_last")
return gate * new_value + (1.0 - gate) * inputs
示例11: biaffine_layer
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def biaffine_layer(deps: tf.Tensor, heads: tf.Tensor, deps_dim: int,
heads_dim: int, output_dim: int, name: str = "biaffine_layer") -> tf.Tensor:
"""Implements a biaffine layer from [Dozat, Manning, 2016].
Args:
deps: the 3D-tensor of dependency states,
heads: the 3D-tensor of head states,
deps_dim: the dimension of dependency states,
heads_dim: the dimension of head_states,
output_dim: the output dimension
name: the name of a layer
Returns:
`answer` the output 3D-tensor
"""
input_shape = [kb.shape(deps)[i] for i in range(tf.keras.backend.ndim(deps))]
first_input = tf.reshape(deps, [-1, deps_dim]) # first_input.shape = (B*L, D1)
second_input = tf.reshape(heads, [-1, heads_dim]) # second_input.shape = (B*L, D2)
with tf.variable_scope(name):
kernel_shape = (deps_dim, heads_dim * output_dim)
kernel = tf.get_variable('kernel', shape=kernel_shape, initializer=xavier_initializer())
first = tf.matmul(first_input, kernel) # (B*L, D2*H)
first = tf.reshape(first, [-1, heads_dim, output_dim]) # (B*L, D2, H)
answer = kb.batch_dot(first, second_input, axes=[1, 1]) # (B*L, H)
first_bias = tf.get_variable('first_bias', shape=(deps_dim, output_dim),
initializer=xavier_initializer())
answer += tf.matmul(first_input, first_bias)
second_bias = tf.get_variable('second_bias', shape=(heads_dim, output_dim),
initializer=xavier_initializer())
answer += tf.matmul(second_input, second_bias)
label_bias = tf.get_variable('label_bias', shape=(output_dim,),
initializer=xavier_initializer())
answer = kb.bias_add(answer, label_bias)
answer = tf.reshape(answer, input_shape[:-1] + [output_dim]) # (B, L, H)
return answer
示例12: _preprocess_symbolic_input
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import bias_add [as 别名]
def _preprocess_symbolic_input(x, data_format, mode, **kwargs):
"""Preprocesses a tensor encoding a batch of images.
# Arguments
x: Input tensor, 3D or 4D.
data_format: Data format of the image tensor.
mode: One of "caffe", "tf" or "torch".
- caffe: will convert the images from RGB to BGR,
then will zero-center each color channel with
respect to the ImageNet dataset,
without scaling.
- tf: will scale pixels between -1 and 1,
sample-wise.
- torch: will scale pixels between 0 and 1 and then
will normalize each channel with respect to the
ImageNet dataset.
# Returns
Preprocessed tensor.
"""
if mode == "tf":
x /= 127.5
x -= 1.0
return x
if mode == "torch":
x /= 255.0
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
else:
if data_format == "channels_first":
# 'RGB'->'BGR'
if backend.ndim(x) == 3:
x = x[::-1, ...]
else:
x = x[:, ::-1, ...]
else:
# 'RGB'->'BGR'
x = x[..., ::-1]
mean = [103.939, 116.779, 123.68]
std = None
mean_tensor = backend.constant(-np.array(mean))
# Zero-center by mean pixel
if backend.dtype(x) != backend.dtype(mean_tensor):
x = backend.bias_add(
x, backend.cast(mean_tensor, backend.dtype(x)), data_format=data_format
)
else:
x = backend.bias_add(x, mean_tensor, data_format)
if std is not None:
x /= std
return x