1 # global unique layer ID dictionary for layer name assignment
 2 _LAYER_UIDS = {}
 3 
 4 def get_layer_uid(layer_name=''):
 5     """Helper function, assigns unique layer IDs."""
 6     if layer_name not in _LAYER_UIDS:
 7         _LAYER_UIDS[layer_name] = 1
 8         return 1
 9     else:
10         _LAYER_UIDS[layer_name] += 1
11         return _LAYER_UIDS[layer_name]

 

 

这里_LAYER_UIDS = {} 是记录layer及其出现次数的字典。

在 get_layer_uid()函数中,若layer_name从未出现过,如今出现了,则将_LAYER_UIDS[layer_name]设为1;否则累加。

作用: 在class Layer中,当未赋variable scope的name时,通过实例化Layer的次数来标定不同的layer_id.

例子:简化一下class Layer可以看出:

 1 class Layer():
 2     def __init__(self):
 3         layer = self.__class__.__name__
 4         name = layer + '_' + str(get_layer_uid(layer))
 5         print(name) 
 6 
 7 layer1 = Layer()
 8 layer2 = Layer()
 9 
10 # Output:
11 # Layer_1
12 # Layer_2
View Code

相关文章:

  • 2021-11-24
  • 2022-12-23
  • 2021-04-08
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
  • 2021-07-07
  • 2021-10-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-15
  • 2021-07-18
相关资源
相似解决方案