cast定義:
cast(x, dtype, name=None)
- 第一個參數 x: 待轉換的數據(張量)
- 第二個參數 dtype: 目標數據類型
- 第三個參數 name: 可選參數,定義操作的名稱
int32轉換為float32:
import tensorflow as tf
t1 = tf.Variable([1,2,3,4,5])
t2 = tf.cast(t1,dtype=tf.float32)
print 't1: {}'.format(t1)
print 't2: {}'.format(t2)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(t2)
print t2.eval()
# print(sess.run(t2))
輸出:
t1: <tf.Variable 'Variable:0' shape=(5,) dtype=int32_ref>
t2: Tensor("Cast:0", shape=(5,), dtype=float32)
[ 1. 2. 3. 4. 5.]
tensorflow中的數據類型列表:
數據類型 | Python 類型 | 描述 |
---|---|---|
DT_FLOAT |
tf.float32 |
32 位浮點數. |
DT_DOUBLE |
tf.float64 |
64 位浮點數. |
DT_INT64 |
tf.int64 |
64 位有符號整型. |
DT_INT32 |
tf.int32 |
32 位有符號整型. |
DT_INT16 |
tf.int16 |
16 位有符號整型. |
DT_INT8 |
tf.int8 |
8 位有符號整型. |
DT_UINT8 |
tf.uint8 |
8 位無符號整型. |
DT_STRING |
tf.string |
可變長度的字節數組.每一個張量元素都是一個字節數組. |
DT_BOOL |
tf.bool |
布爾型. |
DT_COMPLEX64 |
tf.complex64 |
由兩個32位浮點數組成的復數:實數和虛數. |
DT_QINT32 |
tf.qint32 |
用於量化Ops的32位有符號整型. |
DT_QINT8 |
tf.qint8 |
用於量化Ops的8位有符號整型. |
DT_QUINT8 |
tf.quint8 |
用於量化Ops的8位無符號整型. |