tf.keras.backend.shape
Stay organized with collections
Save and categorize content based on your preferences.
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)
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.keras.backend.shape\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/keras/backend/shape) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/keras/backend.py#L1129-L1159) |\n\nReturns the symbolic shape of a tensor or variable.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.backend.shape`](/api_docs/python/tf/keras/backend/shape)\n\n\u003cbr /\u003e\n\n tf.keras.backend.shape(\n x\n )\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Arguments --------- ||\n|-----|-----------------------|\n| `x` | A tensor or variable. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A symbolic shape (which is itself a tensor). ||\n\n\u003cbr /\u003e\n\n#### Examples:\n\n # TensorFlow example\n \u003e\u003e\u003e from keras import backend as K\n \u003e\u003e\u003e tf_session = K.get_session()\n \u003e\u003e\u003e val = np.array([[1, 2], [3, 4]])\n \u003e\u003e\u003e kvar = K.variable(value=val)\n \u003e\u003e\u003e input = keras.backend.placeholder(shape=(2, 4, 5))\n \u003e\u003e\u003e K.shape(kvar)\n \u003ctf.Tensor 'Shape_8:0' shape=(2,) dtype=int32\u003e\n \u003e\u003e\u003e K.shape(input)\n \u003ctf.Tensor 'Shape_9:0' shape=(3,) dtype=int32\u003e\n # To get integer shape (Instead, you can use K.int_shape(x))\n \u003e\u003e\u003e K.shape(kvar).eval(session=tf_session)\n array([2, 2], dtype=int32)\n \u003e\u003e\u003e K.shape(input).eval(session=tf_session)\n array([2, 4, 5], dtype=int32)"]]