Regression loss function is nan












1












$begingroup$


I'm beginner with ANN and DL in general. I have a regression task with target of 2 dimensions, my dataset has only 46 samples (small dataset I think).
I tried the code (below) for a regression with only 1 output, it works normally.
When I change to 2-dimensions Regression, I get a loss function = NaN
I tried to change the optimizer and fix the dropout rate, but nothing changed, any solution?
thank you for your help in advance.



import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

#load data
dataframe = pandas.read_excel("data1.xlsx")
dataframe.isnull().any()

dataset = dataframe.values

#print (dataset)

X = dataset[:, 0:5]
Y = dataset[:,5:7]

def baseline_model():
#create model
model = Sequential()
model.add(Dense(10, input_dim=5, kernel_initializer='normal', activation
='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, kernel_initializer='normal'))

#compile model
#model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
model.compile(loss='mean_squared_error', optimizer='adam')
return model



#fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

estimators=
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, epochs=50,
batch_size=32, verbose=0)))
pipeline = Pipeline(estimators)
kfold = KFold(n_splits=10, random_state = seed)
results = cross_val_score(pipeline, X, Y, cv=kfold)
print ("Result: %.2f (%.2f) MSE" %(results.mean(), results.std()))









share|improve this question







New contributor




Kikio.1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    1












    $begingroup$


    I'm beginner with ANN and DL in general. I have a regression task with target of 2 dimensions, my dataset has only 46 samples (small dataset I think).
    I tried the code (below) for a regression with only 1 output, it works normally.
    When I change to 2-dimensions Regression, I get a loss function = NaN
    I tried to change the optimizer and fix the dropout rate, but nothing changed, any solution?
    thank you for your help in advance.



    import numpy
    import pandas
    from keras.models import Sequential
    from keras.layers import Dense
    from keras.layers import Dropout
    from keras.wrappers.scikit_learn import KerasRegressor
    from sklearn.model_selection import cross_val_score
    from sklearn.model_selection import KFold
    from sklearn.preprocessing import StandardScaler
    from sklearn.pipeline import Pipeline

    #load data
    dataframe = pandas.read_excel("data1.xlsx")
    dataframe.isnull().any()

    dataset = dataframe.values

    #print (dataset)

    X = dataset[:, 0:5]
    Y = dataset[:,5:7]

    def baseline_model():
    #create model
    model = Sequential()
    model.add(Dense(10, input_dim=5, kernel_initializer='normal', activation
    ='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(2, kernel_initializer='normal'))

    #compile model
    #model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model



    #fix random seed for reproducibility
    seed = 7
    numpy.random.seed(seed)

    estimators=
    estimators.append(('standardize', StandardScaler()))
    estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, epochs=50,
    batch_size=32, verbose=0)))
    pipeline = Pipeline(estimators)
    kfold = KFold(n_splits=10, random_state = seed)
    results = cross_val_score(pipeline, X, Y, cv=kfold)
    print ("Result: %.2f (%.2f) MSE" %(results.mean(), results.std()))









    share|improve this question







    New contributor




    Kikio.1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      1












      1








      1





      $begingroup$


      I'm beginner with ANN and DL in general. I have a regression task with target of 2 dimensions, my dataset has only 46 samples (small dataset I think).
      I tried the code (below) for a regression with only 1 output, it works normally.
      When I change to 2-dimensions Regression, I get a loss function = NaN
      I tried to change the optimizer and fix the dropout rate, but nothing changed, any solution?
      thank you for your help in advance.



      import numpy
      import pandas
      from keras.models import Sequential
      from keras.layers import Dense
      from keras.layers import Dropout
      from keras.wrappers.scikit_learn import KerasRegressor
      from sklearn.model_selection import cross_val_score
      from sklearn.model_selection import KFold
      from sklearn.preprocessing import StandardScaler
      from sklearn.pipeline import Pipeline

      #load data
      dataframe = pandas.read_excel("data1.xlsx")
      dataframe.isnull().any()

      dataset = dataframe.values

      #print (dataset)

      X = dataset[:, 0:5]
      Y = dataset[:,5:7]

      def baseline_model():
      #create model
      model = Sequential()
      model.add(Dense(10, input_dim=5, kernel_initializer='normal', activation
      ='relu'))
      model.add(Dropout(0.5))
      model.add(Dense(2, kernel_initializer='normal'))

      #compile model
      #model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
      model.compile(loss='mean_squared_error', optimizer='adam')
      return model



      #fix random seed for reproducibility
      seed = 7
      numpy.random.seed(seed)

      estimators=
      estimators.append(('standardize', StandardScaler()))
      estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, epochs=50,
      batch_size=32, verbose=0)))
      pipeline = Pipeline(estimators)
      kfold = KFold(n_splits=10, random_state = seed)
      results = cross_val_score(pipeline, X, Y, cv=kfold)
      print ("Result: %.2f (%.2f) MSE" %(results.mean(), results.std()))









      share|improve this question







      New contributor




      Kikio.1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I'm beginner with ANN and DL in general. I have a regression task with target of 2 dimensions, my dataset has only 46 samples (small dataset I think).
      I tried the code (below) for a regression with only 1 output, it works normally.
      When I change to 2-dimensions Regression, I get a loss function = NaN
      I tried to change the optimizer and fix the dropout rate, but nothing changed, any solution?
      thank you for your help in advance.



      import numpy
      import pandas
      from keras.models import Sequential
      from keras.layers import Dense
      from keras.layers import Dropout
      from keras.wrappers.scikit_learn import KerasRegressor
      from sklearn.model_selection import cross_val_score
      from sklearn.model_selection import KFold
      from sklearn.preprocessing import StandardScaler
      from sklearn.pipeline import Pipeline

      #load data
      dataframe = pandas.read_excel("data1.xlsx")
      dataframe.isnull().any()

      dataset = dataframe.values

      #print (dataset)

      X = dataset[:, 0:5]
      Y = dataset[:,5:7]

      def baseline_model():
      #create model
      model = Sequential()
      model.add(Dense(10, input_dim=5, kernel_initializer='normal', activation
      ='relu'))
      model.add(Dropout(0.5))
      model.add(Dense(2, kernel_initializer='normal'))

      #compile model
      #model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
      model.compile(loss='mean_squared_error', optimizer='adam')
      return model



      #fix random seed for reproducibility
      seed = 7
      numpy.random.seed(seed)

      estimators=
      estimators.append(('standardize', StandardScaler()))
      estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, epochs=50,
      batch_size=32, verbose=0)))
      pipeline = Pipeline(estimators)
      kfold = KFold(n_splits=10, random_state = seed)
      results = cross_val_score(pipeline, X, Y, cv=kfold)
      print ("Result: %.2f (%.2f) MSE" %(results.mean(), results.std()))






      python neural-network keras regression






      share|improve this question







      New contributor




      Kikio.1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Kikio.1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Kikio.1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 15 mins ago









      Kikio.1Kikio.1

      62




      62




      New contributor




      Kikio.1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Kikio.1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Kikio.1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Kikio.1 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f44302%2fregression-loss-function-is-nan%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Kikio.1 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Kikio.1 is a new contributor. Be nice, and check out our Code of Conduct.













          Kikio.1 is a new contributor. Be nice, and check out our Code of Conduct.












          Kikio.1 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Data Science Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f44302%2fregression-loss-function-is-nan%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ponta tanko

          Tantalo (mitologio)

          Erzsébet Schaár