more comments for network.py
@@ -174,11 +174,13 @@ class Network(Layer):
raise DLPyError('cannot build model, there seems to be a problem.')
self.num_params = 0
+ # before addLayer, self.layers are reordered based on layer_id
+ # the new id is created based on model.summary which contains layer_id.
+ # since it is not compiled, it might contains None or index from it original model summary.
orig_ids = self.summary['Layer Id']
offset_id = 0
- new_ids = []
- max_id = -1
- pool = []
+ new_ids = [] # store new ids
+ pool = [] # pool to store index has been added
for idx, l_id in enumerate(orig_ids):
# encounter None id or duplicated id, start a new Model
if l_id is None or l_id in pool:
@@ -187,18 +189,18 @@ class Network(Layer):
else:
pool.append(l_id)
+ # calculate new index for the layer
if l_id is None:
- max_id = -1
l_id = offset_id
else:
- max_id = max(max_id, l_id)
l_id += offset_id
new_ids.append(l_id)
+ # reassign layer_id
for i, l in enumerate(self.layers):
l.layer_id = new_ids[i]
- self.layers.sort()
+ self.layers.sort() # sort based on layer_id
for layer in self.layers:
option = layer.to_model_params()
@@ -288,22 +290,7 @@ class Network(Layer):
output_tensors.append(outbound_layer.tensor)
break
- func_model = dlpy.model.Model(self.conn, input_tensors, output_tensors)
- # stop_layers will remove some layers from copied_model.layers
- # f_layer_names = [f_l.name for f_l in func_model.layers] #subset
- # c_layer_names = [c_l.name for c_l in copied_model.layers] #set
- #
- # for c_name in c_layer_names:
- # if c_name not in f_layer_names:
- # copied_model.layers.remove(copied_model.layers[c_layer_names.index(c_name)])
- #
- # # traverse the model by to_functional_model(): layer name is used to find depth
- # for l in copied_model.layers:
- # idx = f_layer_names.index(l.name)
- # l.depth = func_model.layers[idx].depth
- # func_model.layers = copied_model.layers
-
- return func_model
+ return dlpy.model.Model(self.conn, input_tensors, output_tensors)
def __deepcopy__(self, memo):
cls = self.__class__
