{
  "id": 177844,
  "title": "Question about loss functions in pytorch",
  "url": "/competitions/osic-pulmonary-fibrosis-progression/discussion/177844",
  "author_name": "",
  "post_date": "2020-08-27T14:31:04.976154300Z",
  "votes": 1,
  "comment_count": 4,
  "views": 0,
  "content": "<p>I am trying to predict  FVC  and Quadratic error with neural network. I wrote a custom loss function, but I'm new to PyTorch and I don't understand how to force a neural network with an external loss function. Does anyone have a link to a tutorial on using custom loss functions or working examples? I will be glad to any hints.</p>\n<p>def common_MSE(predicted, expected_fvc): <br>\n    fvc_se = (expected_fvc - predicted[:, 0]) ** 2<br>\n    fvc_mse = torch.mean(fvc_se)<br>\n    err_mse = torch.mean((fvc_se - predicted[:,1]) ** 2)<br>\n    common_err = torch.FloatTensor([fvc_mse, err_mse])<br>\n    return common_err</p>",
  "messages": [
    {
      "id": "987803",
      "postDate": "08/27/2020 14:31:04",
      "content": "<p>I am trying to predict  FVC  and Quadratic error with neural network. I wrote a custom loss function, but I'm new to PyTorch and I don't understand how to force a neural network with an external loss function. Does anyone have a link to a tutorial on using custom loss functions or working examples? I will be glad to any hints.</p>\n<p>def common_MSE(predicted, expected_fvc): <br>\n    fvc_se = (expected_fvc - predicted[:, 0]) ** 2<br>\n    fvc_mse = torch.mean(fvc_se)<br>\n    err_mse = torch.mean((fvc_se - predicted[:,1]) ** 2)<br>\n    common_err = torch.FloatTensor([fvc_mse, err_mse])<br>\n    return common_err</p>",
      "rawMarkdown": "I am trying to predict  FVC  and Quadratic error with neural network. I wrote a custom loss function, but I'm new to PyTorch and I don't understand how to force a neural network with an external loss function. Does anyone have a link to a tutorial on using custom loss functions or working examples? I will be glad to any hints.\n\ndef common_MSE(predicted, expected_fvc): \n    fvc_se = (expected_fvc - predicted[:, 0]) ** 2\n    fvc_mse = torch.mean(fvc_se)\n    err_mse = torch.mean((fvc_se - predicted[:,1]) ** 2)\n    common_err = torch.FloatTensor([fvc_mse, err_mse])\n    return common_err",
      "votes": null
    },
    {
      "id": "987897",
      "postDate": "08/27/2020 15:46:22",
      "content": "<p>If you are trying to use MSE as a loss function, it is already implemented in PyTorch:<br>\n<a href=\"https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html\" target=\"_blank\">https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html</a></p>\n<pre><code>import torch.nn as nn\ncriterion = nn.MSELoss()\nfor input, label in loader:\n    FVCpred = model(input)\n    loss = criterion(FVCpred, label)\n    loss.backward()\n    optimizer.step()\n</code></pre>\n<p>Something similar to this.<br>\nIf you would like to implement your custom loss, it is also possible (for example: <a href=\"https://discuss.pytorch.org/t/build-your-own-loss-function-in-pytorch/235/40)\" target=\"_blank\">https://discuss.pytorch.org/t/build-your-own-loss-function-in-pytorch/235/40)</a>. I think you can find more and better examples in the notebooks section.</p>",
      "rawMarkdown": "If you are trying to use MSE as a loss function, it is already implemented in PyTorch:\nhttps://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html\n```\nimport torch.nn as nn\ncriterion = nn.MSELoss()\nfor input, label in loader:\n    FVCpred = model(input)\n    loss = criterion(FVCpred, label)\n    loss.backward()\n    optimizer.step()\n```\nSomething similar to this.\nIf you would like to implement your custom loss, it is also possible (for example: https://discuss.pytorch.org/t/build-your-own-loss-function-in-pytorch/235/40). I think you can find more and better examples in the notebooks section.",
      "votes": null
    },
    {
      "id": "988053",
      "postDate": "08/27/2020 18:25:56",
      "content": "<p>Thank you so much. It all worked.</p>",
      "rawMarkdown": "Thank you so much. It all worked.",
      "votes": null
    },
    {
      "id": "989585",
      "postDate": "08/29/2020 00:05:24",
      "content": "<p><a href=\"https://www.kaggle.com/ahmedhshahin\" target=\"_blank\">@ahmedhshahin</a>  what is the optimal value of the metric ?</p>",
      "rawMarkdown": "ahmedhshahin  what is the optimal value of the metric ?",
      "votes": null
    },
    {
      "id": "989631",
      "postDate": "08/29/2020 01:19:32",
      "content": "<p>see this:<br>\n<a href=\"https://www.kaggle.com/c/osic-pulmonary-fibrosis-progression/discussion/176972#983437\" target=\"_blank\">https://www.kaggle.com/c/osic-pulmonary-fibrosis-progression/discussion/176972#983437</a></p>",
      "rawMarkdown": "see this:\nhttps://www.kaggle.com/c/osic-pulmonary-fibrosis-progression/discussion/176972#983437",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 987897,
      "author_name": "ahmedhshahin",
      "author_url": "",
      "post_date": "08/27/2020 15:46:22",
      "content": "<p>If you are trying to use MSE as a loss function, it is already implemented in PyTorch:<br>\n<a href=\"https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html\" target=\"_blank\">https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html</a></p>\n<pre><code>import torch.nn as nn\ncriterion = nn.MSELoss()\nfor input, label in loader:\n    FVCpred = model(input)\n    loss = criterion(FVCpred, label)\n    loss.backward()\n    optimizer.step()\n</code></pre>\n<p>Something similar to this.<br>\nIf you would like to implement your custom loss, it is also possible (for example: <a href=\"https://discuss.pytorch.org/t/build-your-own-loss-function-in-pytorch/235/40)\" target=\"_blank\">https://discuss.pytorch.org/t/build-your-own-loss-function-in-pytorch/235/40)</a>. I think you can find more and better examples in the notebooks section.</p>",
      "votes": null,
      "replies": [
        {
          "id": 988053,
          "author_name": "mavicmed",
          "author_url": "",
          "post_date": "08/27/2020 18:25:56",
          "content": "<p>Thank you so much. It all worked.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 989585,
          "author_name": "azzeineaftiss",
          "author_url": "",
          "post_date": "08/29/2020 00:05:24",
          "content": "<p><a href=\"https://www.kaggle.com/ahmedhshahin\" target=\"_blank\">@ahmedhshahin</a>  what is the optimal value of the metric ?</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 989631,
          "author_name": "ahmedhshahin",
          "author_url": "",
          "post_date": "08/29/2020 01:19:32",
          "content": "<p>see this:<br>\n<a href=\"https://www.kaggle.com/c/osic-pulmonary-fibrosis-progression/discussion/176972#983437\" target=\"_blank\">https://www.kaggle.com/c/osic-pulmonary-fibrosis-progression/discussion/176972#983437</a></p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "987803": "I am trying to predict  FVC  and Quadratic error with neural network. I wrote a custom loss function, but I'm new to PyTorch and I don't understand how to force a neural network with an external loss function. Does anyone have a link to a tutorial on using custom loss functions or working examples? I will be glad to any hints.\n\ndef common_MSE(predicted, expected_fvc): \n    fvc_se = (expected_fvc - predicted[:, 0]) ** 2\n    fvc_mse = torch.mean(fvc_se)\n    err_mse = torch.mean((fvc_se - predicted[:,1]) ** 2)\n    common_err = torch.FloatTensor([fvc_mse, err_mse])\n    return common_err",
    "987897": "If you are trying to use MSE as a loss function, it is already implemented in PyTorch:\nhttps://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html\n```\nimport torch.nn as nn\ncriterion = nn.MSELoss()\nfor input, label in loader:\n    FVCpred = model(input)\n    loss = criterion(FVCpred, label)\n    loss.backward()\n    optimizer.step()\n```\nSomething similar to this.\nIf you would like to implement your custom loss, it is also possible (for example: https://discuss.pytorch.org/t/build-your-own-loss-function-in-pytorch/235/40). I think you can find more and better examples in the notebooks section.",
    "988053": "Thank you so much. It all worked.",
    "989585": "ahmedhshahin  what is the optimal value of the metric ?",
    "989631": "see this:\nhttps://www.kaggle.com/c/osic-pulmonary-fibrosis-progression/discussion/176972#983437"
  },
  "source": "meta"
}