forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cpu sparse embedding op (apache#8460)
* cpu embedding draft * clean up * fix omp thread call * add sparse embedding example * check bound with signel thread * add note * add comments * add operator note * support rsp weight sharing for bucketing * improve workload balance in take add grad rsp kernel * use MSHADOW_CINLINE for cpu kernel * review comments. add unit test for shared rsp weight * remove indexing op-inl.h * Trigger * Trigger
- Loading branch information
1 parent
7d91d1d
commit 8892e6d
Showing
15 changed files
with
926 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# 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 mxnet as mx | ||
|
||
def matrix_fact_net(factor_size, num_hidden, max_user, max_item, sparse_embed=True): | ||
# input | ||
user = mx.symbol.Variable('user') | ||
item = mx.symbol.Variable('item') | ||
score = mx.symbol.Variable('score') | ||
if sparse_embed: | ||
# user feature lookup | ||
user_weight = mx.symbol.Variable('user_weight', stype='row_sparse') | ||
user = mx.symbol.contrib.SparseEmbedding(data=user, weight=user_weight, | ||
input_dim=max_user, output_dim=factor_size) | ||
# item feature lookup | ||
item_weight = mx.symbol.Variable('item_weight', stype='row_sparse') | ||
item = mx.symbol.contrib.SparseEmbedding(data=item, weight=item_weight, | ||
input_dim=max_item, output_dim=factor_size) | ||
else: | ||
# user feature lookup | ||
user = mx.symbol.Embedding(data=user, input_dim=max_user, output_dim=factor_size) | ||
# item feature lookup | ||
item = mx.symbol.Embedding(data=item, input_dim=max_item, output_dim=factor_size) | ||
# non-linear transformation of user features | ||
user = mx.symbol.Activation(data=user, act_type='relu') | ||
user = mx.symbol.FullyConnected(data=user, num_hidden=num_hidden) | ||
# non-linear transformation of item features | ||
item = mx.symbol.Activation(data=item, act_type='relu') | ||
item = mx.symbol.FullyConnected(data=item, num_hidden=num_hidden) | ||
# predict by the inner product, which is elementwise product and then sum | ||
pred = user * item | ||
pred = mx.symbol.sum(data=pred, axis = 1) | ||
pred = mx.symbol.Flatten(data=pred) | ||
# loss layer | ||
pred = mx.symbol.LinearRegressionOutput(data=pred, label=score) | ||
return pred |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# 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 argparse | ||
import logging | ||
import time | ||
import mxnet as mx | ||
import numpy as np | ||
from get_data import get_movielens_iter, get_movielens_data | ||
from matrix_fact_model import matrix_fact_net | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
parser = argparse.ArgumentParser(description="Run matrix factorization with sparse embedding", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument('--num-epoch', type=int, default=3, | ||
help='number of epochs to train') | ||
parser.add_argument('--batch-size', type=int, default=128, | ||
help='number of examples per batch') | ||
parser.add_argument('--print-every', type=int, default=100, | ||
help='logging frequency') | ||
parser.add_argument('--factor-size', type=int, default=128, | ||
help="the factor size of the embedding operation") | ||
parser.add_argument('--use-dense', action='store_true', | ||
help="use the dense embedding operator") | ||
parser.add_argument('--dummy-iter', action='store_true', | ||
help="use the dummy data iterator for speed test") | ||
|
||
MOVIELENS = { | ||
'dataset': 'ml-10m', | ||
'train': './ml-10M100K/r1.train', | ||
'val': './ml-10M100K/r1.test', | ||
'max_user': 71569, | ||
'max_movie': 65135, | ||
} | ||
|
||
if __name__ == '__main__': | ||
head = '%(asctime)-15s %(message)s' | ||
logging.basicConfig(level=logging.INFO, format=head) | ||
|
||
# arg parser | ||
args = parser.parse_args() | ||
logging.info(args) | ||
num_epoch = args.num_epoch | ||
batch_size = args.batch_size | ||
optimizer = 'sgd' | ||
use_sparse = not args.use_dense | ||
factor_size = args.factor_size | ||
dummy_iter = args.dummy_iter | ||
print_every = args.print_every | ||
|
||
momentum = 0.9 | ||
ctx = mx.cpu(0) | ||
learning_rate = 0.1 | ||
|
||
# prepare dataset and iterators | ||
max_user = MOVIELENS['max_user'] | ||
max_movies = MOVIELENS['max_movie'] | ||
get_movielens_data(MOVIELENS['dataset']) | ||
train_iter = get_movielens_iter(MOVIELENS['train'], batch_size, dummy_iter) | ||
val_iter = get_movielens_iter(MOVIELENS['val'], batch_size, dummy_iter) | ||
|
||
# construct the model | ||
net = matrix_fact_net(factor_size, factor_size, max_user, max_movies, sparse_embed=use_sparse) | ||
a = time.time() | ||
|
||
# initialize the module | ||
mod = mx.module.Module(symbol=net, context=ctx, data_names=['user', 'item'], | ||
label_names=['score']) | ||
mod.bind(data_shapes=train_iter.provide_data, label_shapes=train_iter.provide_label) | ||
mod.init_params(initializer=mx.init.Xavier(factor_type="in", magnitude=2.34)) | ||
optim = mx.optimizer.create(optimizer, learning_rate=learning_rate, momentum=momentum, | ||
wd=1e-4, rescale_grad=1.0/batch_size) | ||
mod.init_optimizer(optimizer=optim) | ||
# use MSE as the metric | ||
metric = mx.metric.create(['MSE']) | ||
speedometer = mx.callback.Speedometer(batch_size, print_every) | ||
logging.info('Training started ...') | ||
for epoch in range(num_epoch): | ||
nbatch = 0 | ||
metric.reset() | ||
for batch in train_iter: | ||
nbatch += 1 | ||
mod.forward_backward(batch) | ||
# update all parameters | ||
mod.update() | ||
# update training metric | ||
mod.update_metric(metric, batch.label) | ||
speedometer_param = mx.model.BatchEndParam(epoch=epoch, nbatch=nbatch, | ||
eval_metric=metric, locals=locals()) | ||
speedometer(speedometer_param) | ||
# evaluate metric on validation dataset | ||
score = mod.score(val_iter, ['MSE']) | ||
logging.info('epoch %d, eval MSE = %s ' % (epoch, score[0][1])) | ||
# reset the iterator for next pass of data | ||
train_iter.reset() | ||
val_iter.reset() | ||
logging.info('Training completed.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.