Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def fit(self, train_set, val_set=None):
"""Fit the model to observations.
Parameters
----------
train_set: :obj:`cornac.data.Dataset`, required
User-Item preference data as well as additional modalities.
val_set: :obj:`cornac.data.Dataset`, optional, default: None
User-Item preference data for model selection purposes (e.g., early stopping).
Returns
-------
self : object
"""
Recommender.fit(self, train_set, val_set)
from .skmeans import skmeans
X = self.train_set.matrix
X = sp.csr_matrix(X)
# Skmeans requires rows of X to have a unit L2 norm. We therefore need to make a copy of X as we should not modify the latter.
X1 = X.copy()
X1 = X1.multiply(sp.csc_matrix(1. / (np.sqrt(X1.multiply(X1).sum(1).A1) + 1e-20)).T)
if self.trainable:
res = skmeans(X1, k=self.k, max_iter=self.max_iter, tol=self.tol, verbose=self.verbose,
init_par=self.init_par)
self.centroids = res['centroids']
self.final_par = res['partition']
else:
def fit(self, train_set, val_set=None):
"""Fit the model to observations.
Parameters
----------
train_set: :obj:`cornac.data.Dataset`, required
User-Item preference data as well as additional modalities.
val_set: :obj:`cornac.data.Dataset`, optional, default: None
User-Item preference data for model selection purposes (e.g., early stopping).
Returns
-------
self : object
"""
Recommender.fit(self, train_set)
from cornac.models.pmf import pmf
if self.trainable:
# converting data to the triplet format (needed for cython function pmf)
(uid, iid, rat) = train_set.uir_tuple
rat = np.array(rat, dtype='float32')
if self.variant == 'non_linear': # need to map the ratings to [0,1]
if [self.train_set.min_rating, self.train_set.max_rating] != [0, 1]:
rat = scale(rat, 0., 1., self.train_set.min_rating, self.train_set.max_rating)
uid = np.array(uid, dtype='int32')
iid = np.array(iid, dtype='int32')
if self.verbose:
print('Learning...')
def fit(self, train_set, val_set=None):
"""Fit the model to observations.
Parameters
----------
train_set: :obj:`cornac.data.Dataset`, required
User-Item preference data as well as additional modalities.
val_set: :obj:`cornac.data.Dataset`, optional, default: None
User-Item preference data for model selection purposes (e.g., early stopping).
Returns
-------
self : object
"""
Recommender.fit(self, train_set, val_set)
from ...utils.init_utils import normal
self.n_item = self.train_set.num_items
self.n_user = self.train_set.num_users
self.alpha = self.init_params.get('alpha', train_set.global_mean)
self.beta_u = self.init_params.get('beta_u', normal(self.n_user, std=0.01, random_state=self.seed))
self.beta_i = self.init_params.get('beta_i', normal(self.n_item, std=0.01, random_state=self.seed))
self.gamma_u = self.init_params.get('gamma_u', normal((self.n_user, self.k), std=0.01, random_state=self.seed))
self.gamma_i = self.init_params.get('gamma_i', normal((self.n_item, self.k), std=0.01, random_state=self.seed))
if self.trainable:
self._fit_hft()
return self
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import numpy as np
from ..recommender import Recommender
from ...utils.common import sigmoid
from ...utils.common import scale
from ...exception import ScoreException
class PMF(Recommender):
"""Probabilistic Matrix Factorization.
Parameters
----------
k: int, optional, default: 5
The dimension of the latent factors.
max_iter: int, optional, default: 100
Maximum number of iterations or the number of epochs for SGD.
learning_rate: float, optional, default: 0.001
The learning rate for SGD_RMSProp.
gamma: float, optional, default: 0.9
The weight for previous/current gradient in RMSProp.
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import numpy as np
from ..recommender import Recommender
from ...utils.common import sigmoid
from ...utils.common import scale
from ...exception import ScoreException
class MCF(Recommender):
"""Matrix Co-Factorization.
Parameters
----------
k: int, optional, default: 5
The dimension of the latent factors.
max_iter: int, optional, default: 100
Maximum number of iterations or the number of epochs for SGD.
learning_rate: float, optional, default: 0.001
The learning rate for SGD_RMSProp.
gamma: float, optional, default: 0.9
The weight for previous/current gradient in RMSProp.
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import numpy as np
from ..recommender import Recommender
from ...exception import ScoreException
class VAECF(Recommender):
"""Variational Autoencoder for Collaborative Filtering.
Parameters
----------
k: int, optional, default: 10
The dimension of the stochastic user factors ``z''.
h: int, optional, default: 20
The dimension of the deterministic hidden layer.
n_epochs: int, optional, default: 100
The number of epochs for SGD.
batch_size: int, optional, default: 100
The batch size.
def _validate_models(input_models):
if not hasattr(input_models, "__len__"):
raise ValueError('models have to be an array but {}'.format(type(input_models)))
valid_models = []
for model in input_models:
if isinstance(model, Recommender):
valid_models.append(model)
return valid_models
def __init__(self, k=100, z_dims=[300], max_iter=300, batch_size=300, learning_rate=0.001, name="pcrl",
trainable=True,
verbose=False, w_determinist=True, init_params={'G_s': None, 'G_r': None, 'L_s': None, 'L_r': None}):
Recommender.__init__(self, name=name, trainable=trainable, verbose=verbose)
self.k = k
self.z_dims = z_dims # the dimension of the second hidden layer (we consider a 2-layers PCRL)
self.max_iter = max_iter
self.batch_size = batch_size
self.learning_rate = learning_rate
self.init_params = init_params
self.w_determinist = w_determinist
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import numpy as np
from ..recommender import Recommender
from ...exception import ScoreException
class HFT(Recommender):
"""Hidden Factors and Hidden Topics
Parameters
----------
name: string, default: 'HFT'
The name of the recommender model.
k: int, optional, default: 10
The dimension of the latent factors.
max_iter: int, optional, default: 50
Maximum number of iterations for EM.
grad_iter: int, optional, default: 50
Maximum number of iterations for L-BFGS.