本文整理汇总了Python中tensorflow.keras.backend方法的典型用法代码示例。如果您正苦于以下问题:Python keras.backend方法的具体用法?Python keras.backend怎么用?Python keras.backend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras
的用法示例。
在下文中一共展示了keras.backend方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_args
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def check_args(parsed_args):
"""
Function to check for inherent contradictions within parsed arguments.
For example, batch_size < num_gpus
Intended to raise errors prior to backend initialisation.
Args
parsed_args: parser.parse_args()
Returns
parsed_args
"""
if parsed_args.gpu and parsed_args.batch_size < len(parsed_args.gpu.split(',')):
raise ValueError(
"Batch size ({}) must be equal to or higher than the number of GPUs ({})".format(parsed_args.batch_size,
len(parsed_args.gpu.split(
','))))
return parsed_args
示例2: transition_block
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def transition_block(x, reduction, name, pool=True):
"""A transition block.
# Arguments
x: input tensor.
reduction: float, compression rate at transition layers.
name: string, block label.
# Returns
output tensor for the block.
"""
bn_axis = 3 if backend.image_data_format() == "channels_last" else 1
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_bn")(x)
x = layers.Activation("relu", name=name + "_relu")(x)
x = layers.Conv2D(
int(backend.int_shape(x)[bn_axis] * reduction),
1,
use_bias=False,
name=name + "_conv",
)(x)
if pool:
x = layers.AveragePooling2D(2, strides=2, name=name + "_pool")(x)
return x
示例3: correct_pad
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def correct_pad(backend, inputs, kernel_size):
"""Returns a tuple for zero-padding for 2D convolution with downsampling.
# Arguments
input_size: An integer or tuple/list of 2 integers.
kernel_size: An integer or tuple/list of 2 integers.
# Returns
A tuple.
"""
img_dim = 2 if backend.image_data_format() == 'channels_first' else 1
input_size = backend.int_shape(inputs)[img_dim:(img_dim + 2)]
if isinstance(kernel_size, int):
kernel_size = (kernel_size, kernel_size)
if input_size[0] is None:
adjust = (1, 1)
else:
adjust = (1 - input_size[0] % 2, 1 - input_size[1] % 2)
correct = (kernel_size[0] // 2, kernel_size[1] // 2)
return ((correct[0] - adjust[0], correct[0]),
(correct[1] - adjust[1], correct[1]))
示例4: convert_log
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def convert_log(node, params, layers, lambda_func, node_name, keras_name):
"""
Convert Log layer
:param node: current operation node
:param params: operation attributes
:param layers: available keras layers
:param lambda_func: function for keras Lambda layer
:param node_name: internal converter name
:param keras_name: resulting layer name
:return: None
"""
if len(node.input) != 1:
assert AttributeError('More than 1 input for log layer.')
input_0 = ensure_tf_type(layers[node.input[0]], name="%s_const" % keras_name)
def target_layer(x):
import tensorflow.keras.backend as K
return K.log(x)
lambda_layer = keras.layers.Lambda(target_layer, name=keras_name)
layers[node_name] = lambda_layer(input_0)
lambda_func[keras_name] = target_layer
示例5: convert_exp
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def convert_exp(node, params, layers, lambda_func, node_name, keras_name):
"""
Convert Exp layer
:param node: current operation node
:param params: operation attributes
:param layers: available keras layers
:param lambda_func: function for keras Lambda layer
:param node_name: resulting layer name
:return: None
"""
if len(node.input) != 1:
assert AttributeError('More than 1 input for log layer.')
input_0 = ensure_tf_type(layers[node.input[0]], name="%s_const" % keras_name)
def target_layer(x):
import tensorflow.keras.backend as K
return K.exp(x)
lambda_layer = keras.layers.Lambda(target_layer, name=keras_name)
layers[node_name] = lambda_layer(input_0)
lambda_func[keras_name] = target_layer
示例6: convert_reduce_mean
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def convert_reduce_mean(node, params, layers, lambda_func, node_name, keras_name):
"""
Convert reduce mean.
:param node: current operation node
:param params: operation attributes
:param layers: available keras layers
:param lambda_func: function for keras Lambda layer
:param node_name: internal converter name
:param keras_name: resulting layer name
:return: None
"""
if len(node.input) != 1:
assert AttributeError('More than 1 input for reduce mean layer.')
input_0 = ensure_tf_type(layers[node.input[0]], name="%s_const" % keras_name)
def target_layer(x, axis=params['axes'], keepdims=params['keepdims']):
import tensorflow.keras.backend as K
return K.mean(x, keepdims=(keepdims == 1), axis=axis)
lambda_layer = keras.layers.Lambda(target_layer, name=keras_name)
layers[node_name] = lambda_layer(input_0)
layers[node_name].set_shape(layers[node_name].shape)
lambda_func[keras_name] = target_layer
示例7: convert_pow
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def convert_pow(node, params, layers, lambda_func, node_name, keras_name):
"""
Convert Pow layer
:param node: current operation node
:param params: operation attributes
:param layers: available keras layers
:param lambda_func: function for keras Lambda layer
:param node_name: internal converter name
:param keras_name: resulting layer name
:return: None
"""
if len(node.input) != 2:
assert AttributeError('More than 2 inputs for pow layer.')
input_0 = ensure_tf_type(layers[node.input[0]], name="%s_const" % keras_name)
power = ensure_numpy_type(layers[node.input[1]])
def target_layer(x, a=power):
import tensorflow.keras.backend as K
return K.pow(x, a)
lambda_layer = keras.layers.Lambda(target_layer, name=keras_name)
layers[node_name] = lambda_layer(input_0)
lambda_func[keras_name] = target_layer
示例8: convert_floor
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def convert_floor(node, params, layers, lambda_func, node_name, keras_name):
"""
Convert Floor layer
:param node: current operation node
:param params: operation attributes
:param layers: available keras layers
:param lambda_func: function for keras Lambda layer
:param node_name: internal converter name
:param keras_name: resulting layer name
:return: None
"""
if len(node.input) != 1:
assert AttributeError('More than 1 input for floor layer.')
input_0 = ensure_tf_type(layers[node.input[0]], name="%s_const" % keras_name)
def target_layer(x):
# Floor is absent in keras.backend
import tensorflow as tf
return tf.floor(x)
lambda_layer = keras.layers.Lambda(target_layer, name=keras_name)
layers[node_name] = lambda_layer(input_0)
lambda_func[keras_name] = target_layer
示例9: get_kwargs
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def get_kwargs():
return {
'backend': tfkeras.backend,
'layers': tfkeras.layers,
'models': tfkeras.models,
'utils': tfkeras.utils,
}
示例10: get_submodules_from_kwargs
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def get_submodules_from_kwargs(kwargs):
backend = kwargs.get('backend', _KERAS_BACKEND)
layers = kwargs.get('layers', _KERAS_LAYERS)
models = kwargs.get('models', _KERAS_MODELS)
utils = kwargs.get('utils', _KERAS_UTILS)
for key in kwargs.keys():
if key not in ['backend', 'layers', 'models', 'utils']:
raise TypeError('Invalid keyword argument: %s', key)
return backend, layers, models, utils
示例11: inject_keras_modules
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def inject_keras_modules(func):
import keras
@functools.wraps(func)
def wrapper(*args, **kwargs):
kwargs['backend'] = keras.backend
kwargs['layers'] = keras.layers
kwargs['models'] = keras.models
kwargs['utils'] = keras.utils
return func(*args, **kwargs)
return wrapper
示例12: inject_tfkeras_modules
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def inject_tfkeras_modules(func):
import tensorflow.keras as tfkeras
@functools.wraps(func)
def wrapper(*args, **kwargs):
kwargs['backend'] = tfkeras.backend
kwargs['layers'] = tfkeras.layers
kwargs['models'] = tfkeras.models
kwargs['utils'] = tfkeras.utils
return func(*args, **kwargs)
return wrapper
示例13: conv_block
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def conv_block(x, growth_rate, name, dilation=1):
"""A building block for a dense block.
# Arguments
x: input tensor.
growth_rate: float, growth rate at dense layers.
name: string, block label.
# Returns
Output tensor for the block.
"""
bn_axis = 3 if backend.image_data_format() == "channels_last" else 1
x1 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_0_bn")(
x
)
x1 = layers.Activation("relu", name=name + "_0_relu")(x1)
x1 = layers.Conv2D(4 * growth_rate, 1, use_bias=False, name=name + "_1_conv")(x1)
x1 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_1_bn")(
x1
)
x1 = layers.Activation("relu", name=name + "_1_relu")(x1)
x1 = layers.Conv2D(
growth_rate,
3,
padding="same",
use_bias=False,
dilation_rate=dilation,
name=name + "_2_conv",
)(x1)
x = layers.Concatenate(axis=bn_axis, name=name + "_concat")([x, x1])
return x
示例14: preprocess_input
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def preprocess_input(x, data_format=None, mode='caffe', **kwargs):
"""Preprocesses a tensor or Numpy array encoding a batch of images.
# Arguments
x: Input Numpy or symbolic tensor, 3D or 4D.
The preprocessed data is written over the input data
if the data types are compatible. To avoid this
behaviour, `numpy.copy(x)` can be used.
data_format: Data format of the image tensor/array.
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 or Numpy array.
# Raises
ValueError: In case of unknown `data_format` argument.
"""
if data_format is None:
data_format = backend.image_data_format()
if data_format not in {'channels_first', 'channels_last'}:
raise ValueError('Unknown data_format ' + str(data_format))
if isinstance(x, np.ndarray):
return _preprocess_numpy_input(x, data_format=data_format,
mode=mode, **kwargs)
else:
return _preprocess_symbolic_input(x, data_format=data_format,
mode=mode, **kwargs)
示例15: inject_global_submodules
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import backend [as 别名]
def inject_global_submodules(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
kwargs['backend'] = _KERAS_BACKEND
kwargs['layers'] = _KERAS_LAYERS
kwargs['models'] = _KERAS_MODELS
kwargs['utils'] = _KERAS_UTILS
return func(*args, **kwargs)
return wrapper