{
  "id": 372814,
  "title": "Train and Save Model with kaggle kernel",
  "url": "/competitions/rsna-breast-cancer-detection/discussion/372814",
  "author_name": "Hakeem",
  "post_date": "2022-12-18T08:32:06.713000",
  "votes": 1,
  "comment_count": 4,
  "views": 0,
  "content": "<p>Hi, is there a way i can train my model on kaggle and save it in my kaggle dataset. So my submissions only needs to load the model from my dataset and predict on the test set. Thanks.</p>",
  "messages": [
    {
      "id": 2070586,
      "postDate": "2022-12-20T07:18:05.933Z",
      "content": "<p>You can create dataset manually or using Kaggle API (from notebook).</p>",
      "rawMarkdown": "You can create dataset manually or using Kaggle API (from notebook).",
      "votes": 1
    },
    {
      "id": 2068681,
      "postDate": "2022-12-18T08:32:06.713Z",
      "content": "<p>Hi, is there a way i can train my model on kaggle and save it in my kaggle dataset. So my submissions only needs to load the model from my dataset and predict on the test set. Thanks.</p>",
      "rawMarkdown": "Hi, is there a way i can train my model on kaggle and save it in my kaggle dataset. So my submissions only needs to load the model from my dataset and predict on the test set. Thanks.",
      "votes": 1
    },
    {
      "id": 2070316,
      "postDate": "2022-12-19T20:30:12.817Z",
      "content": "<p>Hi!</p>\n<p>Yes, if you are using PyTorch, you can save the state dict of your model using the <code>state_dict()</code> function and then uploading it to your personal Kaggle dataset. When you want to submit results, you can call the same model and load your trained weights by using the <code>load_state_dict()</code> function and specifying the path to your saved weights in your Kaggle dataset.</p>\n<p>Code to save and load the model state dict:</p>\n<pre><code># Save the state dict\nstate_dict = model.state_dict()\ntorch.save(state_dict, 'model_state.pth')\n\n# Load the state dict from your Kaggle dataset\nstate_dict = torch.load('/kaggle/input/your_dataset/model_state.pth')\n\n# Load the state dict into your model\nmodel.load_state_dict(state_dict)\n</code></pre>\n<p>Add your model state dict to your personal dataset:<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F6347360%2F280dd6d6bb2f0604d7a16f609d6537fc%2FScreenshot%202022-12-19%20212651.png?generation=1671481624644336&amp;alt=media\" alt=\"'How to add a file to a personnal dataset'\"></p>\n<p>I hope this helps!</p>",
      "rawMarkdown": "Hi!\n\nYes, if you are using PyTorch, you can save the state dict of your model using the ```state_dict()``` function and then uploading it to your personal Kaggle dataset. When you want to submit results, you can call the same model and load your trained weights by using the ```load_state_dict()``` function and specifying the path to your saved weights in your Kaggle dataset.\n\nCode to save and load the model state dict:\n```\n# Save the state dict\nstate_dict = model.state_dict()\ntorch.save(state_dict, 'model_state.pth')\n\n# Load the state dict from your Kaggle dataset\nstate_dict = torch.load('/kaggle/input/your_dataset/model_state.pth')\n\n# Load the state dict into your model\nmodel.load_state_dict(state_dict)\n```\n\nAdd your model state dict to your personal dataset:\n!['How to add a file to a personnal dataset'](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F6347360%2F280dd6d6bb2f0604d7a16f609d6537fc%2FScreenshot%202022-12-19%20212651.png?generation=1671481624644336&alt=media)\n\n\nI hope this helps!",
      "votes": 2,
      "replies": [
        {
          "id": 2070649,
          "postDate": "2022-12-20T08:24:27.670Z",
          "content": "<p>assuming i created a kaggle dataset folder called <strong>my_models</strong> ,  how do i save to this folder using<br>\ntorch.save(state_dict, 'model_state.pth'), how do i pass the path of my folder? </p>",
          "rawMarkdown": "assuming i created a kaggle dataset folder called **my_models** ,  how do i save to this folder using\ntorch.save(state_dict, 'model_state.pth'), how do i pass the path of my folder? ",
          "replies": [
            {
              "id": 2070755,
              "postDate": "2022-12-20T09:42:17.957Z",
              "content": "<p>First you can <code>torch.save</code> and then download the <code>.pth</code> file generated by <code>torch.save</code> that you can then upload to <code>my_models</code>. Doing it this way allow you to control which models you upload to <code>my_models</code>.</p>",
              "rawMarkdown": "First you can ```torch.save``` and then download the ```.pth``` file generated by ```torch.save``` that you can then upload to ```my_models```. Doing it this way allow you to control which models you upload to ```my_models```."
            }
          ]
        }
      ]
    }
  ],
  "comments": [
    {
      "id": 2070586,
      "author_name": "Remek Kinas",
      "author_url": "",
      "post_date": "2022-12-20T07:18:05.933000",
      "content": "<p>You can create dataset manually or using Kaggle API (from notebook).</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 2070316,
      "author_name": "Natyu",
      "author_url": "",
      "post_date": "2022-12-19T20:30:12.817000",
      "content": "<p>Hi!</p>\n<p>Yes, if you are using PyTorch, you can save the state dict of your model using the <code>state_dict()</code> function and then uploading it to your personal Kaggle dataset. When you want to submit results, you can call the same model and load your trained weights by using the <code>load_state_dict()</code> function and specifying the path to your saved weights in your Kaggle dataset.</p>\n<p>Code to save and load the model state dict:</p>\n<pre><code># Save the state dict\nstate_dict = model.state_dict()\ntorch.save(state_dict, 'model_state.pth')\n\n# Load the state dict from your Kaggle dataset\nstate_dict = torch.load('/kaggle/input/your_dataset/model_state.pth')\n\n# Load the state dict into your model\nmodel.load_state_dict(state_dict)\n</code></pre>\n<p>Add your model state dict to your personal dataset:<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F6347360%2F280dd6d6bb2f0604d7a16f609d6537fc%2FScreenshot%202022-12-19%20212651.png?generation=1671481624644336&amp;alt=media\" alt=\"'How to add a file to a personnal dataset'\"></p>\n<p>I hope this helps!</p>",
      "votes": 2,
      "replies": [
        {
          "id": 2070649,
          "author_name": "Hakeem",
          "author_url": "",
          "post_date": "2022-12-20T08:24:27.670000",
          "content": "<p>assuming i created a kaggle dataset folder called <strong>my_models</strong> ,  how do i save to this folder using<br>\ntorch.save(state_dict, 'model_state.pth'), how do i pass the path of my folder? </p>",
          "votes": 0,
          "replies": [
            {
              "id": 2070755,
              "author_name": "Natyu",
              "author_url": "",
              "post_date": "2022-12-20T09:42:17.957000",
              "content": "<p>First you can <code>torch.save</code> and then download the <code>.pth</code> file generated by <code>torch.save</code> that you can then upload to <code>my_models</code>. Doing it this way allow you to control which models you upload to <code>my_models</code>.</p>",
              "votes": 0,
              "replies": []
            }
          ]
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2070586": "You can create dataset manually or using Kaggle API (from notebook).",
    "2068681": "Hi, is there a way i can train my model on kaggle and save it in my kaggle dataset. So my submissions only needs to load the model from my dataset and predict on the test set. Thanks.",
    "2070316": "Hi!\n\nYes, if you are using PyTorch, you can save the state dict of your model using the ```state_dict()``` function and then uploading it to your personal Kaggle dataset. When you want to submit results, you can call the same model and load your trained weights by using the ```load_state_dict()``` function and specifying the path to your saved weights in your Kaggle dataset.\n\nCode to save and load the model state dict:\n```\n# Save the state dict\nstate_dict = model.state_dict()\ntorch.save(state_dict, 'model_state.pth')\n\n# Load the state dict from your Kaggle dataset\nstate_dict = torch.load('/kaggle/input/your_dataset/model_state.pth')\n\n# Load the state dict into your model\nmodel.load_state_dict(state_dict)\n```\n\nAdd your model state dict to your personal dataset:\n!['How to add a file to a personnal dataset'](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F6347360%2F280dd6d6bb2f0604d7a16f609d6537fc%2FScreenshot%202022-12-19%20212651.png?generation=1671481624644336&alt=media)\n\n\nI hope this helps!"
  }
}