Skip to content

Instantly share code, notes, and snippets.

@nickgnd
Created March 6, 2022 20:58
Show Gist options
  • Save nickgnd/16b441cf818f20180b2d2afafd7c4db0 to your computer and use it in GitHub Desktop.
Save nickgnd/16b441cf818f20180b2d2afafd7c4db0 to your computer and use it in GitHub Desktop.
<!-- livebook:{"persist_outputs":true} -->
# Random Normal - issue with defn
## Section
```elixir
Mix.install([
{:exla, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "exla"},
{:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true}
])
Nx.Defn.global_default_options(compiler: EXLA)
defmodule RandomNormalTest do
import Nx.Defn
defn create_with_defn(shape) do
Nx.random_normal(shape, 0.0, 1.0)
end
def create(shape) do
Nx.random_normal(shape, 0.0, 1.0)
end
end
```
<!-- livebook:{"output":true} -->
```
{:module, RandomNormalTest, <<70, 79, 82, 49, 0, 0, 9, ...>>, {:create, 1}}
```
```elixir
RandomNormalTest.create({10, 4})
```
<!-- livebook:{"output":true} -->
```
#Nx.Tensor<
f32[10][4]
[
[0.36994102597236633, 0.07317788153886795, 0.49161645770072937, 2.0069003105163574],
[-2.2546796798706055, -1.9848204851150513, -0.7559281587600708, -0.0988297164440155],
[-0.2042686492204666, -0.4669297933578491, -0.4406200051307678, 0.25528794527053833],
[-1.9771993160247803, -0.07872360944747925, 0.18106433749198914, -0.016550522297620773],
[0.8465139865875244, -0.9714727401733398, -0.23460082709789276, -1.1613898277282715],
[-1.0772348642349243, -0.058248281478881836, -1.6772676706314087, 0.6900256872177124],
[-1.0431689023971558, -0.7773556113243103, -0.7354031205177307, 0.24952226877212524],
[-0.04504086822271347, 0.3102203905582428, 0.8773104548454285, -0.26838353276252747],
[1.846353530883789, -1.1995649337768555, 1.1605740785598755, -0.8619170784950256],
[0.9801735281944275, -0.8678684234619141, -0.7064700126647949, -1.3400367498397827]
]
>
```
```elixir
# It works when the shape is given via a tensor
RandomNormalTest.create_with_defn(Nx.tensor([[0, 1], [2, 4]]))
```
<!-- livebook:{"output":true} -->
```
#Nx.Tensor<
f32[2][2]
[
[0.49698734283447266, -0.4807703495025635],
[0.6912095546722412, 0.10434946417808533]
]
>
```
```elixir
# It throws an error when the shape is set via a tuple
RandomNormalTest.create_with_defn({10, 4})
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment