{
  "id": 263422,
  "title": "Can you ensure the reproducibility of PyTorch?",
  "url": "/competitions/rsna-miccai-brain-tumor-radiogenomic-classification/discussion/263422",
  "author_name": "",
  "post_date": "2021-08-09T10:05:43.530128Z",
  "votes": 6,
  "comment_count": 3,
  "views": 0,
  "content": "<p>Hi everyone,</p>\n<p>I'm currently building a simple model and repeating the experiment in a local machine.<br>\nHowever I'm trying to ensure the reproducibility of the model with my past code heritage and the links below, but it is not working.</p>\n<p><a href=\"https://pytorch.org/docs/stable/notes/randomness.html\" target=\"_blank\">https://pytorch.org/docs/stable/notes/randomness.html</a></p>\n<p>Is everyone able to reproduce the results?</p>\n<pre><code>def set_seed(seed):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    torch.use_deterministic_algorithms = True\n\nset_seed(42)\n\ndef seed_worker(worker_id):\n    worker_seed = torch.initial_seed() % 2**32\n    np.random.seed(worker_seed)\n    random.seed(worker_seed)\n\ng = torch.Generator()\ng.manual_seed(42)\n</code></pre>",
  "messages": [
    {
      "id": "1461271",
      "postDate": "08/09/2021 10:05:43",
      "content": "<p>Hi everyone,</p>\n<p>I'm currently building a simple model and repeating the experiment in a local machine.<br>\nHowever I'm trying to ensure the reproducibility of the model with my past code heritage and the links below, but it is not working.</p>\n<p><a href=\"https://pytorch.org/docs/stable/notes/randomness.html\" target=\"_blank\">https://pytorch.org/docs/stable/notes/randomness.html</a></p>\n<p>Is everyone able to reproduce the results?</p>\n<pre><code>def set_seed(seed):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    torch.use_deterministic_algorithms = True\n\nset_seed(42)\n\ndef seed_worker(worker_id):\n    worker_seed = torch.initial_seed() % 2**32\n    np.random.seed(worker_seed)\n    random.seed(worker_seed)\n\ng = torch.Generator()\ng.manual_seed(42)\n</code></pre>",
      "rawMarkdown": "Hi everyone,\n\nI'm currently building a simple model and repeating the experiment in a local machine.\nHowever I'm trying to ensure the reproducibility of the model with my past code heritage and the links below, but it is not working.\n\nhttps://pytorch.org/docs/stable/notes/randomness.html\n\nIs everyone able to reproduce the results?\n\n```\ndef set_seed(seed):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    torch.use_deterministic_algorithms = True\n\nset_seed(42)\n\ndef seed_worker(worker_id):\n    worker_seed = torch.initial_seed() % 2**32\n    np.random.seed(worker_seed)\n    random.seed(worker_seed)\n    \ng = torch.Generator()\ng.manual_seed(42)\n```",
      "votes": null
    },
    {
      "id": "1462182",
      "postDate": "08/09/2021 18:04:49",
      "content": "<p>are you also setting <code>num_workers = 0</code> in Dataloader?</p>",
      "rawMarkdown": "are you also setting `num_workers = 0` in Dataloader?",
      "votes": null
    },
    {
      "id": "1462197",
      "postDate": "08/09/2021 18:13:25",
      "content": "<p>I dont think so this might be a problem if you set the seed at the same value then it is highly likely that you would get the same results.</p>",
      "rawMarkdown": "I dont think so this might be a problem if you set the seed at the same value then it is highly likely that you would get the same results.",
      "votes": null
    },
    {
      "id": "1462274",
      "postDate": "08/09/2021 19:04:20",
      "content": "<p>No, I set <code>num_workers = 4</code>.<br>\nEven now, the problem remains unresolved😂</p>",
      "rawMarkdown": "No, I set `num_workers = 4`.\nEven now, the problem remains unresolved😂",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1462182,
      "author_name": "drhabib",
      "author_url": "",
      "post_date": "08/09/2021 18:04:49",
      "content": "<p>are you also setting <code>num_workers = 0</code> in Dataloader?</p>",
      "votes": null,
      "replies": [
        {
          "id": 1462274,
          "author_name": "atsunorifujita",
          "author_url": "",
          "post_date": "08/09/2021 19:04:20",
          "content": "<p>No, I set <code>num_workers = 4</code>.<br>\nEven now, the problem remains unresolved😂</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1462197,
      "author_name": "alimashoud",
      "author_url": "",
      "post_date": "08/09/2021 18:13:25",
      "content": "<p>I dont think so this might be a problem if you set the seed at the same value then it is highly likely that you would get the same results.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1461271": "Hi everyone,\n\nI'm currently building a simple model and repeating the experiment in a local machine.\nHowever I'm trying to ensure the reproducibility of the model with my past code heritage and the links below, but it is not working.\n\nhttps://pytorch.org/docs/stable/notes/randomness.html\n\nIs everyone able to reproduce the results?\n\n```\ndef set_seed(seed):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    torch.use_deterministic_algorithms = True\n\nset_seed(42)\n\ndef seed_worker(worker_id):\n    worker_seed = torch.initial_seed() % 2**32\n    np.random.seed(worker_seed)\n    random.seed(worker_seed)\n    \ng = torch.Generator()\ng.manual_seed(42)\n```",
    "1462182": "are you also setting `num_workers = 0` in Dataloader?",
    "1462197": "I dont think so this might be a problem if you set the seed at the same value then it is highly likely that you would get the same results.",
    "1462274": "No, I set `num_workers = 4`.\nEven now, the problem remains unresolved😂"
  },
  "source": "meta"
}