Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update several models to be compatible with TensorFlow 2.0 #504

Merged
merged 10 commits into from
Apr 1, 2023
Merged
28 changes: 14 additions & 14 deletions cornac/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def dok_matrix(self):
"""The user-item interaction matrix in DOK sparse format"""
if self.__dok_matrix is None:
self.__dok_matrix = dok_matrix(
(self.num_users, self.num_items), dtype=np.float32
(self.num_users, self.num_items), dtype='float'
)
for u, i, r in zip(*self.uir_tuple):
self.__dok_matrix[u, i] = r
Expand Down Expand Up @@ -364,13 +364,13 @@ def build(
raise ValueError("data is empty after being filtered!")

uir_tuple = (
np.asarray(u_indices, dtype=np.int),
np.asarray(i_indices, dtype=np.int),
np.asarray(r_values, dtype=np.float),
np.asarray(u_indices, dtype='int'),
np.asarray(i_indices, dtype='int'),
np.asarray(r_values, dtype='float'),
)

timestamps = (
np.fromiter((int(data[i][3]) for i in valid_idx), dtype=np.int)
np.fromiter((int(data[i][3]) for i in valid_idx), dtype='int')
if fmt == "UIRT"
else None
)
Expand Down Expand Up @@ -447,7 +447,7 @@ def idx_iter(self, idx_range, batch_size=1, shuffle=False):

Returns
-------
iterator : batch of indices (array of np.int)
iterator : batch of indices (array of 'int')

"""
indices = np.arange(idx_range)
Expand Down Expand Up @@ -481,8 +481,8 @@ def uir_iter(self, batch_size=1, shuffle=False, binary=False, num_zeros=0):

Returns
-------
iterator : batch of users (array of np.int), batch of items (array of np.int),
batch of ratings (array of np.float)
iterator : batch of users (array of 'int'), batch of items (array of 'int'),
batch of ratings (array of 'float')

"""
for batch_ids in self.idx_iter(len(self.uir_tuple[0]), batch_size, shuffle):
Expand Down Expand Up @@ -524,8 +524,8 @@ def uij_iter(self, batch_size=1, shuffle=False, neg_sampling="uniform"):

Returns
-------
iterator : batch of users (array of np.int), batch of positive items (array of np.int),
batch of negative items (array of np.int)
iterator : batch of users (array of 'int'), batch of positive items (array of 'int'),
batch of negative items (array of 'int')

"""

Expand Down Expand Up @@ -562,9 +562,9 @@ def user_iter(self, batch_size=1, shuffle=False):

Returns
-------
iterator : batch of user indices (array of np.int)
iterator : batch of user indices (array of 'int')
"""
user_indices = np.fromiter(self.user_indices, dtype=np.int)
user_indices = np.fromiter(self.user_indices, dtype='int')
for batch_ids in self.idx_iter(len(user_indices), batch_size, shuffle):
yield user_indices[batch_ids]

Expand All @@ -580,9 +580,9 @@ def item_iter(self, batch_size=1, shuffle=False):

Returns
-------
iterator : batch of item indices (array of np.int)
iterator : batch of item indices (array of 'int')
"""
item_indices = np.fromiter(self.item_indices, np.int)
item_indices = np.fromiter(self.item_indices, 'int')
for batch_ids in self.idx_iter(len(item_indices), batch_size, shuffle):
yield item_indices[batch_ids]

Expand Down
6 changes: 3 additions & 3 deletions cornac/data/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def _build_triplet(self, id_map):
self.map_cid.append(id_map[j])
self.val.append(v)

self.map_rid = np.asarray(self.map_rid, dtype=np.int)
self.map_cid = np.asarray(self.map_cid, dtype=np.int)
self.val = np.asarray(self.val, dtype=np.float)
self.map_rid = np.asarray(self.map_rid, dtype='int')
self.map_cid = np.asarray(self.map_cid, dtype='int')
self.val = np.asarray(self.val, dtype='float')

def build(self, id_map=None, **kwargs):
super().build(id_map=id_map)
Expand Down
2 changes: 1 addition & 1 deletion cornac/data/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ def batch_seq(self, batch_ids, max_length=None):
if max_length is None:
max_length = max(len(self.sequences[mapped_id]) for mapped_id in batch_ids)

seq_mat = np.zeros((len(batch_ids), max_length), dtype=np.int)
seq_mat = np.zeros((len(batch_ids), max_length), dtype='int')
for i, mapped_id in enumerate(batch_ids):
idx_seq = self.sequences[mapped_id][:max_length]
for j, idx in enumerate(idx_seq):
Expand Down
6 changes: 3 additions & 3 deletions cornac/eval_methods/base_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def rating_eval(model, metrics, test_set, user_based=False, verbose=False):
miniters=100,
total=len(u_indices),
),
dtype=np.float,
dtype='float',
)

gt_mat = test_set.csr_matrix
Expand Down Expand Up @@ -177,7 +177,7 @@ def pos_items(csr_row):
if len(test_pos_items) == 0:
continue

u_gt_pos = np.zeros(test_set.num_items, dtype=np.int)
u_gt_pos = np.zeros(test_set.num_items, dtype='int')
u_gt_pos[test_pos_items] = 1

val_pos_items = [] if val_mat is None else pos_items(val_mat.getrow(user_idx))
Expand All @@ -187,7 +187,7 @@ def pos_items(csr_row):
else pos_items(train_mat.getrow(user_idx))
)

u_gt_neg = np.ones(test_set.num_items, dtype=np.int)
u_gt_neg = np.ones(test_set.num_items, dtype='int')
u_gt_neg[test_pos_items + val_pos_items + train_pos_items] = 0

item_indices = None if exclude_unknowns else np.arange(test_set.num_items)
Expand Down
8 changes: 4 additions & 4 deletions cornac/eval_methods/propensity_stratified_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def pos_items(csr_row):
if len(test_pos_items) == 0:
continue

u_gt_pos = np.zeros(test_set.num_items, dtype=np.float)
u_gt_pos = np.zeros(test_set.num_items, dtype='float')
u_gt_pos[test_pos_items] = 1

val_pos_items = [] if val_mat is None else pos_items(val_mat.getrow(user_idx))
Expand All @@ -97,7 +97,7 @@ def pos_items(csr_row):
else pos_items(train_mat.getrow(user_idx))
)

u_gt_neg = np.ones(test_set.num_items, dtype=np.int)
u_gt_neg = np.ones(test_set.num_items, dtype='int')
u_gt_neg[test_pos_items + val_pos_items + train_pos_items] = 0

item_indices = None if exclude_unknowns else np.arange(test_set.num_items)
Expand Down Expand Up @@ -256,7 +256,7 @@ def _estimate_propensities(self):
item_freq[i] += 1

# fit the exponential param
data = np.array([e for e in item_freq.values()], dtype=np.float)
data = np.array([e for e in item_freq.values()], dtype='float')
results = powerlaw.Fit(data, discrete=True, fit_method="Likelihood")
alpha = results.power_law.alpha
fmin = results.power_law.xmin
Expand All @@ -277,7 +277,7 @@ def _build_stratified_dataset(self, test_data):

# match the corresponding propensity score for each feedback
test_props = np.array(
[self.props[i] for u, i, r in test_data], dtype=np.float64
[self.props[i] for u, i, r in test_data], dtype='float'
)

# stratify
Expand Down
6 changes: 3 additions & 3 deletions cornac/metrics/ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ def compute(self, pd_scores, gt_pos, gt_neg=None, **kwargs):
if gt_neg is None:
gt_neg = np.logical_not(gt_pos)

pos_scores = pd_scores[gt_pos.astype(np.bool)]
neg_scores = pd_scores[gt_neg.astype(np.bool)]
pos_scores = pd_scores[gt_pos.astype('bool')]
neg_scores = pd_scores[gt_neg.astype('bool')]
ui_scores = np.repeat(pos_scores, len(neg_scores))
uj_scores = np.tile(neg_scores, len(pos_scores))

Expand Down Expand Up @@ -476,7 +476,7 @@ def compute(self, pd_scores, gt_pos, **kwargs):
AP score.

"""
relevant = gt_pos.astype(np.bool)
relevant = gt_pos.astype('bool')
rank = rankdata(-pd_scores, "max")[relevant]
L = rankdata(-pd_scores[relevant], "max")
ans = (L / rank).mean()
Expand Down
2 changes: 2 additions & 0 deletions cornac/models/cdl/recom_cdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ def fit(self, train_set, val_set=None):
def _fit_cdl(self):
import tensorflow.compat.v1 as tf
from .cdl import Model

tf.disable_eager_execution()

R = self.train_set.csc_matrix # csc for efficient slicing over items
n_users, n_items = self.train_set.num_users, self.train_set.num_items
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/cdl/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tensorflow==1.15.2
tensorflow==2.12.0
2 changes: 1 addition & 1 deletion cornac/models/cdr/model.py → cornac/models/cdr/cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# ============================================================================
"""Collaborative Deep Ranking model"""

import tensorflow as tf
import tensorflow.compat.v1 as tf

from ..cdl.cdl import sdae

Expand Down
6 changes: 4 additions & 2 deletions cornac/models/cdr/recom_cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ def fit(self, train_set, val_set=None):
return self

def _fit_cdr(self):
import tensorflow as tf
from .model import Model
import tensorflow.compat.v1 as tf
from .cdr import Model

tf.disable_eager_execution()

n_users = self.train_set.num_users
n_items = self.train_set.num_items
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/cdr/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tensorflow>=1.13.0
tensorflow==2.12.0
4 changes: 2 additions & 2 deletions cornac/models/conv_mf/convmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
# ============================================================================

import tensorflow as tf
import tensorflow.compat.v1 as tf


def conv_layer(
Expand Down Expand Up @@ -116,7 +116,7 @@ def _build_graph(self):
# Fully-connected layers
self.model_output = fc_layer(
input=self.model_output,
num_input=self.model_output.get_shape()[-1].value,
num_input=self.model_output.get_shape()[-1],
num_output=self.vanila_dimension,
)
# Dropout layer
Expand Down
2 changes: 2 additions & 0 deletions cornac/models/conv_mf/recom_convmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ def _fit_convmf(self):
import tensorflow.compat.v1 as tf
from .convmf import CNN_module

tf.disable_eager_execution()

# less verbose TF
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
tf.logging.set_verbosity(tf.logging.ERROR)
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/conv_mf/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tensorflow>=1.15.2
tensorflow==2.12.0
12 changes: 6 additions & 6 deletions cornac/models/cvae/cvae.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
# ============================================================================

import tensorflow as tf
import tensorflow.compat.v1 as tf

from ..cdl.cdl import act_functions

Expand Down Expand Up @@ -152,7 +152,7 @@ def _vae(self, X):
"W1": tf.get_variable(
"W1",
[self.input_dim, self.layers[0]],
initializer=tf.contrib.layers.xavier_initializer(seed=self.seed),
initializer=tf.keras.initializers.glorot_uniform(seed=self.seed),
dtype=tf.float32,
),
"b1": tf.get_variable(
Expand All @@ -164,7 +164,7 @@ def _vae(self, X):
"W2": tf.get_variable(
"W2",
[self.layers[0], self.layers[1]],
initializer=tf.contrib.layers.xavier_initializer(seed=self.seed),
initializer=tf.keras.initializers.glorot_uniform(seed=self.seed),
dtype=tf.float32,
),
"b2": tf.get_variable(
Expand All @@ -176,7 +176,7 @@ def _vae(self, X):
"W_z_mean": tf.get_variable(
"W_z_mean",
[self.layers[1], self.n_z],
initializer=tf.contrib.layers.xavier_initializer(seed=self.seed),
initializer=tf.keras.initializers.glorot_uniform(seed=self.seed),
dtype=tf.float32,
),
"b_z_mean": tf.get_variable(
Expand All @@ -188,7 +188,7 @@ def _vae(self, X):
"W_z_log_sigma": tf.get_variable(
"W_z_log_sigma",
[self.layers[1], self.n_z],
initializer=tf.contrib.layers.xavier_initializer(seed=self.seed),
initializer=tf.keras.initializers.glorot_uniform(seed=self.seed),
dtype=tf.float32,
),
"b_z_log_sigma": tf.get_variable(
Expand Down Expand Up @@ -222,7 +222,7 @@ def _vae(self, X):
"W2": tf.get_variable(
"W2",
[self.n_z, self.layers[1]],
initializer=tf.contrib.layers.xavier_initializer(seed=self.seed),
initializer=tf.keras.initializers.glorot_uniform(seed=self.seed),
dtype=tf.float32,
),
"b2": tf.get_variable(
Expand Down
4 changes: 3 additions & 1 deletion cornac/models/cvae/recom_cvae.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ def _fit_cvae(self):

# VAE initialization
from .cvae import Model
import tensorflow as tf
import tensorflow.compat.v1 as tf

tf.disable_eager_execution()

tf.set_random_seed(self.seed)
model = Model(
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/cvae/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tensorflow>=1.13.0,<2.0
tensorflow==2.12.0
2 changes: 1 addition & 1 deletion cornac/models/ncf/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tensorflow>=1.15.2
tensorflow>=1.15.2,<2.0.0
2 changes: 1 addition & 1 deletion cornac/models/wmf/recom_wmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def fit(self, train_set, val_set=None):
return self

def _fit_cf(self,):
import tensorflow as tf
import tensorflow.compat.v1 as tf
from .wmf import Model

np.random.seed(self.seed)
Expand Down
2 changes: 1 addition & 1 deletion cornac/models/wmf/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tensorflow>=1.13.0,<2.0
tensorflow==2.12.0
2 changes: 1 addition & 1 deletion cornac/models/wmf/wmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
# ============================================================================

import tensorflow as tf
import tensorflow.compat.v1 as tf


class Model:
Expand Down
2 changes: 1 addition & 1 deletion tests/cornac/data/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def setUp(self):
[4, 4, 0],
[0, 4, 0],
[0, 4, 0],
], dtype=np.int)
], dtype='int')

def test_init(self):
try:
Expand Down