Skip to content

Commit

Permalink
Update voting matrix integer type for numpy compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
dotpyu authored Apr 8, 2024
1 parent 08d39b2 commit 3ffc1d8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
18 changes: 9 additions & 9 deletions labelmodels/hmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def estimate_label_model(self, votes, seq_starts, config=None):
init_random(config.random_seed)

# Converts to CSR and integers to standardize input
votes = sparse.csr_matrix(votes, dtype=np.int)
seq_starts = np.array(seq_starts, dtype=np.int)
votes = sparse.csr_matrix(votes, dtype=np.int32)
seq_starts = np.array(seq_starts, dtype=np.int32)

batches = self._create_minibatches(
votes, seq_starts, config.batch_size, shuffle_seqs=True)
Expand All @@ -125,10 +125,10 @@ def get_most_probable_labels(self, votes, seq_starts):
:return: vector of length m, where element is the most likely predicted labels
"""
# Converts to CSR and integers to standardize input
votes = sparse.csr_matrix(votes, dtype=np.int)
seq_starts = np.array(seq_starts, dtype=np.int)
votes = sparse.csr_matrix(votes, dtype=np.int32)
seq_starts = np.array(seq_starts, dtype=np.int32)

out = np.ndarray((votes.shape[0],), dtype=np.int)
out = np.ndarray((votes.shape[0],), dtype=np.int32)

offset = 0
for votes, seq_starts in self._create_minibatches(votes, seq_starts, 32):
Expand Down Expand Up @@ -185,8 +185,8 @@ def get_label_distribution(self, votes, seq_starts):
k x k matrix will be all zeros.
"""
# Converts to CSR and integers to standardize input
votes = sparse.csr_matrix(votes, dtype=np.int)
seq_starts = np.array(seq_starts, dtype=np.int)
votes = sparse.csr_matrix(votes, dtype=np.int32)
seq_starts = np.array(seq_starts, dtype=np.int32)

out_unary = np.zeros((votes.shape[0], self.num_classes))
out_pairwise = np.zeros((votes.shape[0], self.num_classes, self.num_classes))
Expand Down Expand Up @@ -274,7 +274,7 @@ def get_transition_matrix(self):

def _create_minibatches(self, votes, seq_starts, batch_size, shuffle_seqs=False):
# Computes explicit seq ends so that we can shuffle the sequences
seq_ends = np.ndarray((seq_starts.shape[0],), dtype=np.int)
seq_ends = np.ndarray((seq_starts.shape[0],), dtype=np.int32)
for i in range(1, seq_starts.shape[0]):
seq_ends[i-1] = seq_starts[i] - 1
seq_ends[-1] = votes.shape[0] - 1
Expand Down Expand Up @@ -306,7 +306,7 @@ def _create_minibatches(self, votes, seq_starts, batch_size, shuffle_seqs=False)
rel_seq_start_batches = []
for seq_start_batch, seq_end_batch in zip(seq_start_batches, seq_end_batches):
vote_batch = []
rel_seq_start_batch = np.zeros((len(seq_start_batch),), dtype=np.int)
rel_seq_start_batch = np.zeros((len(seq_start_batch),), dtype=np.int32)
total_len = 0
for i, (start, end) in enumerate(zip(seq_start_batch, seq_end_batch)):
vote_batch.append(votes[start:end+1])
Expand Down
24 changes: 12 additions & 12 deletions labelmodels/linked_hmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def estimate_label_model(self, label_votes, link_votes, seq_starts, config=None)
init_random(config.random_seed)

# Converts to CSR and integers to standardize input
label_votes = sparse.csr_matrix(label_votes, dtype=np.int)
link_votes = sparse.csr_matrix(link_votes, dtype=np.int)
seq_starts = np.array(seq_starts, dtype=np.int)
label_votes = sparse.csr_matrix(label_votes, dtype=np.int32)
link_votes = sparse.csr_matrix(link_votes, dtype=np.int32)
seq_starts = np.array(seq_starts, dtype=np.int32)

