Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ profile = "black"

[project]
name = "turftopic"
version = "0.27.0"
version = "0.27.1"
description = "Topic modeling with contextual representations from sentence transformers."
authors = [
{ name = "Márton Kardos <power.up1163@gmail.com>", email = "martonkardos@cas.au.dk" }
Expand Down
24 changes: 12 additions & 12 deletions turftopic/models/_snmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,18 @@ def fit_new_components(self, X: np.ndarray, n_new_components: int):
):
G, F, error = _step(G, F)
difference = prev_error - error
if (error < self.error_at_init) and (
(prev_error - error) / self.error_at_init
) < self.tol:
if self.verbose:
print(f"Converged after {i} iterations")
self.n_iter_ = i
break
prev_error = error
if self.verbose:
print(
f"Iteration: {i}, Error: {error}, init_error: {self.error_at_init}, difference from previous: {difference}"
)
# if (error < self.error_at_init) and (
# (prev_error - error) / self.error_at_init
# ) < self.tol:
# if self.verbose:
# print(f"Converged after {i} iterations")
# self.n_iter_ = i
# break
# prev_error = error
# if self.verbose:
# print(
# f"Iteration: {i}, Error: {error}, init_error: {self.error_at_init}, difference from previous: {difference}"
# )
self.components_ = np.array(F.T)
self.n_iter_ = i
self.n_components = old_n_components + n_new_components
Expand Down
45 changes: 29 additions & 16 deletions turftopic/models/senstopic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np
from rich.console import Console
from sentence_transformers import SentenceTransformer
from rich.progress import track
from sklearn.base import copy
from sklearn.exceptions import NotFittedError
from sklearn.feature_extraction.text import CountVectorizer
Expand Down Expand Up @@ -455,26 +455,39 @@ def fit_transform_dynamic(
document_topic_matrix = self.transform(
raw_documents, embeddings=embeddings
)
time_labels, self.time_bin_edges = self.bin_timestamps(
timestamps, bins
)
n_comp, n_vocab = self.components_.shape
n_bins = len(self.time_bin_edges) - 1
self.axial_temporal_components_ = np.full(
(n_bins, n_comp, n_vocab),
np.nan,
dtype=self.components_.dtype,
)
self.temporal_importance_ = np.zeros((n_bins, n_comp))
for i_timebin in np.unique(time_labels):
console = Console()
with console.status("Fitting temporally conditioned topics") as status:
status.update("Labelling documents based on time bins.")
time_labels, self.time_bin_edges = self.bin_timestamps(
timestamps, bins
)
console.log("Documents binned.")
status.update("Initializing components.")
n_comp, n_vocab = self.components_.shape
n_bins = len(self.time_bin_edges) - 1
self.axial_temporal_components_ = np.full(
(n_bins, n_comp, n_vocab),
np.nan,
dtype=self.components_.dtype,
)
console.log("Components initialized.")
self.temporal_importance_ = np.zeros((n_bins, n_comp))
for i_timebin in track(
np.unique(time_labels),
description="Calculating temporal components for each time slice.",
console=console,
):
t_dt = document_topic_matrix[time_labels == i_timebin]
t_X = self.embeddings[time_labels == i_timebin]
t_imp, t_comp = self._fit_timebin(t_X, t_dt)
self.temporal_importance_[i_timebin, :] = t_imp
self.axial_temporal_components_[i_timebin, :, :] = t_comp
self.estimate_components(
self.feature_importance,
)
console.log("Temporal components computed.")
with console.status("Post-processing components."):
self.estimate_components(
self.feature_importance,
)
console.log("Temporal fitting done.")
return document_topic_matrix

@property
Expand Down
Loading