第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Tensorflow 2.0 Hugging Face Transformers

Tensorflow 2.0 Hugging Face Transformers

至尊寶的傳說 2022-11-09 16:21:59
概括:我想微調(diào) BERT 以在自定義數(shù)據(jù)集上進(jìn)行句子分類。我遵循了一些我發(fā)現(xiàn)的例子,比如這個,這非常有幫助。我也看過這個要點(diǎn)。我遇到的問題是,在對某些樣本進(jìn)行推理時,輸出的維度超出了我的預(yù)期。當(dāng)我對 23 個樣本進(jìn)行推理時,我得到一個具有 numpy 維度數(shù)組 (1472, 42) 的元組,其中 42 是類數(shù)。我希望尺寸(23、42)。代碼和其他詳細(xì)信息:我使用 Keras 對經(jīng)過訓(xùn)練的模型進(jìn)行推理,如下所示:preds = model.predict(features)特征被標(biāo)記并轉(zhuǎn)換為數(shù)據(jù)集的地方:for sample, ground_truth in tests:    test_examples.append(InputExample(text=sample, category_index=ground_truth))features = convert_examples_to_tf_dataset(test_examples, tokenizer)哪里sample可以是例如"A test sentence I want classified"并且ground_truth可以是例如12哪個是編碼標(biāo)簽。因?yàn)槲疫M(jìn)行推理,所以我提供的作為基本事實(shí)的內(nèi)容當(dāng)然不重要。
查看完整描述

2 回答

?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個贊

我發(fā)現(xiàn)了問題——如果你在使用 Tensorflow 數(shù)據(jù)集(tf.data.Dataset)時得到意外的維度,可能是因?yàn)闆]有運(yùn)行.batch

所以在我的例子中:

features = convert_examples_to_tf_dataset(test_examples, tokenizer)

添加:

features = features.batch(BATCH_SIZE)

使這項工作如我所料。所以,這不是與 相關(guān)的問題TFBertForSequenceClassification,只是因?yàn)槲业妮斎氩徽_。我還想添加對這個答案的引用,這讓我發(fā)現(xiàn)了問題。


查看完整回答
反對 回復(fù) 2022-11-09
?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個贊

我報告了我的示例,其中我嘗試預(yù)測 3 個文本樣本并獲得 (3, 42) 作為輸出形狀


### define model

config = BertConfig.from_pretrained(

    'bert-base-multilingual-cased',

    num_labels=42,

    output_hidden_states=False,

    output_attentions=False

)

model = TFBertForSequenceClassification.from_pretrained('bert-base-multilingual-cased', config=config)


optimizer = tf.keras.optimizers.Adam(learning_rate=3e-05, epsilon=1e-08)

loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

metric = tf.keras.metrics.SparseCategoricalAccuracy(name='accuracy')

model.compile(optimizer=optimizer,

              loss=loss,

              metrics=[metric])


### import tokenizer

tokenizer = AutoTokenizer.from_pretrained("bert-base-multilingual-cased")


### utility functions for text encoding

def return_id(str1, str2, length):


    inputs = tokenizer.encode_plus(str1, str2,

        add_special_tokens=True,

        max_length=length)


    input_ids =  inputs["input_ids"]

    input_masks = [1] * len(input_ids)

    input_segments = inputs["token_type_ids"]


    padding_length = length - len(input_ids)

    padding_id = tokenizer.pad_token_id


    input_ids = input_ids + ([padding_id] * padding_length)

    input_masks = input_masks + ([0] * padding_length)

    input_segments = input_segments + ([0] * padding_length)


    return [input_ids, input_masks, input_segments]


### encode 3 sentences

input_ids, input_masks, input_segments = [], [], []

for instance in ['hello hello', 'ciao ciao', 'marco marco']:


    ids, masks, segments = \

    return_id(instance, None, 100)


    input_ids.append(ids)

    input_masks.append(masks)

    input_segments.append(segments)


input_ = [np.asarray(input_ids, dtype=np.int32), 

          np.asarray(input_masks, dtype=np.int32), 

          np.asarray(input_segments, dtype=np.int32)]


### make prediction

model.predict(input_).shape # ===> (3,42)


查看完整回答
反對 回復(fù) 2022-11-09
  • 2 回答
  • 0 關(guān)注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號