[Python] Efficient algorithm for connection between neurons

Discussão em 'Python' iniciado por Stack, Setembro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm experimenting with a new concept of a connected neural network. Each neuron is a function with inputs and an output. Connections between compatible types can be allowed, i.e., a neuron with an int output can connect to another with an int input.

    Currently, I have a "connect" function with a given depth that establishes compatible connections between neurons. Then, I have another "associate" function that searches for possible connections based on a given value. See working demo with test_ai4.py.

    I now want to create a new "learn" function that combines efficiently connections and association by graph searching for connections compatible with the weights of the neurons and connections. An A* algorithm might do the trick, but I'm having trouble figuring out how to implement it because it involves multiple branches to a terminal neuron. The ultimate goal is to obtain a list of possible connections (the best will be transformed into new neuron). I have already implemented an A* algorithm but not with these constraints. Do you have any suggestions?

    The code is available here on GitHub.

    Here is the code demonstration to use "learn" function (to code):

    from brain import Brain
    from connection import Connection
    import ndarrays
    from neuron import Neuron
    import numpy as np

    brain = Brain()
    neuronIds = {}

    w = np.array((-2, -1, 0))
    x = np.array((1, 2, 3))
    y = np.array((4, 5, 6))
    z = np.array((7, 8, 9))

    neuronIds |= ndarrays.add(brain)

    brain.deactivate_all_modules()
    brain.activate_module("ndarrays.operators.arithmetic")

    neuronIds |= ndarrays.add_value(brain, lambda: w, "w")
    neuronIds |= ndarrays.add_value(brain, lambda: x, "x")
    neuronIds |= ndarrays.add_value(brain, lambda: y, "y")
    neuronIds |= ndarrays.add_value(brain, lambda: z, "z")

    #connections = brain.learn(x * y + z, "mul_add", 2)
    connections = brain.learn(w * x + y * z, "mul_add", 3)

    print(brain.connection_str(connections[0]) + " -> " + str(brain.connection_output(connections[0])))
    print(connections[0].origin_input_types())

    try:
    brain.show2d(seed = 0, levelColors = ["red", "green", "blue", "yellow", "cyan", "magenta", "purple", "brown", "orange", "gold", "indigo", "black", "white"])
    except:
    pass

    #brain.show3d(seed = 0, levelColors = ["red", "green", "blue", "yellow", "cyan", "magenta", "purple", "brown", "orange", "gold", "indigo", "black", "white"])

    brain.neurons[neuronIds["x"]].function = lambda: int(input("w? "))
    brain.neurons[neuronIds["x"]].function = lambda: int(input("x? "))
    brain.neurons[neuronIds["y"]].function = lambda: int(input("y? "))
    brain.neurons[neuronIds["z"]].function = lambda: int(input("z? "))

    print(brain.connection_output(connections[0]))

    Continue reading...

Compartilhe esta Página