🚀 Bottleneck T5 ⏳
Bottleneck T5モデルは、潜在空間でテキストを検査および編集するためのインターフェースを探索する多くの実験やデモをサポートしています。このモデルはテキスト用のオートエンコーダで、最大512トークンのテキストを埋め込みベクトルにエンコードし、その埋め込みベクトルから元のテキストを再構築することができます。また、このモデルが生成する埋め込み空間の構造により、潜在空間でのベクトル演算を通じてテキストの意味的な編集が可能になります。
🚀 クイックスタート
Bottleneck T5モデルは、潜在空間でテキストを操作するための強力なツールです。以下の手順で、モデルを使ってテキストのエンコードとデコードを行うことができます。
✨ 主な機能
- テキストのエンコードとデコード:最大512トークンのテキストを埋め込みベクトルにエンコードし、そこから元のテキストを再構築できます。
- 意味的な編集:潜在空間でのベクトル演算を通じて、テキストの意味的な編集が可能です。
- テキストの補間:このモデルが生成する埋め込みを使って、テキスト間の意味的な補間を行うことができます。
📦 インストール
このモデルはT5言語モデルの上に実装されたプロトタイプです。以下のコードで必要なライブラリをインポートし、モデルを初期化することができます。
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
from transformers import AutoTokenizer, AutoModelForCausalLM
class BottleneckT5Autoencoder:
def __init__(self, model_path: str, device='cpu'):
self.device = device
self.tokenizer = AutoTokenizer.from_pretrained(model_path, model_max_length=512)
self.model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True).to(self.device)
self.model.eval()
@torch.no_grad()
def embed(self, text: str) -> torch.FloatTensor:
inputs = self.tokenizer(text, return_tensors='pt').to(self.device)
decoder_inputs = self.tokenizer('', return_tensors='pt').to(self.device)
return self.model(
**inputs,
decoder_input_ids=decoder_inputs['input_ids'],
encode_only=True,
)[0]
@torch.no_grad()
def generate_from_latent(self, latent: torch.FloatTensor, max_length=512, temperature=1.0) -> str:
dummy_text = '.'
dummy = self.embed(dummy_text)
perturb_vector = latent - dummy
self.model.perturb_vector = perturb_vector
input_ids = self.tokenizer(dummy_text, return_tensors='pt').to(self.device).input_ids
output = self.model.generate(
input_ids=input_ids,
max_length=max_length,
do_sample=True,
temperature=temperature,
top_p=0.9,
num_return_sequences=1,
)
return self.tokenizer.decode(output[0], skip_special_tokens=True)
次に、モデルクラスに基づいてこのオートエンコーダクラスを初期化します。
device = 'cuda' if torch.cuda.is_available() else 'cpu'
autoencoder = BottleneckT5Autoencoder(model_path='thesephist/contra-bottleneck-t5-large-wikipedia', device=device)
💻 使用例
基本的な使用法
以下のコードで、テキストのエンコードとデコードを行うことができます。
texts = [
'The quick brown fox jumps over the lazy dog',
'Hi there! My name is Linus, and I spend a lot of my time thinking about latent spaces of neural network models.',
'Notion is a single space where you can think, write, and plan. Capture thoughts, manage projects, or even run an entire company — and do it exactly the way you want.',
]
for t in texts:
embedding = autoencoder.embed(t)
reconstruction = autoencoder.generate_from_latent(embedding)
print(reconstruction)
このコードの出力は以下のようになります。
The quick brown fox jumps over the lazy dog
I'm named after Linus, and I spend a lot of my time thinking about neural networks of latent space models.
Notion is a single place where you can think, plan, and spend time. Capture ideas, manage projects, and even do your own writing — or plan it exactly the way you want.
高度な使用法
モデルを使って補間や意味的な編集を行う方法の詳細な例については、このGoogle Colabノートブックを参照してください。
📚 ドキュメント
モデルの詳細
このモデルが生成する埋め込みを使って、テキストの長さ、トーン、構造、トピックなどの潜在的な属性を利用して、テキスト間の意味的な補間や文の編集を行うことができます。
すべてのBottleneck T5モデルは、英語のWikipediaのフィルタリングされたサブセットで学習されており、百科事典やそれに類するテキストのエンコードとデコードに最適です。高度な技術的な内容、会話的な内容、またはその他の非標準的なテキストは、モデルの分布外となる可能性があり、このような入力に対してはモデルの性能が低下する場合があります。
Bottleneck T5の埋め込みは常に長さ1に正規化されます。エンコーダは長さ1の埋め込みを生成し、デコーダへの入力も長さ1に正規化されます。
プロパティ |
詳細 |
開発者 |
Linus Lee |
モデルタイプ |
注意プーリングされたボトルネックとゲート付きクロスアテンションを持つT5スタイルのエンコーダ - デコーダトランスフォーマー |
言語 (NLP) |
英語 |
ライセンス |
MIT |
ファインチューニング元のモデル |
言語モデリング適応型T5 v1.1 |
学習の詳細
Contraは、言語モデリング適応型T5 v1.1チェックポイントから初期化され、長さでフィルタリングされた英語のWikipediaデータセットのサブセットで、30%のトークンをランダムにマスクしたノイズ除去オートエンコーダとして、Adafactorオプティマイザを使用して1エポック学習されました。
モデルファミリーとチェックポイント
私は最初にthesephist/contra-bottleneck-t5-large-wikipedia
で実験することをおすすめします。これはモデルサイズと出力品質のバランスが良いです。ただし、私は330Mから3Bパラメータまでの4つのバリエーションを学習させています。
📄 ライセンス
このモデルはMITライセンスの下で提供されています。