Keras 백엔드

"백앤드"는 무엇인가요?

Keras는 딥러닝 모델을 개발하기 위한 고수준의 구성요성 요소를 제공하는 모델 레벨의 라이브러리입니다. Keras는 텐서 곱셈, 합성곱 등의 저수준의 연산을 제공하지 않습니다. 대신 Keras의 "백엔드 엔진" 역할을 하는 특수하고 잘 최적화 된 텐서 라이브러리에 의존합니다. 하나의 단일 텐서 라이브러리를 선택하고 Keras 구현을 해당 라이브러리에 묶는 대신, Keras는 모듈 방식으로 문제를 처리하여 여러 다른 백엔드 엔진들을 Keras에 매끄럽게 연결할 수 있게 합니다.

현재 Keras는 TensorFlow, Theano, 그리고 CNTK의 세 가지 백엔드를 지원합니다.

  • TensorFlow is an open-source symbolic tensor manipulation framework developed by Google.
  • Theano is an open-source symbolic tensor manipulation framework developed by LISA Lab at Université de Montréal.
  • CNTK is an open-source toolkit for deep learning developed by Microsoft.

앞으로 더 많은 백엔드 옵션을 지원할 예정입니다.


한 백엔드에서 다른 백엔드로의 전환

Keras를 한 번이라도 실행한 적이 있다면, 아래의 위치에서 Keras 구성 파일을 찾을 수 있습니다.

$HOME/.keras/keras.json

만약 파일이 없다면, 해당 위치에 구성 파일을 만들 수 있습니다.

Windows(윈도우) 사용자를 위한 노트: $HOME%USERPROFILE%로 바꾸십시오.

기본 구성 파일의 내용은 다음과 같습니다.

{
    "image_data_format": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}

단순히 backend 필드의 값을 "theano", "tensorflow" 또는 "cntk"로 바꿔주는 것 만으로 새로운 백엔드를 사용해 Keras 코드를 실행할 수 있습니다.

또는 아래와 같이 환경 변수 KERAS_BACKEND를 정의해 설정 파일에 정의된 것을 대체할 수도 있습니다.

KERAS_BACKEND=tensorflow python -c "from keras import backend"
Using TensorFlow backend.

Keras에서는 "tensorflow", "theano" 그리고 "cntk"외에도 사용자가 지정한 임의의 백엔드를 로드하는 것이 가능합니다. 만약 my_module이라는 이름의 Python 모듈을 백엔드로 사용하고자 한다면, keras.json 파일의 "backend" 변수 값을 아래와 같이 바꿔주어야 합니다.

{
    "image_data_format": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "my_package.my_module"
}

사용하고자 하는 외부 백엔드는 반드시 검증된 것이어야 하며, placeholder, variable 그리고 function 세 함수들을 지원해야 합니다.

만약, 외부 백엔드가 필수 항목이 누락되어 유효하지 않은 경우라면, 누락된 항목/항목들에 대한 오류가 기록됩니다.


keras.json 상세

keras.json 구성 파일은 아래의 설정들을 포함합니다.

{
    "image_data_format": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}

$HOME/.keras/keras.json 파일을 편집하여 설정을 변경할 수 있습니다.

  • image_data_format: String, either "channels_last" or "channels_first". It specifies which data format convention Keras will follow. (keras.backend.image_data_format() returns it.)
  • For 2D data (e.g. image), "channels_last" assumes (rows, cols, channels) while "channels_first" assumes (channels, rows, cols).
  • For 3D data, "channels_last" assumes (conv_dim1, conv_dim2, conv_dim3, channels) while "channels_first" assumes (channels, conv_dim1, conv_dim2, conv_dim3).
  • epsilon: Float, a numeric fuzzing constant used to avoid dividing by zero in some operations.
  • floatx: String, "float16", "float32", or "float64". Default float precision.
  • backend: String, "tensorflow", "theano", or "cntk".

추상화된 Keras 백엔드를 사용하여 새로운 코드 작성하기

만약 Theano(th)와 Tensorflow(tf) 모두와 호환이 되는 Keras 모듈을 작성하고자 한다면, 아래와 같이 추상화된 Keras 백엔드 API를 사용해야 합니다.

다음과 같이 백엔드 모듈을 사용할 수 있습니다.

from keras import backend as K

아래는 입력 placeholder를 인스턴스화하는 코드입니다. 이는 tf.placeholder(), th.tensor.matrix() 또는 th.tensor.tensor() 등을 실행하는 것과 같습니다.

inputs = K.placeholder(shape=(2, 4, 5))
# also works:
inputs = K.placeholder(shape=(None, 4, 5))
# also works:
inputs = K.placeholder(ndim=3)

아래의 코드는 변수를 인스턴스화합니다. tf.Variable() 또는 th.shared()를 실행하는 것과 같습니다.

import numpy as np
val = np.random.random((3, 4, 5))
var = K.variable(value=val)

# all-zeros variable:
var = K.zeros(shape=(3, 4, 5))
# all-ones:
var = K.ones(shape=(3, 4, 5))

구현에 필요한 대부분의 텐서 연산들은 사용법이 TensorFlow나 Theano와 크게 다르지 않습니다.

# Initializing Tensors with Random Numbers
b = K.random_uniform_variable(shape=(3, 4), low=0, high=1) # Uniform distribution
c = K.random_normal_variable(shape=(3, 4), mean=0, scale=1) # Gaussian distribution
d = K.random_normal_variable(shape=(3, 4), mean=0, scale=1)

# Tensor Arithmetic
a = b + c * K.abs(d)
c = K.dot(a, K.transpose(b))
a = K.sum(b, axis=1)
a = K.softmax(b)
a = K.concatenate([b, c], axis=-1)
# etc...

백엔드 함수들

epsilon

keras.backend.epsilon()

수치 식에 사용되는 fuzz factor의 값을 반환합니다.

Returns

A float.

Example

>>> keras.backend.epsilon()
1e-07

set_epsilon

keras.backend.set_epsilon(e)

수치 식에 사용되는 fuzz factor의 값을 설정합니다.

Arguments

  • e: float. New value of epsilon.

Example

>>> from keras import backend as K
>>> K.epsilon()
1e-07
>>> K.set_epsilon(1e-05)
>>> K.epsilon()
1e-05

floatx

keras.backend.floatx()

Returns the default float type, as a string. (e.g. 'float16', 'float32', 'float64').

Returns

String, the current default float type.

Example

>>> keras.backend.floatx()
'float32'

set_floatx

keras.backend.set_floatx(floatx)

디폴트 float 타입을 설정합니다.

Arguments

  • floatx: String, 'float16', 'float32', or 'float64'.

Example

>>> from keras import backend as K
>>> K.floatx()
'float32'
>>> K.set_floatx('float16')
>>> K.floatx()
'float16'

cast_to_floatx

keras.backend.cast_to_floatx(x)

NumPy 배열을 Keras의 디폴트 float 타입으로 변환합니다.

Arguments

  • x: NumPy 배열.

Returns

변환된 NumPy 배열

Example

>>> from keras import backend as K
>>> K.floatx()
'float32'
>>> arr = numpy.array([1.0, 2.0], dtype='float64')
>>> arr.dtype
dtype('float64')
>>> new_arr = K.cast_to_floatx(arr)
>>> new_arr
array([ 1.,  2.], dtype=float32)
>>> new_arr.dtype
dtype('float32')

image_data_format

keras.backend.image_data_format()

Returns the default image data format convention.

Returns

A string, either 'channels_first' or 'channels_last'

Example

>>> keras.backend.image_data_format()
'channels_first'

set_image_data_format

keras.backend.set_image_data_format(data_format)

Sets the value of the data format convention.

Arguments

  • data_format: string. 'channels_first' 또는 'channels_last'.

Example

>>> from keras import backend as K
>>> K.image_data_format()
'channels_first'
>>> K.set_image_data_format('channels_last')
>>> K.image_data_format()
'channels_last'

get_uid

keras.backend.get_uid(prefix='')

디폴트 그래프의 uid 값을 가져옵니다.

Arguments

  • prefix: An optional prefix of the graph.

Returns

그래프의 고유 식별자(uid)


reset_uids

keras.backend.reset_uids()

그래프의 식별자를 재설정합니다.


clear_session

keras.backend.clear_session()

현재 TF 그래프를 없애고, 새로운 TF 그래프를 만듭니다.

오래된 모델 혹은 층과의 혼란을 피할 때 유용합니다.


manual_variable_initialization

keras.backend.manual_variable_initialization(value)

수동 변수 초기화 플래그를 설정합니다.

