pub struct TextVectorization { /* private fields */ }Expand description
語彙 lookup 型のテキスト変換(Keras TextVectorization 相当)。
文字列バッチを標準化・分割(任意で n-gram)し、語彙で id 列へ引いて
[B, L] の Tensor<i32> にする。id は 0 = パディング、1 = OOV、2.. = 語彙。
語彙は from_vocabulary(直接指定)か adapt(コーパスから構築)で作る。
契約の詳細はモジュール doc(fandhe_ai::text)を参照。
Implementations§
Source§impl TextVectorization
impl TextVectorization
Sourcepub fn from_vocabulary<S: AsRef<str>>(
config: TextVectorizationConfig,
vocabulary: &[S],
) -> Result<Self, TextError>
pub fn from_vocabulary<S: AsRef<str>>( config: TextVectorizationConfig, vocabulary: &[S], ) -> Result<Self, TextError>
語彙を直接指定して作る。id は入力順に 2 から振る(0 = ""、1 = "[UNK]")。
語彙は標準化されず、lookup はバイト完全一致(大小文字を区別する)。
"" は EmptyVocabularyToken、"[UNK]" は ReservedVocabularyToken、
重複は DuplicateVocabularyToken。max_tokens が Some(m) のとき、m が 2 以下は
InvalidMaxTokens、予約 2 件を含む語彙数が m を超えると
VocabularyTooLarge(黙って切り詰めない。確保より先に検査する)。
Sourcepub fn from_vocabulary_with_idf<S: AsRef<str>>(
config: TextVectorizationConfig,
vocabulary: &[S],
idf_weights: &[f32],
) -> Result<Self, TextError>
pub fn from_vocabulary_with_idf<S: AsRef<str>>( config: TextVectorizationConfig, vocabulary: &[S], idf_weights: &[f32], ) -> Result<Self, TextError>
語彙と idf 重みを直接指定して作る(transform_tf_idf 用)。
idf_weights は語彙と同じ順・同じ個数(予約 2 件を含まない)。各値は有限かつ 0 以上
(-0.0 は可)。id 順の重みは idf[0] = 0.0、idf[1](OOV)= 与えた重みの平均
(f64 で合計し 1 回だけ f32 へ。語彙が空なら 0.0)、idf[2..] = 与えた重み。
検査順(確保より先): pad_to_max_tokens → max_tokens・語彙数・設定値 →
個数の一致(IdfWeightsLengthMismatch)→ 各値(InvalidIdfWeight)→ 語彙。
Sourcepub fn adapt<S: AsRef<str>>(
config: TextVectorizationConfig,
corpus: &[S],
) -> Result<Self, TextError>
pub fn adapt<S: AsRef<str>>( config: TextVectorizationConfig, corpus: &[S], ) -> Result<Self, TextError>
コーパスから語彙を構築して作る。
コーパスは設定の標準化・分割(・n-gram)を通して数える。予約トークン
(""・"[UNK]")は数えない。語彙は頻度降順で、同頻度はバイト列の辞書順。
max_tokens が Some(m) なら上位 m - 2 件で切り詰める。語彙 1 件の
バイト数は検査しないため、結果を既定の TextLimits の from_vocabulary へ
渡すと読み戻せない場合がある。
Sourcepub fn adapt_tf_idf<S: AsRef<str>>(
config: TextVectorizationConfig,
corpus: &[S],
) -> Result<Self, TextError>
pub fn adapt_tf_idf<S: AsRef<str>>( config: TextVectorizationConfig, corpus: &[S], ) -> Result<Self, TextError>
コーパスから語彙と idf 重みを構築して作る(transform_tf_idf 用)。
語彙は adapt と同一。idf は語 t について ln(1 + N / (1 + df)) を f64 で計算して
1 回だけ f32 へ丸める(N = コーパスの文書数、df = t を含む文書数で、文書内の
重複は 1 と数える。自然対数)。idf[0] = 0.0、idf[1](OOV)は丸めた後の語彙の idf の
平均(f64 で合計し 1 回だけ f32 へ。語彙が空なら 0.0)で、idf_weights() の [2..]
を from_vocabulary_with_idf へ渡すと同じ重みへ往復できる。
Sourcepub fn transform<S: AsRef<str>>(
&self,
inputs: &[S],
) -> Result<Tensor<i32>, TextError>
pub fn transform<S: AsRef<str>>( &self, inputs: &[S], ) -> Result<Tensor<i32>, TextError>
文字列バッチを [B, L] の Tensor<i32> へ変換する。
L は output_sequence_length が Some ならその値、None ならバッチ内の
最長トークン数。短い行は 0 で埋め、長い行は先頭 L 個を残す。語彙にない
トークンは 1(OOV)。B = 0 は [0, L](None なら L = 0)、Some(0) は
[B, 0]。Split::None と空入力 "" は id 0(パディングと区別できない)。
Sourcepub fn transform_multi_hot<S: AsRef<str>>(
&self,
inputs: &[S],
) -> Result<Tensor<f32>, TextError>
pub fn transform_multi_hot<S: AsRef<str>>( &self, inputs: &[S], ) -> Result<Tensor<f32>, TextError>
文字列バッチを [B, W] の Tensor<f32>(出現すれば 1.0・しなければ 0.0)へ変換する。
列 i が id i に対応する(列 0 = パディングは常に 0、列 1 = OOV)。W は
pad_to_max_tokens が false なら vocabulary_size()、true なら max_tokens
(vocabulary_size() 以上の列は常に 0)。output_sequence_length は無視し、
ngrams は適用する。id 0 になるトークン(Split::None と "")は数えない。
B = 0 は [0, W]。dtype を f32 にするのは Linear/Sequential へ直接渡すため。
入力の件数・バイト数・出力要素数(B × W)は確保より先に検査し、1 行のトークン数が
2^24 を超えると TokenCountNotExact。Keras と違い W は予約 2 件ぶん大きい。
Sourcepub fn transform_count<S: AsRef<str>>(
&self,
inputs: &[S],
) -> Result<Tensor<f32>, TextError>
pub fn transform_count<S: AsRef<str>>( &self, inputs: &[S], ) -> Result<Tensor<f32>, TextError>
文字列バッチを [B, W] の Tensor<f32>(各 id の出現回数)へ変換する。
形状・列の意味・検査・output_sequence_length の無視は transform_multi_hot と同じ。
回数は 2^24 以下に限って厳密に数える(超える行は TokenCountNotExact)。
Sourcepub fn transform_tf_idf<S: AsRef<str>>(
&self,
inputs: &[S],
) -> Result<Tensor<f32>, TextError>
pub fn transform_tf_idf<S: AsRef<str>>( &self, inputs: &[S], ) -> Result<Tensor<f32>, TextError>
文字列バッチを [B, W] の Tensor<f32>(出現回数 × idf 重み)へ変換する。
値は count as f32 * idf[id](f32 の積 1 回)。idf を持たないインスタンス
(from_vocabulary/adapt で作ったもの)は、入力を見る前に MissingIdfWeights。
形状・列の意味・検査・output_sequence_length の無視は transform_multi_hot と同じ。
出現しない列は計算せず 0 のまま(pad 時の範囲外の列でも idf を引かない)。
Sourcepub fn idf_weights(&self) -> Option<&[f32]>
pub fn idf_weights(&self) -> Option<&[f32]>
id 順の idf 重み(長さ = vocabulary_size()。index 0 = 0.0、1 = OOV、2.. = 語彙)。
from_vocabulary/adapt で作ったインスタンスは None。
Sourcepub fn vocabulary(&self) -> &[String]
pub fn vocabulary(&self) -> &[String]
id 順の語彙。index 0 = ""、1 = "[UNK]"、2.. = 語彙。
Sourcepub fn vocabulary_size(&self) -> usize
pub fn vocabulary_size(&self) -> usize
語彙数(予約 2 件を含む)。
Sourcepub fn config(&self) -> &TextVectorizationConfig
pub fn config(&self) -> &TextVectorizationConfig
構築時の設定。
Trait Implementations§
Source§impl Clone for TextVectorization
impl Clone for TextVectorization
Auto Trait Implementations§
impl Freeze for TextVectorization
impl RefUnwindSafe for TextVectorization
impl Send for TextVectorization
impl Sync for TextVectorization
impl Unpin for TextVectorization
impl UnsafeUnpin for TextVectorization
impl UnwindSafe for TextVectorization
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more