前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >keras.backend

keras.backend

作者头像
狼啸风云
修改2022-09-03 19:57:18
1.1K0
修改2022-09-03 19:57:18
举报

Keras backend API.

一、Functions

二、重要的函数

1、keras.backend.arange

Creates a 1D tensor containing a sequence of integers.

代码语言:javascript
复制
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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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.

代码语言:javascript
复制
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

代码语言:javascript
复制
<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).

代码语言:javascript
复制
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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
tf.keras.backend.shape(x)

Arguments:

  • x: A tensor or variable.

Returns:

  • A symbolic shape (which is itself a tensor).

Examples:

代码语言:javascript
复制
    # 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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Functions
  • 二、重要的函数
    • 1、keras.backend.arange
      • 2、keras.backend.reshape
        • 3、keras.backend.variable
          • 4、keras.backend.cast
            • 5、keras.backend.greater
              • 6、keras.backend.gather
                • 7、keras.backend.stack
                  • 8、keras.backend.shape
                    • 9、keras.backend.concatenate
                      • 10、keras.backend.max
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档