Search code examples
pythonmachine-learningkerasdeep-learningneural-network

How do I get weights and biases from each iteration of my model?


In my neural network, I need to get weights and biases from the model in each iteration. for example I run 5 epochs - the weights and biases that I get at the end are the weights and biases for the 5th epoch but I want to get the weights and biases of all epoch not only the last one. Here is my code that gives weights of last epoch:

model.layers[0].get_weights()[0]
model.layers[0].get_weights()[1]

Solution

  • You can loop over epochs and save the weights and biases of each epoch in a list:

    weights = []
    biases = []
    for epoch in range(num_epochs):
       
        # training for each epoch
        model.fit(x_train, y_train, ...) 
    
        # add the weights and biases of the current epoch to the lists
        weights.append(model.layers[0].get_weights()[0])
        biases.append(model.layers[0].get_weights()[1])