Skip to content

Commit

Permalink
Don't inclide affinity matrix on the result
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 24, 2023
1 parent 8a583a1 commit 84c8878
Showing 1 changed file with 5 additions and 27 deletions.
32 changes: 5 additions & 27 deletions lib/scholar/cluster/affinity_propagation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ defmodule Scholar.Cluster.AffinityPropagation do
containers: [
:labels,
:cluster_centers_indices,
:affinity_matrix,
:cluster_centers,
:num_clusters,
:iterations
]}
defstruct [
:labels,
:cluster_centers_indices,
:affinity_matrix,
:cluster_centers,
:num_clusters,
:iterations
Expand Down Expand Up @@ -87,8 +85,6 @@ defmodule Scholar.Cluster.AffinityPropagation do
The function returns a struct with the following parameters:
* `:affinity_matrix` - Affinity matrix. It is a negated squared euclidean distance of each pair of points.
* `:clusters_centers` - Cluster centers from the initial data.
* `:cluster_centers_indices` - Indices of cluster centers.
Expand All @@ -103,13 +99,6 @@ defmodule Scholar.Cluster.AffinityPropagation do
%Scholar.Cluster.AffinityPropagation{
labels: Nx.tensor([0, 3, 3, 3]),
cluster_centers_indices: Nx.tensor([0, -1, -1, 3]),
affinity_matrix: Nx.tensor(
[
[-0.0, -6162.0, -5358.0, -5499.0],
[-6162.0, -0.0, -1030.0, -913.0],
[-5358.0, -1030.0, -0.0, -31.0],
[-5499.0, -913.0, -31.0, -0.0]
]),
cluster_centers: Nx.tensor(
[
[12.0, 5.0, 78.0, 2.0],
Expand All @@ -136,8 +125,7 @@ defmodule Scholar.Cluster.AffinityPropagation do
converge_after = opts[:converge_after]
num_samples = Nx.axis_size(data, 0)

{initial_a, initial_r, s, affinity_matrix} =
initialize_matrices(data, self_preference: self_preference)
{initial_a, initial_r, s} = initialize_matrices(data, self_preference: self_preference)

{n, _} = Nx.shape(initial_a)
{normal, _new_key} = Nx.Random.normal(key, 0, 1, shape: {n, n}, type: Nx.type(s))
Expand Down Expand Up @@ -236,7 +224,6 @@ defmodule Scholar.Cluster.AffinityPropagation do
end

%__MODULE__{
affinity_matrix: affinity_matrix,
cluster_centers_indices: cluster_centers_indices,
cluster_centers: cluster_centers,
labels: labels,
Expand All @@ -259,13 +246,6 @@ defmodule Scholar.Cluster.AffinityPropagation do
%Scholar.Cluster.AffinityPropagation{
labels: Nx.tensor([0, 1, 1, 1]),
cluster_centers_indices: Nx.tensor([0, 3]),
affinity_matrix: Nx.tensor(
[
[-0.0, -6162.0, -5358.0, -5499.0],
[-6162.0, -0.0, -1030.0, -913.0],
[-5358.0, -1030.0, -0.0, -31.0],
[-5499.0, -913.0, -31.0, -0.0]
]),
cluster_centers: Nx.tensor(
[
[12.0, 5.0, 78.0, 2.0],
Expand Down Expand Up @@ -331,17 +311,16 @@ defmodule Scholar.Cluster.AffinityPropagation do
{n, _} = Nx.shape(data)
self_preference = opts[:self_preference]

{similarity_matrix, affinity_matrix} =
initialize_similarities(data, self_preference: self_preference)
similarity_matrix = initialize_similarity(data, self_preference: self_preference)

zero = Nx.tensor(0, type: Nx.type(similarity_matrix))
availability_matrix = Nx.broadcast(zero, {n, n})
responsibility_matrix = Nx.broadcast(zero, {n, n})

{availability_matrix, responsibility_matrix, similarity_matrix, affinity_matrix}
{availability_matrix, responsibility_matrix, similarity_matrix}
end

defnp initialize_similarities(data, opts \\ []) do
defnp initialize_similarity(data, opts \\ []) do
n = Nx.axis_size(data, 0)
dist = -Scholar.Metrics.Distance.pairwise_squared_euclidean(data)

Expand All @@ -351,7 +330,6 @@ defmodule Scholar.Cluster.AffinityPropagation do
self_preference -> Nx.broadcast(self_preference, {n})
end

s_modified = dist |> Nx.put_diagonal(fill_in)
{s_modified, dist}
Nx.put_diagonal(dist, fill_in)
end
end

0 comments on commit 84c8878

Please sign in to comment.