Executing objects in a TensorFlow graph using their names
Executing variables and operators by their names is very useful in many scenarios. For example, we may develop a model in a separate module; and thus the variables are not available in a different Python scope according to Python scoping rules. However, if we have a graph, we can execute the nodes of the graph using their names in the graph.
This can be done easily by changing the sess.run
method from the previous code example, using the variable name of the cost in the graph rather than the Python variable cost
by changing sess.run([cost, train_op], ...)
to sess.run(['cost:0', 'train_op'], ...)
.
>>> n_epochs = 500 >>> training_costs = [] >>> with tf.Session(graph=g) as sess: ... ## first, run the variables initializer ... sess.run(tf.global_variables_initializer()) ... ... ## train the model for n_eopchs ... for e in range(n_epochs): ... c, _ = sess.run(['cost:0', 'train_op']...