tf.keras.backend.variable

TensorFlow 1 version View source on GitHub

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