How to create a random tensor with a given shape in pytorch lite (Java)?

Viewed 136

I want to create a random tensor with a shape (BxHxWxC) to test my ptl model. How can I create it?

I have seen the documentation here: https://pytorch.org/javadoc/1.4.0/org/pytorch/Tensor.html

I see fromBlob but not any method to create a random tensor with a specific shape which is pretty normal in pytorch to test a model.

1 Answers

This one works for initializing a tensor with all zeros.

FloatBuffer buffer = Tensor.allocateFloatBuffer(1*224*224*3);
Tensor tensor = Tensor.fromBlob(buffer, new long[]{1, 224, 224, 3});
Related