I have an object type, Layer, that I need to put into an array. The issue is that it takes in two ints, so Nim won't let me have Layers with different input values in the same array.
type
Layer*[One, Two: static int] = object
...
var layers = [new Layer[4, 5], new Layer[3,3], new Layer[7, 10]]
Is there any way to accomplish this? Any advice is welcomed. Thanks!
No this is not possible. An array can only contain elements of the same type. But Layer[4, 5] and Layer[3, 3] are different types.
If One and Two are fields instead of generic type parameters it will work, since they will all have the same type:
type
Layer* = object
one, two: int
...
var layers = [Layer(one: 4, two: 5), Layer(one: 3, two: 3), Layer(one: 7, two: 10)]
whats the reason you used generic type parameters?I am creating a simple toy neural network. The layer object will hold the weights and biases for each layer.
Is there a way that I can hold these Layers in a list? I need to be able to iterate over them, so it doesn't have to be in an array per say. (By the way, I am very new to Nim)
ok, that doesn't sound like you need One and Two to be part of the type. You realy only need that if you need that information at compile-time.
So just define one and two as normal object fields (as shown in my first post) and you will have no problem.
I would suggest you to read the tuple and object types section in the manual: https://nim-lang.org/docs/manual.html#types-tuples-and-object-types
or the Nim by example page about object types: https://nim-by-example.github.io/types/objects/
I am creating a simple toy neural network. The layer object will hold the weights and biases for each layer.
Is there a way that I can hold these Layers in a list? I need to be able to iterate over them, so it doesn't have to be in an array per say. (By the way, I am very new to Nim)
type
Layer*[NumIn, NumOut: static int] = object
weights: Matrix[NumOut, NumIn, float]
biases: Vector[NumOut, float]
var layers = [new Layer[4, 5], new Layer[3,3]] #For example
The previous post was a version of this code to simplify what I wanted.
ah ok that makes sense, then you should use a tuple and the fields iterator as mentioned by @tsojtsoj.
type
Layer*[NumIn, NumOut: static int] = object
weights: Matrix[NumOut, NumIn, float]
biases: Vector[NumOut, float]
var layers = (new Layer[4, 5], new Layer[3,3])
for layer in layers.fields:
# do stuff ..
In general, the more type constraints you put on neural network, the more painful it is to deserialize them when you save/restore the weights.
You can't just read the file and figure out the model, you need to the end-user to actual provide the exact types, including in and out from that file.
If I were to redo Arraymancer with neural network focus (and not just Numpy), I would type-erase everything into a "Tensor" with cases for each float/integer/string supported so that saving restoring models is the simplest possible.