seb
seb
MModular
Created by seb on 10/23/2023 in #questions
Populating values of tensor
Currently working on creating a one-hot encoding input tensor by initializing the tensor with zeros, and then setting individual cells of the 2D tensor to 1. However, trying to set the value with tensor[i][j] = 1 is setting whole rows of the tensor at a time; how can I do this?
2 replies
MModular
Created by seb on 10/22/2023 in #questions
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)
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?
8 replies