{
  "id": 57136,
  "title": "NAN in loss of Keras NN? help!",
  "url": "/competitions/avito-demand-prediction/discussion/57136",
  "author_name": "AmirH",
  "post_date": "2018-05-19T19:33:14.652000",
  "votes": 7,
  "comment_count": 18,
  "views": 0,
  "content": "<p>Hi guys and gals,\nI'm trying to run a keras NN on the data and my loss and metric data is NAN:</p>\n\n<p>This is my keras code (In R), Any ideas?\n(i know i should use RMSE and not MSE, i'm just testing things out, as RMSE requires a side function)\nThanks!!!</p>\n\n<p>library(\"keras\")</p>\n\n<p>model &lt;- keras_model_sequential()</p>\n\n<p>model %&gt;% </p>\n\n<p>layer_dense(units=43,activation=\"relu\",input_shape=c(43))  %&gt;%</p>\n\n<p>layer_dropout(rate=0.2) %&gt;%</p>\n\n<p>layer_dense(units = 22,activation = \"relu\") %&gt;%</p>\n\n<p>layer_dropout(rate=0.2) %&gt;%</p>\n\n<p>layer_dense(units=1,activation=\"relu\") </p>\n\n<p>summary(model)</p>\n\n<p>model %&gt;%\n  compile(loss =\"mean_squared_error\",\n          optimizer = \"adam\",\n          metrics= c(\"mse\"))</p>\n\n<p>history&lt;-model %&gt;% fit(TrainData, DealProbs, epochs = 1,\n                       callbacks = callback_tensorboard(log_dir = \"logs/run_b\"),\n                       validation_split = 0.2) #train on 80% of train set and will evaluate </p>\n\n<p>The result (I ran it on small part of the data to test)\nEpoch 1/1\n80000/80000 [==============================] - 5s 60us/step - loss: nan - mean_squared_error: nan - val_loss: nan - val_mean_squared_error: nan</p>",
  "messages": [
    {
      "id": 539860,
      "postDate": "2019-05-30T16:07:36.577Z",
      "content": "<p>It is not because of having missing values in the data. It is because the activation function is out of range for handling the input weight vector. Change the activation to softmax in the output layer and try,</p>",
      "rawMarkdown": "It is not because of having missing values in the data. It is because the activation function is out of range for handling the input weight vector. Change the activation to softmax in the output layer and try,",
      "votes": 13,
      "replies": [
        {
          "id": 665795,
          "postDate": "2019-11-05T12:06:49.213Z",
          "content": "<p>Your answer solve my issue. Thanks</p>",
          "rawMarkdown": "Your answer solve my issue. Thanks"
        },
        {
          "id": 808077,
          "postDate": "2020-04-15T06:07:06.347Z",
          "content": "<p>thank you, it solved my issue too, but in my case sigmoid seems to be more suitable</p>",
          "rawMarkdown": "thank you, it solved my issue too, but in my case sigmoid seems to be more suitable"
        }
      ]
    },
    {
      "id": 330915,
      "postDate": "2018-05-20T02:55:56.597Z",
      "content": "<p>I'v faced this problem before. Its definitely NaN's in the input.  Printing out DataFrame.isnull().any() will do the trick.</p>",
      "rawMarkdown": "I'v faced this problem before. Its definitely NaN's in the input.  Printing out DataFrame.isnull().any() will do the trick.",
      "votes": 13
    },
    {
      "id": 330820,
      "postDate": "2018-05-19T19:33:14.653Z",
      "content": "<p>Hi guys and gals,\nI'm trying to run a keras NN on the data and my loss and metric data is NAN:</p>\n\n<p>This is my keras code (In R), Any ideas?\n(i know i should use RMSE and not MSE, i'm just testing things out, as RMSE requires a side function)\nThanks!!!</p>\n\n<p>library(\"keras\")</p>\n\n<p>model &lt;- keras_model_sequential()</p>\n\n<p>model %&gt;% </p>\n\n<p>layer_dense(units=43,activation=\"relu\",input_shape=c(43))  %&gt;%</p>\n\n<p>layer_dropout(rate=0.2) %&gt;%</p>\n\n<p>layer_dense(units = 22,activation = \"relu\") %&gt;%</p>\n\n<p>layer_dropout(rate=0.2) %&gt;%</p>\n\n<p>layer_dense(units=1,activation=\"relu\") </p>\n\n<p>summary(model)</p>\n\n<p>model %&gt;%\n  compile(loss =\"mean_squared_error\",\n          optimizer = \"adam\",\n          metrics= c(\"mse\"))</p>\n\n<p>history&lt;-model %&gt;% fit(TrainData, DealProbs, epochs = 1,\n                       callbacks = callback_tensorboard(log_dir = \"logs/run_b\"),\n                       validation_split = 0.2) #train on 80% of train set and will evaluate </p>\n\n<p>The result (I ran it on small part of the data to test)\nEpoch 1/1\n80000/80000 [==============================] - 5s 60us/step - loss: nan - mean_squared_error: nan - val_loss: nan - val_mean_squared_error: nan</p>",
      "rawMarkdown": "Hi guys and gals,\nI'm trying to run a keras NN on the data and my loss and metric data is NAN:\n\nThis is my keras code (In R), Any ideas?\n(i know i should use RMSE and not MSE, i'm just testing things out, as RMSE requires a side function)\nThanks!!!\n\n\nlibrary(\"keras\")\n\nmodel &lt;- keras_model_sequential()\n\nmodel %&gt;% \n\n  layer_dense(units=43,activation=\"relu\",input_shape=c(43))  %&gt;%\n\n  layer_dropout(rate=0.2) %&gt;%\n\n  layer_dense(units = 22,activation = \"relu\") %&gt;%\n\n   layer_dropout(rate=0.2) %&gt;%\n  \n\n  layer_dense(units=1,activation=\"relu\") \n\nsummary(model)\n\nmodel %&gt;%\n  compile(loss =\"mean_squared_error\",\n          optimizer = \"adam\",\n          metrics= c(\"mse\"))\n\nhistory&lt;-model %&gt;% fit(TrainData, DealProbs, epochs = 1,\n                       callbacks = callback_tensorboard(log_dir = \"logs/run_b\"),\n                       validation_split = 0.2) #train on 80% of train set and will evaluate \n\nThe result (I ran it on small part of the data to test)\nEpoch 1/1\n80000/80000 [==============================] - 5s 60us/step - loss: nan - mean_squared_error: nan - val_loss: nan - val_mean_squared_error: nan",
      "votes": 7
    },
    {
      "id": 330824,
      "postDate": "2018-05-19T19:37:27.707Z",
      "content": "<p>You for sure have some nans in your input. Check your continuous variables, especially the ones you've FE'd (if you do FE for your NNet?) and make sure there aren't any NANs in there.</p>",
      "rawMarkdown": "You for sure have some nans in your input. Check your continuous variables, especially the ones you've FE'd (if you do FE for your NNet?) and make sure there aren't any NANs in there.",
      "votes": 5,
      "replies": [
        {
          "id": 330825,
          "postDate": "2018-05-19T19:39:00.443Z",
          "content": "<p>Thanks my friend, i will check that out!\nI handled only NAs, not NANs</p>",
          "rawMarkdown": "Thanks my friend, i will check that out!\nI handled only NAs, not NANs"
        },
        {
          "id": 580923,
          "postDate": "2019-07-21T04:32:21.853Z",
          "content": "<p>Handling nan in input solved the issue... Thanks!</p>",
          "rawMarkdown": "Handling nan in input solved the issue... Thanks!",
          "votes": 3
        },
        {
          "id": 1616331,
          "postDate": "2021-12-13T10:52:42.493Z",
          "content": "<p>Solved my problem. Had issues with NaNs.</p>",
          "rawMarkdown": "Solved my problem. Had issues with NaNs."
        }
      ]
    },
    {
      "id": 1495197,
      "postDate": "2021-08-29T11:36:22.047Z",
      "content": "<p>I had the nan loss problem in image data, I was using <strong>np.empty</strong> for generating the batches of images and looks like it was the reason of the nan loss, changed <strong>np.empty</strong> to <strong>np.zeros</strong> and problem solved.</p>",
      "rawMarkdown": "I had the nan loss problem in image data, I was using **np.empty** for generating the batches of images and looks like it was the reason of the nan loss, changed **np.empty** to **np.zeros** and problem solved."
    },
    {
      "id": 959399,
      "postDate": "2020-08-05T14:53:11.320Z",
      "content": "<p>Guys even i tried making a an ANN with keras .....i had double checked but there r no NaN's in my input this is my code...model = tf.keras.Sequential([\n                             tf.keras.layers.Dense(200, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(125, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(625, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(100, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(175, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(750, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(600, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(25, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(2, activation='softmax'),\n                             tf.keras.layers.BatchNormalization(),\nIn my compilation i made sure loss was binary crossentropy loss becuz i hv probability distributions....but still i get loss alone as nan while accuracy as 0.2724 like here....in my first epoch....Epoch 1/20\n15/15 [==============================] - 0s 17ms/step - loss: 'nan' - accuracy: 0.3841\nHow can i fix this issue??</p>",
      "rawMarkdown": "Guys even i tried making a an ANN with keras .....i had double checked but there r no NaN's in my input this is my code...model = tf.keras.Sequential([\n                             tf.keras.layers.Dense(200, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(125, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(625, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(100, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(175, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(750, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(600, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(25, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(2, activation='softmax'),\n                             tf.keras.layers.BatchNormalization(),\nIn my compilation i made sure loss was binary crossentropy loss becuz i hv probability distributions....but still i get loss alone as nan while accuracy as 0.2724 like here....in my first epoch....Epoch 1/20\n15/15 [==============================] - 0s 17ms/step - loss: 'nan' - accuracy: 0.3841\nHow can i fix this issue??",
      "replies": [
        {
          "id": 960421,
          "postDate": "2020-08-06T11:41:04.723Z",
          "content": "<p><a href=\"/sidhivinayak\">@sidhivinayak</a> \nHi,\nnot sure about that,\nbut maybe try removing the excess BatchNormalization you have after your softmax?</p>",
          "rawMarkdown": "@sidhivinayak \nHi,\nnot sure about that,\nbut maybe try removing the excess BatchNormalization you have after your softmax?\n",
          "votes": 1
        },
        {
          "id": 960592,
          "postDate": "2020-08-06T14:24:06.020Z",
          "content": "<p>ok lemme try that thank you!</p>",
          "rawMarkdown": "ok lemme try that thank you!"
        }
      ]
    },
    {
      "id": 805629,
      "postDate": "2020-04-12T21:19:34.157Z",
      "content": "<p>model = Sequential()\nmodel.add(Conv2D(16,(3,3),activation='relu' , input_shape = X_train[0].shape))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.1))</p>\n\n<p>model.add(Conv2D(32,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.2))</p>\n\n<p>model.add(Conv2D(64,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.3))</p>\n\n<p>model.add(Conv2D(128,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.3))</p>\n\n<p>model.add(Flatten())</p>\n\n<p>model.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))</p>\n\n<p>model.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))</p>\n\n<p>model.add(Dense(5, activation='softmax'))</p>\n\n<p>i am using this and getting loss:Nan</p>",
      "rawMarkdown": "\nmodel = Sequential()\nmodel.add(Conv2D(16,(3,3),activation='relu' , input_shape = X_train[0].shape))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.1))\n\n\n\nmodel.add(Conv2D(32,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.2))\n\n\n\n\nmodel.add(Conv2D(64,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.3))\n\n\nmodel.add(Conv2D(128,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.3))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(5, activation='softmax'))\n\ni am using this and getting loss:Nan",
      "replies": [
        {
          "id": 825134,
          "postDate": "2020-04-28T20:00:12.040Z",
          "content": "<p>How did you compile the model ? </p>",
          "rawMarkdown": "How did you compile the model ? "
        }
      ]
    },
    {
      "id": 546366,
      "postDate": "2019-06-06T14:16:52.513Z",
      "content": "<p>NaN's in the input , this is my case.</p>",
      "rawMarkdown": " NaN's in the input , this is my case."
    },
    {
      "id": 393944,
      "postDate": "2018-09-26T05:20:11.983Z",
      "content": "<p>Hi there,</p>\n\n<p>I am also facing this issue. I am creating AutoEncoder (DAE).\nStructure is</p>\n\n<pre><code>cols = X.shape[1]\n</code></pre>\n\n<p>xt, xval = train_test_split(X, test_size = 0.1, random_state = 7)</p>\n\n<p>inp = Input(shape=(cols,))</p>\n\n<p>encoded = Dense(cols * 2, activation='relu')(inp)</p>\n\n<p>encoded = Dropout(0.5)(encoded)</p>\n\n<p>encoded = Dense(cols , activation='relu')(encoded)</p>\n\n<p>encoded = Dropout(0.2)(encoded)</p>\n\n<p>encoded = Dense(cols // 4, activation='relu')(encoded)</p>\n\n<p>decoded = Dense(cols // 4, activation='relu')(encoded)</p>\n\n<p>encoded = Dropout(0.2)(encoded)</p>\n\n<p>decoded = Dense(cols, activation='relu')(decoded)</p>\n\n<p>encoded = Dropout(0.5)(encoded)</p>\n\n<p>decoded = Dense(cols * 2, activation='relu')(decoded)</p>\n\n<p>decoded = Dense(cols, activation='sigmoid')(decoded)</p>\n\n<p>autoencoder = Model(inp, decoded)</p>\n\n<p>autoencoder.compile(optimizer='adam', loss='mse')</p>\n\n<p>I also tried following to fillna.</p>\n\n<p>X = X.fillna(0)</p>\n\n<p>X[np.isnan(X)] = 0</p>\n\n<p>for i in X.columns: X[i] = X[i].fillna(0)</p>\n\n<p>Then also I am getting nan as train &amp; val loss\nCan anyone please help..</p>\n\n<p>Thanks</p>",
      "rawMarkdown": "Hi there,\n\nI am also facing this issue. I am creating AutoEncoder (DAE).\nStructure is\n\n    cols = X.shape[1]\n\nxt, xval = train_test_split(X, test_size = 0.1, random_state = 7)\n\ninp = Input(shape=(cols,))\n\nencoded = Dense(cols * 2, activation='relu')(inp)\n\nencoded = Dropout(0.5)(encoded)\n\nencoded = Dense(cols , activation='relu')(encoded)\n\nencoded = Dropout(0.2)(encoded)\n\nencoded = Dense(cols // 4, activation='relu')(encoded)\n\n\ndecoded = Dense(cols // 4, activation='relu')(encoded)\n\nencoded = Dropout(0.2)(encoded)\n\ndecoded = Dense(cols, activation='relu')(decoded)\n\nencoded = Dropout(0.5)(encoded)\n\ndecoded = Dense(cols * 2, activation='relu')(decoded)\n\ndecoded = Dense(cols, activation='sigmoid')(decoded)\n\nautoencoder = Model(inp, decoded)\n\nautoencoder.compile(optimizer='adam', loss='mse')\n\nI also tried following to fillna.\n\nX = X.fillna(0)\n\nX[np.isnan(X)] = 0\n\nfor i in X.columns: X[i] = X[i].fillna(0)\n\nThen also I am getting nan as train &amp; val loss\nCan anyone please help..\n\nThanks",
      "replies": [
        {
          "id": 660664,
          "postDate": "2019-10-29T12:49:43.093Z",
          "content": "<p>Hi Prashant,</p>\n\n<p>in my case it was the exact 0 values for NaN. Maybe one of both:\n```</p>\n\n<h1>Not exact 0 value</h1>\n\n<p>X.fillna(1e-10)\n<code>\n</code></p>\n\n<h1>Forward fill last known value</h1>\n\n<p>X.fillna(method='ffill')\n```\nThanks,\nDennis</p>",
          "rawMarkdown": "Hi Prashant,\n\nin my case it was the exact 0 values for NaN. Maybe one of both:\n```\n#Not exact 0 value\nX.fillna(1e-10)\n```\n```\n#Forward fill last known value\nX.fillna(method='ffill')\n```\nThanks,\nDennis"
        }
      ]
    },
    {
      "id": 1090900,
      "postDate": "2020-11-25T16:53:06.187Z",
      "rawMarkdown": "",
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 539860,
      "author_name": "TejaChebrolu",
      "author_url": "",
      "post_date": "2019-05-30T16:07:36.577000",
      "content": "<p>It is not because of having missing values in the data. It is because the activation function is out of range for handling the input weight vector. Change the activation to softmax in the output layer and try,</p>",
      "votes": 13,
      "replies": [
        {
          "id": 665795,
          "author_name": "Serkan Peldek",
          "author_url": "",
          "post_date": "2019-11-05T12:06:49.213000",
          "content": "<p>Your answer solve my issue. Thanks</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 808077,
          "author_name": "Vadim Placinta",
          "author_url": "",
          "post_date": "2020-04-15T06:07:06.347000",
          "content": "<p>thank you, it solved my issue too, but in my case sigmoid seems to be more suitable</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 330915,
      "author_name": "Shanth",
      "author_url": "",
      "post_date": "2018-05-20T02:55:56.597000",
      "content": "<p>I'v faced this problem before. Its definitely NaN's in the input.  Printing out DataFrame.isnull().any() will do the trick.</p>",
      "votes": 13,
      "replies": []
    },
    {
      "id": 330824,
      "author_name": "عثمان",
      "author_url": "",
      "post_date": "2018-05-19T19:37:27.707000",
      "content": "<p>You for sure have some nans in your input. Check your continuous variables, especially the ones you've FE'd (if you do FE for your NNet?) and make sure there aren't any NANs in there.</p>",
      "votes": 5,
      "replies": [
        {
          "id": 330825,
          "author_name": "AmirH",
          "author_url": "",
          "post_date": "2018-05-19T19:39:00.443000",
          "content": "<p>Thanks my friend, i will check that out!\nI handled only NAs, not NANs</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 580923,
          "author_name": "Kranthi Kumar",
          "author_url": "",
          "post_date": "2019-07-21T04:32:21.853000",
          "content": "<p>Handling nan in input solved the issue... Thanks!</p>",
          "votes": 3,
          "replies": []
        },
        {
          "id": 1616331,
          "author_name": "Himanshu",
          "author_url": "",
          "post_date": "2021-12-13T10:52:42.493000",
          "content": "<p>Solved my problem. Had issues with NaNs.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 1495197,
      "author_name": "Samyar Rahimi",
      "author_url": "",
      "post_date": "2021-08-29T11:36:22.047000",
      "content": "<p>I had the nan loss problem in image data, I was using <strong>np.empty</strong> for generating the batches of images and looks like it was the reason of the nan loss, changed <strong>np.empty</strong> to <strong>np.zeros</strong> and problem solved.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 959399,
      "author_name": "Sidhivinayak",
      "author_url": "",
      "post_date": "2020-08-05T14:53:11.320000",
      "content": "<p>Guys even i tried making a an ANN with keras .....i had double checked but there r no NaN's in my input this is my code...model = tf.keras.Sequential([\n                             tf.keras.layers.Dense(200, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(125, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(625, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(100, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(175, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(750, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(600, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(25, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(2, activation='softmax'),\n                             tf.keras.layers.BatchNormalization(),\nIn my compilation i made sure loss was binary crossentropy loss becuz i hv probability distributions....but still i get loss alone as nan while accuracy as 0.2724 like here....in my first epoch....Epoch 1/20\n15/15 [==============================] - 0s 17ms/step - loss: 'nan' - accuracy: 0.3841\nHow can i fix this issue??</p>",
      "votes": 0,
      "replies": [
        {
          "id": 960421,
          "author_name": "AmirH",
          "author_url": "",
          "post_date": "2020-08-06T11:41:04.723000",
          "content": "<p><a href=\"/sidhivinayak\">@sidhivinayak</a> \nHi,\nnot sure about that,\nbut maybe try removing the excess BatchNormalization you have after your softmax?</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 960592,
          "author_name": "Sidhivinayak",
          "author_url": "",
          "post_date": "2020-08-06T14:24:06.020000",
          "content": "<p>ok lemme try that thank you!</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 805629,
      "author_name": "Nikunj Pithwa",
      "author_url": "",
      "post_date": "2020-04-12T21:19:34.157000",
      "content": "<p>model = Sequential()\nmodel.add(Conv2D(16,(3,3),activation='relu' , input_shape = X_train[0].shape))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.1))</p>\n\n<p>model.add(Conv2D(32,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.2))</p>\n\n<p>model.add(Conv2D(64,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.3))</p>\n\n<p>model.add(Conv2D(128,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.3))</p>\n\n<p>model.add(Flatten())</p>\n\n<p>model.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))</p>\n\n<p>model.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))</p>\n\n<p>model.add(Dense(5, activation='softmax'))</p>\n\n<p>i am using this and getting loss:Nan</p>",
      "votes": 0,
      "replies": [
        {
          "id": 825134,
          "author_name": "Aakarsh Yadav",
          "author_url": "",
          "post_date": "2020-04-28T20:00:12.040000",
          "content": "<p>How did you compile the model ? </p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 546366,
      "author_name": "Yang Yang",
      "author_url": "",
      "post_date": "2019-06-06T14:16:52.513000",
      "content": "<p>NaN's in the input , this is my case.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 393944,
      "author_name": "Prashant Kikani",
      "author_url": "",
      "post_date": "2018-09-26T05:20:11.983000",
      "content": "<p>Hi there,</p>\n\n<p>I am also facing this issue. I am creating AutoEncoder (DAE).\nStructure is</p>\n\n<pre><code>cols = X.shape[1]\n</code></pre>\n\n<p>xt, xval = train_test_split(X, test_size = 0.1, random_state = 7)</p>\n\n<p>inp = Input(shape=(cols,))</p>\n\n<p>encoded = Dense(cols * 2, activation='relu')(inp)</p>\n\n<p>encoded = Dropout(0.5)(encoded)</p>\n\n<p>encoded = Dense(cols , activation='relu')(encoded)</p>\n\n<p>encoded = Dropout(0.2)(encoded)</p>\n\n<p>encoded = Dense(cols // 4, activation='relu')(encoded)</p>\n\n<p>decoded = Dense(cols // 4, activation='relu')(encoded)</p>\n\n<p>encoded = Dropout(0.2)(encoded)</p>\n\n<p>decoded = Dense(cols, activation='relu')(decoded)</p>\n\n<p>encoded = Dropout(0.5)(encoded)</p>\n\n<p>decoded = Dense(cols * 2, activation='relu')(decoded)</p>\n\n<p>decoded = Dense(cols, activation='sigmoid')(decoded)</p>\n\n<p>autoencoder = Model(inp, decoded)</p>\n\n<p>autoencoder.compile(optimizer='adam', loss='mse')</p>\n\n<p>I also tried following to fillna.</p>\n\n<p>X = X.fillna(0)</p>\n\n<p>X[np.isnan(X)] = 0</p>\n\n<p>for i in X.columns: X[i] = X[i].fillna(0)</p>\n\n<p>Then also I am getting nan as train &amp; val loss\nCan anyone please help..</p>\n\n<p>Thanks</p>",
      "votes": 0,
      "replies": [
        {
          "id": 660664,
          "author_name": "Dennis_AFK",
          "author_url": "",
          "post_date": "2019-10-29T12:49:43.093000",
          "content": "<p>Hi Prashant,</p>\n\n<p>in my case it was the exact 0 values for NaN. Maybe one of both:\n```</p>\n\n<h1>Not exact 0 value</h1>\n\n<p>X.fillna(1e-10)\n<code>\n</code></p>\n\n<h1>Forward fill last known value</h1>\n\n<p>X.fillna(method='ffill')\n```\nThanks,\nDennis</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 1090900,
      "author_name": "",
      "author_url": "",
      "post_date": "2020-11-25T16:53:06.187000",
      "content": "",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "539860": "It is not because of having missing values in the data. It is because the activation function is out of range for handling the input weight vector. Change the activation to softmax in the output layer and try,",
    "330915": "I'v faced this problem before. Its definitely NaN's in the input.  Printing out DataFrame.isnull().any() will do the trick.",
    "330820": "Hi guys and gals,\nI'm trying to run a keras NN on the data and my loss and metric data is NAN:\n\nThis is my keras code (In R), Any ideas?\n(i know i should use RMSE and not MSE, i'm just testing things out, as RMSE requires a side function)\nThanks!!!\n\n\nlibrary(\"keras\")\n\nmodel &lt;- keras_model_sequential()\n\nmodel %&gt;% \n\n  layer_dense(units=43,activation=\"relu\",input_shape=c(43))  %&gt;%\n\n  layer_dropout(rate=0.2) %&gt;%\n\n  layer_dense(units = 22,activation = \"relu\") %&gt;%\n\n   layer_dropout(rate=0.2) %&gt;%\n  \n\n  layer_dense(units=1,activation=\"relu\") \n\nsummary(model)\n\nmodel %&gt;%\n  compile(loss =\"mean_squared_error\",\n          optimizer = \"adam\",\n          metrics= c(\"mse\"))\n\nhistory&lt;-model %&gt;% fit(TrainData, DealProbs, epochs = 1,\n                       callbacks = callback_tensorboard(log_dir = \"logs/run_b\"),\n                       validation_split = 0.2) #train on 80% of train set and will evaluate \n\nThe result (I ran it on small part of the data to test)\nEpoch 1/1\n80000/80000 [==============================] - 5s 60us/step - loss: nan - mean_squared_error: nan - val_loss: nan - val_mean_squared_error: nan",
    "330824": "You for sure have some nans in your input. Check your continuous variables, especially the ones you've FE'd (if you do FE for your NNet?) and make sure there aren't any NANs in there.",
    "1495197": "I had the nan loss problem in image data, I was using **np.empty** for generating the batches of images and looks like it was the reason of the nan loss, changed **np.empty** to **np.zeros** and problem solved.",
    "959399": "Guys even i tried making a an ANN with keras .....i had double checked but there r no NaN's in my input this is my code...model = tf.keras.Sequential([\n                             tf.keras.layers.Dense(200, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(125, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(625, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(100, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(175, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(750, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(600, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(25, activation='relu', kernel_initializer='he_uniform'),\n                             tf.keras.layers.BatchNormalization(),\n                             tf.keras.layers.Dense(2, activation='softmax'),\n                             tf.keras.layers.BatchNormalization(),\nIn my compilation i made sure loss was binary crossentropy loss becuz i hv probability distributions....but still i get loss alone as nan while accuracy as 0.2724 like here....in my first epoch....Epoch 1/20\n15/15 [==============================] - 0s 17ms/step - loss: 'nan' - accuracy: 0.3841\nHow can i fix this issue??",
    "805629": "\nmodel = Sequential()\nmodel.add(Conv2D(16,(3,3),activation='relu' , input_shape = X_train[0].shape))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.1))\n\n\n\nmodel.add(Conv2D(32,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.2))\n\n\n\n\nmodel.add(Conv2D(64,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.3))\n\n\nmodel.add(Conv2D(128,(3,3),activation='relu'  ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2,2))\nmodel.add(Dropout(0.3))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(5, activation='softmax'))\n\ni am using this and getting loss:Nan",
    "546366": " NaN's in the input , this is my case.",
    "393944": "Hi there,\n\nI am also facing this issue. I am creating AutoEncoder (DAE).\nStructure is\n\n    cols = X.shape[1]\n\nxt, xval = train_test_split(X, test_size = 0.1, random_state = 7)\n\ninp = Input(shape=(cols,))\n\nencoded = Dense(cols * 2, activation='relu')(inp)\n\nencoded = Dropout(0.5)(encoded)\n\nencoded = Dense(cols , activation='relu')(encoded)\n\nencoded = Dropout(0.2)(encoded)\n\nencoded = Dense(cols // 4, activation='relu')(encoded)\n\n\ndecoded = Dense(cols // 4, activation='relu')(encoded)\n\nencoded = Dropout(0.2)(encoded)\n\ndecoded = Dense(cols, activation='relu')(decoded)\n\nencoded = Dropout(0.5)(encoded)\n\ndecoded = Dense(cols * 2, activation='relu')(decoded)\n\ndecoded = Dense(cols, activation='sigmoid')(decoded)\n\nautoencoder = Model(inp, decoded)\n\nautoencoder.compile(optimizer='adam', loss='mse')\n\nI also tried following to fillna.\n\nX = X.fillna(0)\n\nX[np.isnan(X)] = 0\n\nfor i in X.columns: X[i] = X[i].fillna(0)\n\nThen also I am getting nan as train &amp; val loss\nCan anyone please help..\n\nThanks",
    "1090900": ""
  }
}