現在我有一個模型配置爲使用feed_dict
輸入它的輸入。代碼看起來像這樣:在feed_dict和隊列之間輕鬆切換以輸入到TensorFlow模型
# model.py
class MyModel(object):
def __init__(self, hyperparams):
self.build_model(hyperparams)
def build_model(self, hps):
self.input_data = tf.placeholder(dtype=tf.float32, shape=[hps.batch_size, hps.nfeats])
self.labels = tf.placeholder(dtype=tf.float32, shape=[hps.batch_size])
# Define hidden layers, loss, training step, etc.
# train.py
model = MyModel(hps)
for _ in range(100):
x, y = some_python_function() # Read a batch from disk, preprocess
sess.run(model.train_step, feed_dict={model.input_data: x, model.labels: y})
由於性能原因,我想切換到使用隊列進行培訓。但我想保持使用feed_dict
的能力,例如進行推理或測試。
有沒有一個優雅的方式來做到這一點?我想要做的是,在使用隊列時,將我的隊列的出隊操作返回的張量替換爲佔位符變量。我認爲tf.assign
會做到這一點的方式,即:
single_x, single_y = tf.parse_single_example(...)
x, y = tf.train.batch([single_x, single_y], batch_size)
model = MyModel(hps)
sess.run([tf.assign(model.input_data, x), tf.assign(model.labels, y)])
for _ in range(100):
sess.run(model.train_step)
但是這引起了AttributeError: 'Tensor' object has no attribute 'assign'
。 tf.assign
的API文檔將第一個參數描述爲:「可變的Tensor
。應該來自Variable
節點,可能未初始化。」這是否意味着我的佔位符不可變?我可以讓他們如此嗎?還是我以錯誤的方式接近這個?
最小可運行示例here。