i'm not able to understand this line in chapter 9 jupyter notebook in Saving and Restoring Model Section:
theta = tf.get_default_graph().get_tensor_by_name("theta:0")
can you explain it .What is get_tensor_by_name used for ? i tried reading official page but didn't understood completely.I think get_tensor_by_name is used to get a tensor but then what is ":0" in it ?
Hi @nitml,
A TensorFlow graph contains operations and tensors. The operations represent the computations, and the tensors represent their inputs and outputs. The name of an operation can be whatever you want, for example:
import tensorflow as tf
a = tf.add(1.0, 2.0, name="addition")
When you create an operation, TensorFlow also creates its output tensors and it returns them. So for example, in the code above, the addition operation is created, and its output tensor is returned. So a is the output tensor, not the operation itself.
Tensors have the same name as the operation that outputs them, plus an output index. The first output tensor has a :0 suffix. The second output tensor (for operations that have more than one output) has a :1 suffix, and so on. For example, look at the tensor a defined above:
>>> a
<tf.Tensor 'addition:0' shape=() dtype=float32>
>>> a.name
'addition:0'
So the code just gets the default graph, and finds the tensor named theta:0. This represents the value (i.e., the "output") of the theta variable.
Hope this helps.
Great explanation....
I have one more doubt ,in capture 9 jupyter notebook ,why file_writer.flush() was used only in Name Scope Section and not in training Curve Visualization Section ? Why was it used ?
In last part of chapter 9 to avoid defining threshold outside relu() function we used :
for relu_index in range(5):
with tf.variable_scope("relu", reuse=(relu_index >= 1)) as scope :
relus.append(relu(X))
output = tf.add_n(relus, name="output")
What does as scope means ? when should we use it ?
Thanks for your time !!!
When you read or write from a file, there is usually a buffer. When writing to a file, calling the flush() method ensures that all the buffer is written to disk immediately (or else this might happen later, for example when the buffer is full, or after some delay). When you call close(), it should call flush() before closing the file, so normally there is no reason to call flush() just before calling close(), it is redundant. Perhaps I ran into a bug where there was missing data in the events file and I tried several hypotheses, including that the buffer was not flushed properly. Anyway, I'm not sure why I put a flush() there, I think you can safely remove it (but it does not hurt).
Regarding the as scope, you can also remove it since I don't seem to use it in the with block or after it. Using the as keyword is useful when you need to give an object a name so you can handle it later on. For example:
with open("test", "w") as f:
f.write("test")
Hope this helps,
Aur茅lien
Most helpful comment
Hi @nitml,
A TensorFlow graph contains operations and tensors. The operations represent the computations, and the tensors represent their inputs and outputs. The name of an operation can be whatever you want, for example:
When you create an operation, TensorFlow also creates its output tensors and it returns them. So for example, in the code above, the
additionoperation is created, and its output tensor is returned. Soais the output tensor, not the operation itself.Tensors have the same name as the operation that outputs them, plus an output index. The first output tensor has a
:0suffix. The second output tensor (for operations that have more than one output) has a:1suffix, and so on. For example, look at the tensoradefined above:So the code just gets the default graph, and finds the tensor named
theta:0. This represents the value (i.e., the "output") of thethetavariable.Hope this helps.