Skip to main content

TextVectorization

Struct TextVectorization 

Source
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

Source

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(黙って切り詰めない。確保より先に検査する)。

Source

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)→ 語彙。

Source

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 へ 渡すと読み戻せない場合がある。

Source

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 へ渡すと同じ重みへ往復できる。

Source

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(パディングと区別できない)。

Source

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 件ぶん大きい。

Source

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)。

Source

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 を引かない)。

Source

pub fn idf_weights(&self) -> Option<&[f32]>

id 順の idf 重み(長さ = vocabulary_size()。index 0 = 0.0、1 = OOV、2.. = 語彙)。 from_vocabulary/adapt で作ったインスタンスは None。

Source

pub fn vocabulary(&self) -> &[String]

id 順の語彙。index 0 = ""、1 = "[UNK]"、2.. = 語彙。

Source

pub fn vocabulary_size(&self) -> usize

語彙数(予約 2 件を含む)。

Source

pub fn config(&self) -> &TextVectorizationConfig

構築時の設定。

Trait Implementations§

Source§

impl Clone for TextVectorization

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TextVectorization

設定と語彙件数だけを出す。語彙の全文は出さない(A09)。

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V