Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
コサイン類似度のいろんな書き方
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
nogawanogawa
April 25, 2024
1.6k
4
Share
コサイン類似度のいろんな書き方
nogawanogawa
April 25, 2024
More Decks by nogawanogawa
See All by nogawanogawa
Amazon Bedrockを用いた新着募集のモデレーション半自動化への取り組み
nogawanogawa
2
270
推薦システムにおけるPost Processの取り組み
nogawanogawa
2
530
Python型チェッカー ty を使ってみた話
nogawanogawa
2
1.8k
Devinを導入してドキュメンテーションで変わったこと
nogawanogawa
2
170
相互推薦システム開発の舞台裏と今後の展望
nogawanogawa
2
390
機械学習で使用しているGCSの料金を激減させた話
nogawanogawa
2
5.5k
How to Index Item IDs for Recommendation Foundation Models
nogawanogawa
0
630
CommonLitコンペで学んだこと
nogawanogawa
2
2.4k
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
110
Being A Developer After 40
akosma
91
590k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.5k
Designing Powerful Visuals for Engaging Learning
tmiket
1
350
A Modern Web Designer's Workflow
chriscoyier
698
190k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
270
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
430
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
530
Transcript
© 2024 Wantedly, Inc. コサイン類似度の いろんな書き方 みんなのPython勉強会 #103 Apr. 25
2024 - @nogawanogawa
自己紹介 © 2024 Wantedly, Inc. - 角川拓也 - X: @nogawanogawa
- 所属 - ウォンテッドリー株式会社 データサイエンティスト - ex. - ITコンサル @コンサル会社 - SE @金融系SIer
コサイン類似度 © 2024 Wantedly, Inc. - 機械学習の分野ではよく使用される計算 - テキストや画像など情報を embedding
(埋め込み、高次元のベクトルのこと )で表現することが多い - こうしたテキストや画像の類似している度合いをコサイン類似度を使って表現 - こうした状況では大量にコサイン類似度を計算することがある
コサイン類似度計算のパターン © 2024 Wantedly, Inc. 今回は こちらを中心に お話します
多くのライブラリで効率的に計算できるようになっている © 2024 Wantedly, Inc. それぞれどうやって書くのかやってみたい!
Baseline: Numpyでの最もポピュラーな書き方 © 2024 Wantedly, Inc.
行列計算で書き換え © 2024 Wantedly, Inc. cos cos cos cos cos
cos cos cos 書き換え イメージ
scikit-learnの関数を使う書き方 © 2024 Wantedly, Inc. 関数にわたすだけ
Numpyの計算をJAXで置き換えた書き方 © 2024 Wantedly, Inc. jnp.*の関数を使う
PyTorchで提供されているコサイン類似度関数を使った書き方 © 2024 Wantedly, Inc. 関数にわたすだけ
Numba(Python/NumpyのコードのJITコンパイラ)を使った書き方 © 2024 Wantedly, Inc. Numbaの 書き方で関数を定義
速度測定 © 2024 Wantedly, Inc. 実験の詳細: https://www.nogawanogawa.work/entry/cos_sim_cpu 適切な書き方をすると baselineの4倍ほど高速 今回測定した中では
Numbaが最も高速
まとめ © 2024 Wantedly, Inc. • コサイン類似度にも書き方はたくさんある • そもそも想定する計算パターンが異なっていることも ◦
ライブラリによって計算パターンが違ったりする ◦ 調べた中ではsklearnは全ての組み合わせを計算することが想定されていそう に見えた • 書き方次第で実行速度がかなり変わる ◦ Numpyだけでも行列計算を用いれば十分高速に計算できる ◦ 今回CPUで実行した中ではNumbaが最も高速な結果に ◦ 計算部分だけ見ればJAXのほうが高速になることも見られた