將張量轉換為新類型。
用法
tf.cast(
x, dtype, name=None
)參數
-
x數字類型的Tensor或SparseTensor或IndexedSlices。它可能是uint8,uint16,uint32,uint64,int8,int16,int32,int64,float16,float32,float64,complex64,complex128,bfloat16。 -
dtype目標類型。支持的 dtypes 列表與x相同。 -
name操作的名稱(可選)。
返回
-
Tensor或SparseTensor或IndexedSlices具有與x相同的形狀和與dtype相同的類型。
拋出
-
TypeError如果x不能轉換為dtype。
該操作將 x(在 Tensor 的情況下)或 x.values(在 SparseTensor 或 IndexedSlices 的情況下)轉換為 dtype 。
例如:
x = tf.constant([1.8, 2.2], dtype=tf.float32)
tf.cast(x, tf.int32)
<tf.Tensor:shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>
注意 tf.cast 有一個別名 tf.dtypes.cast :
x = tf.constant([1.8, 2.2], dtype=tf.float32)
tf.dtypes.cast(x, tf.int32)
<tf.Tensor:shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>
該操作支持數據類型(對於x和dtype) 的uint8,uint16,uint32,uint64,int8,int16,int32,int64,float16,float32,float64,complex64,complex128,bfloat16.如果從複雜類型轉換(complex64,complex128) 到實數類型,隻有實數部分x被退回。如果從真實類型轉換為複雜類型(complex64,complex128),返回值的虛部設置為0.這裏對複雜類型的處理與 numpy 的行為相匹配。
請注意,將 nan 和 inf values 轉換為整數類型具有未定義的行為。
相關用法
- Python tf.case用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代碼示例
- Python tf.compat.v1.Variable.eval用法及代碼示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代碼示例
- Python tf.compat.v1.layers.conv3d用法及代碼示例
- Python tf.compat.v1.strings.length用法及代碼示例
- Python tf.clip_by_norm用法及代碼示例
- Python tf.compat.v1.data.Dataset.snapshot用法及代碼示例
- Python tf.compat.v1.data.experimental.SqlDataset.reduce用法及代碼示例
- Python tf.compat.v1.feature_column.categorical_column_with_vocabulary_file用法及代碼示例
- Python tf.compat.v1.data.TextLineDataset.from_tensors用法及代碼示例
- Python tf.compat.v1.variable_scope用法及代碼示例
- Python tf.compat.v1.data.experimental.SqlDataset.as_numpy_iterator用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.covariance用法及代碼示例
- Python tf.compat.v1.placeholder用法及代碼示例
- Python tf.compat.v1.layers.Conv3D用法及代碼示例
- Python tf.compat.v1.train.get_or_create_global_step用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.cast。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
