From 017e29b0254dab754985611a5207a8a265bc4ded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 10 Sep 2024 17:06:00 +0200 Subject: [PATCH] RNN -> RadiusNN --- lib/scholar/cluster/dbscan.ex | 4 +- lib/scholar/neighbors/rnn_classifier.ex | 18 +++--- lib/scholar/neighbors/rnn_regressor.ex | 14 ++--- mix.exs | 4 +- .../scholar/neighbors/rnn_classifier_test.exs | 60 +++++++++---------- test/scholar/neighbors/rnn_regressor_test.exs | 24 ++++---- 6 files changed, 62 insertions(+), 62 deletions(-) diff --git a/lib/scholar/cluster/dbscan.ex b/lib/scholar/cluster/dbscan.ex index 1a76f1d1..bcda4de4 100644 --- a/lib/scholar/cluster/dbscan.ex +++ b/lib/scholar/cluster/dbscan.ex @@ -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] diff --git a/lib/scholar/neighbors/rnn_classifier.ex b/lib/scholar/neighbors/rnn_classifier.ex index e25ea8f1..ddc241e4 100644 --- a/lib/scholar/neighbors/rnn_classifier.ex +++ b/lib/scholar/neighbors/rnn_classifier.ex @@ -1,4 +1,4 @@ -defmodule Scholar.Neighbors.RNNClassifier do +defmodule Scholar.Neighbors.RadiusNNClassifier do @moduledoc """ The Radius Nearest Neighbors. @@ -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], @@ -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] @@ -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] @@ -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] diff --git a/lib/scholar/neighbors/rnn_regressor.ex b/lib/scholar/neighbors/rnn_regressor.ex index 17fad774..1e5f4f21 100644 --- a/lib/scholar/neighbors/rnn_regressor.ex +++ b/lib/scholar/neighbors/rnn_regressor.ex @@ -1,4 +1,4 @@ -defmodule Scholar.Neighbors.RNNRegressor do +defmodule Scholar.Neighbors.RadiusNNRegressor do @moduledoc """ The Radius Nearest Neighbors. @@ -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], @@ -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] @@ -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] diff --git a/mix.exs b/mix.exs index bbbebaff..d2be045e 100644 --- a/mix.exs +++ b/mix.exs @@ -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: [ diff --git a/test/scholar/neighbors/rnn_classifier_test.exs b/test/scholar/neighbors/rnn_classifier_test.exs index bc9d6474..1d4bc57a 100644 --- a/test/scholar/neighbors/rnn_classifier_test.exs +++ b/test/scholar/neighbors/rnn_classifier_test.exs @@ -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([ @@ -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 @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/scholar/neighbors/rnn_regressor_test.exs b/test/scholar/neighbors/rnn_regressor_test.exs index 1f14c993..38be8798 100644 --- a/test/scholar/neighbors/rnn_regressor_test.exs +++ b/test/scholar/neighbors/rnn_regressor_test.exs @@ -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([ @@ -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 @@ -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, @@ -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 @@ -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 @@ -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