ModularM
Modular3y ago
7 replies
seb

List of memory-only structs

I am trying to create a struct for a neural network layer with memory-only list of neurons:
struct Neuron:
    '''Implementation of a neuron.'''
    var weights: Tensor[DType.float64]
    var bias: Float64
    var nin: Int

    fn __init__(inout self, nin: Int):
        self.weights = rand[DType.float64](nin)
        self.bias = random_float64(-1.0, 1.0)
        self.nin = nin
    
    fn __call__(self, x: Tensor[DType.float64]) raises -> Float64:
        if x.num_elements() != self.nin:
            raise Error('Input values do not match number of weights')
        
        var sum: Float64 = self.bias
        for i in range(self.nin):
            sum += x[i] * self.weights[i]
        return math.tanh(sum)

But I'm struggling to compose these into a list of any sort, whether with a heap array, vector, or variadic list, getting this error: candidate not viable: method argument #1 cannot bind generic !mlirtype to memory-only type 'Neuron'. Is there a way to compose structs in this way?
Was this page helpful?