Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to statically declare a Tensor?

I started exploring the mojo programming language and now I am trying to figure out how to properly use the Tensor module. I can't find how to statically declare the values inside a tensor.

For the moment, this is what I do to fill a tensor with values:

let dim1 = 2
let dim2 = 3

var matrice1 = Tensor[DType.float32](dim1, dim2)
for i in range(dim1):
    for j in range(dim2):
        matrice1[Index(i,j)] = 1

But I am looking for a way to do something like this:

var matrice1 = Tensor[DType.float32](dim1, dim2)
matrice1 = [[1, 2, 3],[1, 2, 3]]
like image 454
Blue_Mecha Avatar asked Aug 09 '26 08:08

Blue_Mecha


1 Answers

You can initialize a Tensor from a literal like this:

let dim1 = 2
let dim2 = 3

let tensor = Tensor[DType.float32](
    TensorShape(dim1, dim2),
    1, 2, 3,
    1, 2, 3
)

print(tensor)
like image 174
ephemer Avatar answered Aug 13 '26 21:08

ephemer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!