{
  "id": 363163,
  "title": "CPU vs GPU. Different results with the same code. Different Public Scores.",
  "url": "/competitions/g2net-detecting-continuous-gravitational-waves/discussion/363163",
  "author_name": "Petr Kushnir",
  "post_date": "2022-10-31T11:54:46.734000",
  "votes": 6,
  "comment_count": 9,
  "views": 0,
  "content": "<p>Hello everybody!</p>\n<p>Found an interesting thing.</p>\n<p>I am exploring versions of <a href=\"https://www.kaggle.com/masterray\" target=\"_blank\">https://www.kaggle.com/masterray</a> notebook:<br>\n<a href=\"url\" target=\"_blank\">https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3/notebook?scriptVersionId=109514186</a> </p>\n<p>And found out that Version 8 and Version 11 has the same code, but were run on CPU (Version 8 ) and GPU (Version 11) and had differente submissions results. And of course differente Public Scores (Version 8 - 0.640 vs Version 11 - 0.608).</p>\n<p>I'm a newbie in Data Science. And got some questions:</p>\n<ol>\n<li><p>Is it because of floating point operations are managed slightly differently on the cpu and gpu?</p></li>\n<li><p>Is it always better to use CPU? :-) </p></li>\n</ol>\n<hr>\n<p>Version 8<br>\n<a href=\"url\" target=\"_blank\">https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3/notebook?scriptVersionId=109464914</a><br>\nCPU<br>\nPublic Score<br>\n0.640</p>\n<p>Version 11<br>\n<a href=\"url\" target=\"_blank\">https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3?scriptVersionId=109514186</a><br>\nGPU T4 x2<br>\nPublic Score<br>\n0.608</p>",
  "messages": [
    {
      "id": 2011188,
      "postDate": "2022-10-31T12:14:13.487Z",
      "content": "<p>No, its not cause of CPU. It is just the random shuffle which leads to different solutions per run. I checked the code, the seed in only on torch stuff, but there is also this line:* kfold = StratifiedKFold(n_splits=nfold, random_state=42,** shuffle=True*<em>)</em> which leads to different models per run. The 64% was a lucky shot.</p>",
      "rawMarkdown": "No, its not cause of CPU. It is just the random shuffle which leads to different solutions per run. I checked the code, the seed in only on torch stuff, but there is also this line:* kfold = StratifiedKFold(n_splits=nfold, random_state=42,** shuffle=True**)* which leads to different models per run. The 64% was a lucky shot.",
      "votes": 6,
      "replies": [
        {
          "id": 2011201,
          "postDate": "2022-10-31T12:21:27.273Z",
          "content": "<p>Tnx for clear clarification!</p>",
          "rawMarkdown": "Tnx for clear clarification!"
        },
        {
          "id": 2013026,
          "postDate": "2022-11-01T15:31:36.927Z",
          "content": "<p>Can you explain further? If I want to conduct a controlled variable experiment and don't want any randomness to affect the CV, which random seeds should I fix?</p>\n<p>I currently see two places in this program where I can fix random seeds, one is here.<br>\n<code>kfold = StratifiedKFold(n_splits=nfold, random_state=42, shuffle=True)</code><br>\n I think it has been set to 42, which assumes that the samples for each fold in the Kfold are already fixed. I'm not sure if I'm right?</p>\n<p>There is also one for torch, which I see the author has written as such.<br>\n<code>torch.manual_seed(42 + ifold + 1)</code><br>\nThis means that this seed is not fixed right?</p>\n<p>So as long as it is changed to 42 here it will produce the exact same CV every time it is trained?</p>\n<p>I'm new to Kaggle and I'm having to face such experiments for the first time, so if you could give some advice on controlling the variables, it would be greatly appreciated!</p>",
          "rawMarkdown": "Can you explain further? If I want to conduct a controlled variable experiment and don't want any randomness to affect the CV, which random seeds should I fix?\n\nI currently see two places in this program where I can fix random seeds, one is here.\n`kfold = StratifiedKFold(n_splits=nfold, random_state=42, shuffle=True)`\n I think it has been set to 42, which assumes that the samples for each fold in the Kfold are already fixed. I'm not sure if I'm right?\n\nThere is also one for torch, which I see the author has written as such.\n`torch.manual_seed(42 + ifold + 1)`\nThis means that this seed is not fixed right?\n\nSo as long as it is changed to 42 here it will produce the exact same CV every time it is trained?\n\nI'm new to Kaggle and I'm having to face such experiments for the first time, so if you could give some advice on controlling the variables, it would be greatly appreciated!"
        },
        {
          "id": 2014119,
          "postDate": "2022-11-02T10:39:23.040Z",
          "content": "<p>If you want a solid seed for reproducable results in order to perform fair comparisons you can seed everything with the following code:</p>\n<h1>------- Seeding <strong><em><em>__</em></em></strong> DO NOT CHANGE THESE!!!!!</h1>\n<p>def seed_everything(seed_value):<br>\n    random.seed(seed_value)<br>\n    np.random.seed(seed_value)<br>\n    torch.manual_seed(seed_value)<br>\n    os.environ['PYTHONHASHSEED'] = str(seed_value)    <br>\n    if torch.cuda.is_available(): <br>\n        torch.cuda.manual_seed(seed_value)<br>\n        torch.cuda.manual_seed_all(seed_value)<br>\n        torch.backends.cudnn.deterministic = True<br>\n        torch.backends.cudnn.benchmark = True<br>\nseed = 42<br>\nseed_everything(seed)</p>\n<h1>------- Seeding <strong><em><em>__</em></em></strong> DO NOT CHANGE THESE!!!!!</h1>\n<p>However, keep in mind that when you are training a NN, every small change can result in different models. In the kfold = StratifiedKFold(n_splits=nfold, random_state=42, shuffle=True), the shuffle term can lead to different models even if the folds are seeded/same. This is because, a different order which you feed the NN will result also in different NN even if the total tr data is the same per fold. This also happens when you change the batch size.</p>\n<p>Hope that helped :)</p>",
          "rawMarkdown": "If you want a solid seed for reproducable results in order to perform fair comparisons you can seed everything with the following code:\n\n# ------- Seeding __________ DO NOT CHANGE THESE!!!!!\ndef seed_everything(seed_value):\n    random.seed(seed_value)\n    np.random.seed(seed_value)\n    torch.manual_seed(seed_value)\n    os.environ['PYTHONHASHSEED'] = str(seed_value)    \n    if torch.cuda.is_available(): \n        torch.cuda.manual_seed(seed_value)\n        torch.cuda.manual_seed_all(seed_value)\n        torch.backends.cudnn.deterministic = True\n        torch.backends.cudnn.benchmark = True\nseed = 42\nseed_everything(seed)\n# ------- Seeding __________ DO NOT CHANGE THESE!!!!!\n\nHowever, keep in mind that when you are training a NN, every small change can result in different models. In the kfold = StratifiedKFold(n_splits=nfold, random_state=42, shuffle=True), the shuffle term can lead to different models even if the folds are seeded/same. This is because, a different order which you feed the NN will result also in different NN even if the total tr data is the same per fold. This also happens when you change the batch size.\n\nHope that helped :)",
          "votes": 5
        },
        {
          "id": 2014269,
          "postDate": "2022-11-02T13:22:19.713Z",
          "content": "<p>Thank you very much for the answer and the code, it's very helpful for newbies.</p>\n<p>I would like to confirm that fixing these seeds is common practice when people are picking hyperparameters for their models other than batch size? </p>\n<p>At least I think for this competition, the training set is only 600 samples and any randomness introduced will affect the CV to a large extent and we cannot pick the best model and hyperparameters out of these 600 training samples without eliminating randomness.</p>",
          "rawMarkdown": "Thank you very much for the answer and the code, it's very helpful for newbies.\n\nI would like to confirm that fixing these seeds is common practice when people are picking hyperparameters for their models other than batch size? \n\nAt least I think for this competition, the training set is only 600 samples and any randomness introduced will affect the CV to a large extent and we cannot pick the best model and hyperparameters out of these 600 training samples without eliminating randomness.",
          "votes": 1
        },
        {
          "id": 2014343,
          "postDate": "2022-11-02T14:07:54.793Z",
          "content": "<p>I just discovered something interesting, I copied the code \"def seed_everything(seed_value):\" into my ipynb and I can print out the exact same thing every time I train, which is great.<br>\nBut the interesting thing is that after I converted this ipynb file to a py file, each time I trained, the numbers printed out a little differently, but running it a few more times, I found that two of the results were exactly the same, thinking that the py file would introduce a new random factor compared to the ipynb file, and that this random factor could not take many values, and it would only take a few runs for the exact same result to appear once. What do you think about this? Do you know what the reason is?</p>",
          "rawMarkdown": "I just discovered something interesting, I copied the code \"def seed_everything(seed_value):\" into my ipynb and I can print out the exact same thing every time I train, which is great.\nBut the interesting thing is that after I converted this ipynb file to a py file, each time I trained, the numbers printed out a little differently, but running it a few more times, I found that two of the results were exactly the same, thinking that the py file would introduce a new random factor compared to the ipynb file, and that this random factor could not take many values, and it would only take a few runs for the exact same result to appear once. What do you think about this? Do you know what the reason is?",
          "votes": 1
        },
        {
          "id": 2014416,
          "postDate": "2022-11-02T14:43:50.907Z",
          "content": "<p>This may be cause of different order in loading files. If you run it locally for example, the load order will be different comparing to loading from kaggle api.</p>",
          "rawMarkdown": "This may be cause of different order in loading files. If you run it locally for example, the load order will be different comparing to loading from kaggle api.",
          "votes": 1
        },
        {
          "id": 2014451,
          "postDate": "2022-11-02T15:02:13.167Z",
          "content": "<p>Haha, but I am running both files in the same folder, both locally, both with pycharm pro</p>",
          "rawMarkdown": "Haha, but I am running both files in the same folder, both locally, both with pycharm pro"
        },
        {
          "id": 2014460,
          "postDate": "2022-11-02T15:10:42.090Z",
          "content": "<p>Then this is strange 😆. NN chaotic black nature!<br>\nHowever keep in mind, that randomness is desirable, since finding a good shot is more about gambling. Also it is needed when building an ensemble solution( which is the only way for most of competitions). Randomness can create different models which can lead to a great ensemble 😁👌</p>",
          "rawMarkdown": "Then this is strange 😆. NN chaotic black nature!\nHowever keep in mind, that randomness is desirable, since finding a good shot is more about gambling. Also it is needed when building an ensemble solution( which is the only way for most of competitions). Randomness can create different models which can lead to a great ensemble 😁👌",
          "votes": 1
        }
      ]
    },
    {
      "id": 2011158,
      "postDate": "2022-10-31T11:54:46.733Z",
      "content": "<p>Hello everybody!</p>\n<p>Found an interesting thing.</p>\n<p>I am exploring versions of <a href=\"https://www.kaggle.com/masterray\" target=\"_blank\">https://www.kaggle.com/masterray</a> notebook:<br>\n<a href=\"url\" target=\"_blank\">https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3/notebook?scriptVersionId=109514186</a> </p>\n<p>And found out that Version 8 and Version 11 has the same code, but were run on CPU (Version 8 ) and GPU (Version 11) and had differente submissions results. And of course differente Public Scores (Version 8 - 0.640 vs Version 11 - 0.608).</p>\n<p>I'm a newbie in Data Science. And got some questions:</p>\n<ol>\n<li><p>Is it because of floating point operations are managed slightly differently on the cpu and gpu?</p></li>\n<li><p>Is it always better to use CPU? :-) </p></li>\n</ol>\n<hr>\n<p>Version 8<br>\n<a href=\"url\" target=\"_blank\">https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3/notebook?scriptVersionId=109464914</a><br>\nCPU<br>\nPublic Score<br>\n0.640</p>\n<p>Version 11<br>\n<a href=\"url\" target=\"_blank\">https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3?scriptVersionId=109514186</a><br>\nGPU T4 x2<br>\nPublic Score<br>\n0.608</p>",
      "rawMarkdown": "Hello everybody!\n\nFound an interesting thing.\n\nI am exploring versions of https://www.kaggle.com/masterray notebook:\n[https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3/notebook?scriptVersionId=109514186](url) \n\nAnd found out that Version 8 and Version 11 has the same code, but were run on CPU (Version 8 ) and GPU (Version 11) and had differente submissions results. And of course differente Public Scores (Version 8 - 0.640 vs Version 11 - 0.608).\n\nI'm a newbie in Data Science. And got some questions:\n\n1. Is it because of floating point operations are managed slightly differently on the cpu and gpu?\n\n2. Is it always better to use CPU? :-) \n\n---\nVersion 8\n[https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3/notebook?scriptVersionId=109464914](url)\nCPU\nPublic Score\n0.640\n\nVersion 11\n[https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3?scriptVersionId=109514186](url)\nGPU T4 x2\nPublic Score\n0.608",
      "votes": 6
    }
  ],
  "comments": [
    {
      "id": 2011188,
      "author_name": "",
      "author_url": "",
      "post_date": "2022-10-31T12:14:13.487000",
      "content": "<p>No, its not cause of CPU. It is just the random shuffle which leads to different solutions per run. I checked the code, the seed in only on torch stuff, but there is also this line:* kfold = StratifiedKFold(n_splits=nfold, random_state=42,** shuffle=True*<em>)</em> which leads to different models per run. The 64% was a lucky shot.</p>",
      "votes": 6,
      "replies": [
        {
          "id": 2011201,
          "author_name": "Petr Kushnir",
          "author_url": "",
          "post_date": "2022-10-31T12:21:27.273000",
          "content": "<p>Tnx for clear clarification!</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 2013026,
          "author_name": "Chen Lin",
          "author_url": "",
          "post_date": "2022-11-01T15:31:36.927000",
          "content": "<p>Can you explain further? If I want to conduct a controlled variable experiment and don't want any randomness to affect the CV, which random seeds should I fix?</p>\n<p>I currently see two places in this program where I can fix random seeds, one is here.<br>\n<code>kfold = StratifiedKFold(n_splits=nfold, random_state=42, shuffle=True)</code><br>\n I think it has been set to 42, which assumes that the samples for each fold in the Kfold are already fixed. I'm not sure if I'm right?</p>\n<p>There is also one for torch, which I see the author has written as such.<br>\n<code>torch.manual_seed(42 + ifold + 1)</code><br>\nThis means that this seed is not fixed right?</p>\n<p>So as long as it is changed to 42 here it will produce the exact same CV every time it is trained?</p>\n<p>I'm new to Kaggle and I'm having to face such experiments for the first time, so if you could give some advice on controlling the variables, it would be greatly appreciated!</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 2014119,
          "author_name": "",
          "author_url": "",
          "post_date": "2022-11-02T10:39:23.040000",
          "content": "<p>If you want a solid seed for reproducable results in order to perform fair comparisons you can seed everything with the following code:</p>\n<h1>------- Seeding <strong><em><em>__</em></em></strong> DO NOT CHANGE THESE!!!!!</h1>\n<p>def seed_everything(seed_value):<br>\n    random.seed(seed_value)<br>\n    np.random.seed(seed_value)<br>\n    torch.manual_seed(seed_value)<br>\n    os.environ['PYTHONHASHSEED'] = str(seed_value)    <br>\n    if torch.cuda.is_available(): <br>\n        torch.cuda.manual_seed(seed_value)<br>\n        torch.cuda.manual_seed_all(seed_value)<br>\n        torch.backends.cudnn.deterministic = True<br>\n        torch.backends.cudnn.benchmark = True<br>\nseed = 42<br>\nseed_everything(seed)</p>\n<h1>------- Seeding <strong><em><em>__</em></em></strong> DO NOT CHANGE THESE!!!!!</h1>\n<p>However, keep in mind that when you are training a NN, every small change can result in different models. In the kfold = StratifiedKFold(n_splits=nfold, random_state=42, shuffle=True), the shuffle term can lead to different models even if the folds are seeded/same. This is because, a different order which you feed the NN will result also in different NN even if the total tr data is the same per fold. This also happens when you change the batch size.</p>\n<p>Hope that helped :)</p>",
          "votes": 5,
          "replies": []
        },
        {
          "id": 2014269,
          "author_name": "Chen Lin",
          "author_url": "",
          "post_date": "2022-11-02T13:22:19.713000",
          "content": "<p>Thank you very much for the answer and the code, it's very helpful for newbies.</p>\n<p>I would like to confirm that fixing these seeds is common practice when people are picking hyperparameters for their models other than batch size? </p>\n<p>At least I think for this competition, the training set is only 600 samples and any randomness introduced will affect the CV to a large extent and we cannot pick the best model and hyperparameters out of these 600 training samples without eliminating randomness.</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 2014343,
          "author_name": "Chen Lin",
          "author_url": "",
          "post_date": "2022-11-02T14:07:54.793000",
          "content": "<p>I just discovered something interesting, I copied the code \"def seed_everything(seed_value):\" into my ipynb and I can print out the exact same thing every time I train, which is great.<br>\nBut the interesting thing is that after I converted this ipynb file to a py file, each time I trained, the numbers printed out a little differently, but running it a few more times, I found that two of the results were exactly the same, thinking that the py file would introduce a new random factor compared to the ipynb file, and that this random factor could not take many values, and it would only take a few runs for the exact same result to appear once. What do you think about this? Do you know what the reason is?</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 2014416,
          "author_name": "",
          "author_url": "",
          "post_date": "2022-11-02T14:43:50.907000",
          "content": "<p>This may be cause of different order in loading files. If you run it locally for example, the load order will be different comparing to loading from kaggle api.</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 2014451,
          "author_name": "Chen Lin",
          "author_url": "",
          "post_date": "2022-11-02T15:02:13.167000",
          "content": "<p>Haha, but I am running both files in the same folder, both locally, both with pycharm pro</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 2014460,
          "author_name": "",
          "author_url": "",
          "post_date": "2022-11-02T15:10:42.090000",
          "content": "<p>Then this is strange 😆. NN chaotic black nature!<br>\nHowever keep in mind, that randomness is desirable, since finding a good shot is more about gambling. Also it is needed when building an ensemble solution( which is the only way for most of competitions). Randomness can create different models which can lead to a great ensemble 😁👌</p>",
          "votes": 1,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2011188": "No, its not cause of CPU. It is just the random shuffle which leads to different solutions per run. I checked the code, the seed in only on torch stuff, but there is also this line:* kfold = StratifiedKFold(n_splits=nfold, random_state=42,** shuffle=True**)* which leads to different models per run. The 64% was a lucky shot.",
    "2011158": "Hello everybody!\n\nFound an interesting thing.\n\nI am exploring versions of https://www.kaggle.com/masterray notebook:\n[https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3/notebook?scriptVersionId=109514186](url) \n\nAnd found out that Version 8 and Version 11 has the same code, but were run on CPU (Version 8 ) and GPU (Version 11) and had differente submissions results. And of course differente Public Scores (Version 8 - 0.640 vs Version 11 - 0.608).\n\nI'm a newbie in Data Science. And got some questions:\n\n1. Is it because of floating point operations are managed slightly differently on the cpu and gpu?\n\n2. Is it always better to use CPU? :-) \n\n---\nVersion 8\n[https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3/notebook?scriptVersionId=109464914](url)\nCPU\nPublic Score\n0.640\n\nVersion 11\n[https://www.kaggle.com/code/masterray/basic-spectrogram-image-classification-v3?scriptVersionId=109514186](url)\nGPU T4 x2\nPublic Score\n0.608"
  }
}