Skip to main content

module

tensor

Implements the Tensor type.

Example:

from tensor import Tensor, TensorSpec, TensorShape, rand
from utils.index import Index

var height = 256
var width = 256
var channels = 3

# Create the tensor of dimensions height, width, channels
# and fill with random values.
var image = rand[DType.float32](height, width, channels)

# Declare the grayscale image.
var spec = TensorSpec(DType.float32, height, width)
var gray_scale_image = Tensor[DType.float32](spec)

# Perform the RGB to grayscale transform.
for y in range(height):
for x in range(width):
var r = image[y,x,0]
var g = image[y,x,1]
var b = image[y,x,2]
gray_scale_image[Index(y,x)] = 0.299 * r + 0.587 * g + 0.114 * b

print(gray_scale_image.shape())

Structs

  • Tensor: A tensor type which owns its underlying data and is parameterized on DType.