Skip to content

Instantly share code, notes, and snippets.

@jeregrine
Created May 1, 2023 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeregrine/83764b9b44d4377094d07d20f5f66ee7 to your computer and use it in GitHub Desktop.
Save jeregrine/83764b9b44d4377094d07d20f5f66ee7 to your computer and use it in GitHub Desktop.

What is a Tensor

Mix.install([
  {:nx, "~> 0.5.3"}
])

Section

Nx.tensor(42)
Nx.tensor(42)
|> Nx.add(10)
Nx.tensor(42)
|> Nx.add(10.5)
Nx.tensor([1, 2, 3, 42, 5])
Nx.shape(Nx.tensor([1, 2, 3, 42, 5]))
Nx.tensor([1, 2, 3, 42, 5])
|> Nx.add(10)
Nx.tensor([1, 2, 3, 42, 5])
|> Nx.mean()
# Random key seed
k = Nx.Random.key(1)
# roll 1000 dice with a shape of 1x1000
{dice_rolls, _} = Nx.Random.randint(k, 1, 6, shape: {1000})

# 2.96
Nx.divide(Nx.sum(dice_rolls), 1000)
# or
# 2.96
Nx.mean(dice_rolls)

# {1000} vector dice_roll*dice_roll
Nx.multiply(dice_rolls, dice_rolls)
# 10.85
|> Nx.mean()
Nx.tensor([
  [1, 2, 3, 4, 5],
  [12, 22, 32, 42, 52]
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment