keras.backend

参考 keras.backend - 云+社区 - 腾讯云

Keras backend API.

一、Functions

二、重要的函数

1、keras.backend.arange

Creates a 1D tensor containing a sequence of integers.

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

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.

Example:

Compat aliases

2、keras.backend.reshape

Reshapes a tensor to the specified shape.

tf.keras.backend.reshape(
    x,
    shape
)

Arguments:

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

Returns:

  • A tensor.

Example:

Compat aliases

3、keras.backend.variable

Instantiates a variable and returns it.

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

Arguments:

  • value: Numpy array, initial value of the tensor.
  • dtype: Tensor type.
  • name: Optional name string for the tensor.
  • constraint: Optional projection function to be applied to the variable after an optimizer update.

Returns:

  • A variable instance (with Keras metadata included).

Examples:

<pre class="devsite-click-to-copy prettyprint lang-py">
<code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">import numpy as np </code>
<code class="no-select nocode">    &gt;&gt;&gt; from keras import backend as K </code>
<code class="no-select nocode">    &gt;&gt;&gt; val = np.array([[1, 2], [3, 4]]) </code>
<code class="no-select nocode">    &gt;&gt;&gt; kvar = K.variable(value=val, dtype=&#39;float64&#39;, name=&#39;example_var&#39;) </code>
<code class="no-select nocode">    &gt;&gt;&gt; K.dtype(kvar) </code>
<code class="no-select nocode">    &#39;float64&#39; </code>
<code class="no-select nocode">    &gt;&gt;&gt; print(kvar) </code>
<code class="no-select nocode">    example_var </code>
<code class="no-select nocode">    &gt;&gt;&gt; kvar.eval() </code>
<code class="no-select nocode">    array([[ 1.,  2.], </code>
<code class="no-select nocode">           [ 3.,  4.]]) </code>
</pre>

Compat aliases

4、keras.backend.cast

Casts a tensor to a different dtype and returns it.

tf.keras.backend.cast(
    x,
    dtype
)

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.

Examples:

Cast a float32 variable to a float64 tensor

<pre class="devsite-click-to-copy prettyprint lang-py">
<code class="devsite-terminal" data-terminal-prefix="&gt;&gt;&gt;">import tensorflow as tf </code>
<code class="no-select nocode">    &gt;&gt;&gt; from tensorflow.keras import backend as K </code>
<code class="no-select nocode">    &gt;&gt;&gt; input = K.ones(shape=(1,3)) </code>
<code class="no-select nocode">    &gt;&gt;&gt; print(input) </code>
<code class="no-select nocode">    &gt;&gt;&gt; cast_input = K.cast(input, dtype=&#39;float64&#39;) </code>
<code class="no-select nocode">    &gt;&gt;&gt; print(cast_input) </code>
<code class="no-select nocode"> </code>
<code class="no-select nocode">    &lt;tf.Variable &#39;Variable:0&#39; shape=(1, 3) dtype=float32, </code>
<code class="no-select nocode">         numpy=array([[1., 1., 1.]], dtype=float32)&gt; </code>
<code class="no-select nocode">    tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float64) </code>
</pre>

Compat aliases

5、keras.backend.greater

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

tf.keras.backend.greater(
    x,
    y
)

Arguments:

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

Returns:

  • A bool tensor.

Compat aliases

6、keras.backend.gather

Retrieves the elements of indices indices in the tensor reference.

tf.keras.backend.gather(
    reference,
    indices
)

Arguments:

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

Returns:

  • A tensor of same type as reference.

Compat aliases

7、keras.backend.stack

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

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

Arguments:

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

Returns:

  • A tensor.

Example:

    

Compat aliases

8、keras.backend.shape

Returns the symbolic shape of a tensor or variable.

tf.keras.backend.shape(x)

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)
    >>> input = keras.backend.placeholder(shape=(2, 4, 5))
    >>> K.shape(kvar)
    <tf.Tensor 'Shape_8:0' shape=(2,) dtype=int32>
    >>> K.shape(input)
    <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(input).eval(session=tf_session)
    array([2, 4, 5], dtype=int32)

Compat aliases

9、keras.backend.concatenate

Concatenates a list of tensors alongside the specified axis.

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

Arguments:

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

Returns:

  • A tensor.

Example:

Compat aliases

10、keras.backend.max

Maximum value in a tensor.

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

Arguments:

  • x: A tensor or variable.
  • axis: An integer, the axis to find maximum values.
  • 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.

Compat aliases