Upload 3 files
Browse filesFiles changed (3)
- 384_bit_comp.ipynbAdded+165
- 384_bit_comp.modelAdded+3
- 384_bit_comp.vocabAdded+259
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# pip -q install sentencepiece\n", "# pip -q install numpy\n", "# pip -q install sentence_transformers\n", "# pip -q install datasets\n", "import sentencepiece as spm\n", "import numpy as np\n", "from datasets import load_dataset\n", "from collections import Counter\n", "from sentence_transformers import SentenceTransformer" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "model = SentenceTransformer('all-MiniLM-L6-v2')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\"My favourite food is anything I didn't have to cook myself.\", 'Now if he does off himself, everyone will think hes having a laugh screwing with people instead of actually dead', 'WHY THE FUCK IS BAYLESS ISOING', 'To make her feel threatened', 'Dirty Southern Wankers', \"OmG pEyToN iSn'T gOoD eNoUgH tO hElP uS iN tHe PlAyOfFs! Dumbass Broncos fans circa December 2015.\", 'Yes I heard abt the f bombs! That has to be why. Thanks for your reply:) until then hubby and I will anxiously wait 😝', 'We need more boards and to create a bit more space for [NAME]. Then we’ll be good.', 'Damn youtube and outrage drama is super lucrative for reddit', 'It might be linked to the trust factor of your friend.']\n" ] } ], "source": [ "dataset = load_dataset(\"go_emotions\")\n", "texts = dataset[\"train\"][\"text\"]\n", "print(texts[:10])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def binarize(embeddings, sensitivity=0.1):\n", "\treturn np.where(embeddings >= sensitivity, 1, 0)\n", "\n", "def preprocess(strings):\n", "\treturn \"\\n\".join([\"\".join(map(str, s)) for s in processed_string])\n", "\n", "# Obtain sentence embeddings\n", "embeddings = model.encode(texts)\n", "binary_hashes = binarize(embeddings)\n", "binary_string = preprocess(binary_hashes)\n", "print(binary_string[:500])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Save passage to a temporary file\n", "with open(\"passage.txt\", \"w\") as f:\n", "\tf.write(binary_string)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Training options documentation: https://github.com/google/sentencepiece/blob/master/doc/options.md\n", "# Training takes 3 hours to complete on GTX 1650 mobile\n", "spm.SentencePieceTrainer.train(\n", "\tinput='passage.txt',\n", "\tmodel_prefix='384_bit_comp',\n", "\tvocab_size=256 + 3, # To exclude <unk>, </s>, <s>\n", "\tcharacter_coverage=1.00,\n", "\tmax_sentencepiece_length=384,\n", "\tmodel_type='unigram',\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "length: 13\n", "encoded_tokens: ['▁0000000', '0000000000000001000000000000000000000', '00000000001000100', '1000000', '00000000000000000000000000000001000000000000000000000000000000000000000000000000000000', '00000000000000000001000000000000000000000000000000000', '0000000000000000000000000000000001000', '00000000000000000000000100000000000000000', '00000000010', '0000000000000000000000000000000000000100', '00000000000100000000000000000', '00000000010', '00001000']\n", "encoded_ids: 1ab2ed09d7a9617206894e0608\n", "same?: True\n", "count: Counter({'00000000010': 2, '▁0000000': 1, '0000000000000001000000000000000000000': 1, '00000000001000100': 1, '1000000': 1, '00000000000000000000000000000001000000000000000000000000000000000000000000000000000000': 1, '00000000000000000001000000000000000000000000000000000': 1, '0000000000000000000000000000000001000': 1, '00000000000000000000000100000000000000000': 1, '0000000000000000000000000000000000000100': 1, '00000000000100000000000000000': 1, '00001000': 1})\n" ] } ], "source": [ "bpe_processor = spm.SentencePieceProcessor(model_file='384_bit_comp.model')\n", "\n", "def encode_id(bit_text):\n", "\tencoded_pieces = bpe_processor.encode_as_pieces(bit_text)\n", "\tencoded_ids = [bpe_processor.piece_to_id(s) - 3 for s in encoded_pieces]\n", "\tassert any([id_ <= 255 for id_ in encoded_ids])\n", "\tstring_ids = \"\".join([format(id_, \"02x\") for id_ in encoded_ids])\n", "\treturn string_ids\n", "\n", "def decode_id(hex_string):\n", "\tu8_array = np.frombuffer(bytes.fromhex(hex_string), dtype='<u1') + 3\n", "\tencoded_tokens = [bpe_processor.id_to_piece(int(id_)) for id_ in u8_array]\n", "\treturn encoded_tokens\n", "\n", "# Encode text\n", "new_sentence = \"000000000000000000000010000000000000000000000000000000100010010000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000100000000000000000000000000100000000000000000000000000000000000000100000000000001000000000000000000000000001000001000\"\n", "encoded_tokens = bpe_processor.encode_as_pieces(new_sentence)\n", "encoded_ids = encode_id(new_sentence)\n", "decoded_tokens = decode_id(encoded_ids)\n", "\n", "print(\"length:\", len(encoded_tokens))\n", "print(\"encoded_tokens:\", encoded_tokens)\n", "print(\"encoded_ids:\", encoded_ids)\n", "print(\"same?:\", encoded_tokens == decoded_tokens)\n", "\n", "count = Counter(encoded_tokens)\n", "print(\"count:\", count)" ] } ], "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.11.4" } }, "nbformat": 4, "nbformat_minor": 2}123version https://git-lfs.github.com/spec/v1oid sha256:88c8692c639e695577c0c9fde7581d27e5fd47d34f9fda3d9ca2b9ef913276basize 249691123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259<unk> 0<s> 0</s> 0▁ -3.528450000000000000000000 -3.6559600000000000000000000000 -3.7853100000000000000000000000000000 -3.916640000000000000000000000000000000000000 -4.0823400000000000000000000000000000000000000000 -4.1679600000000010 -4.241390010000 -4.3234700001000 -4.336241000000 -4.43932000000000000000000000000000000000000000000000000000000000 -4.48283000000010000 -4.4890200000000000000000000000000000000000000000000000000000 -4.50439000000100 -4.6056400000000000000001000 -4.636650001000 -4.63744100000000 -4.6783200000000000000000000000000000000000000000000000000000000000 -4.680820000000000100000 -4.68338000000000001 -4.69539001000000000 -4.744170000100000000000 -4.748460000000010000000000 -4.7544101000000000000000000 -4.7687600000000000000000000000000000000000000000000000000000000000000 -4.789210100000000000000 -4.80865▁0000000 -4.810390000001000000000 -4.8389900000000000000000001 -4.88581000001000000 -4.92111000000000000000000000000000000000000000000000000000000000000000000 -4.933050000000000001000 -4.935480000000000000000000000000000000000000000000000000000000000000000000 -4.973440000000000000100 -5.000721000000000000 -5.062670000000000000001 -5.066990000000000000000000000000000000000000000000000000000000000000000000000 -5.0725700100000000000000000 -5.0780900000000000000000000000000000000000000000000000000000000000000000 -5.07911000000100000000000000000 -5.126410001000000000000 -5.153070000000000000001000 -5.15563000000000000000001 -5.179571000000000000000000000000 -5.1829600000000000000000000010 -5.18872000001000000000 -5.19212010000000000000000 -5.216560000000001000000000 -5.21729100000000000000000 -5.2201100000000000000000000000000000010 -5.22249000000000001000000 -5.24417000010000000000000000000000000 -5.2559100000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -5.28546000000000000001000000000 -5.28713▁000000000000 -5.2924800000000000000000000010000000 -5.29561▁000000100000000 -5.31073000000000000001000000 -5.327140001000000000000000 -5.33971000010000000000000 -5.34512▁0000000000000000000000000000000000000000000 -5.38274▁0000000000000000000000 -5.39199001000000000000000000000 -5.39265▁0000000000000000000000000000000000000000000000000 -5.3984400000000000000000000000100 -5.401730000000100000000000000000000000000000 -5.413130000000000000100000 -5.418550100000000000000000000000000000000000 -5.42138000001000000000000000000 -5.421760010000000000000000000000000000000000 -5.4231600000000100000000000000000000 -5.44607000100000000000000000000 -5.454020000000000000000000000000000000000000000000000001 -5.46818000000000000000000100 -5.4731100000010000000000000 -5.4837600000000000000000000000010000 -5.49755000000000000000000000000001000 -5.504580000000000000000000001000 -5.5182300000000000100000000000000000 -5.5338500000000000000000000000001000 -5.53677▁000000000000000000000000000000 -5.54138010000000000000000000000000000000 -5.5492800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -5.573700000000000000000010000000000 -5.57618000001000000000000000000000000000 -5.5791200000000000000010000000000 -5.5820900000100000000000000000000 -5.58931▁00000000000000000 -5.60556000000000010000000000000 -5.6111200000000000000000001000000000 -5.61525001000000000000000000000000000000 -5.6190300000000000000000000001000000000 -5.6195500000000010000000000000000000 -5.6248300000000000000001000000000000 -5.6449200000000000000000000000000000000000010 -5.647710000000000000000000000000000 -5.6569900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -5.661560000000000000000000000000000000001000 -5.66588000000000000000000000000010000 -5.6681900000001000000000000000000000 -5.69042000000000000000000000000000000001 -5.69428000100000000000000000000000000 -5.702151000000000000000000000000000000000000 -5.7041610000000000000000000000000000000000000000000000 -5.71607000000000000000001000000000 -5.73038000000010000000000000000 -5.7337301000000000000000000000000000000000000000000000000000000 -5.7715110000000000000000000000000000000000000000000000000 -5.7766200000000000000000000000000001 -5.779320000001000000000000000000000000000000 -5.79451001000000000000000000000000000000000000000000000000000000 -5.798800000010000000000000000000000000000000000 -5.80258000000000000000000000000000000000000000000010 -5.802650001000000000000000000000000000000000 -5.8133200000000000000000000000100000000000000000 -5.8196400001000000000000000000000000000000000000 -5.825630000000000000000000000000000001000000000 -5.8314500000000000000000000000000000000000000000000100 -5.84554000000100000000000000000000000000000000000000000000000 -5.8500700000000100000000000000000000000000000000 -5.8681800000000000000000000100000000 -5.8928100100000000000000000000000000000000000000000000000 -5.90395000000000000100000000000000000000 -5.90702000000000000000000010000000000000 -5.9175810000000000000000000000000000000000000000 -5.919700000001000000000000000000000000000000000000000000 -5.9252500000000000000000000000000000000000000000010000 -5.928520000000000000000000000000000000000000010000000000 -5.93005▁0000001 -5.93155▁0000000000000000000000000000000000000000000000000000000000000000000000000000 -5.9361400000000000000000010000000000000000000000 -5.93977110 -5.945291010 -5.9482200010000000000000000000000000000000000000000000000000 -5.952▁00000000000000000000000000000000000 -5.9630300100000000000000000000000000000000000000000000000000000 -5.974700000000000000001000000000000000000000000 -5.986060000000000000000000000000000000000000100 -5.9886800000000000000000000000010000000000000000000000000 -5.99035000000000000000000001000000000000000000000000000 -5.9948800000000001000000000000000000000 -6.0022000010000000000000000000000000000000000000000000000000000 -6.01749000000000000000000000000000100000 -6.019571001 -6.02025100000000000000000000000000000000000000000000 -6.0324700000100000000000000000000000000000000000000000000000000 -6.0454500000000000000000000000000100000000000000000000 -6.066510000000000010000000000000000000000000000000000000 -6.066850000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -6.0791500000000000000000000000000000000000000001000000000 -6.0865100000000000010000000000000000000000000000000000 -6.0873500000000000001000000000000000000000000000000000000 -6.093430000000000000000100000000000000000000000000000000000000000 -6.094240000000000000000000000000000000100000000 -6.10272010010 -6.1031600000000000010000000000000000 -6.11175000000000000000100000000000000 -6.12087000000000000000000000000000000000000000000000000001000 -6.1420910001 -6.1457200000100000000000000000000000000000000000000000000 -6.161320000000000100000000000000000000000000000000000000 -6.18804000000000000000000000000000000000000000000000000000000000000010 -6.2186100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -6.2314500000000000000000000000000000000000000000000000000000010000 -6.2363700001000000000000000000000000000000000000000000000000000 -6.24755011000 -6.2593000000000000001000000000000000000000000000000000 -6.2603800000000000010000000000000000000000000000 -6.270780000000000000000000000000001000000000000000 -6.2825200000000000000000001000000000000000000000000000000000 -6.282900000100000000000000000000000000000000000000000000000000000 -6.2836200000000000000000000000000000010000000000000000000000 -6.29415100000000000000000000000000000000000000000000000000000000000000 -6.30668000000000000001000000000000000000000000000000000000000000 -6.31072000000000000000000000000000001000000000000000000000000000 -6.31490000000000000000000000001000000000000000000000000 -6.3200001000001000 -6.33768000000000001000000000000000000000000000000000000000000000000000000 -6.341430000000000000001000000000000000000000 -6.357050000000000001000000000000000000000000000000000000 -6.360220000000011 -6.36820000000000000000001000000000000000000000000000000 -6.36884000000000000000000000010000000000000000000000000000000000000000 -6.390450000000000000000000000000000100000000000000 -6.39119010000000000000000000000000000000000000000000000000000000000000000000000 -6.43118000000000000000010000000000000000000000000000000000000000000000 -6.432900001100 -6.440950000000000000000000000000000000010000000000000000 -6.4642700000000000000000000000010000000000000000000000000000000000 -6.46542▁000000100000000000000000000000000000000000000000000000000000000000000 -6.4844500000000000000000000000001000000000000000000000000000000000000 -6.5025900001010 -6.510771000001 -6.516881000010000 -6.51997▁010000 -6.525650000000000100000000000000000000000000000000000000000000000000000000000 -6.52890000000000000000000000000000000000000000000000000000000001000000000 -6.572331000000000000000000000000000000000000000000000000000000000000000000 -6.573720001000000000000000000000000000000000000000000000000000000000000000000000000000 -6.593480000000100000000000000000000000000000000000000000000000000000000 -6.6037100000001001000 -6.608590101000000000 -6.618630000000000000000000000000000000000000000000000000000000000000000000000001 -6.6186910000001000 -6.6289900000000000000000000100000000000000000000000000000000000000 -6.6368100000000000000000000000000000000000000000000000000010000000000 -6.64363000001000000000000000000000000000000000000000000000000000000000000000000 -6.655130100000000000000000000000000000000000000000000000000000000000000000000000000 -6.655590000000000000000000010000000000000000000000000000000000000000000000000000000000 -6.65961000000000000000000000000000000100000000000000000000000000 -6.6616100110000000000 -6.66161100000000000000000000000000000000000000000000000000000000000000000000000000000000000 -6.663320000000000000000000000010000000000000000000000000000000000000000000000 -6.66687000000000000000000000000000000000000000000000000000000000001000 -6.6764600000001000000010 -6.6929600000000000000000000000000000001000000000000000000000000000000000000000000000000000000 -6.696330000001000000001 -6.723630001010000 -6.72549▁000000000000000000000000000000010000000000000000000000000000000000000 -6.7322000000000000000000000000000000000000000000000000010000000 -6.73234000010000000000000000000000000000000000000000000000000000000000000 -6.7338600000000001001000 -6.76230000010000000000000000000000000000000000000000000000000000000000000000000000000 -6.790720000000000000000000000000000000000000000100000000000000000000000 -6.791160010100000000000 -6.7918400000000000100001 -6.793350000000000000000000000000000001000000000000000000000000000000000000 -6.794070000000000000010100 -6.8007401100000000000000000 -6.80084000000000000000010000000000000000000000000000000000000000000000000000000000000000000 -6.813200000000000000000000000000100000000000000000000000000000000000000000000000 -6.8195000000000000000001000000000000000000000000000000000000000000000000000000 -6.8205200001000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -6.84508001000000100 -6.8523000100100000000000000000 -6.855110100000001000000000 -6.874020000000000000100000000000000000000000000000000000000000000000000000000 -6.8858400000000001000100 -6.89382▁000010 -6.9212100000000000000000000000000000000000000000000000000000000000000000000000000000000000010 -6.92647000000000000000000000000000000000000010000000000000000000000000000000000 -6.93266▁000000000000000010000000000000000000000000000000 -6.948820100010000000000 -6.951750000011000000000 -6.989150010000000010 -6.992050000000000000101000 -6.99590100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -7.006830000000000001000000000000000000000000000000000000000000000000000000000000000000 -7.04102000000000000100000010000000000 -7.0483200000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000 -7.06727000001010000000 -7.0696900010000000100 -7.078100010001000000000 -7.0898501000000001000 -7.094741 -8.42410 -8.4242384_bit_comp.ipynbAdded+165
384_bit_comp.modelAdded+3
384_bit_comp.vocabAdded+259