Skip to content

Fix convert to tf - #907

Merged
thomwolf merged 3 commits into
huggingface:masterfrom
dhpollack:fix_convert_to_tf
Aug 5, 2019
Merged

thomwolf merged 3 commits into
huggingface:masterfrom
dhpollack:fix_convert_to_tf

Conversation

@dhpollack

@dhpollack dhpollack commented Jul 26, 2019 •

Copy link
Copy Markdown
Contributor

I struggled with this same problem for a long time. The naive assign op way, puts all of the weights into both the checkpoint file (.ckpt.data-XXXXX-of-YYYYY) and the meta file (.ckpt.meta). This is because assign adds an operation to the graph. So basically, you have two instructions in your meta file, one that initializes the variable with random values and then another that assigns the pytorch values to these tensors. But really, you want to initialize everything once with the meta file and then read the data file which should have your pytorch weights in it. Tensorflow hides this functionality deep within it's source code and every answer on stackoverflow tells one to use assign. But the tf.keras.backend.set_value function does simply replace the weights of a variable. However, this function makes some assumptions about your session and your graph so I had to change your code a bit. Long story short, doing easy things in tensorflow is hard.

So what's the difference?

  1. the meta file will be about 1mb instead of 400+ mb
  2. the script runs in about 10 seconds instead of 3 minutes

@codecov-io

Copy link
Copy Markdown

Codecov Report

Merging #907 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #907   +/-   ##
=======================================
  Coverage   79.03%   79.03%           
=======================================
  Files          34       34           
  Lines        6234     6234           
=======================================
  Hits         4927     4927           
  Misses       1307     1307

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 46cc9dd...09ecf22. Read the comment docs.

@thomwolf
thomwolf merged commit 0e91870 into huggingface:master Aug 5, 2019
@thomwolf

thomwolf commented Aug 5, 2019

Copy link
Copy Markdown
Member

Ok thanks David!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants