【发布时间】:2018-08-23 03:08:08
【问题描述】:
我想绕过 Estimator 框架,直接在会话中使用 tensorflow 特征列和特征。我读了tensorflow's low level introduction on feature column。问题是tf.feature_column.input_layer 在构造时需要features 提要,但训练和预测时间的特征提要不同。查看tf.Estimator代码,似乎方法是再次调用相同的构造回调函数来获取图形。我想出了下面的例子,但是如果我在第二次构造之后跳过表初始化,它会在未初始化的表上失败;或者如果我运行表初始化,它会抱怨表已经初始化。根据their research paper,这是设计使然,因为他们总是希望从保存点重新加载新模型。但这对于像强化学习这样我们想要在训练循环中同时进行更新和推理的情况来说效率很低。目前还不清楚他们想如何进行开发验证。
构建用于预测的图形和提要特征的正确方法是什么?
training_features = {
'sales' : [[5], [10], [8], [9]],
'department': ['sports', 'sports', 'gardening', 'gardening']}
test_features = {
'sales' : [[10], [20], [16], [18]],
'department': ['sports', 'sports', 'gardening', 'gardening']}
department_column = tf.feature_column.categorical_column_with_vocabulary_list(
'department', ['sports', 'gardening'])
department_column = tf.feature_column.indicator_column(department_column)
columns = [
tf.feature_column.numeric_column('sales'),
department_column
]
# similar to a tf.Estimator's model_fn callback
def mkgraph(features):
with tf.variable_scope('feature_test', reuse=tf.AUTO_REUSE):
inputs = tf.feature_column.input_layer(features, columns)
alpha = tf.placeholder(tf.float32, name='alpha')
output = inputs * alpha
return output, alpha
with tf.Graph().as_default() as g:
output, alpha = mkgraph(training_features)
print('output', output)
print('alpha', alpha)
var_init = tf.global_variables_initializer()
table_init = tf.tables_initializer()
with tf.Session(graph=g) as sess:
sess.run([var_init, table_init])
print(sess.run(output, feed_dict={alpha: 100.0})) # works here
print('testing')
output, alpha = mkgraph(test_features)
print('output', output)
print('alpha', alpha)
table_init = tf.tables_initializer()
# sess.run([table_init]) # with this, it fails on 'table already initialized'
# without table_init run, it fails on 'table not initialized'
print(sess.run(output, feed_dict={alpha: 200.0}))
【问题讨论】:
-
嗨,我遇到了与
feature_column类似的困境。这个问题你解决了吗?添加一个在训练和测试之间切换的开关会有帮助吗? -
对于开发验证部分,我认为训练循环已经做了交叉验证,所以我们不需要在外面做。但是对于 RL 类情况下迭代馈送训练数据和推理数据的一般问题没有答案
-
我想我的例子比你用 RL 做的更简单。但总的来说,我需要在训练集和测试集之间切换,使用
feature_column。添加一个开关对我有帮助,但我不确定这是否是最好的方法。希望我下面的回答可以帮助到你。
标签: tensorflow tensorflow-estimator