Skip to content

Commit 2cea021

Browse files
committed
fix .A function
1 parent c0f1829 commit 2cea021

File tree

14 files changed

+72
-74
lines changed

14 files changed

+72
-74
lines changed

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/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
}

cornac/models/cvaecf/cvaecf.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def loss(self, x, x_, mu_qz, logvar_qz, mu_qhx, logvar_qhx, mu_qhy, logvar_qhy,
139139
ll_choices = {
140140
"mult": x * torch.log(x_ + EPS),
141141
"bern": x * torch.log(x_ + EPS) + (1 - x) * torch.log(1 - x_ + EPS),
142-
"gaus": -(x - x_) ** 2,
142+
"gaus": -((x - x_) ** 2),
143143
"pois": x * torch.log(x_ + EPS) - x_,
144144
}
145145

@@ -160,29 +160,34 @@ def loss(self, x, x_, mu_qz, logvar_qz, mu_qhx, logvar_qhx, mu_qhy, logvar_qhy,
160160
std_ph = torch.exp(0.5 * logvar_ph)
161161

162162
# KL(q(h|x)||p(h|x))
163-
kld_hx = -0.5 * (1 + 2.0 * torch.log(std_qhx) - (mu_qhx - mu_ph).pow(2) - std_qhx.pow(
164-
2)) # assuming std_ph is 1 for now
163+
kld_hx = -0.5 * (
164+
1 + 2.0 * torch.log(std_qhx) - (mu_qhx - mu_ph).pow(2) - std_qhx.pow(2)
165+
) # assuming std_ph is 1 for now
165166
kld_hx = torch.sum(kld_hx, dim=1)
166167

167168
# KL(q(h|x)||q(h|y))
168-
kld_hy = -0.5 * (1 + 2.0 * torch.log(std_qhx) - 2.0 * torch.log(std_qhy) - (
169-
(mu_qhx - mu_qhy).pow(2) + std_qhx.pow(2)) / std_qhy.pow(2)) # assuming std_ph is 1 for now
169+
kld_hy = -0.5 * (
170+
1
171+
+ 2.0 * torch.log(std_qhx)
172+
- 2.0 * torch.log(std_qhy)
173+
- ((mu_qhx - mu_qhy).pow(2) + std_qhx.pow(2)) / std_qhy.pow(2)
174+
) # assuming std_ph is 1 for now
170175
kld_hy = torch.sum(kld_hy, dim=1)
171176

172177
return torch.mean(beta * kld_z + alpha_1 * kld_hx + alpha_2 * kld_hy - ll)
173178

174179

175180
def learn(
176-
cvae,
177-
train_set,
178-
n_epochs,
179-
batch_size,
180-
learn_rate,
181-
beta,
182-
alpha_1,
183-
alpha_2,
184-
verbose,
185-
device=torch.device("cpu"),
181+
cvae,
182+
train_set,
183+
n_epochs,
184+
batch_size,
185+
learn_rate,
186+
beta,
187+
alpha_1,
188+
alpha_2,
189+
verbose,
190+
device=torch.device("cpu"),
186191
):
187192
optimizer = torch.optim.Adam(params=cvae.parameters(), lr=learn_rate)
188193

@@ -197,11 +202,11 @@ def learn(
197202
):
198203
y_batch = y[u_ids, :]
199204
y_batch.data = np.ones(len(y_batch.data)) # Binarize data
200-
y_batch = y_batch.A
205+
y_batch = y_batch.toarray()
201206
y_batch = torch.tensor(y_batch, dtype=torch.float32, device=device)
202207

203208
x_batch = x[u_ids, :]
204-
x_batch = x_batch.A
209+
x_batch = x_batch.toarray()
205210
x_batch = torch.tensor(x_batch, dtype=torch.float32, device=device)
206211

207212
# Reconstructed batch

cornac/models/cvaecf/recom_cvaecf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,25 +219,25 @@ def score(self, user_idx, item_idx=None):
219219
if item_idx is None:
220220
y_u = self.r_mat[user_idx].copy()
221221
y_u.data = np.ones(len(y_u.data))
222-
y_u = torch.tensor(y_u.A, dtype=torch.float32, device=self.device)
222+
y_u = torch.tensor(y_u.toarray(), dtype=torch.float32, device=self.device)
223223
z_u, _ = self.cvae.encode_qz(y_u)
224224

225225
x_u = self.u_adj_mat[user_idx].copy()
226226
x_u.data = np.ones(len(x_u.data))
227-
x_u = torch.tensor(x_u.A, dtype=torch.float32, device=self.device)
227+
x_u = torch.tensor(x_u.toarray(), dtype=torch.float32, device=self.device)
228228
h_u, _ = self.cvae.encode_qhx(x_u)
229229

230230
known_item_scores = self.cvae.decode(z_u, h_u).data.cpu().numpy().flatten()
231231
return known_item_scores
232232
else:
233233
y_u = self.r_mat[user_idx].copy()
234234
y_u.data = np.ones(len(y_u.data))
235-
y_u = torch.tensor(y_u.A, dtype=torch.float32, device=self.device)
235+
y_u = torch.tensor(y_u.toarray(), dtype=torch.float32, device=self.device)
236236
z_u, _ = self.cvae.encode_qz(y_u)
237237

238238
x_u = self.u_adj_mat[user_idx].copy()
239239
x_u.data = np.ones(len(x_u.data))
240-
x_u = torch.tensor(x_u.A, dtype=torch.float32, device=self.device)
240+
x_u = torch.tensor(x_u.toarray(), dtype=torch.float32, device=self.device)
241241
h_u, _ = self.cvae.encode_qhx(x_u)
242242

243243
user_pred = (

cornac/models/pcrl/pcrl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def learn(self, train_set):
379379

380380
for epoch in range(self.n_epoch):
381381
for idx in train_set.item_iter(self.batch_size, shuffle=False):
382-
batch_C = self.aux_data[idx].A
382+
batch_C = self.aux_data[idx].toarray()
383383
EE = self.sess.run(E_, feed_dict={C: batch_C})
384384
z_c = self.sess.run(X_g, feed_dict={C: batch_C, E: EE})
385385
feed_dict = {

cornac/models/vaecf/recom_vaecf.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class VAECF(Recommender):
3838
likelihood: str, default: 'mult'
3939
Name of the likelihood function used for modeling the observations.
4040
Supported choices:
41-
41+
4242
mult: Multinomial likelihood
4343
bern: Bernoulli likelihood
4444
gaus: Gaussian likelihood
@@ -193,16 +193,10 @@ def score(self, user_idx, item_idx=None):
193193
if item_idx is None:
194194
x_u = self.r_mat[user_idx].copy()
195195
x_u.data = np.ones(len(x_u.data))
196-
z_u, _ = self.vae.encode(
197-
torch.tensor(x_u.A, dtype=torch.float32, device=self.device)
198-
)
196+
z_u, _ = self.vae.encode(torch.tensor(x_u.toarray(), dtype=torch.float32, device=self.device))
199197
return self.vae.decode(z_u).data.cpu().numpy().flatten()
200198
else:
201199
x_u = self.r_mat[user_idx].copy()
202200
x_u.data = np.ones(len(x_u.data))
203-
z_u, _ = self.vae.encode(
204-
torch.tensor(x_u.A, dtype=torch.float32, device=self.device)
205-
)
206-
return (
207-
self.vae.decode(z_u).data.cpu().numpy().flatten()[item_idx]
208-
) # Fix me I am not efficient
201+
z_u, _ = self.vae.encode(torch.tensor(x_u.toarray(), dtype=torch.float32, device=self.device))
202+
return self.vae.decode(z_u).data.cpu().numpy().flatten()[item_idx] # Fix me I am not efficient

cornac/models/vaecf/vaecf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def loss(self, x, x_, mu, logvar, beta):
8989
ll_choices = {
9090
"mult": x * torch.log(x_ + EPS),
9191
"bern": x * torch.log(x_ + EPS) + (1 - x) * torch.log(1 - x_ + EPS),
92-
"gaus": -(x - x_) ** 2,
92+
"gaus": -((x - x_) ** 2),
9393
"pois": x * torch.log(x_ + EPS) - x_,
9494
}
9595

@@ -129,7 +129,7 @@ def learn(
129129
):
130130
u_batch = train_set.matrix[u_ids, :]
131131
u_batch.data = np.ones(len(u_batch.data)) # Binarize data
132-
u_batch = u_batch.A
132+
u_batch = u_batch.toarray()
133133
u_batch = torch.tensor(u_batch, dtype=torch.float32, device=device)
134134

135135
# Reconstructed batch

0 commit comments

Comments
 (0)