在下面的代码中,我将目标附加到Chunk_Obj变量“forprop_list”。当我在第111行迭代for循环时,变量“forprop_list”似乎记住了先前在该循环中追加的值。 我觉得这是一个简单的修复,但我一直无法看到它。
这与将对象分配给变量有关吗?是什么导致循环继续追加而不是使用新的空“forprop_list”?
import os, pprint
import Data_Obj, ChunkObj
# Takes an index from the txt files for parameters. Takes a data index for the data input.
class Network:
data_obj = None
net_index = int
data_index = int
hparams = []
# Both of the following use the same referenced object:
# Holds all the chunk objects by POSITION
nn_chunks_dict = {}
# Holds all the chunk objects by LAYER
nn_layers_dict = {}
# Generates the neural network configuration and chunks required for the current network.
def __init__(self, nn_index=1, data_index=1):
self.net_index = nn_index
self.data_index = data_index
self.data_obj = Data_Obj.DataObj(data_index)
# Reads from txt file.
# Instantiates the chunks, adds those to the dictionaries.
self.configure_from_txt()
print("\n", "The ./NetConfigs txt file was successfully read to 'nn_coords_dict'. \n")
# Iterates the layers in the nn_layers_dict.
# Generates forward propagation dictionaries via prop_connect.
# i = ex. layer1, layer2, layer3...
print("nn_chunks_dict: ",self.nn_chunks_dict, "\n")
print("nn_chunks dict keys", self.nn_chunks_dict.keys())
for i in self.nn_chunks_dict:
print("i", i)
self.chunk_prop(i)
print("\nThis network is initialized!")
# Reads from the index txt file.
# Instatiates each chunk from those parameters and adds to the dictionary.
def configure_from_txt(self):
param_txt = open(os.path.join('./NetConfigs/%s.txt' % self.net_index)).readlines()
# Extracts hyperparameters.
for i in param_txt[0].strip('\n').split(';'):
self.hparams.append(i.split(','))
# Removes the hyperparameters and '\n' for configuring the network.
del param_txt[0:2]
# Extracts the position of the chunk from the first 2
# indexes of the attribs to a string list.
for i in param_txt:
attribs_targs = i.strip('\n').split(':')
attribs = attribs_targs[0].split(',')
position = [str(attribs[0]), str(attribs[1])]
if len(attribs_targs) > 1:
temp_targets = attribs_targs[1]
temp_targets = temp_targets.split(';')
targets = []
for i in temp_targets:
targets.append(i)
else:
targets = None
temp_config_dict = {"activation": attribs[2],
"num_layers": int(attribs[3]),
"width": int(attribs[4]),
"targets": targets,
"position": position}
temp_obj = ChunkObj.ChunkObj(self, temp_config_dict)
print("TEMP OBJECT KEY NAME:", temp_obj.key_name)
# Assigns the current iterative chunk attributes(temp_config_dict) to the POSITION dictionary.
self.nn_chunks_dict[temp_obj.key_name] = temp_obj
# Assigns the chunk obj to a list of the layers.
if ("layer%s" % position[0]) in self.nn_layers_dict:
print("LAYER: layer%s" % position[0])
self.nn_layers_dict["layer%s" % position[0]].append(temp_obj)
else:
self.nn_layers_dict["layer%s" % position[0]] = [temp_obj]
print("LAYER: layer%s" % position[0])
print("\n This networks configuration dictionary: \n")
pprint.pprint(self.nn_chunks_dict)
pprint.pprint(self.nn_layers_dict)
def chunk_prop(self, chunk_name):
x = self.nn_chunks_dict[chunk_name]
# else, targets are the next layer chunks
if x.targets is None:
# self.placehold_obj.forprop_list = []
print("\nchunk: ", x.key_name, "\nTargets are NOT specified")
print("Sequential layer: ", str(int(x.position[0]) + 1))
if x.backprop:
backpropvar = True
# if a next layer exists...
next_layer = str(int(x.position[0]) + 1)
if ("layer%s" % next_layer) in self.nn_layers_dict:
for i in self.nn_layers_dict["layer%s" % next_layer]:
x.forprop_list.append(str(i.key_name))
# for later use:
"""
if backpropvar:
i.backprop_list.append(self.placehold_obj.key_name)
print("Passed self to target backprop.")
"""
# else, the chunk is the last layer, forward prop will go to "y".
else:
# Checks to see if this chunk is in the last layer (will require Y for dimensions).
print("Next layer does not exist. Last layer == True")
x.last_layer = True
x.forprop_list = ["y"]
# if targets are specified...
else:
x.forprop_list = x.targets
print("self.placeholder.key_name: ", x.key_name)
print("Targets ARE specified: ", x.forprop_list)
print("forprop list: ", x.forprop_list)
def test():
Network()
print("\n", "Network_Obj test complete!")
if __name__ == '__main__':
test()