File size: 4,602 Bytes
e9ab399 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "58d45708",
"metadata": {},
"outputs": [],
"source": [
"from transformers import XLNetTokenizer, XLNetModel, XLNetConfig, AutoTokenizer, AutoModelWithLMHead, pipeline"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e0314358",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"model.ckpt-320000.data-00000-of-00001 model.ckpt-320000.meta\r\n",
"model.ckpt-320000.index\r\n"
]
}
],
"source": [
"# !tar -zxf xlnet-large-2021-09-06.tar.gz\n",
"# !rm xlnet-large-2021-09-06.tar.gz\n",
"!ls xlnet-large"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "59d2c8b5",
"metadata": {},
"outputs": [],
"source": [
"# !wget https://raw.githubusercontent.com/huseinzol05/malaya/master/pretrained-model/xlnet/tokenizer/sp10m.cased.v9.vocab\n",
"# !wget https://raw.githubusercontent.com/huseinzol05/malaya/master/pretrained-model/xlnet/tokenizer/sp10m.cased.v9.model"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f35e09f4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('./tokenizer_config.json',\n",
" './special_tokens_map.json',\n",
" './spiece.model',\n",
" './added_tokens.json')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tokenizer = XLNetTokenizer('sp10m.cased.v9.model', do_lower_case = False)\n",
"tokenizer.save_pretrained('./')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4438ff5c",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"config = {\n",
" \"d_head\": 64,\n",
" \"d_inner\": 4096,\n",
" \"d_model\": 1024,\n",
" \"ff_activation\": \"gelu\",\n",
" \"n_head\": 16,\n",
" \"n_layer\": 20,\n",
" \"n_token\": 32000,\n",
" \"untie_r\": True\n",
"}\n",
"\n",
"with open('config.json', 'w') as fopen:\n",
" json.dump(config, fopen)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a265f23c",
"metadata": {},
"outputs": [],
"source": [
"# !transformers-cli convert --model_type xlnet \\\n",
"# --tf_checkpoint xlnet-large/model.ckpt-320000 \\\n",
"# --config config.json \\\n",
"# --pytorch_dump_output ./"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "22b94055",
"metadata": {},
"outputs": [],
"source": [
"config = XLNetConfig(f'./config.json')\n",
"config.vocab_size = 32000\n",
"config.d_inner = 4096\n",
"config.d_model = 1024\n",
"config.n_head = 16\n",
"config.n_layer = 20"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "17c6d447",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Some weights of the model checkpoint at ./ were not used when initializing XLNetModel: ['lm_loss.weight', 'lm_loss.bias']\n",
"- This IS expected if you are initializing XLNetModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
"- This IS NOT expected if you are initializing XLNetModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
]
}
],
"source": [
"model = XLNetModel.from_pretrained('./', config = config)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d0fc0138",
"metadata": {},
"outputs": [],
"source": [
"tokenizer = XLNetTokenizer.from_pretrained('./',do_lower_case = False)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ec2c0661",
"metadata": {},
"outputs": [],
"source": [
"model.save_pretrained('./')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|