本文整理汇总了Python中tensorflow.keras.backend.backend方法的典型用法代码示例。如果您正苦于以下问题:Python backend.backend方法的具体用法?Python backend.backend怎么用?Python backend.backend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.backend
的用法示例。
在下文中一共展示了backend.backend方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_swish
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def get_swish(**kwargs):
def swish(x):
"""Swish activation function: x * sigmoid(x).
Reference: [Searching for Activation Functions](https://arxiv.org/abs/1710.05941)
"""
if backend.backend() == 'tensorflow':
try:
# The native TF implementation has a more
# memory-efficient gradient implementation
return backend.tf.nn.swish(x)
except AttributeError:
pass
return x * backend.sigmoid(x)
return swish
示例2: get_dropout
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def get_dropout(**kwargs):
"""Wrapper over custom dropout. Fix problem of ``None`` shape for tf.keras.
It is not possible to define FixedDropout class as global object,
because we do not have modules for inheritance at first time.
Issue:
https://github.com/tensorflow/tensorflow/issues/30946
"""
class FixedDropout(layers.Dropout):
def _get_noise_shape(self, inputs):
if self.noise_shape is None:
return self.noise_shape
symbolic_shape = backend.shape(inputs)
noise_shape = [symbolic_shape[axis] if shape is None else shape
for axis, shape in enumerate(self.noise_shape)]
return tuple(noise_shape)
return FixedDropout
示例3: correct_pad
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend 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: _se_block
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def _se_block(inputs, filters, se_ratio, prefix):
x = GlobalAveragePooling2D(name=prefix + 'squeeze_excite/AvgPool')(inputs)
if K.image_data_format() == 'channels_first':
x = Reshape((filters, 1, 1))(x)
else:
x = Reshape((1, 1, filters))(x)
x = Conv2D(_depth(filters * se_ratio),
kernel_size=1,
padding='same',
name=prefix + 'squeeze_excite/Conv')(x)
x = ReLU(name=prefix + 'squeeze_excite/Relu')(x)
x = Conv2D(filters,
kernel_size=1,
padding='same',
name=prefix + 'squeeze_excite/Conv_1')(x)
x = Activation(hard_sigmoid)(x)
#if K.backend() == 'theano':
## For the Theano backend, we have to explicitly make
## the excitation weights broadcastable.
#x = Lambda(
#lambda br: K.pattern_broadcast(br, [True, True, True, False]),
#output_shape=lambda input_shape: input_shape,
#name=prefix + 'squeeze_excite/broadcast')(x)
x = Multiply(name=prefix + 'squeeze_excite/Mul')([inputs, x])
return x
示例5: swish
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def swish(x):
"""Swish activation function.
# Arguments
x: Input tensor.
# Returns
The Swish activation: `x * sigmoid(x)`.
# References
[Searching for Activation Functions](https://arxiv.org/abs/1710.05941)
"""
if K.backend() == 'tensorflow':
try:
# The native TF implementation has a more
# memory-efficient gradient implementation
return K.tf.nn.swish(x)
except AttributeError:
pass
return x * K.sigmoid(x)
示例6: tpu_compatible
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def tpu_compatible():
'''Fit the tpu problems we meet while using keras tpu model'''
if not hasattr(tpu_compatible, 'once'):
tpu_compatible.once = True
else:
return
import tensorflow as tf
import tensorflow.keras.backend as K
_version = tf.__version__.split('.')
is_correct_version = int(_version[0]) >= 1 and (int(_version[0]) >= 2 or int(_version[1]) >= 13)
from tensorflow.contrib.tpu.python.tpu.keras_support import KerasTPUModel
def initialize_uninitialized_variables():
sess = K.get_session()
uninitialized_variables = set([i.decode('ascii') for i in sess.run(tf.report_uninitialized_variables())])
init_op = tf.variables_initializer(
[v for v in tf.global_variables() if v.name.split(':')[0] in uninitialized_variables]
)
sess.run(init_op)
_tpu_compile = KerasTPUModel.compile
def tpu_compile(self,
optimizer,
loss=None,
metrics=None,
loss_weights=None,
sample_weight_mode=None,
weighted_metrics=None,
target_tensors=None,
**kwargs):
if not is_correct_version:
raise ValueError('You need tensorflow >= 1.3 for better keras tpu support!')
_tpu_compile(self, optimizer, loss, metrics, loss_weights,
sample_weight_mode, weighted_metrics,
target_tensors, **kwargs)
initialize_uninitialized_variables() # for unknown reason, we should run this after compile sometimes
KerasTPUModel.compile = tpu_compile
示例7: replace_keras_to_tf_keras
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def replace_keras_to_tf_keras():
tpu_compatible()
import tensorflow as tf
sys.modules['keras'] = tf.keras
globals()['keras'] = tf.keras
import keras.backend as K
K.tf = tf
示例8: refresh_keras_backend
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def refresh_keras_backend(use_tpu=True):
clean_keras_module()
import keras.backend as K
if use_tpu and K.backend() != 'theano':
clean_keras_module()
replace_keras_to_tf_keras()
import keras.backend as K
return K
示例9: clear_session_after_test
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def clear_session_after_test():
"""Test wrapper to clean up after TensorFlow and CNTK tests.
This wrapper runs for all the tests in the keras test suite.
"""
yield
if K.backend() == 'tensorflow' or K.backend() == 'cntk':
K.clear_session()
示例10: test_replace_imports
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def test_replace_imports():
python_code = """
import keras
from keras import backend as K
import os
import keras_contrib
import keras_contrib.layers as lay
import keras.layers
from keras.layers import Dense
if K.backend() == 'tensorflow':
import tensorflow as tf
function = tf.max
"""
expected_code = """
from tensorflow import keras
from tensorflow.keras import backend as K
import os
import keras_contrib
import keras_contrib.layers as lay
import tensorflow.keras.layers
from tensorflow.keras.layers import Dense
if K.backend() == 'tensorflow':
import tensorflow as tf
function = tf.max
"""
code_with_replacement = replace_imports_in_text(python_code, False)
assert expected_code == code_with_replacement
assert python_code == replace_imports_in_text(code_with_replacement, True)
示例11: _keras_wrap
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def _keras_wrap(func, session):
"""Constructs function encapsulated in the graph and the session."""
@wraps(func)
def _wrapped(*args, **kwargs):
with session.graph.as_default():
tf.keras.backend.set_session(session)
return func(*args, **kwargs)
return _wrapped
示例12: __call__
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def __call__(cls, *args, **kwargs):
obj = cls.__new__(cls, *args, **kwargs)
from .keras_model import KerasModel
if issubclass(cls, KerasModel):
from tensorflow.keras import backend as K
if K.backend() != 'tensorflow':
obj.__init__(*args, **kwargs)
return obj
K.clear_session()
obj.graph = tf.Graph()
with obj.graph.as_default():
if hasattr(cls, '_config_session'):
obj.sess = cls._config_session()
else:
obj.sess = tf.Session()
else:
obj.graph = tf.Graph()
for meth in dir(obj):
if meth == '__class__':
continue
attr = getattr(obj, meth)
if callable(attr):
if issubclass(cls, KerasModel):
wrapped_attr = _keras_wrap(attr, obj.sess)
else:
wrapped_attr = _graph_wrap(attr, obj.graph)
setattr(obj, meth, wrapped_attr)
obj.__init__(*args, **kwargs)
return obj
示例13: preprocess_input
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def preprocess_input(x):
"""
"mode" option description in preprocess_input
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.
"""
x = _preprocess_input(x, mode='tf', backend=K)
#x /= 255.
#mean = [0.485, 0.456, 0.406]
#std = [0.229, 0.224, 0.225]
#x[..., 0] -= mean[0]
#x[..., 1] -= mean[1]
#x[..., 2] -= mean[2]
#if std is not None:
#x[..., 0] /= std[0]
#x[..., 1] /= std[1]
#x[..., 2] /= std[2]
return x
示例14: block1
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def block1(x, filters, kernel_size=3, stride=1,
conv_shortcut=True, name=None):
"""A residual block.
# Arguments
x: input tensor.
filters: integer, filters of the bottleneck layer.
kernel_size: default 3, kernel size of the bottleneck layer.
stride: default 1, stride of the first layer.
conv_shortcut: default True, use convolution shortcut if True,
otherwise identity shortcut.
name: string, block label.
# Returns
Output tensor for the residual block.
"""
bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
if conv_shortcut is True:
shortcut = layers.Conv2D(4 * filters, 1, strides=stride,
name=name + '_0_conv')(x)
shortcut = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_0_bn')(shortcut)
else:
shortcut = x
x = layers.Conv2D(filters, 1, strides=stride, name=name + '_1_conv')(x)
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_1_bn')(x)
x = layers.Activation('relu', name=name + '_1_relu')(x)
x = layers.Conv2D(filters, kernel_size, padding='same',
name=name + '_2_conv')(x)
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_2_bn')(x)
x = layers.Activation('relu', name=name + '_2_relu')(x)
x = layers.Conv2D(4 * filters, 1, name=name + '_3_conv')(x)
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_3_bn')(x)
x = layers.Add(name=name + '_add')([shortcut, x])
x = layers.Activation('relu', name=name + '_out')(x)
return x
示例15: block2
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import backend [as 别名]
def block2(x, filters, kernel_size=3, stride=1,
conv_shortcut=False, name=None):
"""A residual block.
# Arguments
x: input tensor.
filters: integer, filters of the bottleneck layer.
kernel_size: default 3, kernel size of the bottleneck layer.
stride: default 1, stride of the first layer.
conv_shortcut: default False, use convolution shortcut if True,
otherwise identity shortcut.
name: string, block label.
# Returns
Output tensor for the residual block.
"""
bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
preact = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_preact_bn')(x)
preact = layers.Activation('relu', name=name + '_preact_relu')(preact)
if conv_shortcut is True:
shortcut = layers.Conv2D(4 * filters, 1, strides=stride,
name=name + '_0_conv')(preact)
else:
shortcut = layers.MaxPooling2D(
1, strides=stride)(x) if stride > 1 else x
x = layers.Conv2D(filters, 1, strides=1, use_bias=False,
name=name + '_1_conv')(preact)
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_1_bn')(x)
x = layers.Activation('relu', name=name + '_1_relu')(x)
x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name=name + '_2_pad')(x)
x = layers.Conv2D(filters, kernel_size, strides=stride,
use_bias=False, name=name + '_2_conv')(x)
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_2_bn')(x)
x = layers.Activation('relu', name=name + '_2_relu')(x)
x = layers.Conv2D(4 * filters, 1, name=name + '_3_conv')(x)
x = layers.Add(name=name + '_out')([shortcut, x])
return x