이 boolean 플래그는 변수가 인스턴스화 될 때 초기화 되어야 하는지(기본값), 혹은 사용자가 직접 초기화를 처리해야 하는지 여부를 결정합니다. (e.g. via tf.initialize_all_variables()).

Arguments

  • value: Python boolean.

learning_phase

keras.backend.learning_phase()

학습 단계를 나타내는 플래그를 반환합니다.

해당 플래그 변수는 학습과 테스트시에 다른 행동을 취하는 Keras 함수에 입력으로 전달되는 bool형 텐서 (0 = 테스트, 1 = 학습)입니다.

Returns

Learning phase (scalar integer tensor or Python integer).


set_learning_phase

keras.backend.set_learning_phase(value)

학습 단계 변수를 주어진 값으로 고정합니다.

Arguments

  • value: 학습 단계 값, 0 또는 1(정수).

Raises

  • ValueError: value0 또는 1이 아닌 경우.

is_sparse

keras.backend.is_sparse(tensor)

Returns whether a tensor is a sparse tensor.

Arguments

  • tensor: A tensor instance.

Returns

A boolean.

Example

>>> from keras import backend as K
>>> a = K.placeholder((2, 2), sparse=False)
>>> print(K.is_sparse(a))
False
>>> b = K.placeholder((2, 2), sparse=True)
>>> print(K.is_sparse(b))
True

to_dense

keras.backend.to_dense(tensor)

Converts a sparse tensor into a dense tensor and returns it.

Arguments

  • tensor: A tensor instance (potentially sparse).

Returns

A dense tensor.

Examples

>>> from keras import backend as K
>>> b = K.placeholder((2, 2), sparse=True)
>>> print(K.is_sparse(b))
True
>>> c = K.to_dense(b)
>>> print(K.is_sparse(c))
False

variable

keras.backend.variable(value, dtype=None, name=None, constraint=None)

변수를 인스턴스화한 후 반환합니다.

Arguments

  • value: NumPy 배열, 텐서의 초기 값.
  • dtype: 텐서 타입.
  • name: 텐서의 이름(선택사항).
  • constraint: 옵티마이저 업데이트 후 변수에 적용되는 투영 함수입니다(선택사항).

Returns

변수 인스턴스(Keras 메타 데이터 포함).

Examples

>>> from keras import backend as K
>>> val = np.array([[1, 2], [3, 4]])
>>> kvar = K.variable(value=val, dtype='float64', name='example_var')
>>> K.dtype(kvar)
'float64'
>>> print(kvar)
example_var
>>> K.eval(kvar)
array([[ 1.,  2.],
       [ 3.,  4.]])

constant

keras.backend.constant(value, dtype=None, shape=None, name=None)

상수 텐서를 만듭니다.

Arguments

  • value: A constant value (or list)
  • value: 상수 값(또는 리스트)
  • dtype: 결과의 텐서의 요소의 형태.
  • shape: 결과 텐서의 크기(선택사항).
  • name: 텐서의 이름(선택사항).

Returns

상수 텐서


is_keras_tensor

keras.backend.is_keras_tensor(x)

x가 Keras 텐서인지 아닌지를 반환합니다.

"Keras 텐서"란 Keras 층(Layer 클래스) 또는 Input에 의해 반환된 텐서입니다.

Arguments

  • x: 후보 텐서.

Returns

A boolean: 주어진 인자가 Keras 텐서인지의 여부.

Raises

  • ValueError: x가 심볼릭 텐서가 아닌 경우.

Examples

>>> from keras import backend as K
>>> from keras.layers import Input, Dense
>>> np_var = numpy.array([1, 2])
>>> K.is_keras_tensor(np_var) # A numpy array is not a symbolic tensor.
ValueError
>>> k_var = tf.placeholder('float32', shape=(1,1))
>>> # A variable indirectly created outside of keras is not a Keras tensor.
>>> K.is_keras_tensor(k_var)
False
>>> keras_var = K.variable(np_var)
>>> # A variable created with the keras backend is not a Keras tensor.
>>> K.is_keras_tensor(keras_var)
False
>>> keras_placeholder = K.placeholder(shape=(2, 4, 5))
>>> # A placeholder is not a Keras tensor.
>>> K.is_keras_tensor(keras_placeholder)
False
>>> keras_input = Input([10])
>>> K.is_keras_tensor(keras_input) # An Input is a Keras tensor.
True
>>> keras_layer_output = Dense(10)(keras_input)
>>> # Any Keras layer output is a Keras tensor.
>>> K.is_keras_tensor(keras_layer_output)
True

is_tensor

keras.backend.is_tensor(x)

placeholder

keras.backend.placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None)

Instantiates a placeholder tensor and returns it.

Arguments

  • shape: Shape of the placeholder (integer tuple, may include None entries).
  • ndim: Number of axes of the tensor. At least one of {shape, ndim} must be specified. If both are specified, shape is used.
  • dtype: Placeholder type.
  • sparse: Boolean, whether the placeholder should have a sparse type.
  • name: Optional name string for the placeholder.

Returns

Tensor instance (with Keras metadata included).

Examples

>>> from keras import backend as K
>>> input_ph = K.placeholder(shape=(2, 4, 5))
>>> input_ph._keras_shape
(2, 4, 5)
>>> input_ph
<tf.Tensor 'Placeholder_4:0' shape=(2, 4, 5) dtype=float32>

is_placeholder

keras.backend.is_placeholder(x)

Returns whether x is a placeholder.

Arguments

  • x: A candidate placeholder.

Returns

Boolean.


shape

keras.backend.shape(x)

Returns the symbolic shape of a tensor or variable.

Arguments

  • x: A tensor or variable.

Returns

A symbolic shape (which is itself a tensor).

Examples

# TensorFlow example
>>> from keras import backend as K
>>> tf_session = K.get_session()
>>> val = np.array([[1, 2], [3, 4]])
>>> kvar = K.variable(value=val)
>>> inputs = keras.backend.placeholder(shape=(2, 4, 5))
>>> K.shape(kvar)
<tf.Tensor 'Shape_8:0' shape=(2,) dtype=int32>
>>> K.shape(inputs)
<tf.Tensor 'Shape_9:0' shape=(3,) dtype=int32>
# To get integer shape (Instead, you can use K.int_shape(x))
>>> K.shape(kvar).eval(session=tf_session)
array([2, 2], dtype=int32)
>>> K.shape(inputs).eval(session=tf_session)
array([2, 4, 5], dtype=int32)

int_shape

keras.backend.int_shape(x)

Returns the shape of tensor or variable as a tuple of int or None entries.

Arguments

  • x: Tensor or variable.

Returns

A tuple of integers (or None entries).

Examples

>>> from keras import backend as K
>>> inputs = K.placeholder(shape=(2, 4, 5))
>>> K.int_shape(inputs)
(2, 4, 5)
>>> val = np.array([[1, 2], [3, 4]])
>>> kvar = K.variable(value=val)
>>> K.int_shape(kvar)
(2, 2)

Numpy implementation

def int_shape(x):
    return x.shape

ndim

keras.backend.ndim(x)

Returns the number of axes in a tensor, as an integer.

Arguments

  • x: Tensor or variable.

Returns

Integer (scalar), number of axes.

Examples

>>> from keras import backend as K
>>> inputs = K.placeholder(shape=(2, 4, 5))
>>> val = np.array([[1, 2], [3, 4]])
>>> kvar = K.variable(value=val)
>>> K.ndim(inputs)
3
>>> K.ndim(kvar)
2

Numpy implementation

def ndim(x):
    return x.ndim

dtype

keras.backend.dtype(x)

Returns the dtype of a Keras tensor or variable, as a string.

Arguments

  • x: Tensor or variable.

Returns

String, dtype of x.

Examples

>>> from keras import backend as K
>>> K.dtype(K.placeholder(shape=(2,4,5)))
'float32'
>>> K.dtype(K.placeholder(shape=(2,4,5), dtype='float32'))
'float32'
>>> K.dtype(K.placeholder(shape=(2,4,5), dtype='float64'))
'float64'
# Keras variable
>>> kvar = K.variable(np.array([[1, 2], [3, 4]]))
>>> K.dtype(kvar)
'float32_ref'
>>> kvar = K.variable(np.array([[1, 2], [3, 4]]), dtype='float32')
>>> K.dtype(kvar)
'float32_ref'

Numpy implementation

def dtype(x):
    return x.dtype.name

eval

keras.backend.eval(x)

Evaluates the value of a variable.

Arguments

  • x: A variable.

Returns

A Numpy array.

Examples

>>> from keras import backend as K
>>> kvar = K.variable(np.array([[1, 2], [3, 4]]), dtype='float32')
>>> K.eval(kvar)
array([[ 1.,  2.],
       [ 3.,  4.]], dtype=float32)

