This short post shows how to replace some operations in a TensorFlow graph that has been reloaded from a .pb file. This is actually quite simple to do, but all the tutorials that I found when I needed to do this were for older versions of TensorFlow, so it took me way longer than it should have.
First of all, let’s create a toy graph and save it to a file.
This graph includes a matrix of shape (2, 4) which gets multiplied by another matrix of shape (4, 3), so that the resulting matrix has shape (2, 3). This is then added to another matrix of shape (2, 3) to give the final result.
Then, the graph is saved and one has to specify what are the input and output tensors of the graph. In this case they are
Now, we want to reload the graph but we would like
b to be matrices of shape (2, 6) and (6, 3) instead of (4, 3) and (4, 3).
So we create two new tensors
new_b with the desired shapes and multiply them together with an operation that we call
Then, inside the session we reload the graph that we previously saved. They key part is to add this argument when the graph is reloaded:
which replaces the old
matmul operation with the new one. Now, when we run the tensor with name
out:0 graph we need to feed a matrix of shape (2,6) instead of (4,6), because the placeholder has been changed!
Graphics adapted from pngtree.com