Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add utility functions to NX #616

Merged
merged 5 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions nx/lib/nx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9167,6 +9167,70 @@ defmodule Nx do
end
end

@doc """
Finds the variance of a tensor.

The variance is the average of the squared deviations from the mean.
The mean is typically calculated as sum(x) / N, where N = total of elements.
If, however, ddof is specified, the divisor N - ddof is used instead.
josevalim marked this conversation as resolved.
Show resolved Hide resolved

## Examples

iex> Nx.variance(Nx.tensor([[1, 2], [3, 4]]))
#Nx.Tensor<
f32
1.25
>

iex> Nx.variance(Nx.tensor([[1, 2], [3, 4]]), ddof: 1)
#Nx.Tensor<
f32
1.6666666269302368
>
"""
@doc type: :aggregation
@spec variance(tensor :: Nx.Tensor.t(), opts :: Keyword.t()) :: Nx.Tensor.t()
def variance(tensor, opts \\ []) do
%T{shape: shape} = tensor = to_tensor(tensor)
josevalim marked this conversation as resolved.
Show resolved Hide resolved

total = Tuple.product(shape)
ddof = Keyword.get(opts, :ddof, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ddof = Keyword.get(opts, :ddof, 0)
ddof = Keyword.fetch!(opts, :ddof)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

mean = mean(tensor)

tensor
|> subtract(mean)
|> power(2)
|> sum()
|> divide(total - ddof)
end

@doc """
Finds the standard deviation of a tensor.

if ddof is specified, the divisor N - ddof is used to calculate the variance.
josevalim marked this conversation as resolved.
Show resolved Hide resolved

## Examples

iex> Nx.standard_deviation(Nx.tensor([[1, 2], [3, 4]]))
#Nx.Tensor<
f32
1.1180340051651
>

iex> Nx.standard_deviation(Nx.tensor([[1, 2], [3, 4]]), ddof: 1)
#Nx.Tensor<
f32
1.29099440574646
>
"""
@doc type: :aggregation
@spec standard_deviation(tensor :: Nx.Tensor.t(), ddof :: Keyword.t()) :: Nx.Tensor.t()
josevalim marked this conversation as resolved.
Show resolved Hide resolved
def standard_deviation(tensor, opts \\ []) do
tensor = to_tensor(tensor)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tensor = to_tensor(tensor)

In this specific case we can just delegate to variance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

sqrt(variance(tensor, opts))
end

## Sigils

@doc """
Expand Down
24 changes: 24 additions & 0 deletions nx/test/nx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1920,4 +1920,28 @@ defmodule NxTest do
|> elem(0)
end
end

describe "variance/1" do
test "should calculate the variance of a tensor" do
t = Nx.tensor([[4, 5], [2, 3], [1, 0]])
assert Nx.variance(t) == Nx.tensor(2.9166667461395264)
end

test "should use the optional ddof" do
t = Nx.tensor([[4, 5], [2, 3], [1, 0]])
assert Nx.variance(t, ddof: 1) == Nx.tensor(3.5)
end
end

describe "standard_deviation/1" do
test "should calculate the standard deviation of a tensor" do
t = Nx.tensor([[4, 5], [2, 3], [1, 0]])
assert Nx.standard_deviation(t) == Nx.tensor(1.707825127659933)
end

test "should use the optional ddof" do
t = Nx.tensor([[4, 5], [2, 3], [1, 0]])
assert Nx.standard_deviation(t, ddof: 1) == Nx.tensor(1.8708287477493286)
end
end
end