Numpy implementation

def eval(x):
    return x

zeros

keras.backend.zeros(shape, dtype=None, name=None)

Instantiates an all-zeros variable and returns it.

Arguments

  • shape: Tuple of integers, shape of returned Keras variable
  • dtype: String, data type of returned Keras variable
  • name: String, name of returned Keras variable

Returns

A variable (including Keras metadata), filled with 0.0. Note that if shape was symbolic, we cannot return a variable, and will return a dynamically-shaped tensor instead.

Example

>>> from keras import backend as K
>>> kvar = K.zeros((3,4))
>>> K.eval(kvar)
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]], dtype=float32)

Numpy implementation

def zeros(shape, dtype=floatx(), name=None):
    return np.zeros(shape, dtype=dtype)

ones

keras.backend.ones(shape, dtype=None, name=None)

Instantiates an all-ones variable and returns it.

Arguments

  • shape: Tuple of integers, shape of returned Keras variable.
  • dtype: String, data type of returned Keras variable.
  • name: String, name of returned Keras variable.

Returns

A Keras variable, filled with 1.0. Note that if shape was symbolic, we cannot return a variable, and will return a dynamically-shaped tensor instead.

Example

>>> from keras import backend as K
>>> kvar = K.ones((3,4))
>>> K.eval(kvar)
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]], dtype=float32)

Numpy implementation

def ones(shape, dtype=floatx(), name=None):
    return np.ones(shape, dtype=dtype)

eye

keras.backend.eye(size, dtype=None, name=None)

Instantiate an identity matrix and returns it.

Arguments

  • size: Integer, number of rows/columns.
  • dtype: String, data type of returned Keras variable.
  • name: String, name of returned Keras variable.

Returns

A Keras variable, an identity matrix.

Example

>>> from keras import backend as K
>>> kvar = K.eye(3)
>>> K.eval(kvar)
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]], dtype=float32)

Numpy implementation

def eye(size, dtype=None, name=None):
    return np.eye(size, dtype=dtype)

zeros_like

keras.backend.zeros_like(x, dtype=None, name=None)

Instantiates an all-zeros variable of the same shape as another tensor.

Arguments

  • x: Keras variable or Keras tensor.
  • dtype: String, dtype of returned Keras variable. None uses the dtype of x.
  • name: String, name for the variable to create.

Returns

A Keras variable with the shape of x filled with zeros.

Example

>>> from keras import backend as K
>>> kvar = K.variable(np.random.random((2,3)))
>>> kvar_zeros = K.zeros_like(kvar)
>>> K.eval(kvar_zeros)
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)

Numpy implementation

def zeros_like(x, dtype=floatx(), name=None):
    return np.zeros_like(x, dtype=dtype)

ones_like

keras.backend.ones_like(x, dtype=None, name=None)

Instantiates an all-ones variable of the same shape as another tensor.

Arguments

  • x: Keras variable or tensor.
  • dtype: String, dtype of returned Keras variable. None uses the dtype of x.
  • name: String, name for the variable to create.

Returns

A Keras variable with the shape of x filled with ones.

Example

>>> from keras import backend as K
>>> kvar = K.variable(np.random.random((2,3)))
>>> kvar_ones = K.ones_like(kvar)
>>> K.eval(kvar_ones)
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)

Numpy implementation

def ones_like(x, dtype=floatx(), name=None):
    return np.ones_like(x, dtype=dtype)

identity

keras.backend.identity(x, name=None)

Returns a tensor with the same content as the input tensor.

Arguments

  • x: The input tensor.
  • name: String, name for the variable to create.

Returns

A tensor of the same shape, type and content.


random_uniform_variable

keras.backend.random_uniform_variable(shape, low, high, dtype=None, name=None, seed=None)

Instantiates a variable with values drawn from a uniform distribution.

Arguments

  • shape: Tuple of integers, shape of returned Keras variable.
  • low: Float, lower boundary of the output interval.
  • high: Float, upper boundary of the output interval.
  • dtype: String, dtype of returned Keras variable.
  • name: String, name of returned Keras variable.
  • seed: Integer, random seed.

Returns

A Keras variable, filled with drawn samples.

Example

# TensorFlow example
>>> kvar = K.random_uniform_variable((2,3), 0, 1)
>>> kvar
<tensorflow.python.ops.variables.Variable object at 0x10ab40b10>
>>> K.eval(kvar)
array([[ 0.10940075,  0.10047495,  0.476143  ],
       [ 0.66137183,  0.00869417,  0.89220798]], dtype=float32)

Numpy implementation

def random_uniform_variable(shape, low, high, dtype=None, name=None, seed=None):
    return (high - low) * np.random.random(shape).astype(dtype) + low

random_normal_variable

keras.backend.random_normal_variable(shape, mean, scale, dtype=None, name=None, seed=None)

Instantiates a variable with values drawn from a normal distribution.

Arguments

  • shape: Tuple of integers, shape of returned Keras variable.
  • mean: Float, mean of the normal distribution.
  • scale: Float, standard deviation of the normal distribution.
  • dtype: String, dtype of returned Keras variable.
  • name: String, name of returned Keras variable.
  • seed: Integer, random seed.

Returns

A Keras variable, filled with drawn samples.

Example

# TensorFlow example
>>> kvar = K.random_normal_variable((2,3), 0, 1)
>>> kvar
<tensorflow.python.ops.variables.Variable object at 0x10ab12dd0>
>>> K.eval(kvar)
array([[ 1.19591331,  0.68685907, -0.63814116],
       [ 0.92629528,  0.28055015,  1.70484698]], dtype=float32)

Numpy implementation

def random_normal_variable(shape, mean, scale, dtype=None, name=None, seed=None):
    return scale * np.random.randn(*shape).astype(dtype) + mean

count_params

keras.backend.count_params(x)

Returns the static number of elements in a Keras variable or tensor.

Arguments

  • x: Keras variable or tensor.

Returns

Integer, the number of elements in x, i.e., the product of the array's static dimensions.

Example

>>> kvar = K.zeros((2,3))
>>> K.count_params(kvar)
6
>>> K.eval(kvar)
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)

Numpy implementation

def count_params(x):
    return x.size

cast

keras.backend.cast(x, dtype)

Casts a tensor to a different dtype and returns it.

You can cast a Keras variable but it still returns a Keras tensor.

Arguments

  • x: Keras tensor (or variable).
  • dtype: String, either ('float16', 'float32', or 'float64').

Returns

Keras tensor with dtype dtype.

Example

>>> from keras import backend as K
>>> input = K.placeholder((2, 3), dtype='float32')
>>> input
<tf.Tensor 'Placeholder_2:0' shape=(2, 3) dtype=float32>
# It doesn't work in-place as below.
>>> K.cast(input, dtype='float16')
<tf.Tensor 'Cast_1:0' shape=(2, 3) dtype=float16>
>>> input
<tf.Tensor 'Placeholder_2:0' shape=(2, 3) dtype=float32>
# you need to assign it.
>>> input = K.cast(input, dtype='float16')
>>> input
<tf.Tensor 'Cast_2:0' shape=(2, 3) dtype=float16>

update

keras.backend.update(x, new_x)

Update the value of x to new_x.

Arguments

  • x: A Variable.
  • new_x: A tensor of same shape as x.

Returns

The variable x updated.


update_add

keras.backend.update_add(x, increment)

Update the value of x by adding increment.

Arguments

  • x: A Variable.
  • increment: A tensor of same shape as x.

Returns

The variable x updated.


update_sub

keras.backend.update_sub(x, decrement)

Update the value of x by subtracting decrement.

Arguments

  • x: A Variable.
  • decrement: A tensor of same shape as x.

Returns

The variable x updated.


moving_average_update

keras.backend.moving_average_update(x, value, momentum)

Compute the moving average of a variable.

Arguments

  • x: A Variable.
  • value: A tensor with the same shape as x.
  • momentum: The moving average momentum.

Returns

An operation to update the variable.


dot

keras.backend.dot(x, y)

Multiplies 2 tensors (and/or variables) and returns a tensor.

When attempting to multiply a nD tensor with a nD tensor, it reproduces the Theano behavior. (e.g. (2, 3) * (4, 3, 5) -> (2, 4, 5))

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A tensor, dot product of x and y.

Examples

