Skip to content

Commit d20b93e

Browse files
authored
Upgrade Numpy 2.0 (#664)
1 parent fffadc2 commit d20b93e

File tree

25 files changed

+111
-111
lines changed

25 files changed

+111
-111
lines changed

.circleci/config.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ jobs:
1010
- image: cimg/python:3.10.2
1111
environment:
1212
LIMIT_NUMPY_VERSION: 2.0.0
13-
LIMIT_SCIPY_VERSION: 1.13.1
1413
steps:
1514
- checkout
1615
- python/install-packages:
@@ -20,7 +19,7 @@ jobs:
2019
no_output_timeout: 30m
2120
command: |
2221
pip install --upgrade pip
23-
pip install --only-binary=numpy,scipy "numpy<$LIMIT_NUMPY_VERSION" "scipy<=$LIMIT_SCIPY_VERSION" Cython pytest pytest-cov codecov
22+
pip install --only-binary=numpy,scipy "numpy>$LIMIT_NUMPY_VERSION" Cython pytest pytest-cov codecov
2423
pip install -e .[tests]
2524
- run:
2625
name: Run tests

.github/workflows/python-package.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
branches: [ master ]
99
pull_request:
1010
branches: [ master ]
11-
11+
1212
jobs:
1313
build:
1414
name: Building on ${{ matrix.os }}
@@ -20,7 +20,6 @@ jobs:
2020
python-version: ["3.9", "3.10", "3.11", "3.12"]
2121
env:
2222
LIMIT_NUMPY_VERSION: 2.0.0
23-
LIMIT_SCIPY_VERSION: 1.13.1
2423
steps:
2524
- name: Get number of CPU cores
2625
uses: SimenB/github-actions-cpu-cores@v2
@@ -63,7 +62,7 @@ jobs:
6362

6463
- name: Install other dependencies
6564
run: |
66-
python${{ matrix.python-version }} -m pip install Cython pytest pytest-cov flake8 "numpy<${{ env.LIMIT_NUMPY_VERSION }}" "scipy<=${{ env.LIMIT_SCIPY_VERSION }}"
65+
python${{ matrix.python-version }} -m pip install Cython pytest pytest-cov flake8 "numpy>${{ env.LIMIT_NUMPY_VERSION }}" scipy
6766
python${{ matrix.python-version }} setup.py build_ext -j${{ steps.cpu-cores.outputs.count }}
6867
python${{ matrix.python-version }} -m pip install -e .[tests]
6968

.github/workflows/python-publish.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ on:
1414

1515
env:
1616
LIMIT_NUMPY_VERSION: 2.0.0
17-
LIMIT_SCIPY_VERSION: 1.13.1
1817

1918
jobs:
2019
build-wheels:
@@ -59,7 +58,7 @@ jobs:
5958
run: python${{ matrix.python-version }} -m pip install wheel setuptools pip --upgrade
6059

6160
- name: Install numpy, scipy
62-
run: pip install "numpy<${{ env.LIMIT_NUMPY_VERSION }}" "scipy<=${{ env.LIMIT_SCIPY_VERSION }}"
61+
run: pip install "numpy>${{ env.LIMIT_NUMPY_VERSION }}" scipy
6362

6463
- name: Install other dependencies
6564
run: |

cornac/metrics/ranking.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def dcg_score(gt_pos, pd_rank, k=-1):
9191
else:
9292
truncated_pd_rank = pd_rank
9393

94-
ranked_scores = np.in1d(truncated_pd_rank, gt_pos).astype(int)
94+
ranked_scores = np.isin(truncated_pd_rank, gt_pos).astype(int)
9595
gain = 2**ranked_scores - 1
9696
discounts = np.log2(np.arange(len(ranked_scores)) + 2)
9797

@@ -162,7 +162,7 @@ def compute(self, gt_pos, pd_rank, **kwargs):
162162
truncated_pd_rank = pd_rank
163163

164164
# Compute CRR
165-
rec_rank = np.where(np.in1d(truncated_pd_rank, gt_pos))[0]
165+
rec_rank = np.where(np.isin(truncated_pd_rank, gt_pos))[0]
166166
if len(rec_rank) == 0:
167167
return 0.0
168168
rec_rank = rec_rank + 1 # +1 because indices starts from 0 in python
@@ -210,7 +210,7 @@ def compute(self, gt_pos, pd_rank, **kwargs):
210210
Mean Reciprocal Rank score.
211211
212212
"""
213-
matched_items = np.nonzero(np.in1d(pd_rank, gt_pos))[0]
213+
matched_items = np.nonzero(np.isin(pd_rank, gt_pos))[0]
214214

215215
if len(matched_items) == 0:
216216
raise ValueError(
@@ -267,7 +267,7 @@ def compute(self, gt_pos, pd_rank, **kwargs):
267267
else:
268268
truncated_pd_rank = pd_rank
269269

270-
tp = np.sum(np.in1d(truncated_pd_rank, gt_pos))
270+
tp = np.sum(np.isin(truncated_pd_rank, gt_pos))
271271
tp_fn = len(gt_pos)
272272
tp_fp = self.k if self.k > 0 else len(truncated_pd_rank)
273273

@@ -470,11 +470,11 @@ def compute(self, item_indices, pd_scores, gt_pos, gt_neg=None, **kwargs):
470470
471471
"""
472472

473-
gt_pos_mask = np.in1d(item_indices, gt_pos)
473+
gt_pos_mask = np.isin(item_indices, gt_pos)
474474
gt_neg_mask = (
475475
np.logical_not(gt_pos_mask)
476476
if gt_neg is None
477-
else np.in1d(item_indices, gt_neg)
477+
else np.isin(item_indices, gt_neg)
478478
)
479479

480480
pos_scores = pd_scores[gt_pos_mask]
@@ -519,7 +519,7 @@ def compute(self, item_indices, pd_scores, gt_pos, **kwargs):
519519
AP score.
520520
521521
"""
522-
relevant = np.in1d(item_indices, gt_pos)
522+
relevant = np.isin(item_indices, gt_pos)
523523
rank = rankdata(-pd_scores, "max")[relevant]
524524
L = rankdata(-pd_scores[relevant], "max")
525525
ans = (L / rank).mean()

cornac/models/beacon/recom_beacon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def _remove_diag(self, adj_matrix):
270270

271271
def _normalize(self, adj_matrix: csr_matrix):
272272
"""Symmetrically normalize adjacency matrix."""
273-
row_sum = adj_matrix.sum(1).A.squeeze()
273+
row_sum = adj_matrix.sum(1).toarray().squeeze()
274274
d_inv_sqrt = np.power(
275275
row_sum,
276276
-0.5,

cornac/models/bivaecf/bivae.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import torch.nn as nn
2121
from tqdm.auto import trange
2222

23-
2423
EPS = 1e-10
2524

2625
ACT = {
@@ -136,7 +135,7 @@ def loss(self, x, x_, mu, mu_prior, std, kl_beta):
136135
# Likelihood
137136
ll_choices = {
138137
"bern": x * torch.log(x_ + EPS) + (1 - x) * torch.log(1 - x_ + EPS),
139-
"gaus": -(x - x_) ** 2,
138+
"gaus": -((x - x_) ** 2),
140139
"pois": x * torch.log(x_ + EPS) - x_,
141140
}
142141

@@ -198,7 +197,7 @@ def learn(
198197
i_count = 0
199198
for i_ids in train_set.item_iter(batch_size, shuffle=False):
200199
i_batch = tx[i_ids, :]
201-
i_batch = i_batch.A
200+
i_batch = i_batch.toarray()
202201
i_batch = torch.tensor(i_batch, dtype=dtype, device=device)
203202

204203
# Reconstructed batch
@@ -228,7 +227,7 @@ def learn(
228227
u_count = 0
229228
for u_ids in train_set.user_iter(batch_size, shuffle=False):
230229
u_batch = x[u_ids, :]
231-
u_batch = u_batch.A
230+
u_batch = u_batch.toarray()
232231
u_batch = torch.tensor(u_batch, dtype=dtype, device=device)
233232

234233
# Reconstructed batch
@@ -259,7 +258,7 @@ def learn(
259258
# infer mu_beta
260259
for i_ids in train_set.item_iter(batch_size, shuffle=False):
261260
i_batch = tx[i_ids, :]
262-
i_batch = i_batch.A
261+
i_batch = i_batch.toarray()
263262
i_batch = torch.tensor(i_batch, dtype=dtype, device=device)
264263

265264
beta, _, i_mu, _ = bivae(i_batch, user=False, theta=bivae.theta)
@@ -268,7 +267,7 @@ def learn(
268267
# infer mu_theta
269268
for u_ids in train_set.user_iter(batch_size, shuffle=False):
270269
u_batch = x[u_ids, :]
271-
u_batch = u_batch.A
270+
u_batch = u_batch.toarray()
272271
u_batch = torch.tensor(u_batch, dtype=dtype, device=device)
273272

274273
theta, _, u_mu, _ = bivae(u_batch, user=True, beta=bivae.beta)

cornac/models/bpr/recom_bpr.pyx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ from ...utils.common import scale
3737
from ...utils.init_utils import zeros, uniform
3838

3939

40+
DTYPE = np.float32
41+
4042
cdef extern from "recom_bpr.h" namespace "recom_bpr" nogil:
4143
cdef int get_thread_num()
4244

@@ -119,7 +121,7 @@ class BPR(Recommender, ANNMixin):
119121
seed=None
120122
):
121123
super().__init__(name=name, trainable=trainable, verbose=verbose)
122-
self.k = k
124+
self.k = int(k)
123125
self.max_iter = max_iter
124126
self.learning_rate = learning_rate
125127
self.lambda_reg = lambda_reg
@@ -144,10 +146,10 @@ class BPR(Recommender, ANNMixin):
144146
n_users, n_items = self.total_users, self.total_items
145147

146148
if self.u_factors is None:
147-
self.u_factors = (uniform((n_users, self.k), random_state=self.rng) - 0.5) / self.k
149+
self.u_factors = (uniform((n_users, self.k), random_state=self.rng, dtype=DTYPE) - 0.5) / self.k
148150
if self.i_factors is None:
149-
self.i_factors = (uniform((n_items, self.k), random_state=self.rng) - 0.5) / self.k
150-
self.i_biases = zeros(n_items) if self.i_biases is None or self.use_bias is False else self.i_biases
151+
self.i_factors = (uniform((n_items, self.k), random_state=self.rng, dtype=DTYPE) - 0.5) / self.k
152+
self.i_biases = zeros(n_items, dtype=DTYPE) if self.i_biases is None or self.use_bias is False else self.i_biases
151153

152154
def _prepare_data(self, train_set):
153155
X = train_set.matrix # csr_matrix

cornac/models/cdl/recom_cdl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def _fit_cdl(self, train_set):
243243
feed_dict = {
244244
model.text_mask: corruption_mask[batch_ids, :],
245245
model.text_input: text_feature[batch_ids],
246-
model.ratings: batch_R.A,
246+
model.ratings: batch_R.toarray(),
247247
model.C: batch_C,
248248
model.item_ids: batch_ids,
249249
}

cornac/models/ctr/ctr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _df_simplex(gamma, v, lambda_v, x):
2929

3030

3131
def _is_on_simplex(v, s):
32-
if v.sum() < s + 1e-10 and np.alltrue(v > 0):
32+
if v.sum() < s + 1e-10 and np.all(v > 0):
3333
return True
3434
return False
3535

cornac/models/cvae/recom_cvae.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
import numpy as np
1717
from tqdm.auto import trange
1818

19-
from ..recommender import Recommender
20-
from ..recommender import ANNMixin, MEASURE_DOT
2119
from ...exception import ScoreException
2220
from ...utils import get_rng
2321
from ...utils.init_utils import xavier_uniform
22+
from ..recommender import MEASURE_DOT, Recommender
2423

2524

2625
class CVAE(Recommender):
@@ -175,9 +174,10 @@ def _fit_cvae(self, train_set):
175174
) # normalization
176175

177176
# VAE initialization
178-
from .cvae import Model
179177
import tensorflow.compat.v1 as tf
180178

179+
from .cvae import Model
180+
181181
tf.disable_eager_execution()
182182

183183
tf.set_random_seed(self.seed)
@@ -216,7 +216,7 @@ def _fit_cvae(self, train_set):
216216

217217
feed_dict = {
218218
model.x: document[batch_ids],
219-
model.ratings: batch_R.A,
219+
model.ratings: batch_R.toarray(),
220220
model.C: batch_C,
221221
model.item_ids: batch_ids,
222222
}

0 commit comments

Comments
 (0)