batches = self._create_minibatches(
label_votes, link_votes, seq_starts, config.batch_size, shuffle_seqs=True)
Expand Down Expand Up @@ -193,11 +193,11 @@ def get_most_probable_labels(self, label_votes, link_votes, seq_starts):
:return: vector of length m, where element is the most likely predicted labels
"""
# Converts to CSR and integers to standardize input
label_votes = sparse.csr_matrix(label_votes, dtype=np.int)
link_votes = sparse.csr_matrix(link_votes, dtype=np.int)
seq_starts = np.array(seq_starts, dtype=np.int)
label_votes = sparse.csr_matrix(label_votes, dtype=np.int32)
link_votes = sparse.csr_matrix(link_votes, dtype=np.int32)
seq_starts = np.array(seq_starts, dtype=np.int32)

out = np.ndarray((label_votes.shape[0],), dtype=np.int)
out = np.ndarray((label_votes.shape[0],), dtype=np.int32)

offset = 0
for label_votes, link_votes, seq_starts in self._create_minibatches(
Expand Down Expand Up @@ -262,9 +262,9 @@ def get_label_distribution(self, label_votes, link_votes, seq_starts):
k x k matrix will be all zeros.
"""
# Converts to CSR and integers to standardize input
label_votes = sparse.csr_matrix(label_votes, dtype=np.int)
link_votes = sparse.csr_matrix(link_votes, dtype=np.int)
seq_starts = np.array(seq_starts, dtype=np.int)
label_votes = sparse.csr_matrix(label_votes, dtype=np.int32)
link_votes = sparse.csr_matrix(link_votes, dtype=np.int32)
seq_starts = np.array(seq_starts, dtype=np.int32)

out_unary = np.zeros((label_votes.shape[0], self.num_classes))
out_pairwise = np.zeros((label_votes.shape[0], self.num_classes, self.num_classes))
Expand Down Expand Up @@ -362,7 +362,7 @@ def _create_minibatches(self, label_votes, link_votes, seq_starts,
"of rows")

# Computes explicit seq ends so that we can shuffle the sequences
seq_ends = np.ndarray((seq_starts.shape[0],), dtype=np.int)
seq_ends = np.ndarray((seq_starts.shape[0],), dtype=np.int32)
for i in range(1, seq_starts.shape[0]):
seq_ends[i - 1] = seq_starts[i] - 1
seq_ends[-1] = label_votes.shape[0] - 1
Expand Down Expand Up @@ -398,7 +398,7 @@ def _create_minibatches(self, label_votes, link_votes, seq_starts,
for seq_start_batch, seq_end_batch in zip(seq_start_batches, seq_end_batches):
label_vote_batch = []
link_vote_batch = []
rel_seq_start_batch = np.zeros((len(seq_start_batch),), dtype=np.int)
rel_seq_start_batch = np.zeros((len(seq_start_batch),), dtype=np.int32)
total_len = 0
for i, (start, end) in enumerate(zip(seq_start_batch, seq_end_batch)):
label_vote_batch.append(label_votes[start:end + 1])
Expand Down
4 changes: 2 additions & 2 deletions labelmodels/naive_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def estimate_label_model(self, votes, config=None):
init_random(config.random_seed)

# Converts to CSR to standardize input
votes = sparse.csr_matrix(votes, dtype=np.int)
votes = sparse.csr_matrix(votes, dtype=np.int32)

batches = self._create_minibatches(
votes, config.batch_size, shuffle_rows=True)
Expand All @@ -96,7 +96,7 @@ def get_label_distribution(self, votes):
the true class label for the corresponding example
"""
# Converts to CSR to standardize input
votes = sparse.csr_matrix(votes, dtype=np.int)
votes = sparse.csr_matrix(votes, dtype=np.int32)

labels = np.ndarray((votes.shape[0], self.num_classes))
batches = self._create_minibatches(votes, 4096, shuffle_rows=False)
Expand Down

0 comments on commit 3ffc1d8

Please sign in to comment.