# dot product between tensors
>>> x = K.placeholder(shape=(2, 3))
>>> y = K.placeholder(shape=(3, 4))
>>> xy = K.dot(x, y)
>>> xy
<tf.Tensor 'MatMul_9:0' shape=(2, 4) dtype=float32>
# dot product between tensors
>>> x = K.placeholder(shape=(32, 28, 3))
>>> y = K.placeholder(shape=(3, 4))
>>> xy = K.dot(x, y)
>>> xy
<tf.Tensor 'MatMul_9:0' shape=(32, 28, 4) dtype=float32>
# Theano-like behavior example
>>> x = K.random_uniform_variable(shape=(2, 3), low=0, high=1)
>>> y = K.ones((4, 3, 5))
>>> xy = K.dot(x, y)
>>> K.int_shape(xy)
(2, 4, 5)

Numpy implementation

def dot(x, y):
    return np.dot(x, y)

batch_dot

keras.backend.batch_dot(x, y, axes=None)

Batchwise dot product.

batch_dot is used to compute dot product of x and y when x and y are data in batches, i.e. in a shape of (batch_size, :). batch_dot results in a tensor or variable with less dimensions than the input. If the number of dimensions is reduced to 1, we use expand_dims to make sure that ndim is at least 2.

Arguments

  • x: Keras tensor or variable with ndim >= 2.
  • y: Keras tensor or variable with ndim >= 2.
  • axes: int or tuple(int, int). Target dimensions to be reduced.

Returns

A tensor with shape equal to the concatenation of x's shape (less the dimension that was summed over) and y's shape (less the batch dimension and the dimension that was summed over). If the final rank is 1, we reshape it to (batch_size, 1).

Examples

Assume x = [[1, 2], [3, 4]] and y = [[5, 6], [7, 8]] batch_dot(x, y, axes=1) = [[17], [53]] which is the main diagonal of x.dot(y.T), although we never have to calculate the off-diagonal elements.

Pseudocode:

inner_products = []
for xi, yi in zip(x, y):
    inner_products.append(xi.dot(yi))
result = stack(inner_products)

Shape inference: Let x's shape be (100, 20) and y's shape be (100, 30, 20). If axes is (1, 2), to find the output shape of resultant tensor, loop through each dimension in x's shape and y's shape:

  • x.shape[0] : 100 : append to output shape
  • x.shape[1] : 20 : do not append to output shape, dimension 1 of x has been summed over. (dot_axes[0] = 1)
  • y.shape[0] : 100 : do not append to output shape, always ignore first dimension of y
  • y.shape[1] : 30 : append to output shape
  • y.shape[2] : 20 : do not append to output shape, dimension 2 of y has been summed over. (dot_axes[1] = 2) output_shape = (100, 30)
>>> x_batch = K.ones(shape=(32, 20, 1))
>>> y_batch = K.ones(shape=(32, 30, 20))
>>> xy_batch_dot = K.batch_dot(x_batch, y_batch, axes=(1, 2))
>>> K.int_shape(xy_batch_dot)
(32, 1, 30)

Numpy implementation

Show the Numpy implementation wzxhzdk:94

transpose

keras.backend.transpose(x)

Transposes a tensor and returns it.

Arguments

  • x: Tensor or variable.

Returns

A tensor.

Examples

>>> var = K.variable([[1, 2, 3], [4, 5, 6]])
>>> K.eval(var)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]], dtype=float32)
>>> var_transposed = K.transpose(var)
>>> K.eval(var_transposed)
array([[ 1.,  4.],
       [ 2.,  5.],
       [ 3.,  6.]], dtype=float32)
>>> inputs = K.placeholder((2, 3))
>>> inputs
<tf.Tensor 'Placeholder_11:0' shape=(2, 3) dtype=float32>
>>> input_transposed = K.transpose(inputs)
>>> input_transposed
<tf.Tensor 'transpose_4:0' shape=(3, 2) dtype=float32>

Numpy implementation

def transpose(x):
    return np.transpose(x)

gather

keras.backend.gather(reference, indices)

Retrieves the elements of indices indices in the tensor reference.

Arguments

  • reference: A tensor.
  • indices: An integer tensor of indices.

Returns

A tensor of same type as reference.

Numpy implementation

def gather(reference, indices):
    return reference[indices]

max

keras.backend.max(x, axis=None, keepdims=False)

Maximum value in a tensor.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to find maximum values. If None (default), finds the maximum over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with maximum values of x.

Numpy implementation

def max(x, axis=None, keepdims=False):
    if isinstance(axis, list):
        axis = tuple(axis)
    return np.max(x, axis=axis, keepdims=keepdims)

min

keras.backend.min(x, axis=None, keepdims=False)

Minimum value in a tensor.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to find minimum values. If None (default), finds the minimum over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with miminum values of x.

Numpy implementation

def min(x, axis=None, keepdims=False):
    if isinstance(axis, list):
        axis = tuple(axis)
    return np.min(x, axis=axis, keepdims=keepdims)

sum

keras.backend.sum(x, axis=None, keepdims=False)

Sum of the values in a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to sum over. If None (default), sums over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with sum of x.

Numpy implementation

def sum(x, axis=None, keepdims=False):
    if isinstance(axis, list):
        axis = tuple(axis)
    return np.sum(x, axis=axis, keepdims=keepdims)

prod

keras.backend.prod(x, axis=None, keepdims=False)

Multiplies the values in a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the product. If None (default), computes the product over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with the product of elements of x.

Numpy implementation

def prod(x, axis=None, keepdims=False):
    if isinstance(axis, list):
        axis = tuple(axis)
    return np.prod(x, axis=axis, keepdims=keepdims)

cumsum

keras.backend.cumsum(x, axis=0)

Cumulative sum of the values in a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer, the axis to compute the sum.

Returns

A tensor of the cumulative sum of values of x along axis. Numpy implementation

def cumsum(x, axis=0):
    return np.cumsum(x, axis=axis)

cumprod

keras.backend.cumprod(x, axis=0)

Cumulative product of the values in a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer, the axis to compute the product.

Returns

A tensor of the cumulative product of values of x along axis. Numpy implementation

def cumprod(x, axis=0):
    return np.cumprod(x, axis=axis)

var

keras.backend.var(x, axis=None, keepdims=False)

Variance of a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the variance. If None (default), computes the variance over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with the variance of elements of x. Numpy implementation

def var(x, axis=None, keepdims=False):
    if isinstance(axis, list):
        axis = tuple(axis)
    return np.var(x, axis=axis, keepdims=keepdims)

std

keras.backend.std(x, axis=None, keepdims=False)

Standard deviation of a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the standard deviation. If None (default), computes the standard deviation over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

A tensor with the standard deviation of elements of x. Numpy implementation

def std(x, axis=None, keepdims=False):
    if isinstance(axis, list):
        axis = tuple(axis)
    return np.std(x, axis=axis, keepdims=keepdims)

mean

keras.backend.mean(x, axis=None, keepdims=False)

Mean of a tensor, alongside the specified axis.

Arguments

  • x: A tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the mean. If None (default), computes the mean over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1 for each entry in axis. If keepdims is True, the reduced dimensions are retained with length 1.

Returns

A tensor with the mean of elements of x. Numpy implementation

def mean(x, axis=None, keepdims=False):
    if isinstance(axis, list):
        axis = tuple(axis)
    return np.mean(x, axis=axis, keepdims=keepdims)

any

keras.backend.any(x, axis=None, keepdims=False)

Bitwise reduction (logical OR).

Arguments

  • x: Tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the logical or. If None (default), computes the logical or over all dimensions.
  • keepdims: whether the drop or broadcast the reduction axes.

Returns

A uint8 tensor (0s and 1s). Numpy implementation

def any(x, axis=None, keepdims=False):
    if isinstance(axis, list):
        axis = tuple(axis)
    return np.any(x, axis=axis, keepdims=keepdims)

all

keras.backend.all(x, axis=None, keepdims=False)

Bitwise reduction (logical AND).

Arguments

  • x: Tensor or variable.
  • axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the logical and. If None (default), computes the logical and over all dimensions.
  • keepdims: whether the drop or broadcast the reduction axes.

Returns

A uint8 tensor (0s and 1s). Numpy implementation

def all(x, axis=None, keepdims=False):
    if isinstance(axis, list):
        axis = tuple(axis)
    return np.all(x, axis=axis, keepdims=keepdims)

argmax

keras.backend.argmax(x, axis=-1)

Returns the index of the maximum value along an axis.

Arguments

  • x: Tensor or variable.
  • axis: axis along which to perform the reduction.

Returns

A tensor. Numpy implementation

def argmax(x, axis=-1):
    return np.argmax(x, axis=axis)

argmin

keras.backend.argmin(x, axis=-1)

Returns the index of the minimum value along an axis.

Arguments

  • x: Tensor or variable.
  • axis: axis along which to perform the reduction.

Returns

A tensor. Numpy implementation

def argmin(x, axis=-1):
    return np.argmin(x, axis=axis)

square

