title: Keras Regression model loss: nan. How to fix it?
body: <p>I followed the code in the book 'hands-on machine learning with scikit-learn and tensorflow' to build a multiple outputs neural network in Keras. However, I keep getting a loss: nan output. How to fix this?</p>
  <pre><code>from sklearn.datasets import fetch_california_housing
  
  housing = fetch_cawwwlifornia_housing()
  
  X_train_full, X_test, y_train_full, y_test = train_test_split(
      housing.data, housing.target)
  
  X_train, X_valid, y_train, y_valid = train_test_split(
      X_train_full, y_train_full)
  
  scaler = StandardScaler()
  X_train_scaled = scaler.fit_transform(X_train)
  X_valid_scaled = scaler.transform(X_valid)
  X_test_scaled = scaler.transform(X_test)
  
  X_train_A, X_train_B = X_train[:, :5], X_train[:, 2:]
  X_valid_A, X_valid_B = X_valid[:, :5], X_valid[:, 2:]
  X_test_A, X_test_B = X_test[:, :5], X_test[:, 2:]
  X_new_A, X_new_B = X_test_A[:3], X_test_B[:3]
  
  input_A = keras.layers.Input(shape=[5], name=&quot;wide_input&quot;)
  input_B = keras.layers.Input(shape=[6], name=&quot;deep_input&quot;)
  hidden1 = keras.layers.Dense(30, activation=&quot;relu&quot;)(input_B)
  hidden2 = keras.layers.Dense(30, activation=&quot;relu&quot;)(hidden1)
  concat = keras.layers.concatenate([input_A, hidden2])
  output = keras.layers.Dense(1, name=&quot;main_output&quot;)(concat)
  aux_output = keras.layers.Dense(1, name=&quot;aux_output&quot;)(hidden2)
  model = keras.models.Model(inputs=[input_A, input_B],
                             outputs=[output, aux_output])
  model.compile(loss=[&quot;mse&quot;, &quot;mse&quot;], loss_weights=[0.9, 0.1], optimizer=&quot;sgd&quot;)
  
  history = model.fit(
      [X_train_A, X_train_B], [y_train, y_train], epochs=20,
      validation_data=([X_valid_A, X_valid_B], [y_valid, y_valid]))
  </code></pre>
  <p><strong>output</strong></p>
  <pre><code>Train on 11610 samples, validate on 3870 samples
  Epoch 1/20
  11610/11610 [==============================] - 6s 525us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 2/20
  11610/11610 [==============================] - 4s 336us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 3/20
  11610/11610 [==============================] - 5s 428us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 4/20
  11610/11610 [==============================] - 5s 424us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 5/20
  11610/11610 [==============================] - 5s 414us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 6/20
  11610/11610 [==============================] - 5s 400us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 7/20
  11610/11610 [==============================] - 5s 392us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 8/20
  11610/11610 [==============================] - 5s 405us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 9/20
  11610/11610 [==============================] - 4s 369us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 10/20
  11610/11610 [==============================] - 5s 405us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 11/20
  11610/11610 [==============================] - 5s 423us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 12/20
  11610/11610 [==============================] - 5s 454us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 13/20
  11610/11610 [==============================] - 4s 380us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 14/20
  11610/11610 [==============================] - 5s 446us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 15/20
  11610/11610 [==============================] - 5s 411us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 16/20
  11610/11610 [==============================] - 5s 457us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 17/20
  11610/11610 [==============================] - 5s 415us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 18/20
  11610/11610 [==============================] - 5s 411us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 19/20
  11610/11610 [==============================] - 5s 388us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  Epoch 20/20
  11610/11610 [==============================] - 4s 363us/sample - loss: nan - main_output_loss: nan - aux_output_loss: nan - val_loss: nan - val_main_output_loss: nan - val_aux_output_loss: nan
  </code></pre>