{
  "id": 339316,
  "title": "About pairwise train code ,anyone knows why zhou select neg samples by np.random.random(size=10000)>0.9",
  "url": "/competitions/AI4Code/discussion/339316",
  "author_name": "",
  "post_date": "2022-07-24T07:02:16.183074200Z",
  "votes": 4,
  "comment_count": 4,
  "views": 0,
  "content": "<p>Anyone knows why he set 0.9 here ? Could we choose neg samples by other methods?  and because of the random , it seems not stable when train this model. I want to find some better ways to choose neg samples , butI failed….<br>\n<a href=\"https://www.kaggle.com/code/yuanzhezhou/ai4code-pairwise-bertsmall-training?scriptVersionId=96291806&amp;cellId=17\" target=\"_blank\">https://www.kaggle.com/code/yuanzhezhou/ai4code-pairwise-bertsmall-training?scriptVersionId=96291806&amp;cellId=17</a><br>\n`<br>\ndef generate_triplet(df, mode='train'):<br>\n triplets = []<br>\n  ids = df.id.unique()<br>\n  random_drop = np.random.random(size=10000)&gt;0.9<br>\n  count = 0</p>\n<p>for id, df_tmp in tqdm(df.groupby('id')):<br>\n    df_tmp_markdown = df_tmp[df_tmp['cell_type']=='markdown']</p>\n<pre><code>df_tmp_code = df_tmp[df_tmp['cell_type']=='code']\ndf_tmp_code_rank = df_tmp_code['rank'].values\ndf_tmp_code_cell_id = df_tmp_code['cell_id'].values\n\nfor cell_id, rank in df_tmp_markdown[['cell_id', 'rank']].values:\n  labels = np.array([(r==(rank+1)) for r in df_tmp_code_rank]).astype('int')\n\n  for cid, label in zip(df_tmp_code_cell_id, labels):\n    count += 1\n    if label==1:\n      triplets.append( [cell_id, cid, label] )\n      # triplets.append( [cid, cell_id, label] )\n    elif mode == 'test':\n      triplets.append( [cell_id, cid, label] )\n      # triplets.append( [cid, cell_id, label] )\n    elif random_drop[count%10000]:\n      triplets.append( [cell_id, cid, label] )\n      # triplets.append( [cid, cell_id, label] )\n</code></pre>\n<p>return triplets</p>\n<p>triplets = generate_triplet(train_df)<br>\nval_triplets = generate_triplet(val_df, mode = 'test')`</p>",
  "messages": [
    {
      "id": "1868708",
      "postDate": "07/24/2022 07:02:16",
      "content": "<p>Anyone knows why he set 0.9 here ? Could we choose neg samples by other methods?  and because of the random , it seems not stable when train this model. I want to find some better ways to choose neg samples , butI failed….<br>\n<a href=\"https://www.kaggle.com/code/yuanzhezhou/ai4code-pairwise-bertsmall-training?scriptVersionId=96291806&amp;cellId=17\" target=\"_blank\">https://www.kaggle.com/code/yuanzhezhou/ai4code-pairwise-bertsmall-training?scriptVersionId=96291806&amp;cellId=17</a><br>\n`<br>\ndef generate_triplet(df, mode='train'):<br>\n triplets = []<br>\n  ids = df.id.unique()<br>\n  random_drop = np.random.random(size=10000)&gt;0.9<br>\n  count = 0</p>\n<p>for id, df_tmp in tqdm(df.groupby('id')):<br>\n    df_tmp_markdown = df_tmp[df_tmp['cell_type']=='markdown']</p>\n<pre><code>df_tmp_code = df_tmp[df_tmp['cell_type']=='code']\ndf_tmp_code_rank = df_tmp_code['rank'].values\ndf_tmp_code_cell_id = df_tmp_code['cell_id'].values\n\nfor cell_id, rank in df_tmp_markdown[['cell_id', 'rank']].values:\n  labels = np.array([(r==(rank+1)) for r in df_tmp_code_rank]).astype('int')\n\n  for cid, label in zip(df_tmp_code_cell_id, labels):\n    count += 1\n    if label==1:\n      triplets.append( [cell_id, cid, label] )\n      # triplets.append( [cid, cell_id, label] )\n    elif mode == 'test':\n      triplets.append( [cell_id, cid, label] )\n      # triplets.append( [cid, cell_id, label] )\n    elif random_drop[count%10000]:\n      triplets.append( [cell_id, cid, label] )\n      # triplets.append( [cid, cell_id, label] )\n</code></pre>\n<p>return triplets</p>\n<p>triplets = generate_triplet(train_df)<br>\nval_triplets = generate_triplet(val_df, mode = 'test')`</p>",
      "rawMarkdown": "Anyone knows why he set 0.9 here ? Could we choose neg samples by other methods?  and because of the random , it seems not stable when train this model. I want to find some better ways to choose neg samples , butI failed....\nhttps://www.kaggle.com/code/yuanzhezhou/ai4code-pairwise-bertsmall-training?scriptVersionId=96291806&cellId=17\n`\ndef generate_triplet(df, mode='train'):\n triplets = []\n  ids = df.id.unique()\n  random_drop = np.random.random(size=10000)>0.9\n  count = 0\n\n  for id, df_tmp in tqdm(df.groupby('id')):\n    df_tmp_markdown = df_tmp[df_tmp['cell_type']=='markdown']\n\n    df_tmp_code = df_tmp[df_tmp['cell_type']=='code']\n    df_tmp_code_rank = df_tmp_code['rank'].values\n    df_tmp_code_cell_id = df_tmp_code['cell_id'].values\n\n    for cell_id, rank in df_tmp_markdown[['cell_id', 'rank']].values:\n      labels = np.array([(r==(rank+1)) for r in df_tmp_code_rank]).astype('int')\n\n      for cid, label in zip(df_tmp_code_cell_id, labels):\n        count += 1\n        if label==1:\n          triplets.append( [cell_id, cid, label] )\n          # triplets.append( [cid, cell_id, label] )\n        elif mode == 'test':\n          triplets.append( [cell_id, cid, label] )\n          # triplets.append( [cid, cell_id, label] )\n        elif random_drop[count%10000]:\n          triplets.append( [cell_id, cid, label] )\n          # triplets.append( [cid, cell_id, label] )\n    \n  return triplets\n\ntriplets = generate_triplet(train_df)\nval_triplets = generate_triplet(val_df, mode = 'test')`",
      "votes": null
    },
    {
      "id": "1868914",
      "postDate": "07/24/2022 10:37:25",
      "content": "<p>We ran this code with the same hyperparameters, but the results were worse. I would to make some experiments with that, but Pairwise is so slow and unstable (depending on seed choice).</p>",
      "rawMarkdown": "We ran this code with the same hyperparameters, but the results were worse. I would to make some experiments with that, but Pairwise is so slow and unstable (depending on seed choice).",
      "votes": null
    },
    {
      "id": "1870039",
      "postDate": "07/25/2022 08:16:52",
      "content": "<p>I remember him mentioning somewhere that this roughly translates to 5% sampling (of possible -ve pairs). He mentions that anything more or less deteriorates the score. </p>\n<p>for repeatability did u try numpy.random.seed(42)? There are some caveats though</p>\n<p>I wanted to explore other triplet generating algos, but didnt get a chance so far. There are quite a few available</p>",
      "rawMarkdown": "I remember him mentioning somewhere that this roughly translates to 5% sampling (of possible -ve pairs). He mentions that anything more or less deteriorates the score. \n\nfor repeatability did u try numpy.random.seed(42)? There are some caveats though\n\nI wanted to explore other triplet generating algos, but didnt get a chance so far. There are quite a few available",
      "votes": null
    },
    {
      "id": "1870251",
      "postDate": "07/25/2022 12:09:44",
      "content": "<p>I'll try this numpy.random.seed(42),  I tried some other ways to generate triplet, but the result is not good</p>",
      "rawMarkdown": "I'll try this numpy.random.seed(42),  I tried some other ways to generate triplet, but the result is not good",
      "votes": null
    },
    {
      "id": "1871442",
      "postDate": "07/26/2022 09:16:21",
      "content": "<p>I think it's a way to balance the dataset..<br>\nBut yes it is very sensitive to the seed.. (which is not good)</p>",
      "rawMarkdown": "I think it's a way to balance the dataset..\nBut yes it is very sensitive to the seed.. (which is not good)",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1868914,
      "author_name": "vad13irt",
      "author_url": "",
      "post_date": "07/24/2022 10:37:25",
      "content": "<p>We ran this code with the same hyperparameters, but the results were worse. I would to make some experiments with that, but Pairwise is so slow and unstable (depending on seed choice).</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1870039,
      "author_name": "allohvk",
      "author_url": "",
      "post_date": "07/25/2022 08:16:52",
      "content": "<p>I remember him mentioning somewhere that this roughly translates to 5% sampling (of possible -ve pairs). He mentions that anything more or less deteriorates the score. </p>\n<p>for repeatability did u try numpy.random.seed(42)? There are some caveats though</p>\n<p>I wanted to explore other triplet generating algos, but didnt get a chance so far. There are quite a few available</p>",
      "votes": null,
      "replies": [
        {
          "id": 1870251,
          "author_name": "zhzeng",
          "author_url": "",
          "post_date": "07/25/2022 12:09:44",
          "content": "<p>I'll try this numpy.random.seed(42),  I tried some other ways to generate triplet, but the result is not good</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1871442,
      "author_name": "thedevastator",
      "author_url": "",
      "post_date": "07/26/2022 09:16:21",
      "content": "<p>I think it's a way to balance the dataset..<br>\nBut yes it is very sensitive to the seed.. (which is not good)</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1868708": "Anyone knows why he set 0.9 here ? Could we choose neg samples by other methods?  and because of the random , it seems not stable when train this model. I want to find some better ways to choose neg samples , butI failed....\nhttps://www.kaggle.com/code/yuanzhezhou/ai4code-pairwise-bertsmall-training?scriptVersionId=96291806&cellId=17\n`\ndef generate_triplet(df, mode='train'):\n triplets = []\n  ids = df.id.unique()\n  random_drop = np.random.random(size=10000)>0.9\n  count = 0\n\n  for id, df_tmp in tqdm(df.groupby('id')):\n    df_tmp_markdown = df_tmp[df_tmp['cell_type']=='markdown']\n\n    df_tmp_code = df_tmp[df_tmp['cell_type']=='code']\n    df_tmp_code_rank = df_tmp_code['rank'].values\n    df_tmp_code_cell_id = df_tmp_code['cell_id'].values\n\n    for cell_id, rank in df_tmp_markdown[['cell_id', 'rank']].values:\n      labels = np.array([(r==(rank+1)) for r in df_tmp_code_rank]).astype('int')\n\n      for cid, label in zip(df_tmp_code_cell_id, labels):\n        count += 1\n        if label==1:\n          triplets.append( [cell_id, cid, label] )\n          # triplets.append( [cid, cell_id, label] )\n        elif mode == 'test':\n          triplets.append( [cell_id, cid, label] )\n          # triplets.append( [cid, cell_id, label] )\n        elif random_drop[count%10000]:\n          triplets.append( [cell_id, cid, label] )\n          # triplets.append( [cid, cell_id, label] )\n    \n  return triplets\n\ntriplets = generate_triplet(train_df)\nval_triplets = generate_triplet(val_df, mode = 'test')`",
    "1868914": "We ran this code with the same hyperparameters, but the results were worse. I would to make some experiments with that, but Pairwise is so slow and unstable (depending on seed choice).",
    "1870039": "I remember him mentioning somewhere that this roughly translates to 5% sampling (of possible -ve pairs). He mentions that anything more or less deteriorates the score. \n\nfor repeatability did u try numpy.random.seed(42)? There are some caveats though\n\nI wanted to explore other triplet generating algos, but didnt get a chance so far. There are quite a few available",
    "1870251": "I'll try this numpy.random.seed(42),  I tried some other ways to generate triplet, but the result is not good",
    "1871442": "I think it's a way to balance the dataset..\nBut yes it is very sensitive to the seed.. (which is not good)"
  },
  "source": "meta"
}