2017-06-17 2 views
0

Wenn eine Hash-Lookup-Tabelle mit tf.contrib.Dataset.map() verwenden, schlägt es mit dem folgenden Fehler:Tensorflow: Inkompatible Typen für dataset.map() in tf.contrib.data

TypeError: In op 'hash_table_Lookup', input types ([tf.string, tf.string, tf.int32]) are not compatible with expected types ([tf.string_ref, tf.string, tf.int32])

-Code zu reproduzieren:

from __future__ import absolute_import 
from __future__ import division 
from __future__ import print_function 

import tensorflow as tf 

initializer = tf.contrib.lookup.KeyValueTensorInitializer(
    ['one', 'two', 'three'], [1, 2, 3]) 
hash_table = tf.contrib.lookup.HashTable(initializer, -1) 

tensor = tf.convert_to_tensor(['one', 'two', 'three']) 
dataset = tf.contrib.data.Dataset.from_tensor_slices(tensor) 
dataset = dataset.map(lambda k: hash_table.lookup(k)) 

Es klagt über und tf.string ist inkompatibel.

Es ist seltsam, dass es eine tf.string_ref und nicht eine tf.string erwartet. Weiß jemand, warum das so ist und was ich dagegen tun kann?

Die Probleme beziehen sich auf table_ref wird tf.string_refhere.

Antwort

1

Dies ist ein Fehler, der in TensorFlow 1.3 behoben wurde. Wenn Sie TensorFlow 1.2, die folgende Abhilfe verwenden sollte funktionieren:

from __future__ import absolute_import 
from __future__ import division 
from __future__ import print_function 

import tensorflow as tf 

# Use internal library implementation of `lookup_ops` in TensorFlow 1.2. 
from tensorflow.python.ops import lookup_ops 
initializer = lookup_ops.KeyValueTensorInitializer(
    ['one', 'two', 'three'], [1, 2, 3]) 
hash_table = lookup_ops.HashTable(initializer, -1) 

tensor = tf.convert_to_tensor(['one', 'two', 'three']) 
dataset = tf.contrib.data.Dataset.from_tensor_slices(tensor) 
dataset = dataset.map(lambda k: hash_table.lookup(k)) 

Bis TensorFlow 1.2, die tf.contrib.lookup Bibliothek "reference types" verwendet, um die Lookup-Tabellen darstellen, während in der internen Bibliothek (verwendet tf.contrib.lookup von 1,3 ab zu implementieren) die moderneren und kompatiblen "resource types" werden verwendet.