Convolution in nengo

I wanted to perform convolution using Nengo networks.
I requested ChatGPT to provide me with a code, but there is an issue with the code (explained below).
I encountered a “ValueError: cannot reshape array of size 0 into shape (10,10)” error related to the line “matrix_values = x[9:].reshape((10, 10))” in the convolution function.

what i need to do to fix this problem?

Thankls

import numpy as np
import nengo

# Step 2: Define the filter K and matrix M
K = np.random.rand(3, 3)  # Example random filter
M = np.random.rand(10, 10)  # Example random matrix

with nengo.Network() as model:  # Step 3: Create a Nengo Network
    # Step 4: Create ensembles to represent the filter and the matrix
    filter_ens = nengo.Ensemble(n_neurons=100, dimensions=3*3)  # Adjust the number of neurons as needed
    matrix_ens = nengo.Ensemble(n_neurons=100, dimensions=10*10)  # Adjust the number of neurons as needed

    # Step 6: Define input nodes to provide the filter and matrix as input to the ensembles
    filter_input = nengo.Node(output=K.flatten())
    matrix_input = nengo.Node(output=M.flatten())

    # Step 7: Connect the input nodes to the ensembles
    nengo.Connection(filter_input, filter_ens)
    nengo.Connection(matrix_input, matrix_ens)

    # Step 8: Create an ensemble to represent the output of the convolution
    output_ens = nengo.Ensemble(n_neurons=100, dimensions=8*8)  # Adjust the number of neurons as needed

    # Step 9: Define a function to compute the convolution
    def convolution(x):
        filter_values = x[:9].reshape((3, 3))
        matrix_values = x[9:].reshape((10, 10))
        return np.convolve(matrix_values.flatten(), filter_values.flatten(), mode='valid')

    # Step 10: Connect the ensembles to compute the convolution
    nengo.Connection(filter_ens, output_ens, function=convolution)
    nengo.Connection(matrix_ens, output_ens)

    # Step 11: Add a probe to collect the output activity
    output_probe = nengo.Probe(output_ens, synapse=0.1)  # Adjust the synapse time constant as needed

# Step 12: Run the simulation and collect the results
with nengo.Simulator(model) as sim:
    sim.run(1.0)  # Run the simulation for 1 second

output_data = sim.data[output_probe]

print(output_data)  # Print the output data

Hi DBM. Welcome to the forum!

In general, I would not recommend programming with ChatGPT. It is not a substitute for having an understanding of what is actually going on. We’ve got lots of examples in the documentation, which are a good place to start learning about Nengo.

In your specific example, the problem is that you can’t compute a function that performs multiplication between elements in different ensembles. So you’ll need to create one or more ensembles that each contain both values being multiplied. See the following examples:

In general, though, this is not a good approach to convolution, since it will likely require a large number of neurons even to get decent results on relatively small images. Depending on your application, a better approach might be to train a convolutional network that can then be converted to an SNN: