Tilak Gupta
Tilak Gupta

Reputation: 1

ValueError: operands could not be broadcast together with shapes (0,) (5,4385)

I am working on PSO optimised LSTM for prediction of wind speed. I think i have built the code correctly but i am getting this error in PSO optimisation. The issue seems to be in the fitness function, particularly in the broadcasting of layer weights. Please help me in solving this issue. i have attached the images of code output and error msgs from the jupyter notebook.

# Fitness Function

def getFitness(params):
\# Check if params is empty
if len(params) == 0:
print("Empty params array received")
return np.inf

    layer_1 = modelGA.get_layer('layer_1').get_weights()
    layer_2 = modelGA.get_layer('layer_2').get_weights()
    
    # Calculate the sizes of weights and biases
    layer1_W1_size = layer_1[0].size
    layer1_W2_size = layer_1[1].size
    layer1_B_size = layer_1[2].size
    layer2_W_size = layer_2[0].size
    layer2_B_size = layer_2[1].size
    
    # Ensure correct parameter sizes
    expected_size = layer1_W1_size + layer1_W2_size + layer1_B_size + layer2_W_size + layer2_B_size
    if len(params) != expected_size:
        print(f"Mismatch in params size. Expected: {expected_size}, Actual: {len(params)}")
        return np.inf  # Return infinity as fitness if size is incorrect
    
    offset = 0
    
    # Reshape and assign weights and biases from params
    layer1_W1 = params[offset:offset + layer1_W1_size].reshape(layer_1[0].shape)
    offset += layer1_W1_size
    layer1_W2 = params[offset:offset + layer1_W2_size].reshape(layer_1[1].shape)
    offset += layer1_W2_size
    layer1_B = params[offset:offset + layer1_B_size].reshape(layer_1[2].shape)
    offset += layer1_B_size
    layer2_W = params[offset:offset + layer2_W_size].reshape(layer_2[0].shape)
    offset += layer2_W_size
    layer2_B = params[offset:offset + layer2_B_size].reshape(layer_2[1].shape)
    
    modelGA.get_layer('layer_1').set_weights([layer1_W1, layer1_W2, layer1_B])
    modelGA.get_layer('layer_2').set_weights([layer2_W, layer2_B])
    
    predY = modelGA.predict(trainX)
    loss = rmse(trainY.reshape(-1), predY.reshape(-1))
    
    # Check for NaN loss
    if np.isnan(loss):
        print("NaN loss encountered with parameters:")
        print(params)
    
    return loss

# Computed loss for all particles

def f(params):
print('Number of particles: %d' %params.shape\[0\])
losses = np.array(\[getFitness(params\[i\]) for i in range(params.shape\[0\])\])
print('List of losses for all particles')
print(losses)
return losses

# Pyswarms algorithm

import numpy as np
import pyswarms as ps

# Define the options for PSO, including inertia coefficient 'w'

options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9}

# Get the weights of the layers from the model

layer_1 = modelGA.get_layer('layer_1').get_weights()
layer_2 = modelGA.get_layer('layer_2').get_weights()

# Calculate the total number of parameters (weights and biases) in the neural network

layer1_W1_size = layer_1\[0\].size
layer1_W2_size = layer_1\[1\].size
layer1_B_size = layer_1\[2\].size
layer2_W_size = layer_2\[0\].size
layer2_B_size = layer_2\[1\].size

dimensions = layer1_W1_size + layer1_W2_size + layer1_B_size + layer2_W_size + layer2_B_size

# Print the total number of parameters

print("Number of params in Neural Network: %d" % dimensions)

def f(params):
print('Number of particles: %d' % params.shape\[0\])
losses = np.array(\[getFitness(params\[i\]) for i in range(params.shape\[0\])\])
print('List of losses for all particles:')
print(losses)
return losses

# Initialize the PSO optimizer

optimizer = ps.single.GlobalBestPSO(n_particles=5, dimensions=dimensions, options=options)

# Print initial swarm position

print("Initial swarm position:", optimizer.swarm.position)

# Optimize the neural network parameters using the PSO algorithm

cost, pos = optimizer.optimize(f, iters=50)
print("Optimized parameters:", pos)

enter image description here

enter image description here

Upvotes: 0

Views: 32

Answers (0)

Related Questions