GithubHelp home page GithubHelp logo

pytorch-matrix-factorization's Introduction

PyTorchでより深いMatrix Factorization

10年前のNetflix Prizeで確立された(?)、Matrix Factrizationは多くの場合、SVDというアルゴリズムで解くことができるが、ロジックと数式をぼんやりと見ていたら、Deep Learningでもできるっぽいなと思った。

ググると、Pytorchでの実装をここなっている人[1], Kerasでの実装を行っている人[2]を見つけることができた。[2]によると、内積を計算することを最終目標とするのであるが、どうやらその内部は非線形であってもいいらしく、表現力を高めるような深いネットワークの構成でも性能がでるようである。

Pytorchで実装を行い、簡単に性能をそれなりに出せたので忘備録として残しておく。

Matrix Factorization

気持ちはこうで、実際にはすべてを同一に最適化できないので、ミニバッチを切り出して順次学習していく

一つのデータセットの粒度は、下図のようにEmbedding Layerで整数値をEmbeddingしたあと、内積を計算して映画の評価点を予想する。

具体的な実装

厳密なMatrix Factrizationでの定義であるところのコサイン類似度を計算して、UserとItemの近さを出してもよいが、せっかくDeepLearningフレームワークを利用するので、内積ではなく別のベクトルの結合方法を行うことができ、その上の全結合を利用することもできる。
そして、実際に性能が良いようである[2]。

Denseをすべて消しても動作するけど、性能が改善するのでネットワークを深くしている

Pytorchでネットワークの定義をこのようにしてみた。

class MF(nn.Module):
    def __init__(self, input_items, input_users):
        super(MF, self).__init__()
        print('item size', input_items)

        self.l_b1 = nn.Embedding(num_embeddings=input_items, embedding_dim=768)
        self.l_b2 = nn.Linear(
            in_features=768, out_features=512, bias=True)

        self.l_a1 = nn.Embedding(num_embeddings=input_users, embedding_dim=768)
        self.l_a2 = nn.Linear(
            in_features=768, out_features=512, bias=True)

        self.l_l1 = nn.Linear(
            in_features=512, out_features=1, bias=True)

    def encode_item(self, x):
        x = self.l_b1(x)
        x = F.relu(self.l_b2(x))
        return x

    def encode_user(self, x):
        x = self.l_a1(x)
        x = F.relu(self.l_a2(x))
        return x


    def forward(self, inputs):
        item_vec, user_vec = inputs
        item_vec = self.encode_item(item_vec)
        user_vec = self.encode_user(user_vec)
        return F.relu(self.l_l1(user_vec * item_vec))

データ・セット

(アカデミックのBitTorrent経由でダウンロードできる)

前処理と学習

断片になったcsvを結合

$ python3 10_preprocessing.py

userとmovieにインデックスをふる

$ python3 20_make_movieId_userId_defs.py

trainとtestデータに分割

$ python3 30_split_train_test.py

Pythonの標準データフレームに変換

$ python3 40_make_sparse_matrix.py

学習

$ python3 60_matrix_factorization.py

コード

結果

NetFlix Prizeのロスは0.8前後なのでここまでは下がらないけど、シンプルなモデルでそここそこの結果を出せる。

$ python3 60_matrix_factorization.py 
user size 480189 item size 17770
item size 17770
rmse 3.736581492357837
rmse 1.9679039708406718
rmse 1.8339715235914231
rmse 1.723884858665941
rmse 1.6683048474397226
rmse 1.6050250868125016
rmse 1.5326026325846749
rmse 1.5022485451718044
rmse 1.436461702330561
rmse 1.4173315269913291
rmse 1.4158676689106966
rmse 1.3402933459666742
rmse 1.2894482545594244
rmse 1.261695349458166
rmse 1.2595036448705352
rmse 1.3063188623813087
...
rmse 1.080400405348696

参考

pytorch-matrix-factorization's People

Contributors

gink03 avatar

Stargazers

 avatar hwsdien avatar  avatar SnowWind avatar 張君瑞 avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

nullees

pytorch-matrix-factorization's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.