Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
observed_mask = ~missing_mask
missing_mask_flat = missing_mask.flatten()
observed_mask_flat = observed_mask.flatten()
columns, rows = np.meshgrid(np.arange(n_features), np.arange(n_samples))
# training data
i_tr = rows.flatten()[observed_mask_flat]
j_tr = columns.flatten()[observed_mask_flat]
ij_tr = np.vstack([i_tr, j_tr]).T # input to factorizer
y_tr = X.flatten()[observed_mask_flat] # output of factorizer
ij_tr, y_tr = shuffle(ij_tr, y_tr)
# make a keras model
main_input = Input(shape=(2,), dtype='int32')
embed = KerasMatrixFactorizer(
rank=self.rank,
input_dim_i=n_samples,
input_dim_j=n_features,
embeddings_regularizer=regularizers.l2(self.l2_penalty),
use_bias=self.use_bias
)(main_input)
model = Model(inputs=main_input, outputs=embed)
optimizer = import_from(
'keras.optimizers', self.optimization_algorithm
)(lr=self.learning_rate)
model.compile(optimizer=optimizer, loss=self.loss)
callbacks = [EarlyStopping(patience=self.patience, min_delta=self.min_improvement)]
model.fit(
ij_tr,
y_tr,
batch_size=int(len(y_tr) * (1 - self.validation_frac)),
def __init__(
self,
rank,
input_dim_i,
input_dim_j,
embeddings_regularizer=None,
use_bias=True,
**kwargs
):
self.rank = rank
self.input_dim_i = input_dim_i
self.input_dim_j = input_dim_j
self.embeddings_regularizer = regularizers.get(embeddings_regularizer)
self.use_bias = use_bias
super(KerasMatrixFactorizer, self).__init__(**kwargs)