How To Correctly Implement Lazy Loading In Tensorflow?
The following code (while trying to replicate code structure in https://danijar.com/structuring-your-tensorflow-models/ ) import tensorflow as tf class Model: def __init__(s
Solution 1:
The problem is that the call to sess.run(tf.global_variables_initializer())
happens before the variables are created, in the first call to model.output
on the following line.
To fix the problem, you must somehow access model.output
before calling sess.run(tf.global_variables_initializer())
. For example, the following code works:
import tensorflow as tf
classModel:
def__init__(self, x):
self.x = x
self._output = None @propertydefoutput(self):
# NOTE: You must use `if self._output is None` when `self._output` can# be a tensor, because `if self._output` on a tensor object will raise# an exception.if self._output isNone:
weight = tf.Variable(tf.constant(4.0))
bias = tf.Variable(tf.constant(2.0))
self._output = tf.multiply(self.x, weight) + bias
return self._output
defmain():
x = tf.placeholder(tf.float32)
model = Model(x)
# The variables are created on this line.
output_t = model.output
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
output = sess.run(output_t, {x: 4.0})
print(output)
if __name__ == '__main__':
main()
Post a Comment for "How To Correctly Implement Lazy Loading In Tensorflow?"