From e688582b45db4e7b0ca90402b2d1c402777f1679 Mon Sep 17 00:00:00 2001 From: wassname Date: Sun, 7 May 2023 19:23:22 +0800 Subject: [PATCH] misc --- .../001_mjc_CCS-checkpoint.ipynb | 8020 ++++++++++++++++- 001_mjc_CCS.ipynb | 8020 ++++++++++++++++- 2 files changed, 15718 insertions(+), 322 deletions(-) diff --git a/.ipynb_checkpoints/001_mjc_CCS-checkpoint.ipynb b/.ipynb_checkpoints/001_mjc_CCS-checkpoint.ipynb index 4e2cec5..6ff8b27 100644 --- a/.ipynb_checkpoints/001_mjc_CCS-checkpoint.ipynb +++ b/.ipynb_checkpoints/001_mjc_CCS-checkpoint.ipynb @@ -13,8 +13,8 @@ "execution_count": 1, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:18:19.033245Z", - "start_time": "2023-05-07T09:18:17.562000Z" + "end_time": "2023-05-07T11:21:20.941342Z", + "start_time": "2023-05-07T11:21:19.408201Z" } }, "outputs": [ @@ -64,8 +64,8 @@ "execution_count": 2, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:18:27.830076Z", - "start_time": "2023-05-07T09:18:19.034316Z" + "end_time": "2023-05-07T11:21:30.089243Z", + "start_time": "2023-05-07T11:21:20.942966Z" } }, "outputs": [ @@ -99,7 +99,7 @@ " warn(msg)\n", "/home/wassname/miniforge3/envs/dlk2/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:145: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/usr/share/gconf/cinnamon.default.path')}\n", " warn(msg)\n", - "/home/wassname/miniforge3/envs/dlk2/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:145: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('@/tmp/.ICE-unix/5335,unix/wassname-fractal-desktop'), PosixPath('local/wassname-fractal-desktop')}\n", + "/home/wassname/miniforge3/envs/dlk2/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:145: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('local/wassname-fractal-desktop'), PosixPath('@/tmp/.ICE-unix/5335,unix/wassname-fractal-desktop')}\n", " warn(msg)\n", "/home/wassname/miniforge3/envs/dlk2/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:145: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('0'), PosixPath('1')}\n", " warn(msg)\n", @@ -111,7 +111,7 @@ "Either way, this might cause trouble in the future:\n", "If you get `CUDA error: invalid device function` errors, the above might be the cause and the solution is to make sure only one ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] in the paths that we search based on your env.\n", " warn(msg)\n", - "Loading checkpoint shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:06<00:00, 3.04s/it]\n" + "Loading checkpoint shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:06<00:00, 3.19s/it]\n" ] }, { @@ -212,15 +212,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T01:39:14.495263Z", - "start_time": "2023-05-07T01:39:14.495255Z" + "end_time": "2023-05-07T11:21:30.093059Z", + "start_time": "2023-05-07T11:21:30.090420Z" } }, "outputs": [], - "source": [] + "source": [ + "# get the tokens for 0 and 1, we will use these later...\n", + "id_0, id_1 = tokenizer('0')['input_ids'][-1], tokenizer('1')['input_ids'][-1]" + ] }, { "cell_type": "markdown", @@ -235,11 +238,11 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:18:30.433340Z", - "start_time": "2023-05-07T09:18:27.832057Z" + "end_time": "2023-05-07T11:21:34.754867Z", + "start_time": "2023-05-07T11:21:30.094318Z" }, "scrolled": false }, @@ -249,7 +252,7 @@ "output_type": "stream", "text": [ "Found cached dataset amazon_polarity (/home/wassname/.cache/huggingface/datasets/amazon_polarity/amazon_polarity/3.0.0/a27b32b7e7b88eb274a8fa8ba0f654f1fe998a87c22547557317793b5d2772dc)\n", - "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00, 54.08it/s]\n" + "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00, 53.01it/s]\n" ] } ], @@ -267,27 +270,57 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 5, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:20:39.763220Z", - "start_time": "2023-05-07T09:20:39.760724Z" + "end_time": "2023-05-07T11:21:34.760016Z", + "start_time": "2023-05-07T11:21:34.756939Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Review: \"Whoever wrote the screenplay for this movie obviously never consulted any books about Lucille Ball, especially her autobiography. I've never seen so many mistakes in a biopic, ranging from her early years in Celoron and Jamestown to her later years with Desi. I could write a whole list of factual errors, but it would go on for pages. In all, I believe that Lucille Ball is one of those inimitable people who simply cannot be portrayed by anyone other than themselves. If I were Lucie Arnaz and Desi, Jr., I would be irate at how many mistakes were made in this film. The filmmakers tried hard, but the movie seems awfully sloppy to me.\"\n", + "###\n", + "Is this review negative? 1\n", + "###\n", + "Review: \"This version of Anna Christie is in German. Greta Garbo again plays Anna Christie, but all of the other characters have different actors from the English version. Both were filmed back to back because Garbo had such a following in Germany. Garbo herself supposedly favored her Anna Christie in this version over the English version. It's a good tale and a must-see for Garbo fans.\"\n", + "###\n", + "Is this review negative? 0\n", + "###\n", + "Review: \"I think this is a lovely family movie. There are plenty of hilarious scenes and heart-warming moments to be had throughout the movie. The actors are great and the effects well executed throughout. Danny Glover plays George Knox who manages the terrible baseball team 'The Angels' and is great throughout the film. Also fantastic are the young actors Joseph Gordon-Levitt and Milton Davis Jr. Christopher Lloyd is good as Al 'The Angel' and the effects are great in this top notch Disney movie. A touching and heart-warming movie which everyone should enjoy.\"\n", + "###\n", + "Is this review positive? 1\n", + "###\n", + "Review: \"The movie was the worst.... not!\"\n", + "###\n", + "Is this review negative? \n" + ] + } + ], "source": [ "def format_imdb(text, label):\n", " return f\"\"\"Review: \"Whoever wrote the screenplay for this movie obviously never consulted any books about Lucille Ball, especially her autobiography. I've never seen so many mistakes in a biopic, ranging from her early years in Celoron and Jamestown to her later years with Desi. I could write a whole list of factual errors, but it would go on for pages. In all, I believe that Lucille Ball is one of those inimitable people who simply cannot be portrayed by anyone other than themselves. If I were Lucie Arnaz and Desi, Jr., I would be irate at how many mistakes were made in this film. The filmmakers tried hard, but the movie seems awfully sloppy to me.\"\n", - "This is negative? 1\n", + "###\n", + "Is this review negative? 1\n", "###\n", "Review: \"This version of Anna Christie is in German. Greta Garbo again plays Anna Christie, but all of the other characters have different actors from the English version. Both were filmed back to back because Garbo had such a following in Germany. Garbo herself supposedly favored her Anna Christie in this version over the English version. It's a good tale and a must-see for Garbo fans.\"\n", - "This review is negative? 0\n", + "###\n", + "Is this review negative? 0\n", "###\n", "Review: \"I think this is a lovely family movie. There are plenty of hilarious scenes and heart-warming moments to be had throughout the movie. The actors are great and the effects well executed throughout. Danny Glover plays George Knox who manages the terrible baseball team 'The Angels' and is great throughout the film. Also fantastic are the young actors Joseph Gordon-Levitt and Milton Davis Jr. Christopher Lloyd is good as Al 'The Angel' and the effects are great in this top notch Disney movie. A touching and heart-warming movie which everyone should enjoy.\"\n", - "This review is positive? 1\n", + "###\n", + "Is this review positive? 1\n", "###\n", "Review: \"{text}\"\n", - "This review is {label}? \"\"\"" + "###\n", + "Is this review {'positive' if label else 'negative'}? \"\"\"\n", + "\n", + "\n", + "\n", + "print(format_imdb(\"The movie was the worst.... not!\", 0))" ] }, { @@ -312,116 +345,183 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 6, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:20:39.793592Z", - "start_time": "2023-05-07T09:20:39.766812Z" + "end_time": "2023-05-07T11:21:34.782525Z", + "start_time": "2023-05-07T11:21:34.760992Z" } }, "outputs": [], "source": [ - "def get_encoder_hidden_states(model, tokenizer, input_text, layer=-1):\n", - " \"\"\"\n", - " Given an encoder model and some text, gets the encoder hidden states (in a given layer, by default the last) \n", - " on that input text (where the full text is given to the encoder).\n", + "# def get_encoder_hidden_states(model, tokenizer, input_text, layer=-1):\n", + "# \"\"\"\n", + "# Given an encoder model and some text, gets the encoder hidden states (in a given layer, by default the last) \n", + "# on that input text (where the full text is given to the encoder).\n", "\n", - " Returns a numpy array of shape (hidden_dim,)\n", - " \"\"\"\n", - " # tokenize\n", - " encoder_text_ids = tokenizer(input_text, truncation=True, return_tensors=\"pt\").input_ids.to(model.device)\n", + "# Returns a numpy array of shape (hidden_dim,)\n", + "# \"\"\"\n", + "# # tokenize\n", + "# encoder_text_ids = tokenizer(input_text, truncation=True, return_tensors=\"pt\").input_ids.to(model.device)\n", "\n", - " # forward pass\n", - " with torch.no_grad():\n", - " output = model(encoder_text_ids, output_hidden_states=True)\n", + "# # forward pass\n", + "# with torch.no_grad():\n", + "# output = model(encoder_text_ids, output_hidden_states=True)\n", "\n", - " # get the appropriate hidden states\n", - " hs_tuple = output[\"hidden_states\"]\n", + "# # get the appropriate hidden states\n", + "# hs_tuple = output[\"hidden_states\"]\n", " \n", - " hs = hs_tuple[layer][0, -1].detach().cpu().numpy()\n", + "# hs = hs_tuple[layer][0, -1].detach().cpu().numpy()\n", "\n", - " return hs\n", + "# return hs\n", "\n", - "def get_encoder_decoder_hidden_states(model, tokenizer, input_text, layer=-1):\n", - " \"\"\"\n", - " Given an encoder-decoder model and some text, gets the encoder hidden states (in a given layer, by default the last) \n", - " on that input text (where the full text is given to the encoder).\n", + "# def get_encoder_decoder_hidden_states(model, tokenizer, input_text, layer=-1):\n", + "# \"\"\"\n", + "# Given an encoder-decoder model and some text, gets the encoder hidden states (in a given layer, by default the last) \n", + "# on that input text (where the full text is given to the encoder).\n", "\n", - " Returns a numpy array of shape (hidden_dim,)\n", - " \"\"\"\n", - " # tokenize\n", - " encoder_text_ids = tokenizer(input_text, return_tensors=\"pt\").input_ids.to(model.device)\n", - " decoder_text_ids = tokenizer(\"\", return_tensors=\"pt\").input_ids.to(model.device)\n", + "# Returns a numpy array of shape (hidden_dim,)\n", + "# \"\"\"\n", + "# # tokenize\n", + "# encoder_text_ids = tokenizer(input_text, return_tensors=\"pt\").input_ids.to(model.device)\n", + "# decoder_text_ids = tokenizer(\"\", return_tensors=\"pt\").input_ids.to(model.device)\n", "\n", - " # forward pass\n", - " with torch.no_grad():\n", - " output = model(encoder_text_ids, decoder_input_ids=decoder_text_ids, output_hidden_states=True)\n", + "# # forward pass\n", + "# with torch.no_grad():\n", + "# output = model(encoder_text_ids, decoder_input_ids=decoder_text_ids, output_hidden_states=True)\n", "\n", - " # get the appropriate hidden states\n", - " hs_tuple = output[\"encoder_hidden_states\"]\n", - " hs = hs_tuple[layer][0, -1].detach().cpu().numpy()\n", + "# # get the appropriate hidden states\n", + "# hs_tuple = output[\"encoder_hidden_states\"]\n", + "# hs = hs_tuple[layer][0, -1].detach().cpu().numpy()\n", "\n", - " return hs\n", + "# return hs\n", "\n", - "def get_decoder_hidden_states(model, tokenizer, input_text, layer=-1):\n", + "def get_decoder_hidden_states(model, tokenizer, input_text, layers=[2, -2]):\n", " \"\"\"\n", " Given a decoder model and some text, gets the hidden states (in a given layer, by default the last) on that input text\n", "\n", " Returns a numpy array of shape (hidden_dim,)\n", " \"\"\"\n", " # tokenize (adding the EOS token this time)\n", - " input_ids = tokenizer(input_text + tokenizer.eos_token, return_tensors=\"pt\").input_ids.to(model.device)\n", + " input_ids = tokenizer(input_text, return_tensors=\"pt\").input_ids.to(model.device)\n", "\n", " # forward pass\n", + " \n", " with torch.no_grad():\n", " # FIXME: should be a batch, to speed it up\n", - " output = model(input_ids, output_hidden_states=True)\n", + " output = model(input_ids, \n", + " output_hidden_states=True\n", + "# , output_attentions=True\n", + " )\n", "\n", - " # get the last layer, last token hidden states\n", - " hs_tuple = output[\"hidden_states\"]\n", - " hs = hs_tuple[layer][0, -1].detach().cpu().numpy()\n", + " # the output is large, so we will just select what we want 1) the first token with[:, 0]\n", + " # 2) selected layers with [layers]\n", + "# output['attentions'] = [output['attentions'][i] for i in layers]\n", + "# output['attentions'] = [v.detach().cpu()[:, -1] for v in output['attentions']]\n", + "# output['attentions'] = torch.concat(output['attentions'])\n", + " \n", + " output['hidden_states'] = [output['hidden_states'][i] for i in layers]\n", + " # dims [Batch, Token, Probs?]\n", + " output['hidden_states'] = [v.detach().cpu()[:, -1] for v in output['hidden_states']]\n", + " output['hidden_states'] = torch.concat(output['hidden_states'])\n", + " \n", + " o = output['logits'].detach().cpu().float().softmax(-1)\n", + " print(input_text)\n", + " print(tokenizer.decode(o.argmax(-1)[0]))\n", + " \n", + " prob_0, prob1 = o[0, 1][[id_0, id_1]]\n", + " output['ans'] = (prob1/(prob_0+prob1)).item()\n", + "\n", + " return dict(hidden_states=output['hidden_states'], ans=output['ans']\n", + "# , attentions=output['attentions']\n", + " )\n", + "# hs_tuple = output[\"hidden_states\"]\n", + "# hs_tuple = (a.detach().cpu().numpy() for a in hs_tuple)\n", + "# return output\n", + "# hs = hs_tuple[layer][0, -1].detach().cpu().numpy()\n", " \n", " # FIXME pass full output, as I want to check model zero shot accuracy!\n", "\n", - " return hs\n", + "# return hs\n", "\n", - "def get_hidden_states(model, tokenizer, input_text, layer=-1, model_type=\"encoder\"):\n", - " fn = {\"encoder\": get_encoder_hidden_states, \"encoder_decoder\": get_encoder_decoder_hidden_states,\n", + "def get_hidden_states(model, tokenizer, input_text, layers=[2, -2], model_type=\"encoder\"):\n", + " fn = {\n", + "# \"encoder\": get_encoder_hidden_states, \"encoder_decoder\": get_encoder_decoder_hidden_states,\n", " \"decoder\": get_decoder_hidden_states}[model_type]\n", "\n", - " return fn(model, tokenizer, input_text, layer=layer)" + " return fn(model, tokenizer, input_text, layers=layers)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:18:38.861641Z", - "start_time": "2023-05-07T09:18:38.859920Z" + "end_time": "2023-05-07T11:21:34.803314Z", + "start_time": "2023-05-07T11:21:34.783680Z" } }, "outputs": [], - "source": [] + "source": [ + "# print(format_imdb(text, 0))" + ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 8, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:20:40.118106Z", - "start_time": "2023-05-07T09:20:39.794801Z" + "end_time": "2023-05-07T11:21:35.557546Z", + "start_time": "2023-05-07T11:21:34.804117Z" } }, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Review: \"Whoever wrote the screenplay for this movie obviously never consulted any books about Lucille Ball, especially her autobiography. I've never seen so many mistakes in a biopic, ranging from her early years in Celoron and Jamestown to her later years with Desi. I could write a whole list of factual errors, but it would go on for pages. In all, I believe that Lucille Ball is one of those inimitable people who simply cannot be portrayed by anyone other than themselves. If I were Lucie Arnaz and Desi, Jr., I would be irate at how many mistakes were made in this film. The filmmakers tried hard, but the movie seems awfully sloppy to me.\"\n", + "###\n", + "Is this review negative? 1\n", + "###\n", + "Review: \"This version of Anna Christie is in German. Greta Garbo again plays Anna Christie, but all of the other characters have different actors from the English version. Both were filmed back to back because Garbo had such a following in Germany. Garbo herself supposedly favored her Anna Christie in this version over the English version. It's a good tale and a must-see for Garbo fans.\"\n", + "###\n", + "Is this review negative? 0\n", + "###\n", + "Review: \"I think this is a lovely family movie. There are plenty of hilarious scenes and heart-warming moments to be had throughout the movie. The actors are great and the effects well executed throughout. Danny Glover plays George Knox who manages the terrible baseball team 'The Angels' and is great throughout the film. Also fantastic are the young actors Joseph Gordon-Levitt and Milton Davis Jr. Christopher Lloyd is good as Al 'The Angel' and the effects are great in this top notch Disney movie. A touching and heart-warming movie which everyone should enjoy.\"\n", + "###\n", + "Is this review positive? 1\n", + "###\n", + "Review: \"My lovely Pat has one of the GREAT voices of her generation. I have listened to this CD for YEARS and I still LOVE IT. When I'm in a good mood it makes me feel better. A bad mood just evaporates like sugar in the rain. This CD just oozes LIFE. Vocals are jusat STUUNNING and lyrics just kill. One of life's hidden gems. This is a desert isle CD in my book. Why she never made it big is just beyond me. Everytime I play this, no matter black, white, young, old, male, female EVERYBODY says one thing \"Who was that singing ?\"\"\n", + "###\n", + "Is this review negative? \n", + "The: TheThe' F this scriptplay for this film should has readed a of on theifer Ball. or the autobiography. The'm read read a many in in a movieopic. andanging from the age life to Neweloron, Jamestown, her marriage years in Desi Arn The' go a book book of errorsual errors, but I would be on forever pages. The fact fair this' this thisille Ball was one of the peoplecomitable women who should cannot be portrayed on anyone other than herself. I you had toille,az, Ii Arn I., I would be furate at the this in were made in this movie. I only isakers should to to but they result was tofully rppy. me.\n", + "Re#\n", + "\" this the fair?\n", + "10Is#\n", + "Yesview: \"I movie of the Karie is a my, Ita Garbo is plays the,ie, a this the the other characters are different names. the original version. The versions filmed in- back, thebo was a a short in Germany. Thebo is wasly didored the German Christie in the version, the English version. I's a good movie, a good-see for Garbo fans.\"\n", + "###\n", + "Re this review negative? 1\n", + "###\n", + "Review: \"This' this is a greately movie film. It are some of laughilarious moments, thewwarming moments. keep found. the film. I story are all, the story are done.. Ianny Dever is a,x, isages to local team team theThe Brookels.' and is a in. movie. The,astic is the two actors who Gordon-Levitt and Bres ' who. who Lloyd plays also as thevinThe W' Mc the rest are great. this film-ch family film.\" I musting and fun-warming film that is should see.\"\n", + "###\n", + "Is this review negative? 0\n", + "###\n", + "Review: \"This husbandely wiferic been of the mostREATEATST in all generation. She' been to her album for yearsEARS. it still loveVE it! I I amm feeling the bad mood,' me happy good. When great mood and makesaporates. a in water rain. I is is makesozes withIFE. Iocals are greatawss perfectUNFFNING. theics are make. I of the's great gems.\" I CD a must islandle CD for my collection.\" I is isn made it big is a a me. Ione I listen this CD I matter what or blue, or, old, male, female,VERYONEDY lov ' thing:W is that?\"?\"\"\n", + "###\n", + "Is this review positive? 0\n" + ] + }, { "data": { "text/plain": [ - "array([-0.05655, 0.01492, -0.0974 , ..., -0.03537, 0.01362, 0.01608],\n", - " dtype=float16)" + "{'hidden_states': tensor([[-0.0331, 0.0093, -0.0510, ..., -0.0213, 0.0366, 0.0545],\n", + " [-0.9800, 3.0742, 2.6406, ..., 4.4609, 1.9453, -0.5400]],\n", + " dtype=torch.float16),\n", + " 'ans': 0.6738358736038208}" ] }, - "execution_count": 19, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -436,11 +536,49 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:20:40.121193Z", - "start_time": "2023-05-07T09:20:40.119127Z" + "end_time": "2023-05-07T10:58:32.555372Z", + "start_time": "2023-05-07T10:58:32.555364Z" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "ExecuteTime": { + "end_time": "2023-05-07T11:21:35.560533Z", + "start_time": "2023-05-07T11:21:35.558659Z" + } + }, + "outputs": [], + "source": [ + "# neg_hs[0]['hidden_states'][-1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2023-05-07T10:45:46.521935Z", + "start_time": "2023-05-07T10:45:46.517116Z" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "ExecuteTime": { + "end_time": "2023-05-07T11:21:35.585212Z", + "start_time": "2023-05-07T11:21:35.562285Z" } }, "outputs": [ @@ -449,30 +587,34 @@ "output_type": "stream", "text": [ "Review: \"Whoever wrote the screenplay for this movie obviously never consulted any books about Lucille Ball, especially her autobiography. I've never seen so many mistakes in a biopic, ranging from her early years in Celoron and Jamestown to her later years with Desi. I could write a whole list of factual errors, but it would go on for pages. In all, I believe that Lucille Ball is one of those inimitable people who simply cannot be portrayed by anyone other than themselves. If I were Lucie Arnaz and Desi, Jr., I would be irate at how many mistakes were made in this film. The filmmakers tried hard, but the movie seems awfully sloppy to me.\"\n", - "This is negative? 1\n", + "###\n", + "Is this review negative? 1\n", "###\n", "Review: \"This version of Anna Christie is in German. Greta Garbo again plays Anna Christie, but all of the other characters have different actors from the English version. Both were filmed back to back because Garbo had such a following in Germany. Garbo herself supposedly favored her Anna Christie in this version over the English version. It's a good tale and a must-see for Garbo fans.\"\n", - "This review is negative? 0\n", + "###\n", + "Is this review negative? 0\n", "###\n", "Review: \"I think this is a lovely family movie. There are plenty of hilarious scenes and heart-warming moments to be had throughout the movie. The actors are great and the effects well executed throughout. Danny Glover plays George Knox who manages the terrible baseball team 'The Angels' and is great throughout the film. Also fantastic are the young actors Joseph Gordon-Levitt and Milton Davis Jr. Christopher Lloyd is good as Al 'The Angel' and the effects are great in this top notch Disney movie. A touching and heart-warming movie which everyone should enjoy.\"\n", - "This review is positive? 1\n", + "###\n", + "Is this review positive? 1\n", "###\n", "Review: \"My lovely Pat has one of the GREAT voices of her generation. I have listened to this CD for YEARS and I still LOVE IT. When I'm in a good mood it makes me feel better. A bad mood just evaporates like sugar in the rain. This CD just oozes LIFE. Vocals are jusat STUUNNING and lyrics just kill. One of life's hidden gems. This is a desert isle CD in my book. Why she never made it big is just beyond me. Everytime I play this, no matter black, white, young, old, male, female EVERYBODY says one thing \"Who was that singing ?\"\"\n", - "This review is 0? \n" + "###\n", + "Is this review positive? \n" ] } ], "source": [ - "print(format_imdb(text, 0))" + "print(format_imdb(text, 1))" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 11, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:20:40.136489Z", - "start_time": "2023-05-07T09:20:40.122604Z" + "end_time": "2023-05-07T11:21:35.600642Z", + "start_time": "2023-05-07T11:21:35.586155Z" } }, "outputs": [], @@ -511,18 +653,18 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 12, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:20:40.152559Z", - "start_time": "2023-05-07T09:20:40.137543Z" + "end_time": "2023-05-07T11:21:35.616885Z", + "start_time": "2023-05-07T11:21:35.601707Z" } }, "outputs": [], "source": [ "\n", "\n", - "def get_hidden_states_many_examples(model, tokenizer, data, model_type, n=100, layer=-1):\n", + "def get_hidden_states_many_examples(model, tokenizer, data, model_type, n=100, layers=[2, -2]):\n", " \"\"\"\n", " Given an encoder-decoder model, a list of data, computes the contrast hidden states on n random examples.\n", " Returns numpy arrays of shape (n, hidden_dim) for each candidate label, along with a boolean numpy array of shape (n,)\n", @@ -546,8 +688,9 @@ " break\n", " \n", " # get hidden states\n", - " neg_hs = get_hidden_states(model, tokenizer, format_imdb(text, 0), model_type=model_type, layer=layer)\n", - " pos_hs = get_hidden_states(model, tokenizer, format_imdb(text, 1), model_type=model_type, layer=layer)\n", + "# print(format_imdb(text, 0))\n", + " neg_hs = get_hidden_states(model, tokenizer, format_imdb(text, 0), model_type=model_type, layers=layers)\n", + " pos_hs = get_hidden_states(model, tokenizer, format_imdb(text, 1), model_type=model_type, layers=layers)\n", "\n", " # collect\n", " all_neg_hs.append(neg_hs)\n", @@ -563,11 +706,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 13, "metadata": { "ExecuteTime": { - "end_time": "2023-05-07T09:21:25.091032Z", - "start_time": "2023-05-07T09:20:40.153598Z" + "end_time": "2023-05-07T11:22:30.270761Z", + "start_time": "2023-05-07T11:21:35.617903Z" } }, "outputs": [ @@ -575,7 +718,7422 @@ "name": "stderr", "output_type": "stream", "text": [ - "get_hidden_states: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 100/100 [00:44<00:00, 2.23examples/s]\n" + "\r", + "get_hidden_states: 0%| | 0/100 [00:00