1

I have a function

init_from_checkpoint(
ckpt_dir_or_file,
assignment_map)

where assignment_map supports following syntax:

'checkpoint_scope_name/': 'scope_name/' - will load all variables in current scope_name from checkpoint_scope_name with matching tensor names.

For example,

init_from_checkpoint('/tmp/model.ckpt',
                     {'old_scope_1/var1': 'new_scope_1/var1',
                      'old_scope_1/var2': 'new_scope_1/var2'})

Right now, I have two list

old_scope_1_list=[old_scope_1/var1, old_scope_1/var2, ...,old_scope_1/var100]
new_scope_1_list=[new_scope_1/var1, new_scope_1/var2, ...,new_scope_1/var100]

How could I call the function init_from_checkpoint using old_scope_1_list and new_scope_1_list to make the calling function effectively in python? My current solution is that write 100 lines as bellow without using the two lists, but it looks ineffective way

init_from_checkpoint('/tmp/model.ckpt',
                         {'old_scope_1/var1': 'new_scope_1/var1',
                          'old_scope_1/var2': 'new_scope_1/var2',
                          ...
                          'old_scope_1/var100': 'new_scope_1/var100'})
Jame
  • 3,746
  • 6
  • 52
  • 101
  • 1
    Possible duplicate of [Map two lists into a dictionary in Python](https://stackoverflow.com/questions/209840/map-two-lists-into-a-dictionary-in-python) – Mel Dec 20 '17 at 13:33
  • You could also just have a list of the variable names and just string concatenate each one of those with the old/new scope to get what you need so you only have one place to change stuff. – Jeff B Dec 20 '17 at 13:54

1 Answers1

4

You can use zip function to make list of pairs from old_scope_1_list and new_scope_1_list. And then use dict on that pairs to create mapping you need.

In [67]: old_scope_1_list=['old_scope_1/var1', 
'old_scope_1/var2','old_scope_1/var100']
...: new_scope_1_list=['new_scope_1/var1', 
'new_scope_1/var2','new_scope_1/var100']


In [68]: zip(old_scope_1_list, new_scope_1_list)
Out[68]: <zip at 0x7f4dd084c748>

In [69]: x = zip(old_scope_1_list, new_scope_1_list)

In [70]: dict(x)
Out[70]: 
{'old_scope_1/var1': 'new_scope_1/var1',
'old_scope_1/var100': 'new_scope_1/var100',
'old_scope_1/var2': 'new_scope_1/var2'}

So in your case the code would be:

init_from_checkpoint('/tmp/model.ckpt', dict(zip(old_scope_1_list, new_scope_1_list)))
running.t
  • 5,329
  • 3
  • 32
  • 50