Comments (4)
After some more time searching the internet, I found out how to do this. We need to append the code :0 to the name of the op to get its associated tensor. I don't know why, but here's how to do it:
bar = tf.get_default_graph().get_tensor_by_name('network/layer_conv1/Relu:0')
print(bar)
Which gives the following output:
Tensor("network/layer_conv1/Relu:0", shape=(?, 24, 24, 64), dtype=float32)
And we can now run the session to get the output of the convolutional layer as follows:
baz = session.run(bar, feed_dict={x: images_test[0:10, :, :, :]})
print(baz.shape)
Which outputs:
(10, 24, 24, 64)
And this represents:
[input_image, height, width, output_channel]
This took a long time to figure out and the solution is not obvious at all. One must know low-level details of both Pretty Tensor and TensorFlow to figure out how to do this. Please consider these things both when designing the API's and when documenting them.
from prettytensor.
Unfortunately, these graphs have many, many nodes in them. The namespace can help and the result of the activation would be prefix/layer_name/relu
so you can find it that way as a one-off. I've outlined a few more choices below.
The easiest way is to assign the values back to a variable and do something with it, of course this ends up introducing a lot of line noise:
x_pretty = x_pretty.conv2d(...)
do_something_with_image(x_pretty)
x_pretty = x_pretty.max_pool()
I would find that solution to be less than ideal, so I will lay out a couple of other ones:
with pt.defaults_scope(activation_fn=tf.nn.relu):
seq = x_pretty.sequential() # Now each call changes seq.
seq.conv2d(kernel=5, depth=64, name='layer_conv1')
do_something(seq.as_layer()) # as_layer takes a snapshot
seq.max_pool(kernel=2, stride=2)
seq.conv2d(kernel=5, depth=64, name='layer_conv2')
do_something(seq.as_layer()) # as_layer takes a snapshot
seq.max_pool(kernel=2, stride=2).flatten()
seq.fully_connected(size=256, name='layer_fc1')
do_something_else(seq.as_layer())
seq.fully_connected(size=128, name='layer_fc2')
do_something_else(seq.as_layer())
y_pred, loss = seq.as_layer().softmax_classifier(class_count=10, labels=y_true)
You could also use the callback _method_complete
on any Pretty Tensor object. It is called at the end of each method call in order to support side-effectful execution (sequential) or the standard execution; the type coming in can be anything Tensor like, so you'd have to call super for it to be wrapped as a PT.
If you feel like this would be a useful general abstraction to allow a user-defined callback, then I welcome the contribution. It would probably be a good way to standardize summaries as well :)
from prettytensor.
Thanks for the quick answer!
The reason I like Pretty Tensor is the elegant syntax when using the chained-mode of constructing the network, so I don't want to ruin that.
I think it would be OK for my project if I just get the output of the layers using their names. However, when I try the following (note that I have actually enclosed the above code in the namespace 'network' in my own code):
bar = tf.get_default_graph().get_tensor_by_name('network/layer_conv1/Relu')
print(bar)
I get this error:
ValueError: The name 'network/layer_conv1/Relu' refers to an Operation, not a Tensor. Tensor names must be of the form "<op_name>:<output_index>".
If instead I have:
bar = tf.get_default_graph().get_operation_by_name('network/layer_conv1/Relu')
print(bar)
I get the following output:
name: "network/layer_conv1/Relu"
op: "Relu"
input: "network/layer_conv1/add"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
I then try and execute this in the TensorFlow session to get the output of the convolutional layer:
baz = session.run(bar, feed_dict={x: images_test[0:10, :, :, :]})
print(baz)
But I just get None
as the result.
How should I do this? What is the reason?
Thanks again.
from prettytensor.
For people who come across this with searching, the OP's tutorials are really helpful:
https://github.com/Hvass-Labs/TensorFlow-Tutorials
from prettytensor.
Related Issues (20)
- How to do regularization? HOT 3
- AttributeError: type object 'GraphKeys' has no attribute 'GLOBAL_VARIABLES' HOT 3
- AttributeError: module 'tensorflow' has no attribute 'complex_abs' HOT 3
- TypeError: Expected int32, got <prettytensor.pretty_tensor_class.Layer > of type 'Layer' instead. HOT 16
- weights reuse in {gru, lstm}_cell/_sequence methods HOT 2
- Bug with template, phase=Unbound and batch_normalize=True
- loading from checkpoint
- conv2d 'kernel'
- run baby_names.py error HOT 1
- PrettyTensor's template.construct() yielding ValueError HOT 2
- TypeError: slice indices must be integers or None or have an __index__ method
- using tensorboard with prettytensor
- manipulating output layer of tensorflow during learning
- how to use the dropout and l2loss ?
- Prettytensor not working with TF1.8 HOT 4
- With Tensorflow 0.12, it is throwing "TypeError: zeros_initializer() takes at least 1 argument (0 given)".
- How can i use inspect_checkpoint tool to print all the weight ?
- AttributeError: 'VariableScope' object has no attribute 'current_scope HOT 2
- AttributeError: module 'tensorflow' has no attribute 'GraphKeys'. In python while importing tflearn. HOT 2
- c++
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prettytensor.