7

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]]

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Note that Tensor is now considered deprecated in Mojo and will be removed – at least from the standard library – soon (see youtu.be/uIG9q9foIw0?si=Me9Ll6BczscHh6fa&t=1947)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.