tf.keras.backend.is_keras_tensor

View source on GitHub

Returns whether x is a Keras tensor.

tf.keras.backend.is_keras_tensor(x)

A "Keras tensor" is a tensor that was returned by a Keras layer, (Layer class) or by Input.

Arguments:

  • x: A candidate tensor.

Returns:

A boolean: Whether the argument is a Keras tensor.

Raises:

  • ValueError: In case x is not a symbolic tensor.

Examples:

<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; import numpy</code>
<code class="no-select nocode">    &gt;&gt;&gt; from keras import backend as K</code>
<code class="no-select nocode">    &gt;&gt;&gt; from keras.layers import Input, Dense</code>
<code class="no-select nocode">    &gt;&gt;&gt; np_var = numpy.array([1, 2])</code>
<code class="no-select nocode">    &gt;&gt;&gt; K.is_keras_tensor(np_var) # A numpy array is not a symbolic tensor.</code>
<code class="no-select nocode">    ValueError</code>
<code class="no-select nocode">    &gt;&gt;&gt; k_var = tf.compat.v1.placeholder(&#39;float32&#39;, shape=(1,1))</code>
<code class="no-select nocode">    &gt;&gt;&gt; K.is_keras_tensor(k_var) # A variable indirectly created outside of</code>
<code class="no-select nocode">    keras is not a Keras tensor.</code>
<code class="no-select nocode">    False</code>
<code class="no-select nocode">    &gt;&gt;&gt; keras_var = K.variable(np_var)</code>
<code class="no-select nocode">    &gt;&gt;&gt; K.is_keras_tensor(keras_var)  # A variable created with the keras</code>
<code class="no-select nocode">    backend is not a Keras tensor.</code>
<code class="no-select nocode">    False</code>
<code class="no-select nocode">    &gt;&gt;&gt; keras_placeholder = K.placeholder(shape=(2, 4, 5))</code>
<code class="no-select nocode">    &gt;&gt;&gt; K.is_keras_tensor(keras_placeholder)  # A placeholder is not a Keras</code>
<code class="no-select nocode">    tensor.</code>
<code class="no-select nocode">    False</code>
<code class="no-select nocode">    &gt;&gt;&gt; keras_input = Input([10])</code>
<code class="no-select nocode">    &gt;&gt;&gt; K.is_keras_tensor(keras_input) # An Input is a Keras tensor.</code>
<code class="no-select nocode">    True</code>
<code class="no-select nocode">    &gt;&gt;&gt; keras_layer_output = Dense(10)(keras_input)</code>
<code class="no-select nocode">    &gt;&gt;&gt; K.is_keras_tensor(keras_layer_output) # Any Keras layer output is a</code>
<code class="no-select nocode">    Keras tensor.</code>
<code class="no-select nocode">    True</code>
</pre>

Compat aliases