keras.backend.square(x)

Element-wise square.

Arguments

  • x: Tensor or variable.

Returns

A tensor.


abs

keras.backend.abs(x)

Element-wise absolute value.

Arguments

  • x: Tensor or variable.

Returns

A tensor.


sqrt

keras.backend.sqrt(x)

Element-wise square root.

Arguments

  • x: Tensor or variable.

Returns

A tensor. Numpy implementation

def sqrt(x):
    y = np.sqrt(x)
    y[np.isnan(y)] = 0.
    return y

exp

keras.backend.exp(x)

Element-wise exponential.

Arguments

  • x: Tensor or variable.

Returns

A tensor.


log

keras.backend.log(x)

Element-wise log.

Arguments

  • x: Tensor or variable.

Returns

A tensor.


logsumexp

keras.backend.logsumexp(x, axis=None, keepdims=False)

Computes log(sum(exp(elements across dimensions of a tensor))).

This function is more numerically stable than log(sum(exp(x))). It avoids overflows caused by taking the exp of large inputs and underflows caused by taking the log of small inputs.

Arguments

  • x: A tensor or variable.
  • axis: axis: An integer or list of integers in [-rank(x), rank(x)), the axes to compute the logsumexp. If None (default), computes the logsumexp over all dimensions.
  • keepdims: A boolean, whether to keep the dimensions or not. If keepdims is False, the rank of the tensor is reduced by 1. If keepdims is True, the reduced dimension is retained with length 1.

Returns

The reduced tensor. Numpy implementation

def logsumexp(x, axis=None, keepdims=False):
    if isinstance(axis, list):
        axis = tuple(axis)
    return sp.misc.logsumexp(x, axis=axis, keepdims=keepdims)

round

keras.backend.round(x)

Element-wise rounding to the closest integer.

In case of tie, the rounding mode used is "half to even".

Arguments

  • x: Tensor or variable.

Returns

A tensor.


sign

keras.backend.sign(x)

Element-wise sign.

Arguments

  • x: Tensor or variable.

Returns

A tensor.


pow

keras.backend.pow(x, a)

Element-wise exponentiation.

Arguments

  • x: Tensor or variable.
  • a: Python integer.

Returns

A tensor. Numpy implementation

def pow(x, a=1.):
    return np.power(x, a)

clip

keras.backend.clip(x, min_value, max_value)

Element-wise value clipping.

Arguments

  • x: Tensor or variable.
  • min_value: Python float, integer or tensor.
  • max_value: Python float, integer or tensor.

Returns

A tensor. Numpy implementation

def clip(x, min_value, max_value):
    return np.clip(x, min_value, max_value)

equal

keras.backend.equal(x, y)

Element-wise equality between two tensors.

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

def equal(x, y):
    return x == y

not_equal

keras.backend.not_equal(x, y)

Element-wise inequality between two tensors.

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

def not_equal(x, y):
    return x != y

greater

keras.backend.greater(x, y)

Element-wise truth value of (x > y).

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

def greater(x, y):
    return x > y

greater_equal

keras.backend.greater_equal(x, y)

Element-wise truth value of (x >= y).

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

def greater_equal(x, y):
    return x >= y

less

keras.backend.less(x, y)

Element-wise truth value of (x < y).

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

def less(x, y):
    return x < y

less_equal

keras.backend.less_equal(x, y)

Element-wise truth value of (x <= y).

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A bool tensor.

Numpy implementation

def less_equal(x, y):
    return x <= y

maximum

keras.backend.maximum(x, y)

Element-wise maximum of two tensors.

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A tensor.

Numpy implementation

def maximum(x, y):
    return np.maximum(x, y)

minimum

keras.backend.minimum(x, y)

Element-wise minimum of two tensors.

Arguments

  • x: Tensor or variable.
  • y: Tensor or variable.

Returns

A tensor.

Numpy implementation

def minimum(x, y):
    return np.minimum(x, y)

sin

keras.backend.sin(x)

Computes sin of x element-wise.

Arguments

  • x: Tensor or variable.

Returns

A tensor.


cos

keras.backend.cos(x)

Computes cos of x element-wise.

Arguments

  • x: Tensor or variable.

Returns

A tensor.


normalize_batch_in_training

keras.backend.normalize_batch_in_training(x, gamma, beta, reduction_axes, epsilon=0.001)

Computes mean and std for batch then apply batch_normalization on batch.

Arguments

  • x: Input tensor or variable.
  • gamma: Tensor by which to scale the input.
  • beta: Tensor with which to center the input.
  • reduction_axes: iterable of integers, axes over which to normalize.
  • epsilon: Fuzz factor.

Returns

A tuple length of 3, (normalized_tensor, mean, variance).


batch_normalization

keras.backend.batch_normalization(x, mean, var, beta, gamma, axis=-1, epsilon=0.001)

Applies batch normalization on x given mean, var, beta and gamma.

I.e. returns: output = (x - mean) / sqrt(var + epsilon) * gamma + beta

Arguments

  • x: Input tensor or variable.
  • mean: Mean of batch.
  • var: Variance of batch.
  • beta: Tensor with which to center the input.
  • gamma: Tensor by which to scale the input.
  • axis: Integer, the axis that should be normalized. (typically the features axis).
  • epsilon: Fuzz factor.

Returns

A tensor.


concatenate

keras.backend.concatenate(tensors, axis=-1)

Concatenates a list of tensors alongside the specified axis.

Arguments

  • tensors: list of tensors to concatenate.
  • axis: concatenation axis.

Returns

A tensor.


reshape

keras.backend.reshape(x, shape)

Reshapes a tensor to the specified shape.

Arguments

  • x: Tensor or variable.
  • shape: Target shape tuple.

Returns

A tensor.


permute_dimensions

keras.backend.permute_dimensions(x, pattern)

Permutes axes in a tensor.

Arguments

  • x: Tensor or variable.
  • pattern: A tuple of dimension indices, e.g. (0, 2, 1).

Returns

A tensor.


resize_images

keras.backend.resize_images(x, height_factor, width_factor, data_format, interpolation='nearest')

Resizes the images contained in a 4D tensor.

Arguments

  • x: Tensor or variable to resize.
  • height_factor: Positive integer.
  • width_factor: Positive integer.
  • data_format: string, "channels_last" or "channels_first".
  • interpolation: A string, one of nearest or bilinear.

Returns

A tensor.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".


resize_volumes

keras.backend.resize_volumes(x, depth_factor, height_factor, width_factor, data_format)

Resizes the volume contained in a 5D tensor.

Arguments

  • x: Tensor or variable to resize.
  • depth_factor: Positive integer.
  • height_factor: Positive integer.
  • width_factor: Positive integer.
  • data_format: string, "channels_last" or "channels_first".

Returns

A tensor.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".


repeat_elements

keras.backend.repeat_elements(x, rep, axis)

Repeats the elements of a tensor along an axis, like np.repeat.

If x has shape (s1, s2, s3) and axis is 1, the output will have shape (s1, s2 * rep, s3).

Arguments

  • x: Tensor or variable.
  • rep: Python integer, number of times to repeat.
  • axis: Axis along which to repeat.

Returns

A tensor.


repeat

keras.backend.repeat(x, n)

Repeats a 2D tensor.

if x has shape (samples, dim) and n is 2, the output will have shape (samples, 2, dim).

Arguments

  • x: Tensor or variable.
  • n: Python integer, number of times to repeat.

Returns

A tensor.


arange

keras.backend.arange(start, stop=None, step=1, dtype='int32')

Creates a 1D tensor containing a sequence of integers.

The function arguments use the same convention as Theano's arange: if only one argument is provided, it is in fact the "stop" argument and "start" is 0.

The default type of the returned tensor is 'int32' to match TensorFlow's default.

Arguments

  • start: Start value.
  • stop: Stop value.
  • step: Difference between two successive values.
  • dtype: Integer dtype to use.

Returns

An integer tensor.


tile

keras.backend.tile(x, n)

Creates a tensor by tiling x by n.

Arguments

  • x: A tensor or variable
  • n: A list of integer. The length must be the same as the number of dimensions in x.

Returns

A tiled tensor.


flatten

keras.backend.flatten(x)

Flatten a tensor.

Arguments

  • x: A tensor or variable.

Returns

A tensor, reshaped into 1-D


batch_flatten

keras.backend.batch_flatten(x)

Turn a nD tensor into a 2D tensor with same 0th dimension.

In other words, it flattens each data samples of a batch.

Arguments

  • x: A tensor or variable.

Returns

A tensor.


expand_dims

keras.backend.expand_dims(x, axis=-1)

Adds a 1-sized dimension at index "axis".

