harvitronix / neural-network-genetic-algorithm

Evolving a neural network with a genetic algorithm.
https://medium.com/@harvitronix/lets-evolve-a-neural-network-with-a-genetic-algorithm-code-included-8809bece164
MIT License
693 stars 238 forks source link

'list' object has no attribute 'train' #8

Open nilu33032 opened 5 years ago

nilu33032 commented 5 years ago

nice code, but I seem to be running into problem when going through the 2nd generation (1st gen works fine), the train_network will throw an error: 'list' object has no attribute ' train'.

so basically -----

def train_network(networks): for network in networks: network.train() <---- 'list' has no attribute 'train' error. pbar.update(1)

btw the database I have hardcoded it in the train, so it's not here

xWuWux commented 10 months ago

The error message "'list' object has no attribute 'train'" suggests that the variable networksis a list, and the code is trying to call the trainmethod on this list. However, the intention is likely to iterate over individual network objects within the list and call the trainmethod on each of them.

Code Analysis: Here, the networksparameter is expected to be a list of network objects. The code iterates through each element in the list (for network in networks) and attempts to call the trainmethod on each element.

Probable Cause: The error may occur if, during the second generation, the list networks is not containing instances of the Networkclass as expected but is instead a list of lists or other data structures.

Suggestions: Ensure Consistency in networks:

Check the code that generates the networks list, especially during the second generation. Verify that networksis a list of Networkinstances, not a list of lists or other data types. Logging for Debugging:

Introduce logging statements in the code to print the type of each element in networksduring the second generation. This can help identify what type of objects are present in the list. def train_network(networks): for network in networks: print(f"Type of network: {type(network)}") network.train() pbar.update(1)

Error Handling:

Implement error handling to gracefully handle cases where the elements in networksare not of the expected type. For example, useif isinstance(network, Network): to check if an element is an instance of the Networkclass before calling the train method. def train_network(networks): for network in networks: if isinstance(network, Network): network.train() pbar.update(1) else: print(f"Skipping non-Network element: {network}") By addressing these suggestions, you can identify and resolve the issue causing the "'list' object has no attribute 'train'" error during the second generation of training.