SlideShare a Scribd company logo
Character Level CNN
About Me
・大学/大学院では理論物理学の研究をしてました。
  超弦理論(量子論 /素粒子論と一般相対性理論の統一を目指す学問)の研究
   →10次元時空に飲み込まれそうだったので現実世界( 4次元時空)に逃げてきました。
・シルバーエッグ テクノロジー に所属
  機械学習エンジニアっぽいことをやってる。
        (入社時はWeb Developer)
http://members.wolfram.com/jeffb/visualization/stringtheory.shtml
・趣味:プログラミングと情報幾何学
・好きな言語: Python
・GitHub: https://github.com/Tdual
話す事
・CNNの説明
・TensorFlowでCNNを実装する方法
・Character Level CNNの説明
・TensorFlowでCharacter Level CNNを実装する方法
まずはCNNの説明から
CNNとは
・Convolutional Neural Networkの略
・Deep Learningの手法の一種
・日本語では「畳み込みニューラルネットワーク」
・二つの特別な層を持つ(Convolution層とPooling層)
・画像認識ではもっともポピュラーで精度も最強
画像分類タスクにおいてはすでに人間より上
ILSVRC(2017)の画像分類タスク: エラー率 0.023
人間の画像分類タスク : エラー率 0.051
(http://image-net.org/challenges/talks_2017/ILSVRC2017_overview.pdf)
(http://karpathy.github.io/2014/09/02/what-i-learned-from-competing-against-a-convnet-on-imagenet/)
一般的なニューラルネットワーク
一般的ニューラルネットワークは Fully Connected層(全結合層)
http://neuralnetworksanddeeplearning.com/chap6.html
全てのニューロン
間で結合がある。
Convolution層(1)
1 0 1 0
0 1 2 1
1 2 2 4
0 1 5 6
畳み込み(Convolution)をする層 (正確には相互相関処理)
インプットに対してフィルターをかけて特徴を顕著化してアウトプットする感じ
インプット
1 0 1
1 5 2
1 1 1
フィルター
⊗
16 21
23 34
Convolution層(2)
1 0 1 0
0 1 2 1
1 2 2 4
0 1 5 6
具体的な計算方法
インプット
1 0 1
1 5 2
1 1 1
フィルター
⊗
16
1*1 + 0*0 + 1*1 + 0*1 + 1*5 + 2*2 + 1*1 + 2*1 + 2*1 =16
インプットの一部にフィルターを当てて行列の内積を取る
1 0 1 0
0 1 2 1
1 2 2 4
0 1 5 6
1 0 1
1 5 2
1 1 1
⊗
16
23
1 0 1 0
0 1 2 1
1 2 2 4
0 1 5 6
1 0 1
1 5 2
1 1 1
⊗
16 21
23
1 0 1 0
0 1 2 1
1 2 2 4
0 1 5 6
1 0 1
1 5 2
1 1 1
⊗
16 21
23 34
Pooling層(1)
微小な位置のズレを無視するための層(位置変化ロバスト)
同じ絵だけど位置が少しずれている。
これを別の画像と認識して学習してしまっては精度が悪くなる。
Pooling層(2)
1 0 1 0
0 1 2 1
1 2 2 2
0 3 5 6
max pooling
対象領域の中で最大の値をとる
1 2
3 6
pooling
1 0 1 0
0 1 2 1
1 2 2 2
0 3 5 6
1
1 0 1 0
0 1 2 1
1 2 2 2
0 3 5 6
1 2
3
1 0 1 0
0 1 2 1
1 2 2 2
0 3 5 6
1 2
1 0 1 0
0 1 2 1
1 2 2 2
0 3 5 6
1 2
3 6
Pooling層(3)
1 0 1 0
0 1 2 1
1 2 2 2
0 3 5 6
上にずらしてみる
0 1 2 1
1 2 2 2
0 3 5 6
0 0 0 0
pooling pooling
1 2
3 6
2 2
3 6ほぼ変わらない
Convolution層とPooling層を扱う時の用語
Stride(1) 畳み込みフィルターや Poolingの適用間隔
横Stride:1, 縦Stride:1
Stride(2)
横Stride:2, 縦Stride:2
Padding(1)
0 0 0 0 0 0
0 1 0 1 0 0
0 0 1 2 1 0
0 1 2 2 4 0
0 0 1 5 6 0
0 0 0 0 0 0
周囲を埋めてアウトプットのサイズを調整する。
インプット
1 0 1 0
0 1 2 1
1 2 2 4
0 1 5 6
0で埋めて元のインプットよりもサイズを大き
くする
Padding(2)
インプット
1 0 1
1 5 2
1 1 1
フィルター
⊗
0 0 0 0 0 0
0 1 0 1 0 0
0 0 1 2 1 0
0 1 2 2 4 0
0 0 1 5 6 0
0 0 0 0 0 0
6 6 9 4
10 16 21 14
6 23 34 35
4
paddingする前の元の
インプットサイズと同じ
サイズのアウトプット得
る
TensorFlowについて
TensorFlowとは
https://www.tensorflow.org/
機械学習の為のライブラリ
・オープンソース
・Google製
・Python・C++で使える(JAVAでも使えるようになった。)
・規模に依存しない(GPU/CPU両方対応など)
・一連の処理はセッション単位
・変数はtensorflow.Variableで扱う。
=
①セッションを作る
②値の初期化
③処理を実行
TensorFlowの基礎(1)
W Xy
・処理中に動的に変えたいものはtensorflow.placeholderを使う。
runの時に
  feed_dictで食わせる。
=
TensorFlowの基礎(2)
W x_phy2
TensorFlowでCNN(1)
ダミーデータの用意
TensorFlowでCNN(2)
畳み込み層
フィルター用意
畳み込み層
Padding
Stride
[1, 縦, 横, 1]
TensorFlowでCNN(3)
プーリング層
手書き文字認識(1) exampleデータセッ
トを取得するため
のヘルパー
こんな感じの手書
き文字画像が入っ
てる
画像データと正解ラベル
を取得できる。
正解ラベルはone-hotベクトル
0 → [1,0,0,0,0,0,0,0,0,0]
1 → [0,1,0,0,0,0,0,0,0,0]
9 → [0,0,0,0,0,0,0,0,0,1]
手書き文字認識(2)
画像データと正解ラベルを入
れるplaceholder
層をつなげていく
損失関数:正解とどれくらい
離れているかを表す量で
これを小さくするように学習す
る。
正解率:アウトプットと正解ラ
ベルの一致した割合。
アウトプットは
10次元ベクト
ル
手書き文字認識(3)
lossを最小化するoptimizer
学習状況を確認を表示
訓練データとは別のテスト
データを使って評価
正答率が97.1%
これを応用するだけでいろんなアプリケーション
が作れる。
CNNで作ったもの①
写真をアップロードしたらト
ランプ大統領の顔を判定
するアプリ
(どれくらい似てるか)
服の分類
(これはumap
で2次元に落と
してる画像)
CNNで作ったもの②
CNNで作ったもの③
似た服を検索でき
るアプリ
demoサイト
http://imagerecommender-env-2.ap-northeast-1.elasticbeanstalk.com/
CNNは画像処理にしか使えないの?
画像処理ばっかりじゃん。
画像以外に使えないの?
Character Level CNN
・オリジナル論文 
 Xiang Zhang, Junbo Zhao, Yann LeCun: Character-level Convolutional Networks for Text Classification
(https://arxiv.org/abs/1509.01626)
・テキスト分類をCNNでやってる。
・文書の文字(Character)の並びしか見てない。(最小単位が文字)
   自然言語処理の多くは、文書は単語の集合としてとらえる(最小単位が単語)のでこれは珍しい
最小単位が文字のメリット/デメリット
メリット
・前処理がいらない(日本語なら分かち書きが必要ない)
・タイポやスペルミスが多い文書でも有効(レビュー ,チャットなど)
・文書じゃない物にも適応できる( URL, Tex, プログラミングコードなど)
デメリット
・単語に比べて粒度が小さいので学習に時間がかかる
ネガポジ判定器を作ってみる。
Amazonのレビューを読んでそれがポジティブな内容かネガティブな内容か判断
  
CNNで作った
判定器
review
I love it because ... 「ポジティブ」です。
データセット
Amazon product data
http://jmcauley.ucsd.edu/data/amazon/
「books」のreviewを使った。
レビュー評価
JSON形式で約9百万レビュー
評価は5段階
 4以上をポジティブ
 2以下をネガティブ
とした。
処理の流れ(オリジナル)
対象文字 abcdefghijklmnopqrstuvwxyz0123456789 -,;.!?:’’’/|_@#$%ˆ&*˜‘+-=<>()[]{}
a → (1,0,0,....,0,0)
b → (0,1,0,....,0,0)
…
{ → (0,0,0,....,1,0)
} → (0,0,0,....,0,1)
70次元ベクトル
①文字をone-hot ベクトルに変換
②6層のCNNの通して最後に3層の全結合層に通して分類
TensorFlowで実装
アーキテクチャ図(on Tensorboard)
結果
テストデータの
損失関数
あまり下がっていな
い。
テストデータの
正答率
0.5付近から変わらない
(Small featureの方)
結果
ダメじゃん・・・・
Large featureに変えたり、ハイパーパラメータを変え
たりして試行錯誤したがダメだった。。。
別のアーキテクチャ
別のアーキテクチャを使っている論文を発見
 Joshua Saxe, Konstantin Berlin: eXpose:A Character-Level
Convolutional Neural Network with Embeddings For Detecting Malicious
URLs, File Paths and Registry Keys(https://arxiv.org/abs/1702.08568)
character level CNNを使って悪意あ
るURLやファイルパスを検知するとい
う内容
処理の流れ
①文字を低次元に埋め込む
②カーネルサイズが違う複数のCNNに入れて特徴を出力
③特徴を一つに結合
④全結合層に通して分類
①文字を埋め込みむ(1)
文字をベクトルで表現したいが
one-hotベクトルだと次元が大きくなりすぎる。
アルファベットだと26次元で済むが、記号やギリシャ文字などが入ってくると次元
はどんどん大きくなる。
さらに日本語だと、ひらがな・カタカナ・漢字があるので約 3000次元になる。1文
字計算するごとに3000次元のベクトル計算をする羽目になる。
a → (1,0,0,....,0)
b → (0,1,0,....,0)
…
z → (0,0,0,....,1)
低次元ベクトルで表現したい。
①文字を埋め込みむ(2)
・埋め込み表現(分散表現)
例:
 a → (1, 3, -2, .... ,2)
 b → (-5, 1, 4, .... ,-8)
 …
・埋め込み方は?
 それもニューラルネットワークに学習させよう。
・埋め込み層を用意する 。
a
b
c
文字数 × 埋め込み次元 行列
埋め込み
対応する文字の行ベクトルを選んで更新する。
one-hotベクトルとは違い全
ての成分に値がある。
②カーネルサイズが違う複数のCNNに入れて特徴を出力(1)
filter数:256
カーネルサイズ:
1×2, 1×3, 1×4, 1×5
(stride 横1,縦1)
②カーネルサイズが違う複数のCNNに入れて特徴を出力(2)
this is a pen. → t h i s i s a p e n .
1×2
1×3
1×4
1×5
解釈
様々なWindowで文字のCooccurrence(共起性)を学習してる
③特徴を一つに結合 & ④全結合層に通して分類
TensorFlowで実装
埋め込み層の学習
tf.nn.embedding_lookupを使う
埋め込み層と更新したい idを入れる
今回は2,4番目を指定
今回は2,4番目だけ更新されてる。
更新前と更新後の差を見る
文字を整数値にマッピングして突っ込むだけで良い。
アーキテクチャ図
結果
テストデータの
損失関数
ちゃんと下がってる
テストデータの
正答率
0.93ぐらい
日本語は?
データセット
chABSA-dataset
https://github.com/chakki-works/chABSA-dataset
上場企業の有価証券報告書をベー
スに作成されたデータセット
何がポジティブ/ネガティブだったの
かを含む
文書
JSON形式
ネガティブとポジティブの数を比
べて多い方を文書の感情とし
た。
この前処理をしたら2830文書が
対象になった。
「何が」
ネガ/ポジ
日本語の処理
英語の時と何も変わらない。
文字(ひらがな、カタカナ、漢字)を整数値にマッピングして
tf.nn.embedding_lookupに突っ込むだけ。
例:
あ → 1
い → 2
…
大 → 1212
坂 → 1213
…
(※数値は適当です)
テストデータの
損失関数
ちゃんと下がってる
テストデータの
正答率
0.9ぐらい
結果
わざと誤字をしてもちゃんと分
類できてる。
誤字
  増加傾向 → 加向
  減少傾向 → 減向
誤字・脱字に対する強さ(1)
誤字・脱字に対する強さ(2)
評価するデータ(文書)の文字をランダムに選んで適当な文字に入
れ替える。
変えた文字数と精度
50文字(文書全体の約 1/6)を
変えても精度が0.8以上
データセットは
chABSA-dataset
word level CNNで同じことをやる
変えた文字の数と精度
50文字ですでに精度が 0.5
character level
まとめ
・CNNはTensorFlowを使うと簡単に実装できる。
・CNNは発想次第でいくらでもアプリケーションに応用できる。
 (自然言語処理にも使える)
・Character level CNNは誤字・脱字に強い
   → SNSメッセージ、チャット、レビューなど最適
  (しかも分かち書き不要)
Thank you

More Related Content

Featured

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Characeter-Level CNN