Arguments

  • x: A tensor or variable.
  • axis: Position where to add a new axis.

Returns

A tensor with expanded dimensions.


squeeze

keras.backend.squeeze(x, axis)

Removes a 1-dimension from the tensor at index "axis".

Arguments

  • x: A tensor or variable.
  • axis: Axis to drop.

Returns

A tensor with the same data as x but reduced dimensions.


temporal_padding

keras.backend.temporal_padding(x, padding=(1, 1))

Pads the middle dimension of a 3D tensor.

Arguments

  • x: Tensor or variable.
  • padding: Tuple of 2 integers, how many zeros to add at the start and end of dim 1.

Returns

A padded 3D tensor.


spatial_2d_padding

keras.backend.spatial_2d_padding(x, padding=((1, 1), (1, 1)), data_format=None)

Pads the 2nd and 3rd dimensions of a 4D tensor.

Arguments

  • x: Tensor or variable.
  • padding: Tuple of 2 tuples, padding pattern.
  • data_format: string, "channels_last" or "channels_first".

Returns

A padded 4D tensor.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".


spatial_3d_padding

keras.backend.spatial_3d_padding(x, padding=((1, 1), (1, 1), (1, 1)), data_format=None)

Pads 5D tensor with zeros along the depth, height, width dimensions.

Pads these dimensions with respectively "padding[0]", "padding[1]" and "padding[2]" zeros left and right.

For 'channels_last' data_format, the 2nd, 3rd and 4th dimension will be padded. For 'channels_first' data_format, the 3rd, 4th and 5th dimension will be padded.

Arguments

  • x: Tensor or variable.
  • padding: Tuple of 3 tuples, padding pattern.
  • data_format: string, "channels_last" or "channels_first".

Returns

A padded 5D tensor.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".


stack

keras.backend.stack(x, axis=0)

Stacks a list of rank R tensors into a rank R+1 tensor.

Arguments

  • x: List of tensors.
  • axis: Axis along which to perform stacking.

Returns

A tensor.

Numpy implementation

def stack(x, axis=0):
    return np.stack(x, axis=axis)

one_hot

keras.backend.one_hot(indices, num_classes)

Computes the one-hot representation of an integer tensor.

Arguments

  • indices: nD integer tensor of shape (batch_size, dim1, dim2, ... dim(n-1))
  • num_classes: Integer, number of classes to consider.

Returns

(n + 1)D one hot representation of the input with shape (batch_size, dim1, dim2, ... dim(n-1), num_classes)


reverse

keras.backend.reverse(x, axes)

Reverses a tensor along the specified axes.

Arguments

  • x: Tensor to reverse.
  • axes: Integer or iterable of integers. Axes to reverse.

Returns

A tensor.

Numpy implementation

def reverse(x, axes):
    if isinstance(axes, list):
        axes = tuple(axes)
    return np.flip(x, axes)

slice

keras.backend.slice(x, start, size)

Extracts a slice from a tensor.

Arguments

  • x: Input tensor.
  • start: Integer list/tuple or tensor indicating the start indices of the slice along each axis.
  • size: Integer list/tuple or tensor indicating how many dimensions to slice along each axis.

Returns

A sliced tensor:

new_x = x[start[0]: start[0] + size[0], ..., start[-1]: start[-1] + size[-1]]

Numpy implementation

def slice(x, start, size):
    slices = [py_slice(i, i + j) for i, j in zip(start, size)]
    return x[tuple(slices)]

get_value

keras.backend.get_value(x)

Returns the value of a variable.

Arguments

  • x: input variable.

Returns

A Numpy array.


batch_get_value

keras.backend.batch_get_value(ops)

Returns the value of more than one tensor variable.

Arguments

  • ops: list of ops to run.

Returns

A list of Numpy arrays.


set_value

keras.backend.set_value(x, value)

Sets the value of a variable, from a Numpy array.

Arguments

  • x: Tensor to set to a new value.
  • value: Value to set the tensor to, as a Numpy array (of the same shape).

batch_set_value

keras.backend.batch_set_value(tuples)

Sets the values of many tensor variables at once.

Arguments

  • tuples: a list of tuples (tensor, value). value should be a Numpy array.

keras.backend.print_tensor(x, message='')

Prints message and the tensor value when evaluated.

Note that print_tensor returns a new tensor identical to x which should be used in the following code. Otherwise the print operation is not taken into account during evaluation.

Example

>>> x = K.print_tensor(x, message="x is: ")

Arguments

  • x: Tensor to print.
  • message: Message to print jointly with the tensor.

Returns

The same tensor x, unchanged.


function

keras.backend.function(inputs, outputs, updates=None)

Instantiates a Keras function.

Arguments

  • inputs: List of placeholder tensors.
  • outputs: List of output tensors.
  • updates: List of update ops.
  • **kwargs: Passed to tf.Session.run.

Returns

Output values as Numpy arrays.

Raises

  • ValueError: if invalid kwargs are passed in.

gradients

keras.backend.gradients(loss, variables)

Returns the gradients of loss w.r.t. variables.

Arguments

  • loss: Scalar tensor to minimize.
  • variables: List of variables.

Returns

A gradients tensor.


stop_gradient

keras.backend.stop_gradient(variables)

Returns variables but with zero gradient w.r.t. every other variable.

Arguments

  • variables: tensor or list of tensors to consider constant with respect to any other variable.

Returns

A single tensor or a list of tensors (depending on the passed argument) that has constant gradient with respect to any other variable.


rnn

keras.backend.rnn(step_function, inputs, initial_states, go_backwards=False, mask=None, constants=None, unroll=False, input_length=None)

Iterates over the time dimension of a tensor.

Arguments

  • step_function: Parameters: inputs: Tensor with shape (samples, ...) (no time dimension), representing input for the batch of samples at a certain time step. states: List of tensors. Returns: outputs: Tensor with shape (samples, ...) (no time dimension), new_states: List of tensors, same length and shapes as 'states'.
  • inputs: Tensor of temporal data of shape (samples, time, ...) (at least 3D).
  • initial_states: Tensor with shape (samples, ...) (no time dimension), containing the initial values for the states used in the step function.
  • go_backwards: Boolean. If True, do the iteration over the time dimension in reverse order and return the reversed sequence.
  • mask: Binary tensor with shape (samples, time), with a zero for every element that is masked.
  • constants: A list of constant values passed at each step.
  • unroll: Whether to unroll the RNN or to use a symbolic loop (while_loop or scan depending on backend).
  • input_length: Static number of timesteps in the input.

Returns

A tuple, (last_output, outputs, new_states).

last_output: The latest output of the rnn, of shape (samples, ...) outputs: Tensor with shape (samples, time, ...) where each entry outputs[s, t] is the output of the step function at time t for sample s. new_states: List of tensors, latest states returned by the step function, of shape (samples, ...).

Raises

  • ValueError: If input dimension is less than 3.
  • ValueError: If unroll is True but input timestep is not a fixed number.
  • ValueError: If mask is provided (not None) but states is not provided (len(states) == 0).

Numpy implementation

Show the Numpy implementation wzxhzdk:195

switch

keras.backend.switch(condition, then_expression, else_expression)

Switches between two operations depending on a scalar value.

Note that both then_expression and else_expression should be symbolic tensors of the same shape.

Arguments

  • condition: tensor (int or bool).
  • then_expression: either a tensor, or a callable that returns a tensor.
  • else_expression: either a tensor, or a callable that returns a tensor.

Returns

The selected tensor.

Raises

  • ValueError: If rank of condition is greater than rank of expressions.

Numpy implementation

def switch(condition, then_expression, else_expression):
    cond_float = condition.astype(floatx())
    while cond_float.ndim < then_expression.ndim:
        cond_float = cond_float[..., np.newaxis]
    return cond_float * then_expression + (1 - cond_float) * else_expression

in_train_phase

keras.backend.in_train_phase(x, alt, training=None)

Selects x in train phase, and alt otherwise.

Note that alt should have the same shape as x.

Arguments

  • x: What to return in train phase (tensor or callable that returns a tensor).
  • alt: What to return otherwise (tensor or callable that returns a tensor).
  • training: Optional scalar tensor (or Python boolean, or Python integer) specifying the learning phase.

Returns

Either x or alt based on the training flag. the training flag defaults to K.learning_phase().


in_test_phase

keras.backend.in_test_phase(x, alt, training=None)

Selects x in test phase, and alt otherwise.

Note that alt should have the same shape as x.

Arguments

  • x: What to return in test phase (tensor or callable that returns a tensor).
  • alt: What to return otherwise (tensor or callable that returns a tensor).
  • training: Optional scalar tensor (or Python boolean, or Python integer) specifying the learning phase.

Returns

