One-Hotエンコーディング(一位有効符号化)とは、N個の状態をエンコードするためにNビットの状態レジスタを使用する方法です。各状態には独立したレジスタビットがあり、任意の時点で有効なビットは1つだけです。
例えば、性別を考えた場合、男性と女性の2つの選択肢があり、2ビットで表現されます:
男性:10
女性:01
英語テキストのOne-Hotエンコーディング
import torch
import torch.nn.functional as F
# サンプルテキスト
sentences = ['Hello, how are you?', 'I am doing well, thank you!', 'Goodbye.']
# 単語辞書の作成
word_dict = {}
dict_word = {}
for idx, token in enumerate(set(" ".join(sentences).split())):
word_dict[token] = idx
dict_word[idx] = token
# テキストを整数シーケンスに変換
sequences = [[word_dict[token] for token in sentence.split()] for sentence in sentences]
# 辞書サイズの取得
vocab_size = len(word_dict)
# 整数シーケンスをOne-Hotエンコーディングに変換
one_hot_output = torch.zeros(len(sentences), vocab_size)
for i, seq in enumerate(sequences):
one_hot_output[i, seq] = 1
# 結果の表示
print("単語辞書:")
print(word_dict)
print("\nテキスト:")
print(sentences)
print("\nテキストシーケンス:")
print(sequences)
print("\nOne-Hotエンコーディング:")
print(one_hot_output)
出力:
単語辞書:
{'am': 0, 'doing': 1, 'thank': 2, 'how': 3, 'I': 4, 'Hello,': 5, 'Goodbye.': 6, 'you!': 7, 'well,': 8, 'you?': 9, 'are': 10}
テキスト:
['Hello, how are you?', 'I am doing well, thank you!', 'Goodbye.']
テキストシーケンス:
[[5, 3, 10, 9], [4, 0, 1, 8, 2, 7], [6]]
One-Hotエンコーディング:
tensor([[0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 1.],
[1., 1., 1., 0., 1., 0., 0., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]])
中国語テキストのOne-Hotエンコーディング
import torch
import torch.nn.functional as F
# サンプルテキスト
sentences = ['你好,最近怎么样? ', '我过得很好,谢谢!', '今天很高兴。']
# 文字辞書の作成
char_dict = {}
dict_char = {}
for idx, char in enumerate(set("".join(sentences))):
char_dict[char] = idx
dict_char[idx] = char
# テキストを整数シーケンスに変換
sequences = [[char_dict[char] for char in sentence] for sentence in sentences]
# 辞書サイズの取得
vocab_size = len(char_dict)
sentences_count = len(sentences)
# 整数シーケンスをOne-Hotエンコーディングに変換
one_hot_output = torch.zeros(sentences_count, vocab_size)
for i, seq in enumerate(sequences):
one_hot_output[i, seq] = 1
# 結果の表示
print("文字辞書:")
print(char_dict)
print("\nテキスト:")
print(sentences)
print("\nテキストシーケンス:")
print(sequences)
print("\nOne-Hotエンコーディング:")
print(one_hot_output)
出力:
文字辞書:
{'好': 0, '我': 1, '样': 2, '?': 3, '过': 4, '兴': 5, '你': 6, '近': 7, ' ': 8, '么': 9, ',': 10, '怎': 11, '天': 12, '。': 13, '谢': 14, '最': 15, '!': 16, '高': 17, '今': 18, '很': 19, '得': 20}
テキスト:
['你好,最近怎么样? ', '我过得很好,谢谢!', '今天很高兴。']
テキストシーケンス:
[[6, 0, 10, 15, 7, 11, 9, 2, 3, 8], [1, 4, 20, 19, 0, 10, 14, 14, 16], [18, 12, 19, 17, 5, 13]]
One-Hotエンコーディング:
tensor([[0., 0., 1., 1., 0., 0., 1., 1., 1., 1., 1., 1., 0., 0., 0., 1., 0., 0.,
0., 0., 0.],
[1., 1., 0., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 1., 0.,
0., 1., 1.],
[0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 1.,
1., 1., 0.]])
ファイルからの読み込みとOne-Hotエンコーディング
タスクファイル.txtの内容:
比較的直感的な符号化方法は、前に述べた辞書シーケンスを使用することです。例えば、3つのカテゴリがある問題の場合、1、2、3でそれぞれこれらのカテゴリを表すことができます。しかし、この符号化方法には問題があり、モデルが誤って異なるカテゴリ間に順序や距離関係が存在すると考える可能性があります。実際には、これらの関係は存在しないか、または実用的な意味を持たないかもしれません。この問題を避けるために、One-Hotエンコーディング(独热符号化)が導入されました。One-Hotエンコーディングの基本的な考え方は、各カテゴリをベクトルにマッピングすることです。そのベクトルでは、要素の値が1であるものが1つだけ、他の要素の値は0です。これにより、各カテゴリ間は互いに独立し、順序や距離関係が存在しません。
import torch
import torch.nn.functional as F
import jieba
# テキストファイルの読み込み
with open('./data/タスクファイル.txt', "r", encoding="utf-8") as file:
content = file.read()
sentences = content.split(' ')
# 結巴による分词
tokenized_sentences = [list(jieba.cut(sentence)) for sentence in sentences]
# 辞書の構築
word_dict = {}
dict_word = {}
for idx, token in enumerate(set([token for sentence in tokenized_sentences for token in sentence])):
word_dict[token] = idx
dict_word[idx] = token
# テキストを整数シーケンスに変換
sequences = [[word_dict[token] for token in sentence] for sentence in tokenized_sentences]
# 辞書サイズの取得
vocab_size = len(word_dict)
# 整数シーケンスをOne-Hotエンコーディングに変換
one_hot_output = torch.zeros(len(sentences), vocab_size)
for i, seq in enumerate(sequences):
one_hot_output[i, seq] = 1
# 結果の表示
print("単語辞書:")
print(word_dict)
print("\nテキスト:")
print(sentences)
print("\n分词結果")
print(tokenized_sentences)
print("\nテキストシーケンス:")
print(sequences)
print("\nOne-Hotエンコーディング:")
print(one_hot_output)
出力:
単語辞書:
{'而': 0, ')': 1, '但是': 2, '三个': 3, '3': 4, '2': 5, '序列': 6, 'hot': 7, '可以': 8, '思想': 9, '对于': 10, '采用': 11, '距离': 12, '这些': 13, '不同': 14, '相互': 15, '引入': 16, '直观': 17, '独立': 18, '实际上': 19, '提到': 20, '类别': 21, '一些': 22, '避免': 23, '基本': 24, '独热': 25, '编码方式': 26, '顺序': 27, '向量': 28, '可能': 29, '之间': 30, '其中': 31, '实际意义': 32, '也': 33, '这': 34, '有': 35, '-': 36, '映射': 37, '存在': 38, '元素': 39, '就是': 40, '只有': 41, '问题': 42, '具有': 43, '字典': 44, '和': 45, '不': 46, '这样': 47, '这种': 48, '错误': 49, '用': 50, '的': 51, '模型': 52, '每个': 53, '比较': 54, '(': 55, '地': 56, '为了': 57, ',': 58, '。': 59, '分别': 60, '是': 61, '其余': 62, 'one': 63, '了': 64, '到': 65, '或': 66, '值': 67, '一个': 68, '表示': 69, '或者': 70, '将': 71, '0': 72, '、': 73, '例如': 74, '1': 75, '认为': 76, '称': 77, '上面': 78, '为': 79, '会': 80, '关系': 81, '编码': 82}
テキスト:
['比较直观的编码方式是采用上面提到的字典序列。', '例如,对于一个有三个类别的问题,可以用1、2和3分别表示这三个类别。', '但是,这种编码方式存在一个问题,就是模型可能会错误地认为不同类别之间存在一些顺序或距离关系,而实际上这些关系可能是不存在的或者不具有实际意义的。', '为了避免这种问题,引入了one-hot编码(也称独热编码)。', 'one-hot编码的基本思想是将每个类别映射到一个向量,其中只有一个元素的值为1,其余元素的值为0。', '这样,每个类别之间就是相互独立的,不存在顺序或距离关系。']
分词结果
[['比较', '直观', '的', '编码方式', '是', '采用', '上面', '提到', '的', '字典', '序列', '。'], ['例如', ',', '对于', '一个', '有', '三个', '类别', '的', '问题', ',', '可以', '用', '1', '、', '2', '和', '3', '分别', '表示', '这', '三个', '类别', '。'], ['但是', ',', '这种', '编码方式', '存在', '一个', '问题', ',', '就是', '模型', '可能', '会', '错误', '地', '认为', '不同', '类别', '之间', '存在', '一些', '顺序', '或', '距离', '关系', ',', '而', '实际上', '这些', '关系', '可能', '是', '不', '存在', '的', '或者', '不', '具有', '实际意义', '的', '。'], ['为了', '避免', '这种', '问题', ',', '引入', '了', 'one', '-', 'hot', '编码', '(', '也', '称', '独热', '编码', ')', '。'], ['one', '-', 'hot', '编码', '的', '基本', '思想', '是', '将', '每个', '类别', '映射', '到', '一个', '向量', ',', '其中', '只有', '一个', '元素', '的', '值', '为', '1', ',', '其余', '元素', '的', '值', '为', '0', '。'], ['这样', ',', '每个', '类别', '之间', '就是', '相互', '独立', '的', ',', '不', '存在', '顺序', '或', '距离', '关系', '。']]
テキストシーケンス:
[[54, 17, 51, 26, 61, 11, 78, 20, 51, 44, 6, 59], [74, 58, 10, 68, 35, 3, 21, 51, 42, 58, 8, 50, 75, 73, 5, 45, 4, 60, 69, 34, 3, 21, 59], [2, 58, 48, 26, 38, 68, 42, 58, 40, 52, 29, 80, 49, 56, 76, 14, 21, 30, 38, 22, 27, 66, 12, 81, 58, 0, 19, 13, 81, 29, 61, 46, 38, 51, 70, 46, 43, 32, 51, 59], [57, 23, 48, 42, 58, 16, 64, 63, 36, 7, 82, 55, 33, 77, 25, 82, 1, 59], [63, 36, 7, 82, 51, 24, 9, 61, 71, 53, 21, 37, 65, 68, 28, 58, 31, 41, 68, 39, 51, 67, 79, 75, 58, 62, 39, 51, 67, 79, 72, 59], [47, 58, 53, 21, 30, 40, 15, 18, 51, 58, 46, 38, 27, 66, 12, 81, 59]]
One-Hotエンコーディング:
tensor([[0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1.,
0., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0.,
1., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 1., 1., 1., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.,
0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0., 1., 1., 0., 0.,
0., 0., 0., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 0.,
0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 0., 0., 0.,
0., 1., 0., 1., 1., 0., 0., 0., 1., 1., 0., 1., 1., 0., 1., 0., 0., 0.,
0., 0., 1., 0., 1., 0., 1., 1., 0., 0., 1., 0., 1., 1., 0., 1., 1., 0.,
0., 0., 1., 0., 1., 1., 0., 1., 0., 0., 0., 0., 1., 0., 1., 0., 1., 0.,
0., 0., 0., 0., 1., 0., 0., 0., 1., 1., 0.],
[0., 1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.,
0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.,
1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.,
0., 1., 0., 1., 1., 1., 0., 0., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0.,
1., 1., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1.,
0., 0., 0., 0., 1., 1., 0., 1., 1., 1., 0., 1., 0., 1., 1., 0., 0., 1.,
1., 0., 0., 1., 0., 0., 0., 1., 0., 0., 1.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 0., 0.,
1., 0., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0., 0.,
0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 1., 1., 0., 0., 0., 1., 0., 1.,
0., 0., 0., 0., 1., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.]])