Skip to content

Commit

Permalink
RNN -> RadiusNN
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 10, 2024
1 parent 4dec8ba commit 017e29b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 62 deletions.
4 changes: 2 additions & 2 deletions lib/scholar/cluster/dbscan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ defmodule Scholar.Cluster.DBSCAN do
y_dummy = Nx.broadcast(Nx.tensor(0), {num_samples})

neighbor_model =
Scholar.Neighbors.RNNClassifier.fit(x, y_dummy,
Scholar.Neighbors.RadiusNNClassifier.fit(x, y_dummy,
num_classes: 1,
radius: opts[:eps],
metric: opts[:metric]
)

{_dist, indices} =
Scholar.Neighbors.RNNClassifier.radius_neighbors(neighbor_model, x)
Scholar.Neighbors.RadiusNNClassifier.radius_neighbors(neighbor_model, x)

n_neighbors = Nx.sum(indices * weights, axes: [1])
core_samples = n_neighbors >= opts[:min_samples]
Expand Down
18 changes: 9 additions & 9 deletions lib/scholar/neighbors/rnn_classifier.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Scholar.Neighbors.RNNClassifier do
defmodule Scholar.Neighbors.RadiusNNClassifier do
@moduledoc """
The Radius Nearest Neighbors.
Expand Down Expand Up @@ -83,8 +83,8 @@ defmodule Scholar.Neighbors.RNNClassifier do
iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> Scholar.Neighbors.RNNClassifier.fit(x, y, num_classes: 2)
%Scholar.Neighbors.RNNClassifier{
iex> Scholar.Neighbors.RadiusNNClassifier.fit(x, y, num_classes: 2)
%Scholar.Neighbors.RadiusNNClassifier{
data: Nx.tensor([
[1, 2],
[2, 4],
Expand Down Expand Up @@ -143,8 +143,8 @@ defmodule Scholar.Neighbors.RNNClassifier do
iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> model = Scholar.Neighbors.RNNClassifier.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.RNNClassifier.predict(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
iex> model = Scholar.Neighbors.RadiusNNClassifier.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.RadiusNNClassifier.predict(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
#Nx.Tensor<
s64[2]
[0, 1]
Expand All @@ -169,8 +169,8 @@ defmodule Scholar.Neighbors.RNNClassifier do
iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> model = Scholar.Neighbors.RNNClassifier.fit(x, y, num_classes: 2)
iex> {probs, mask} = Scholar.Neighbors.RNNClassifier.predict_probability(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
iex> model = Scholar.Neighbors.RadiusNNClassifier.fit(x, y, num_classes: 2)
iex> {probs, mask} = Scholar.Neighbors.RadiusNNClassifier.predict_probability(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
iex> probs
#Nx.Tensor<
f32[2][2]
Expand Down Expand Up @@ -230,8 +230,8 @@ defmodule Scholar.Neighbors.RNNClassifier do
iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> model = Scholar.Neighbors.RNNClassifier.fit(x, y, num_classes: 2)
iex> {distances, mask} = Scholar.Neighbors.RNNClassifier.radius_neighbors(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
iex> model = Scholar.Neighbors.RadiusNNClassifier.fit(x, y, num_classes: 2)
iex> {distances, mask} = Scholar.Neighbors.RadiusNNClassifier.radius_neighbors(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
iex> distances
#Nx.Tensor<
f32[2][4]
Expand Down
14 changes: 7 additions & 7 deletions lib/scholar/neighbors/rnn_regressor.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Scholar.Neighbors.RNNRegressor do
defmodule Scholar.Neighbors.RadiusNNRegressor do
@moduledoc """
The Radius Nearest Neighbors.
Expand Down Expand Up @@ -80,8 +80,8 @@ defmodule Scholar.Neighbors.RNNRegressor do
iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> Scholar.Neighbors.RNNRegressor.fit(x, y, num_classes: 2)
%Scholar.Neighbors.RNNRegressor{
iex> Scholar.Neighbors.RadiusNNRegressor.fit(x, y, num_classes: 2)
%Scholar.Neighbors.RadiusNNRegressor{
data: Nx.tensor(
[
[1, 2],
Expand Down Expand Up @@ -142,8 +142,8 @@ defmodule Scholar.Neighbors.RNNRegressor do
iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> model = Scholar.Neighbors.RNNRegressor.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.RNNRegressor.predict(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
iex> model = Scholar.Neighbors.RadiusNNRegressor.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.RadiusNNRegressor.predict(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
#Nx.Tensor<
f32[2]
[0.5, 1.0]
Expand Down Expand Up @@ -196,8 +196,8 @@ defmodule Scholar.Neighbors.RNNRegressor do
iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> model = Scholar.Neighbors.RNNRegressor.fit(x, y, num_classes: 2)
iex> {distances, mask} = Scholar.Neighbors.RNNRegressor.radius_neighbors(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
iex> model = Scholar.Neighbors.RadiusNNRegressor.fit(x, y, num_classes: 2)
iex> {distances, mask} = Scholar.Neighbors.RadiusNNRegressor.radius_neighbors(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
iex> distances
#Nx.Tensor<
f32[2][4]
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ defmodule Scholar.MixProject do
Scholar.Neighbors.KNNRegressor,
Scholar.Neighbors.LargeVis,
Scholar.Neighbors.NNDescent,
Scholar.Neighbors.RNNClassifier,
Scholar.Neighbors.RNNRegressor,
Scholar.Neighbors.RadiusNNClassifier,
Scholar.Neighbors.RadiusNNRegressor,
Scholar.Neighbors.RandomProjectionForest
],
Utilities: [
Expand Down
60 changes: 30 additions & 30 deletions test/scholar/neighbors/rnn_classifier_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Scholar.Neighbors.RNNClassifierTest do
defmodule Scholar.Neighbors.RadiusNNClassifierTest do
use Scholar.Case, async: true
alias Scholar.Neighbors.RNNClassifier
doctest RNNClassifier
alias Scholar.Neighbors.RadiusNNClassifier
doctest RadiusNNClassifier

defp x do
Nx.tensor([
Expand All @@ -28,7 +28,7 @@ defmodule Scholar.Neighbors.RNNClassifierTest do

describe "fit" do
test "fit with default parameters - :num_classes set to 2" do
model = RNNClassifier.fit(x(), y(), num_classes: 2)
model = RadiusNNClassifier.fit(x(), y(), num_classes: 2)

assert model.weights == :uniform
assert model.num_classes == 2
Expand All @@ -39,51 +39,51 @@ defmodule Scholar.Neighbors.RNNClassifierTest do

describe "predict" do
test "predict with default values" do
model = RNNClassifier.fit(x(), y(), num_classes: 2)
predictions = RNNClassifier.predict(model, x_pred())
model = RadiusNNClassifier.fit(x(), y(), num_classes: 2)
predictions = RadiusNNClassifier.predict(model, x_pred())
assert predictions == Nx.tensor([-1, -1, -1, -1])
end

test "predict with radius set to 10" do
model = RNNClassifier.fit(x(), y(), num_classes: 2, radius: 10)
predictions = RNNClassifier.predict(model, x_pred())
model = RadiusNNClassifier.fit(x(), y(), num_classes: 2, radius: 10)
predictions = RadiusNNClassifier.predict(model, x_pred())
assert predictions == Nx.tensor([1, 1, 1, 1])
end

test "predict with weights set to :distance - classification task" do
model = RNNClassifier.fit(x(), y(), num_classes: 2, radius: 10, weights: :distance)
model = RadiusNNClassifier.fit(x(), y(), num_classes: 2, radius: 10, weights: :distance)

predictions = RNNClassifier.predict(model, x_pred())
predictions = RadiusNNClassifier.predict(model, x_pred())
assert predictions == Nx.tensor([1, 1, 1, 1])
end

test "predict with weights set to :distance and with specific metric" do
model =
RNNClassifier.fit(x(), y(),
RadiusNNClassifier.fit(x(), y(),
num_classes: 2,
radius: 10,
weights: :distance,
metric: {:minkowski, 1.5}
)

predictions = RNNClassifier.predict(model, x_pred())
predictions = RadiusNNClassifier.predict(model, x_pred())
assert predictions == Nx.tensor([1, 1, 1, 1])
end

test "predict with weights set to :distance and with x_pred that contains sample with zero-distance" do
x_pred = Nx.tensor([[3, 6, 7, 5], [1, 6, 1, 1], [3, 7, 9, 2], [5, 2, 1, 2]])

model = RNNClassifier.fit(x(), y(), num_classes: 2, radius: 10, weights: :distance)
model = RadiusNNClassifier.fit(x(), y(), num_classes: 2, radius: 10, weights: :distance)

predictions = RNNClassifier.predict(model, x_pred)
predictions = RadiusNNClassifier.predict(model, x_pred)
assert predictions == Nx.tensor([0, 1, 1, 1])
end
end

describe "predict_proba" do
test "predict_proba with default values except radius set to 10" do
model = RNNClassifier.fit(x(), y(), num_classes: 2, radius: 10)
{predictions, outliers_mask} = RNNClassifier.predict_probability(model, x_pred())
model = RadiusNNClassifier.fit(x(), y(), num_classes: 2, radius: 10)
{predictions, outliers_mask} = RadiusNNClassifier.predict_probability(model, x_pred())

assert_all_close(
predictions,
Expand All @@ -94,9 +94,9 @@ defmodule Scholar.Neighbors.RNNClassifierTest do
end

test "predict_proba with weights set to :distance" do
model = RNNClassifier.fit(x(), y(), num_classes: 2, radius: 10, weights: :distance)
model = RadiusNNClassifier.fit(x(), y(), num_classes: 2, radius: 10, weights: :distance)

{predictions, outliers_mask} = RNNClassifier.predict_probability(model, x_pred())
{predictions, outliers_mask} = RadiusNNClassifier.predict_probability(model, x_pred())

assert_all_close(
predictions,
Expand All @@ -113,14 +113,14 @@ defmodule Scholar.Neighbors.RNNClassifierTest do

test "predict_proba with weights set to :distance and with specific metric" do
model =
RNNClassifier.fit(x(), y(),
RadiusNNClassifier.fit(x(), y(),
num_classes: 2,
radius: 10,
weights: :distance,
metric: {:minkowski, 1.5}
)

{predictions, outliers_mask} = RNNClassifier.predict_probability(model, x_pred())
{predictions, outliers_mask} = RadiusNNClassifier.predict_probability(model, x_pred())

assert_all_close(
predictions,
Expand All @@ -138,9 +138,9 @@ defmodule Scholar.Neighbors.RNNClassifierTest do
test "predict_proba with weights set to :distance and with x_pred that contains sample with zero-distance" do
x_pred = Nx.tensor([[3, 6, 7, 5], [1, 6, 1, 1], [3, 7, 9, 2], [5, 2, 1, 2]])

model = RNNClassifier.fit(x(), y(), num_classes: 2, radius: 10, weights: :distance)
model = RadiusNNClassifier.fit(x(), y(), num_classes: 2, radius: 10, weights: :distance)

{predictions, outliers_mask} = RNNClassifier.predict_probability(model, x_pred)
{predictions, outliers_mask} = RadiusNNClassifier.predict_probability(model, x_pred)

assert_all_close(
predictions,
Expand All @@ -158,8 +158,8 @@ defmodule Scholar.Neighbors.RNNClassifierTest do

describe "radius_neighbors" do
test "radius_neighbors with default values except radius set to 10" do
model = RNNClassifier.fit(x(), y(), num_classes: 2, radius: 10)
{distances, indices} = RNNClassifier.radius_neighbors(model, x_pred())
model = RadiusNNClassifier.fit(x(), y(), num_classes: 2, radius: 10)
{distances, indices} = RadiusNNClassifier.radius_neighbors(model, x_pred())

assert_all_close(
distances,
Expand Down Expand Up @@ -229,13 +229,13 @@ defmodule Scholar.Neighbors.RNNClassifierTest do

test "radius_neighbors with specific metric" do
model =
RNNClassifier.fit(x(), y(),
RadiusNNClassifier.fit(x(), y(),
num_classes: 2,
radius: 10,
metric: {:minkowski, 1.5}
)

{distances, indices} = RNNClassifier.radius_neighbors(model, x_pred())
{distances, indices} = RadiusNNClassifier.radius_neighbors(model, x_pred())

assert_all_close(
distances,
Expand Down Expand Up @@ -313,7 +313,7 @@ defmodule Scholar.Neighbors.RNNClassifierTest do
"expected input tensor to have shape {n_samples, n_features} or {num_samples, num_samples},
got tensor with shape: {5}",
fn ->
RNNClassifier.fit(x, y, num_classes: 5)
RadiusNNClassifier.fit(x, y, num_classes: 5)
end
end

Expand All @@ -325,7 +325,7 @@ defmodule Scholar.Neighbors.RNNClassifierTest do
"expected labels to have shape {num_samples} or {num_samples, num_outputs},
got tensor with shape: {1, 1, 5}",
fn ->
RNNClassifier.fit(x, y, num_classes: 5)
RadiusNNClassifier.fit(x, y, num_classes: 5)
end
end

Expand All @@ -337,7 +337,7 @@ defmodule Scholar.Neighbors.RNNClassifierTest do
"expected labels to have the same size of the first axis as data,
got: 6 != 5",
fn ->
RNNClassifier.fit(x, y, num_classes: 5)
RadiusNNClassifier.fit(x, y, num_classes: 5)
end
end

Expand All @@ -348,7 +348,7 @@ defmodule Scholar.Neighbors.RNNClassifierTest do
assert_raise NimbleOptions.ValidationError,
"required :num_classes option not found, received options: []",
fn ->
RNNClassifier.fit(x, y)
RadiusNNClassifier.fit(x, y)
end
end
end
Expand Down
24 changes: 12 additions & 12 deletions test/scholar/neighbors/rnn_regressor_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Scholar.Neighbors.RNNRegressorTest do
defmodule Scholar.Neighbors.RadiusNNRegressorTest do
use Scholar.Case, async: true
alias Scholar.Neighbors.RNNRegressor
doctest RNNRegressor
alias Scholar.Neighbors.RadiusNNRegressor
doctest RadiusNNRegressor

defp x do
Nx.tensor([
Expand Down Expand Up @@ -29,26 +29,26 @@ defmodule Scholar.Neighbors.RNNRegressorTest do
describe "predict" do
test "predict with weights set to :distance" do
model =
RNNRegressor.fit(x(), y(),
RadiusNNRegressor.fit(x(), y(),
num_classes: 2,
radius: 10,
weights: :distance
)

predictions = RNNRegressor.predict(model, x_pred())
predictions = RadiusNNRegressor.predict(model, x_pred())
assert_all_close(predictions, Nx.tensor([0.69033845, 0.71773642, 0.68217609, 0.75918273]))
end

test "predict with weights set to :distance and with specific metric" do
model =
RNNRegressor.fit(x(), y(),
RadiusNNRegressor.fit(x(), y(),
num_classes: 2,
radius: 10,
weights: :distance,
metric: :cosine
)

predictions = RNNRegressor.predict(model, x_pred())
predictions = RadiusNNRegressor.predict(model, x_pred())
assert_all_close(predictions, Nx.tensor([0.683947, 0.54694187, 0.59806132, 0.86398641]))
end

Expand All @@ -57,14 +57,14 @@ defmodule Scholar.Neighbors.RNNRegressorTest do
Nx.tensor([[1, 4], [0, 3], [2, 5], [0, 3], [0, 3], [1, 4], [2, 5], [0, 3], [1, 4], [2, 5]])

model =
RNNRegressor.fit(x(), y,
RadiusNNRegressor.fit(x(), y,
num_classes: 3,
radius: 10,
weights: :distance,
metric: :cosine
)

predictions = RNNRegressor.predict(model, x_pred())
predictions = RadiusNNRegressor.predict(model, x_pred())

assert_all_close(
predictions,
Expand All @@ -87,7 +87,7 @@ defmodule Scholar.Neighbors.RNNRegressorTest do
"expected input tensor to have shape {n_samples, n_features} or {num_samples, num_samples},
got tensor with shape: {5}",
fn ->
RNNRegressor.fit(x, y, num_classes: 5)
RadiusNNRegressor.fit(x, y, num_classes: 5)
end
end

Expand All @@ -99,7 +99,7 @@ defmodule Scholar.Neighbors.RNNRegressorTest do
"expected labels to have shape {num_samples} or {num_samples, num_outputs},
got tensor with shape: {1, 1, 5}",
fn ->
RNNRegressor.fit(x, y, num_classes: 5)
RadiusNNRegressor.fit(x, y, num_classes: 5)
end
end

Expand All @@ -111,7 +111,7 @@ defmodule Scholar.Neighbors.RNNRegressorTest do
"expected labels to have the same size of the first axis as data,
got: 6 != 5",
fn ->
RNNRegressor.fit(x, y, num_classes: 5)
RadiusNNRegressor.fit(x, y, num_classes: 5)
end
end
end
Expand Down

0 comments on commit 017e29b

Please sign in to comment.