質問編集履歴

2

誤字

2022/11/15 07:13

投稿

yuncy21
yuncy21

スコア2

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
  mask_vecs = outputs[0].numpy()[[i+1 for i in masked_indexs]]
11
11
  ```
12
12
  1行目のelseが何故必要なのかわかりません。
13
- 2行目のmask_vecsは問題文全体のスコアということで合っていますでしょうか。
13
+ 2行目のmask_vecsは問題文全体のベクトルということで合っていますでしょうか。
14
14
  よろしくお願いいたします。
15
15
  ### 実現したいこと
16
16
 

1

内容の修正

2022/11/15 07:12

投稿

yuncy21
yuncy21

スコア2

test CHANGED
File without changes
test CHANGED
@@ -3,16 +3,14 @@
3
3
  bertの勉強をしようと軽く触れた段階です。プログラミング自体初心者です。
4
4
  https://www.ai-shift.co.jp/techblog/550
5
5
  勉強にあたって、上記のサイトのコードを実行してみたところ、attributeエラーが出ました。
6
- 解決方法を教えていただきたいです。
6
+ エラーの意味は分かるのですが、どう修正していけばいいかわかりません。解決方法を教えていただきたいです。
7
7
 
8
8
  ```python
9
9
  tokens = ["[MASK]" if t == "*" else t for i, t in enumerate(tokens)]
10
- masked_indexs = [i for i, v in enumerate(ids[0]) if v == 103]
11
10
  mask_vecs = outputs[0].numpy()[[i+1 for i in masked_indexs]]
12
11
  ```
13
- また、関数内で上記コードの[ ]内どういう動きをしているのかよくわかりません。
12
+ 1行目else何故必要なのかわかりません。
14
- [  ]内にifやforが入るードを見たことがなく、困惑している状況
13
+ 2行目のmask_vecsは問題文全体のスアということで合っていますしょうか
15
- こちらの解説もしていただければ幸いです。
16
14
  よろしくお願いいたします。
17
15
  ### 実現したいこと
18
16
 
@@ -41,24 +39,25 @@
41
39
 
42
40
  ```Python
43
41
  def part6_slover(text, candidate, answer, q):
44
- if max([len(tokenizer.tokenize(c)) for c in candidate]) == 1:
42
+ if max([len(tokenizer.tokenize(c)) for c in candidate]) == 1: #candidate(候補)を順番に見てすべて1単語であれば
45
- return part5_slover(text, candidate, answer)
43
+ return part5_slover(text, candidate, answer) #part5のロジックを使用
46
- tokens = tokenizer.tokenize(text)
44
+ tokens = tokenizer.tokenize(text) #textをトークン化
47
- tokens = ["[MASK]" if t == "*" else t for i, t in enumerate(tokens)]
45
+ tokens = ["[MASK]" if t == "*" else t for i, t in enumerate(tokens)] #トークンを順番に見て*を[MASK]に変更
48
- tokens = ["[CLS]"] + tokens + ["[SEP]"]
46
+ tokens = ["[CLS]"] + tokens + ["[SEP]"] #前後に[CLS],[SEP]を付与
49
47
 
50
- ids = tokenizer.convert_tokens_to_ids(tokens)
48
+ ids = tokenizer.convert_tokens_to_ids(tokens) #id化
51
- ids = torch.tensor(ids).reshape(1,-1)
49
+ ids = torch.tensor(ids).reshape(1,-1) #テンソル化
52
- ids = ids.cuda()
50
+ ids = ids.cuda() #GPU
53
51
 
54
- masked_indexs = [i for i, v in enumerate(ids[0]) if v == 103]
52
+ masked_indexs = [i for i, v in enumerate(ids[0]) if v == 103] #[MASK]のid(103)がでてくるのは何番目か
55
53
 
56
- with torch.no_grad():
54
+ with torch.no_grad(): #文章ベクトルを計算
57
55
  outputs, _ = model.bert(ids)
58
- mask_vecs = outputs[0].numpy()[[i+1 for i in masked_indexs]]
56
+ mask_vecs = outputs[0].numpy()[[i+1 for i in masked_indexs]] #?
59
57
 
60
- c_vecs = np.array([get_bert_vec(c) for c in candidate])
58
+ c_vecs = np.array([get_bert_vec(c) for c in candidate]) #選択肢の文章ベクトルを計算
61
- return candidate[cosine_similarity([mask_vecs[q-1]], c_vecs)[0].argsort()[-1]]
59
+ return candidate[cosine_similarity([mask_vecs[q-1]], c_vecs)[0].argsort()[-1]] #cos類似度の高いものを戻り値に
62
60
  ```
61
+ コメントは独自に意味を汲み取ったものです。誤った捉え方をしていたら教えてほしいです。
63
62
 
64
63