Either x or alt based on K.learning_phase.


relu

keras.backend.relu(x, alpha=0.0, max_value=None, threshold=0.0)

Rectified linear unit.

With default values, it returns element-wise max(x, 0).

Otherwise, it follows: f(x) = max_value for x >= max_value, f(x) = x for threshold <= x < max_value, f(x) = alpha * (x - threshold) otherwise.

Arguments

  • x: A tensor or variable.
  • alpha: A scalar, slope of negative section (default=0.).
  • max_value: float. Saturation threshold.
  • threshold: float. Threshold value for thresholded activation.

Returns

A tensor.

Numpy implementation

def relu(x, alpha=0., max_value=None, threshold=0.):
    if max_value is None:
        max_value = np.inf
    above_threshold = x * (x >= threshold)
    above_threshold = np.clip(above_threshold, 0.0, max_value)
    below_threshold = alpha * (x - threshold) * (x < threshold)
    return below_threshold + above_threshold

elu

keras.backend.elu(x, alpha=1.0)

Exponential linear unit.

Arguments

  • x: A tensor or variable to compute the activation function for.
  • alpha: A scalar, slope of negative section.

Returns

A tensor.

Numpy implementation

def elu(x, alpha=1.):
    return x * (x > 0) + alpha * (np.exp(x) - 1.) * (x < 0)

softmax

keras.backend.softmax(x, axis=-1)

Softmax of a tensor.

Arguments

  • x: A tensor or variable.
  • axis: The dimension softmax would be performed on. The default is -1 which indicates the last dimension.

Returns

A tensor.

Numpy implementation

def softmax(x, axis=-1):
    y = np.exp(x - np.max(x, axis, keepdims=True))
    return y / np.sum(y, axis, keepdims=True)

softplus

keras.backend.softplus(x)

Softplus of a tensor.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

Numpy implementation

def softplus(x):
    return np.log(1. + np.exp(x))

softsign

keras.backend.softsign(x)

Softsign of a tensor.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

Numpy implementation

def softsign(x):
    return x / (1 + np.abs(x))

categorical_crossentropy

keras.backend.categorical_crossentropy(target, output, from_logits=False, axis=-1)

Categorical crossentropy between an output tensor and a target tensor.

Arguments

  • target: A tensor of the same shape as output.
  • output: A tensor resulting from a softmax (unless from_logits is True, in which case output is expected to be the logits).
  • from_logits: Boolean, whether output is the result of a softmax, or is a tensor of logits.
  • axis: Int specifying the channels axis. axis=-1 corresponds to data format channels_last, and axis=1 corresponds to data format channels_first.

Returns

Output tensor.

Raises

  • ValueError: if axis is neither -1 nor one of the axes of output.

sparse_categorical_crossentropy

keras.backend.sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1)

Categorical crossentropy with integer targets.

Arguments

  • target: An integer tensor.
  • output: A tensor resulting from a softmax (unless from_logits is True, in which case output is expected to be the logits).
  • from_logits: Boolean, whether output is the result of a softmax, or is a tensor of logits.
  • axis: Int specifying the channels axis. axis=-1 corresponds to data format channels_last, and axis=1 corresponds to data format channels_first.

Returns

Output tensor.

Raises

  • ValueError: if axis is neither -1 nor one of the axes of output.

binary_crossentropy

keras.backend.binary_crossentropy(target, output, from_logits=False)

Binary crossentropy between an output tensor and a target tensor.

Arguments

  • target: A tensor with the same shape as output.
  • output: A tensor.
  • from_logits: Whether output is expected to be a logits tensor. By default, we consider that output encodes a probability distribution.

Returns

A tensor.


sigmoid

keras.backend.sigmoid(x)

Element-wise sigmoid.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

Numpy implementation

def sigmoid(x):
    return 1. / (1. + np.exp(-x))

hard_sigmoid

keras.backend.hard_sigmoid(x)

Segment-wise linear approximation of sigmoid.

Faster than sigmoid. Returns 0. if x < -2.5, 1. if x > 2.5. In -2.5 <= x <= 2.5, returns 0.2 * x + 0.5.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

Numpy implementation

def hard_sigmoid(x):
    y = 0.2 * x + 0.5
    return np.clip(y, 0, 1)

tanh

keras.backend.tanh(x)

Element-wise tanh.

Arguments

  • x: A tensor or variable.

Returns

A tensor.

Numpy implementation

def tanh(x):
    return np.tanh(x)

dropout

keras.backend.dropout(x, level, noise_shape=None, seed=None)

Sets entries in x to zero at random, while scaling the entire tensor.

Arguments

  • x: tensor
  • level: fraction of the entries in the tensor that will be set to 0.
  • noise_shape: shape for randomly generated keep/drop flags, must be broadcastable to the shape of x
  • seed: random seed to ensure determinism.

Returns

A tensor. Numpy implementation

Show the Numpy implementation wzxhzdk:220

l2_normalize

keras.backend.l2_normalize(x, axis=None)

Normalizes a tensor wrt the L2 norm alongside the specified axis.

Arguments

  • x: Tensor or variable.
  • axis: axis along which to perform normalization.

Returns

A tensor.

Numpy implementation

def l2_normalize(x, axis=-1):
    y = np.max(np.sum(x ** 2, axis, keepdims=True), axis, keepdims=True)
    return x / np.sqrt(y)

in_top_k

keras.backend.in_top_k(predictions, targets, k)

Returns whether the targets are in the top k predictions.

Arguments

  • predictions: A tensor of shape (batch_size, classes) and type float32.
  • targets: A 1D tensor of length batch_size and type int32 or int64.
  • k: An int, number of top elements to consider.

Returns

A 1D tensor of length batch_size and type bool. output[i] is True if predictions[i, targets[i]] is within top-k values of predictions[i].


conv1d

keras.backend.conv1d(x, kernel, strides=1, padding='valid', data_format=None, dilation_rate=1)

1D convolution.

Arguments

  • x: Tensor or variable.
  • kernel: kernel tensor.
  • strides: stride integer.
  • padding: string, "same", "causal" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • dilation_rate: integer dilate rate.

Returns

A tensor, result of 1D convolution.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

conv2d

keras.backend.conv2d(x, kernel, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1))

2D convolution.

Arguments

  • x: Tensor or variable.
  • kernel: kernel tensor.
  • strides: strides tuple.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first". Whether to use Theano or TensorFlow/CNTK data format for inputs/kernels/outputs.
  • dilation_rate: tuple of 2 integers.

Returns

A tensor, result of 2D convolution.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

conv2d_transpose

keras.backend.conv2d_transpose(x, kernel, output_shape, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1))

2D deconvolution (i.e. transposed convolution).

Arguments

  • x: Tensor or variable.
  • kernel: kernel tensor.
  • output_shape: 1D int tensor for the output shape.
  • strides: strides tuple.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first". Whether to use Theano or TensorFlow/CNTK data format for inputs/kernels/outputs.
  • dilation_rate: tuple of 2 integers.

Returns

A tensor, result of transposed 2D convolution.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

separable_conv1d

keras.backend.separable_conv1d(x, depthwise_kernel, pointwise_kernel, strides=1, padding='valid', data_format=None, dilation_rate=1)

1D convolution with separable filters.

Arguments

  • x: input tensor
  • depthwise_kernel: convolution kernel for the depthwise convolution.
  • pointwise_kernel: kernel for the 1x1 convolution.
  • strides: stride integer.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • dilation_rate: integer dilation rate.

Returns

Output tensor.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

separable_conv2d

keras.backend.separable_conv2d(x, depthwise_kernel, pointwise_kernel, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1))

2D convolution with separable filters.

Arguments

  • x: input tensor
  • depthwise_kernel: convolution kernel for the depthwise convolution.
  • pointwise_kernel: kernel for the 1x1 convolution.
  • strides: strides tuple (length 2).
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • dilation_rate: tuple of integers, dilation rates for the separable convolution.

Returns

Output tensor.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

depthwise_conv2d

keras.backend.depthwise_conv2d(x, depthwise_kernel, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1))

2D convolution with separable filters.

Arguments

  • x: input tensor
  • depthwise_kernel: convolution kernel for the depthwise convolution.
  • strides: strides tuple (length 2).
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • dilation_rate: tuple of integers, dilation rates for the separable convolution.

Returns

Output tensor.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

conv3d

keras.backend.conv3d(x, kernel, strides=(1, 1, 1), padding='valid', data_format=None, dilation_rate=(1, 1, 1))

3D convolution.

Arguments

  • x: Tensor or variable.
  • kernel: kernel tensor.
  • strides: strides tuple.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first". Whether to use Theano or TensorFlow/CNTK data format for inputs/kernels/outputs.
  • dilation_rate: tuple of 3 integers.

