2016-02-24 5 views
10

ho provato: appareCome convertire tf.int64 in tf.float32?

test_image = tf.convert_to_tensor(img, dtype=tf.float32) 

Poi seguente errore:

ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int64: 'Tensor("test/ArgMax:0", shape=TensorShape([Dimension(None)]), dtype=int64)' 

risposta

8

Ops, trovo la funzione nell'API ...

tf.to_float(x, name='ToFloat') 
18

Puoi lanciare generalmente utilizzando:

tf.cast(my_tensor, tf.float32) 

Sostituisci tf.float32 con il tipo desiderato.


Edit: Sembra che in questo momento, almeno, che tf.cast non rigetta a un DTYPE non firmato (per esempio tf.uint8). Per ovviare a questo problema, puoi trasmettere l'equivalente firmato e utilizzare tf.bitcast per ottenere tutto il percorso. per esempio.

tf.bitcast(tf.cast(my_tensor, tf.int8), tf.uint8) 
+0

Va notato che tf non può calcolare gradienti per queste operazioni, in modo che non può essere utilizzato per simulare pesi quantizzati. – oarfish

0

È possibile utilizzare uno o tf.cast(x, tf.float32)tf.to_float(x), entrambi i quali gettato a float32.

Esempio:

sess = tf.Session() 

# Create an integer tensor. 
tensor = tf.convert_to_tensor(np.array([0, 1, 2, 3, 4]), dtype=tf.int64) 
sess.run(tensor) 
# array([0, 1, 2, 3, 4]) 

# Use tf.cast() 
tensor_float = tf.cast(tensor, tf.float32) 
sess.run(tensor_float) 
# array([ 0., 1., 2., 3., 4.], dtype=float32) 

# Use tf.to_float() to cast to float32 
tensor_float = tf.to_float(tensor) 
sess.run(tensor_float) 
# array([ 0., 1., 2., 3., 4.], dtype=float32) 
+0

quando lancio un mago con il tipo di 'tf.uint8' in' tf.float32', e ho usato 'matplotlib' per mostrarli, cambio' tf.float32'. Come può mostrare l'immagine principale? – Tavakoli