Surgery of TensorFlow (v1.10) graphs

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 a and out.

Now, we want to reload the graph but we would like a and 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_a and new_b with the desired shapes and multiply them together with an operation that we call matmul_new.

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:

input_map={"matmul:0": new_m}

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!

And voilà.

 Graphics adapted from pngtree.com

Advertisements

3 thoughts on “Surgery of TensorFlow (v1.10) graphs

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: