ahm*_*med 6 python machine-learning deep-learning tensorflow
我在尝试使用Tensorflow的feature_column映射到传递给Dataset map方法的函数内部时遇到问题。尝试使用Dataset.map对输入数据集的分类字符串特征进行热编码作为输入管道的一部分时,会发生这种情况。我收到的错误消息是:tensorflow.python.framework.errors_impl.FailedPreconditionError:表已初始化。
以下代码是重现该问题的基本示例:
import numpy as np
import tensorflow as tf
from tensorflow.contrib.lookup import index_table_from_tensor
# generate tfrecords with two string categorical features and write to file
vlists = dict(season=['Spring', 'Summer', 'Fall', 'Winter'],
day=['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])
writer = tf.python_io.TFRecordWriter('test.tfr')
for s,d in zip(np.random.choice(vlists['season'],50),
np.random.choice(vlists['day'],50)):
example = tf.train.Example(
features = tf.train.Features(
feature={
'season':tf.train.Feature(
bytes_list=tf.train.BytesList(value=[s.encode()])),
'day':tf.train.Feature(
bytes_list=tf.train.BytesList(value=[d.encode()]))
}
)
)
serialized = example.SerializeToString()
writer.write(serialized)
writer.close()
Run Code Online (Sandbox Code Playgroud)
Now there's a tfrecord file in the cwd called test.tfr with 50 records, and each record consists of two string features, 'season' and 'day', The following will then create a Dataset that will parse the tfrecords and create batches of size 4
def parse_record(element):
feats = {
'season': tf.FixedLenFeature((), tf.string),
'day': tf.FixedLenFeature((), tf.string)
}
return tf.parse_example(element, feats)
fname = tf.placeholder(tf.string, [])
ds = tf.data.TFRecordDataset(fname)
ds = ds.batch(4).map(parse_record)
Run Code Online (Sandbox Code Playgroud)
At this point if you create an iterator and call get_next on it several times, it works as expected and you would see output like this each run:
iterator = ds.make_initializable_iterator()
nxt = iterator.get_next()
sess.run(tf.tables_initializer())
sess.run(iterator.initializer, feed_dict={fname:'test.tfr'})
sess.run(nxt)
# output of run(nxt) would look like
# {'day': array([b'Sat', b'Thu', b'Fri', b'Thu'], dtype=object), 'season': array([b'Winter', b'Winter', b'Fall', b'Summer'], dtype=object)}
Run Code Online (Sandbox Code Playgroud)
However, if I wanted to use feature_columns to one hot encode those categoricals as a Dataset transformation using map, then it runs once producing correct output, but on every subsequent call to run(nxt) it gives the Tables already initialized error, eg:
# using the same Dataset ds from above
season_enc = tf.feature_column.categorical_column_with_vocabulary_list(
key='season', vocabulary_list=vlists['season'])
season_col = tf.feature_column.indicator_column(season_enc)
day_enc = tf.feature_column.categorical_column_with_vocabulary_list(
key='day', vocabulary_list=vlists['day'])
day_col = tf.feature_column.indicator_column(day_enc)
cols = [season_col, day_col]
def _encode(element, feat_cols=cols):
return tf.feature_column.input_layer(element, feat_cols)
ds1 = ds.map(_encode)
iterator = ds1.make_initializable_iterator()
nxt = iterator.get_next()
sess.run(tf.tables_initializer())
sess.run(iterator.initializer, feed_dict={fname:'test.tfr'})
sess.run(nxt)
# first run will produce correct one hot encoded output
sess.run(nxt)
# second run will generate
W tensorflow/core/framework/op_kernel.cc:1192] Failed precondition: Table
already initialized.
2018-01-25 19:29:55.802358: W tensorflow/core/framework/op_kernel.cc:1192]
Failed precondition: Table already initialized.
2018-01-25 19:29:55.802612: W tensorflow/core/framework/op_kernel.cc:1192]
Failed precondition: Table already initialized.
Run Code Online (Sandbox Code Playgroud)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Table already initialized.
但是,如果我尝试在不使用feature_columns的情况下手动进行一种热编码,如下所示,则仅当在map函数之前创建表时,该方法才有效,否则会产生与上面相同的错误
# using same original Dataset ds
tables = dict(season=index_table_from_tensor(vlists['season']),
day=index_table_from_tensor(vlists['day']))
def to_dummy(element):
s = tables['season'].lookup(element['season'])
d = tables['day'].lookup(element['day'])
return (tf.one_hot(s, depth=len(vlists['season']), axis=-1),
tf.one_hot(d, depth=len(vlists['day']), axis=-1))
ds2 = ds.map(to_dummy)
iterator = ds2.make_initializable_iterator()
nxt = iterator.get_next()
sess.run(tf.tables_initializer())
sess.run(iterator.initializer, feed_dict={fname:'test.tfr'})
sess.run(nxt)
Run Code Online (Sandbox Code Playgroud)
似乎与feature_columns创建的索引查找表的范围或名称空间有关,但是我不确定如何弄清楚这里发生了什么,我试图更改在何处以及何时定义feature_column对象,但并没有改变。
我刚刚通过最近的另一个问题来到这里,想提出一个可能的解决方案。由于这个问题已经很晚了,我不确定这里的问题是否已经解决。如果已经有好的解决方案请指正。
我真的不知道这个错误是如何发生的。但是从预设的估计器中学习,我意识到可能有一种替代方法来完成这项工作,即在解析示例之前迭代数据集。此方法的一个好处是将特征列映射与映射函数到数据集分开。这可能与此处未知的错误原因有关,因为已知:
当在 tf.data.Dataset.map 函数的“tensorflow.python.ops.gen_lookup_ops”中使用 hash_table 时,由于 tf.data.Dataset.map 不使用默认图,因此无法初始化 hash_table。
我不确定这是否符合您真正想要的,但在代码中使用“test.tfr”生成的潜在示例可能是:
import tensorflow as tf
# using the same Dataset ds from above
vlists = dict(season=['Spring', 'Summer', 'Fall', 'Winter'],
day=['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])
season_enc = tf.feature_column.categorical_column_with_vocabulary_list(
key='season', vocabulary_list=vlists['season'])
season_col = tf.feature_column.indicator_column(season_enc)
day_enc = tf.feature_column.categorical_column_with_vocabulary_list(
key='day', vocabulary_list=vlists['day'])
day_col = tf.feature_column.indicator_column(day_enc)
cols = [season_col, day_col]
def _encode(element, feat_cols=cols):
element = tf.parse_example(element, features=tf.feature_column.make_parse_example_spec(feat_cols))
return tf.feature_column.input_layer(element, feat_cols)
fname = tf.placeholder(tf.string, [])
ds = tf.data.TFRecordDataset(fname)
ds = ds.batch(4)
ds1 = ds#.map(_encode)
iterator = ds1.make_initializable_iterator()
nxt = iterator.get_next()
nxt = _encode(nxt)
with tf.Session() as sess:
sess.run(tf.tables_initializer())
sess.run(iterator.initializer, feed_dict={fname:'test.tfr'})
print(sess.run(nxt))
# first run will produce correct one hot encoded output
print(sess.run(nxt))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
905 次 |
| 最近记录: |