【发布时间】:2022-02-16 02:50:14
【问题描述】:
在任何人建议 pytorch 和其他东西之前,我只专门寻找 Tensorflow + 预训练 + MLM 任务。我知道,有很多 PyTorch 的博客和很多关于 Tensorflow 微调(分类)的博客。
谈到这个问题,我得到了一个语言模型,它是英语 + LaTex,其中文本数据可以表示物理、化学、数学和生物学中的任何文本,任何典型的例子都可以看起来像这样: Link to OCR image
"Find the value of function x in the equation: \n \\( f(x)=\\left\\{\\begin{array}{ll}x^{2} & \\text { if } x<0 \\\\ 2 x & \\text { if } x \\geq 0\\end{array}\\right. \\)"
所以我的语言模型需要理解 \geq \\begin array \eng \left \right 而不是英语,这就是为什么我需要先在预训练的 BERT 或 SciBERT 上训练 MLM 以同时拥有两者。于是我就去网上挖了一些教程:
MLMtraining onTensorflowBUT from Scratch; I need pre-trainedMLMon pre-trained but inPytorch; I needTensorflow- Fine Tuning with Keras; It is for classification but I want
MLM
我已经有了一个微调分类模型。部分代码如下:
tokenizer = transformers.BertTokenizer.from_pretrained('bert-large-uncased')
def regular_encode(texts, tokenizer, maxlen=maxlen):
enc_di = tokenizer.batch_encode_plus(texts, return_token_type_ids=False,padding='max_length',max_length=maxlen,truncation = True,)
return np.array(enc_di['input_ids'])
Xtrain_encoded = regular_encode(X_train.astype('str'), tokenizer, maxlen=maxlen)
ytrain_encoded = tf.keras.utils.to_categorical(y_train, num_classes=classes,dtype = 'int32')
def build_model(transformer, loss='categorical_crossentropy', max_len=maxlen, dense = 512, drop1 = 0.3, drop2 = 0.3):
input_word_ids = tf.keras.layers.Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
sequence_output = transformer(input_word_ids)[0]
cls_token = sequence_output[:, 0, :]
#Fine Tuning Model Start
x = tf.keras.layers.Dropout(drop1)(cls_token)
x = tf.keras.layers.Dense(512, activation='relu')(x)
x = tf.keras.layers.Dropout(drop2)(x)
out = tf.keras.layers.Dense(classes, activation='softmax')(x)
model = tf.keras.Model(inputs=input_word_ids, outputs=out)
return model
我能得到的唯一有用的东西是HuggingFace那个
借助 TensorFlow 和 PyTorch 模型之间的紧密互操作性,您甚至可以保存模型,然后将其重新加载为 PyTorch 模型(反之亦然)
from transformers import AutoModelForSequenceClassification
model.save_pretrained("my_imdb_model")
pytorch_model = AutoModelForSequenceClassification.from_pretrained("my_imdb_model", from_tf=True)
所以也许我可以训练pytorch MLM,然后将其加载为tensorflow 微调分类模型?
有没有其他办法?
【问题讨论】:
标签: tensorflow keras deep-learning nlp huggingface-transformers