Warning: This API is deprecated and will be removed in a future
version of TensorFlow after
the replacement is stable.
Bitcast
Stay organized with collections
Save and categorize content based on your preferences.
Bitcasts a tensor from one type to another without copying data.
Given a tensor `input`, this operation returns a tensor that has the same buffer
data as `input` with datatype `type`.
If the input datatype `T` is larger than the output datatype `type` then the
shape changes from [...] to [..., sizeof(`T`)/sizeof(`type`)].
If `T` is smaller than `type`, the operator requires that the rightmost
dimension be equal to sizeof(`type`)/sizeof(`T`). The shape then goes from
[..., sizeof(`type`)/sizeof(`T`)] to [...].
tf.bitcast() and tf.cast() work differently when real dtype is casted as a complex dtype
(e.g. tf.complex64 or tf.complex128) as tf.cast() make imaginary part 0 while tf.bitcast()
gives module error.
For example,
Example 1:
>>> a = [1., 2., 3.]
>>> equality_bitcast = tf.bitcast(a, tf.complex128)
Traceback (most recent call last):
...
InvalidArgumentError: Cannot bitcast from 1 to 18 [Op:Bitcast]
>>> equality_cast = tf.cast(a, tf.complex128)
>>> print(equality_cast)
tf.Tensor([1.+0.j 2.+0.j 3.+0.j], shape=(3,), dtype=complex128)
Example 2:
>>> tf.bitcast(tf.constant(0xffffffff, dtype=tf.uint32), tf.uint8)
Example 3:
>>> x = [1., 2., 3.]
>>> y = [0., 2., 3.]
>>> equality= tf.equal(x,y)
>>> equality_cast = tf.cast(equality,tf.float32)
>>> equality_bitcast = tf.bitcast(equality_cast,tf.uint8)
>>> print(equality)
tf.Tensor([False True True], shape=(3,), dtype=bool)
>>> print(equality_cast)
tf.Tensor([0. 1. 1.], shape=(3,), dtype=float32)
>>> print(equality_bitcast)
tf.Tensor(
[[ 0 0 0 0]
[ 0 0 128 63]
[ 0 0 128 63]], shape=(3, 4), dtype=uint8)
NOTE: Bitcast is implemented as a low-level cast, so machines with different
endian orderings will give different results. A copy from input buffer to output
buffer is made on BE machines when types are of different sizes in order to get
the same casting results as on LE machines.
Inherited Methods
From class
java.lang.Object
boolean
|
equals(Object arg0)
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
Public Methods
public
Output<U>
asOutput
()
Returns the symbolic handle of a tensor.
Inputs to TensorFlow operations are outputs of another TensorFlow operation. This method is
used to obtain a symbolic handle that represents the computation of the input.
Factory method to create a class wrapping a new Bitcast operation.
Returns
- a new instance of Bitcast
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 2023-03-23 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-03-23 UTC."],[],[],null,["# Bitcast\n\npublic final class **Bitcast** \nBitcasts a tensor from one type to another without copying data.\n\n\nGiven a tensor \\`input\\`, this operation returns a tensor that has the same buffer\ndata as \\`input\\` with datatype \\`type\\`.\n\n\nIf the input datatype \\`T\\` is larger than the output datatype \\`type\\` then the\nshape changes from \\[...\\] to \\[..., sizeof(\\`T\\`)/sizeof(\\`type\\`)\\].\n\n\nIf \\`T\\` is smaller than \\`type\\`, the operator requires that the rightmost\ndimension be equal to sizeof(\\`type\\`)/sizeof(\\`T\\`). The shape then goes from\n\\[..., sizeof(\\`type\\`)/sizeof(\\`T\\`)\\] to \\[...\\].\n\n\ntf.bitcast() and tf.cast() work differently when real dtype is casted as a complex dtype\n(e.g. tf.complex64 or tf.complex128) as tf.cast() make imaginary part 0 while tf.bitcast()\ngives module error.\nFor example,\n\n\nExample 1:\n\n\n\\\u003e\\\u003e\\\u003e a = \\[1., 2., 3.\\]\n\\\u003e\\\u003e\\\u003e equality_bitcast = tf.bitcast(a, tf.complex128)\nTraceback (most recent call last):\n...\nInvalidArgumentError: Cannot bitcast from 1 to 18 \\[Op:Bitcast\\]\n\\\u003e\\\u003e\\\u003e equality_cast = tf.cast(a, tf.complex128)\n\\\u003e\\\u003e\\\u003e print(equality_cast)\ntf.Tensor(\\[1.+0.j 2.+0.j 3.+0.j\\], shape=(3,), dtype=complex128)\n\n\nExample 2:\n\n\n\\\u003e\\\u003e\\\u003e tf.bitcast(tf.constant(0xffffffff, dtype=tf.uint32), tf.uint8)\n\n\nExample 3:\n\n\n\\\u003e\\\u003e\\\u003e x = \\[1., 2., 3.\\]\n\\\u003e\\\u003e\\\u003e y = \\[0., 2., 3.\\]\n\\\u003e\\\u003e\\\u003e equality= tf.equal(x,y)\n\\\u003e\\\u003e\\\u003e equality_cast = tf.cast(equality,tf.float32)\n\\\u003e\\\u003e\\\u003e equality_bitcast = tf.bitcast(equality_cast,tf.uint8)\n\\\u003e\\\u003e\\\u003e print(equality)\ntf.Tensor(\\[False True True\\], shape=(3,), dtype=bool)\n\\\u003e\\\u003e\\\u003e print(equality_cast)\ntf.Tensor(\\[0. 1. 1.\\], shape=(3,), dtype=float32)\n\\\u003e\\\u003e\\\u003e print(equality_bitcast)\ntf.Tensor(\n\\[\\[ 0 0 0 0\\]\n\\[ 0 0 128 63\\]\n\\[ 0 0 128 63\\]\\], shape=(3, 4), dtype=uint8)\n\n\n*NOTE*: Bitcast is implemented as a low-level cast, so machines with different\nendian orderings will give different results. A copy from input buffer to output\nbuffer is made on BE machines when types are of different sizes in order to get\nthe same casting results as on LE machines.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n### Public Methods\n\n|-------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Output](/api_docs/java/org/tensorflow/Output)\\\u003cU\\\u003e | [asOutput](/api_docs/java/org/tensorflow/op/core/Bitcast#asOutput())() Returns the symbolic handle of a tensor. |\n| static \\\u003cU, T\\\u003e [Bitcast](/api_docs/java/org/tensorflow/op/core/Bitcast)\\\u003cU\\\u003e | [create](/api_docs/java/org/tensorflow/op/core/Bitcast#create(org.tensorflow.op.Scope,%20org.tensorflow.Operand\u003cT\u003e,%20java.lang.Class\u003cU\u003e))([Scope](/api_docs/java/org/tensorflow/op/Scope) scope, [Operand](/api_docs/java/org/tensorflow/Operand)\\\u003cT\\\u003e input, Class\\\u003cU\\\u003e type) Factory method to create a class wrapping a new Bitcast operation. |\n| [Output](/api_docs/java/org/tensorflow/Output)\\\u003cU\\\u003e | [output](/api_docs/java/org/tensorflow/op/core/Bitcast#output())() |\n\n### Inherited Methods\n\nFrom class [org.tensorflow.op.PrimitiveOp](/api_docs/java/org/tensorflow/op/PrimitiveOp) \n\n|------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|\n| final boolean | [equals](/api_docs/java/org/tensorflow/op/PrimitiveOp#equals(java.lang.Object))(Object obj) |\n| final int | [hashCode](/api_docs/java/org/tensorflow/op/PrimitiveOp#hashCode())() |\n| [Operation](/api_docs/java/org/tensorflow/Operation) | [op](/api_docs/java/org/tensorflow/op/PrimitiveOp#op())() Returns the underlying [Operation](/api_docs/java/org/tensorflow/Operation) |\n| final String | [toString](/api_docs/java/org/tensorflow/op/PrimitiveOp#toString())() |\n\nFrom class java.lang.Object \n\n|------------------|---------------------------|\n| boolean | equals(Object arg0) |\n| final Class\\\u003c?\\\u003e | getClass() |\n| int | hashCode() |\n| final void | notify() |\n| final void | notifyAll() |\n| String | toString() |\n| final void | wait(long arg0, int arg1) |\n| final void | wait(long arg0) |\n| final void | wait() |\n\nFrom interface [org.tensorflow.Operand](/api_docs/java/org/tensorflow/Operand) \n\n|--------------------------------------------------------------|---------------------------------------------------------------------------------------------------------|\n| abstract [Output](/api_docs/java/org/tensorflow/Output)\\\u003cU\\\u003e | [asOutput](/api_docs/java/org/tensorflow/Operand#asOutput())() Returns the symbolic handle of a tensor. |\n\nPublic Methods\n--------------\n\n#### public [Output](/api_docs/java/org/tensorflow/Output)\\\u003cU\\\u003e\n**asOutput**\n()\n\nReturns the symbolic handle of a tensor.\n\nInputs to TensorFlow operations are outputs of another TensorFlow operation. This method is\nused to obtain a symbolic handle that represents the computation of the input.\n\n\u003cbr /\u003e\n\n#### public static [Bitcast](/api_docs/java/org/tensorflow/op/core/Bitcast)\\\u003cU\\\u003e\n**create**\n([Scope](/api_docs/java/org/tensorflow/op/Scope) scope, [Operand](/api_docs/java/org/tensorflow/Operand)\\\u003cT\\\u003e input, Class\\\u003cU\\\u003e type)\n\nFactory method to create a class wrapping a new Bitcast operation. \n\n##### Parameters\n\n| scope | current scope |\n|-------|---------------|\n\n##### Returns\n\n- a new instance of Bitcast \n\n#### public [Output](/api_docs/java/org/tensorflow/Output)\\\u003cU\\\u003e\n**output**\n()\n\n\u003cbr /\u003e"]]