Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
コサイン類似度のいろんな書き方
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
nogawanogawa
April 25, 2024
1.7k
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
コサイン類似度のいろんな書き方
nogawanogawa
April 25, 2024
More Decks by nogawanogawa
See All by nogawanogawa
Amazon Bedrockを用いた新着募集のモデレーション半自動化への取り組み
nogawanogawa
2
340
推薦システムにおけるPost Processの取り組み
nogawanogawa
2
580
Python型チェッカー ty を使ってみた話
nogawanogawa
2
1.9k
Devinを導入してドキュメンテーションで変わったこと
nogawanogawa
2
200
相互推薦システム開発の舞台裏と今後の展望
nogawanogawa
2
440
機械学習で使用しているGCSの料金を激減させた話
nogawanogawa
2
5.6k
How to Index Item IDs for Recommendation Foundation Models
nogawanogawa
0
670
CommonLitコンペで学んだこと
nogawanogawa
2
2.5k
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.6k
Fireside Chat
paigeccino
43
4k
Marketing to machines
jonoalderson
1
5.8k
Agile that works and the tools we love
rasmusluckow
331
22k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
850
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
530
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Test your architecture with Archunit
thirion
2
2.4k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
540
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
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のほうが高速になることも見られた