[Python] How can I compile a method to run on a GPU using numba

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am developing a multilayer perceptron, and I have the following method inside of the class MultiLayerPerceptron, and I would like to configure this method to run on a GPU, I would like to do this using numba, using the @cuda.jit decorator, but I am not sure how to specify the kernel configuration. Also it it neccesary for me to have NVIDIA cuda api installed?

    def backprop(self, x, y):
    nabla_b = [np.zeros(b.shape) for b in self.biases]
    nabla_w = [np.zeros(w.shape) for w in self.weights]
    activation = x
    activations = [x] # list to store all the activations, layer by layer
    zs = [] # list to store all the z vectors, layer by layer
    for b, w in zip(self.biases, self.weights):
    z = np.dot(w, activation)+b
    zs.append(z)
    activation = self.apply_activation(z)
    activations.append(activation)
    delta = self.cost_function_derivative(activations[-1], y) *self.sigmoid_function_prime2(zs[-1])
    nabla_b[-1] = delta
    nabla_w[-1] = np.dot(delta, activations[-2].transpose())
    for l in range(2, self.num_layers):
    z = zs[-l]
    sp = self.apply_activation_derivative(z)
    delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
    nabla_b[-l] = delta
    nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
    return (nabla_b, nabla_w)

    Continue reading...

Compartilhe esta Página