Open1

keras 覚え書

hiroyuki ichijohiroyuki ichijo

(作成2020.10.18)

自然言語処理用のツール

pad_sequences

from keras.preprocessing.sequence import pad_sequences
data = pad_sequences(sequences, maxlen=100)

配列の長さを0埋めして統一する。maxlenを指定するとそれ以上はカットする。出力はnp.ndarray型
sequence: 配列

Tokenizer

tokenizer = Tokenizer()
tokenizer.fit_on_texts(texts)

インスタンスを作って、fitしている。fitは解析させる意味で、これをしないと始まらない。

  • texts_to_sequences
sequences = tokenizer.texts_to_sequences(texts)
print(type(sequences))
print(sequences)
# <class 'list'>
# [1, 860, 6, 642, 1, 102, 1293, 1, 411, 3096, 2, 1, 274]

数値に置換する。出力はlist型

  • word_index
word_index = tokenizer.word_index
print(type(word_index))
print(word_index)
# <class 'dict'>
# {'the': 1, 'br': 2, 'as': 3, 'a': 4, 'is': 5}

数値への変換辞書を作成する