Returns

A tensor, result of 3D convolution.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

conv3d_transpose

keras.backend.conv3d_transpose(x, kernel, output_shape, strides=(1, 1, 1), padding='valid', data_format=None)

3D deconvolution (i.e. transposed convolution).

Arguments

  • x: input tensor.
  • kernel: kernel tensor.
  • output_shape: 1D int tensor for the output shape.
  • strides: strides tuple.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first". Whether to use Theano or TensorFlow/CNTK data format for inputs/kernels/outputs.

Returns

A tensor, result of transposed 3D convolution.

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

pool2d

keras.backend.pool2d(x, pool_size, strides=(1, 1), padding='valid', data_format=None, pool_mode='max')

2D Pooling.

Arguments

  • x: Tensor or variable.
  • pool_size: tuple of 2 integers.
  • strides: tuple of 2 integers.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • pool_mode: string, "max" or "avg".

Returns

A tensor, result of 2D pooling.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".

  • ValueError: if pool_mode is neither "max" or "avg".

pool3d

keras.backend.pool3d(x, pool_size, strides=(1, 1, 1), padding='valid', data_format=None, pool_mode='max')

3D Pooling.

Arguments

  • x: Tensor or variable.
  • pool_size: tuple of 3 integers.
  • strides: tuple of 3 integers.
  • padding: string, "same" or "valid".
  • data_format: string, "channels_last" or "channels_first".
  • pool_mode: string, "max" or "avg".

Returns

A tensor, result of 3D pooling.

Raises

  • ValueError: if data_format is

neither "channels_last" or "channels_first".

  • ValueError: if pool_mode is neither "max" or "avg".

bias_add

keras.backend.bias_add(x, bias, data_format=None)

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 data_format argument. 2. invalid bias shape. the bias should be either a vector or a tensor with ndim(x) - 1 dimension Numpy implementation

Show the Numpy implementation wzxhzdk:235

random_normal

keras.backend.random_normal(shape, mean=0.0, stddev=1.0, dtype=None, seed=None)

Returns a tensor with normal distribution of values.

Arguments

  • shape: A tuple of integers, the shape of tensor to create.
  • mean: A float, mean of the normal distribution to draw samples.
  • stddev: A float, standard deviation of the normal distribution to draw samples.
  • dtype: String, dtype of returned tensor.
  • seed: Integer, random seed.

Returns

A tensor.


random_uniform

keras.backend.random_uniform(shape, minval=0.0, maxval=1.0, dtype=None, seed=None)

Returns a tensor with uniform distribution of values.

Arguments

  • shape: A tuple of integers, the shape of tensor to create.
  • minval: A float, lower boundary of the uniform distribution to draw samples.
  • maxval: A float, upper boundary of the uniform distribution to draw samples.
  • dtype: String, dtype of returned tensor.
  • seed: Integer, random seed.

Returns

A tensor.


random_binomial

keras.backend.random_binomial(shape, p=0.0, dtype=None, seed=None)

Returns a tensor with random binomial distribution of values.

Arguments

  • shape: A tuple of integers, the shape of tensor to create.
  • p: A float, 0. <= p <= 1, probability of binomial distribution.
  • dtype: String, dtype of returned tensor.
  • seed: Integer, random seed.

Returns

A tensor.


truncated_normal

keras.backend.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=None, seed=None)

Returns a tensor with truncated random normal distribution of values.

The generated values follow a normal distribution with specified mean and standard deviation, except that values whose magnitude is more than two standard deviations from the mean are dropped and re-picked.

Arguments

  • shape: A tuple of integers, the shape of tensor to create.
  • mean: Mean of the values.
  • stddev: Standard deviation of the values.
  • dtype: String, dtype of returned tensor.
  • seed: Integer, random seed.

Returns

A tensor.


ctc_label_dense_to_sparse

keras.backend.ctc_label_dense_to_sparse(labels, label_lengths)

Converts CTC labels from dense to sparse.

Arguments

  • labels: dense CTC labels.
  • label_lengths: length of the labels.

Returns

A sparse tensor representation of the labels.


ctc_batch_cost

keras.backend.ctc_batch_cost(y_true, y_pred, input_length, label_length)

Runs CTC loss algorithm on each batch element.

Arguments

  • y_true: tensor (samples, max_string_length) containing the truth labels.
  • y_pred: tensor (samples, time_steps, num_categories) containing the prediction, or output of the softmax.
  • input_length: tensor (samples, 1) containing the sequence length for each batch item in y_pred.
  • label_length: tensor (samples, 1) containing the sequence length for each batch item in y_true.

Returns

Tensor with shape (samples,1) containing the CTC loss of each element.


ctc_decode

keras.backend.ctc_decode(y_pred, input_length, greedy=True, beam_width=100, top_paths=1)

Decodes the output of a softmax.

Can use either greedy search (also known as best path) or a constrained dictionary search.

Arguments

  • y_pred: tensor (samples, time_steps, num_categories) containing the prediction, or output of the softmax.
  • input_length: tensor (samples, ) containing the sequence length for each batch item in y_pred.
  • greedy: perform much faster best-path search if true. This does not use a dictionary.
  • beam_width: if greedy is false: a beam search decoder will be used with a beam of this width.
  • top_paths: if greedy is false, how many of the most probable paths will be returned.

Returns

  • Tuple: List: if greedy is true, returns a list of one element that contains the decoded sequence. If false, returns the top_paths most probable decoded sequences. Important: blank labels are returned as -1. Tensor (top_paths, ) that contains the log probability of each decoded sequence.

map_fn

keras.backend.map_fn(fn, elems, name=None, dtype=None)

Map the function fn over the elements elems and return the outputs.

Arguments

  • fn: Callable that will be called upon each element in elems
  • elems: tensor
  • name: A string name for the map node in the graph
  • dtype: Output data type.

Returns

Tensor with dtype dtype.


foldl

keras.backend.foldl(fn, elems, initializer=None, name=None)

Reduce elems using fn to combine them from left to right.

Arguments

  • fn: Callable that will be called upon each element in elems and an accumulator, for instance lambda acc, x: acc + x
  • elems: tensor
  • initializer: The first value used (elems[0] in case of None)
  • name: A string name for the foldl node in the graph

Returns

Tensor with same type and shape as initializer.


foldr

keras.backend.foldr(fn, elems, initializer=None, name=None)

Reduce elems using fn to combine them from right to left.

Arguments

  • fn: Callable that will be called upon each element in elems and an accumulator, for instance lambda acc, x: acc + x
  • elems: tensor
  • initializer: The first value used (elems[-1] in case of None)
  • name: A string name for the foldr node in the graph

Returns

Tensor with same type and shape as initializer.


local_conv1d

keras.backend.local_conv1d(inputs, kernel, kernel_size, strides, data_format=None)

Apply 1D conv with un-shared weights.

Arguments

  • inputs: 3D tensor with shape: (batch_size, steps, input_dim)
  • kernel: the unshared weight for convolution, with shape (output_length, feature_dim, filters)
  • kernel_size: a tuple of a single integer, specifying the length of the 1D convolution window
  • strides: a tuple of a single integer, specifying the stride length of the convolution
  • data_format: the data format, channels_first or channels_last

Returns

the tensor after 1d conv with un-shared weights, with shape (batch_size, output_length, filters)

Raises

  • ValueError: If data_format is neither "channels_last" nor "channels_first".

local_conv2d

keras.backend.local_conv2d(inputs, kernel, kernel_size, strides, output_shape, data_format=None)

Apply 2D conv with un-shared weights.

Arguments

  • inputs: 4D tensor with shape: (batch_size, filters, new_rows, new_cols) if data_format='channels_first' or 4D tensor with shape: (batch_size, new_rows, new_cols, filters) if data_format='channels_last'.
  • kernel: the unshared weight for convolution, with shape (output_items, feature_dim, filters)
  • kernel_size: a tuple of 2 integers, specifying the width and height of the 2D convolution window.
  • strides: a tuple of 2 integers, specifying the strides of the convolution along the width and height.
  • output_shape: a tuple with (output_row, output_col)
  • data_format: the data format, channels_first or channels_last

Returns

A 4d tensor with shape: (batch_size, filters, new_rows, new_cols) if data_format='channels_first' or 4D tensor with shape: (batch_size, new_rows, new_cols, filters) if data_format='channels_last'.

Raises

  • ValueError: if data_format is neither channels_last or channels_first.

backend

keras.backend.backend()

Publicly accessible method for determining the current backend.

Returns

String, the name of the backend Keras is currently using.

Example

>>> keras.backend.backend()
'tensorflow'