Getting NaN for loss HELP!

#getting_started #keras #help_request #models #datasets#education

I USE THE 1DCNN TO RUNNING MY OWN DATA BUT I GOT NAN FOR LOSS
I was wondering what is wrong with 2 input code below such that it is outputting NaN???

from keras.layers.core import Activation, Dense
from keras.models import Sequential
from keras.preprocessing import sequence
from sklearn.model_selection import train_test_split
from keras.models import load_model
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Dropout
from numpy import ndarray
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from tensorflow import keras
from keras.layers import Dense,Dropout,Flatten,Conv1D,MaxPooling1D
from tensorflow.keras.callbacks import TensorBoard
import tensorflow as tf

df = pd.read_excel(“demo.xlsx”)
df.shape
(1224, 11)

df_num = df.shape[0]
indexes = np.random.permutation(df_num)
train_indexes = indexes[:int(df_num *0.8)]
test_indexes = indexes[int(df_num *0.8):]
train_df = df.loc[train_indexes]
test_df= df.loc[test_indexes]

scaler = MinMaxScaler(feature_range=(0, 1))
df= scaler.fit_transform(df)

x_train = np.array(train_df.drop(‘Y’,axis=‘columns’))
y_train = np.array(train_df[‘Y’])
x_test = np.array(test_df.drop(‘Y’,axis=‘columns’))
y_test = np.array(test_df[‘Y’])

x_train = np.reshape(x_train, (x_train.shape[0],1,
x_train.shape[1]))
x_test = np.reshape(x_test, (x_test.shape[0],1, x_test.shape[1]))

model = Sequential()
model.add(Conv1D(filters=11,
kernel_size=(1),
padding=‘same’,
input_shape=(x_train.shape[1], x_train.shape[2]),
activation=‘relu’))
model.add(MaxPooling1D(pool_size=(1)))
model.add(Flatten())
model.add(Dense(1,activation=‘sigmoid’))
model.compile(loss=‘binary_crossentropy’,
optimizer=‘adam’,
metrics=[‘accuracy’])

print(model.summary())
Model: “sequential”


Layer (type) Output Shape Param #

conv1d (Conv1D) (None, 1, 11) 121


max_pooling1d (MaxPooling1D) (None, 1, 11) 0


flatten (Flatten) (None, 11) 0


dense (Dense) (None, 1) 12

Total params: 133
Trainable params: 133
Non-trainable params: 0


None

checkpoint_filepath = ‘.\tmp\checkpoint’
model_checkpoint_callback = [tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_filepath, save_weights_only=True)]
checkpoint_filepath = ‘.\tmp\checkpoint’

history = model.fit(x_train, y_train, epochs=200,
batch_size=3,
callbacks=model_checkpoint_callback)
Epoch 1/200
327/327 [==============================] - 0s 477us/step - loss: nan - accuracy: 0.6588
Epoch 2/200
327/327 [==============================] - 0s 483us/step - loss: nan - accuracy: 0.6609
Epoch 3/200
327/327 [==============================] - 0s 481us/step - loss: nan - accuracy: 0.6609
Epoch 4/200
327/327 [==============================] - 0s 476us/step - loss: nan - accuracy: 0.6609
Epoch 5/200
327/327 [==============================] - 0s 480us/step - loss: nan - accuracy: 0.6609
Epoch 6/200
327/327 [==============================] - 0s 477us/step - loss: nan - accuracy: 0.6609
Epoch 7/200
327/327 [==============================] - 0s 470us/step - loss: nan - accuracy: 0.6609
Epoch 8/200
327/327 [==============================] - 0s 477us/step - loss: nan - accuracy: 0.6609
Epoch 9/200
327/327 [==============================] - 0s 470us/step - loss: nan - accuracy: 0.6609
Epoch 10/200
327/327 [==============================] - 0s 474us/step - loss: nan - accuracy: 0.6609
Epoch 11/200
327/327 [==============================] - 0s 480us/step - loss: nan - accuracy: 0.6609
Epoch 12/200
327/327 [==============================] - 0s 480us/step - loss: nan - accuracy: 0.6609
Epoch 13/200
327/327 [==============================] - 0s 474us/step - loss: nan - accuracy: 0.6609
Epoch 14/200
327/327 [==============================] - 0s 473us/step - loss: nan - accuracy: 0.6609
Epoch 15/200
327/327 [==============================] - 0s 479us/step - loss: nan - accuracy: 0.6609
Epoch 16/200
327/327 [==============================] - 0s 477us/step - loss: nan - accuracy: 0.6609
Epoch 17/200
327/327 [==============================] - 0s 479us/step - loss: nan - accuracy: 0.6609
Epoch 18/200
327/327 [==============================] - 0s 477us/step - loss: nan - accuracy: 0.6609
Epoch 19/200
327/327 [==============================] - 0s 474us/step - loss: nan - accuracy: 0.6609
Epoch 20/200
327/327 [==============================] - 0s 474us/step - loss: nan - accuracy: 0.6609
Epoch 21/200
327/327 [==============================] - 0s 477us/step - loss: nan - accuracy: 0.6609
Epoch 22/200
327/327 [==============================] - 0s 474us/step - loss: nan - accuracy: 0.6609
Epoch 23/200
327/327 [==============================] - 0s 470us/step - loss: nan - accuracy: 0.6609
Epoch 24/200
327/327 [==============================] - 0s 474us/step - loss: nan - accuracy: 0.6609

#help_request #models #recommenders
i have used the tensorflow CNN to run my own data. I was wondering what is wrong with 2 input code below such that it is outputting NaN???

from keras.layers.core import Activation, Dense
from keras.models import Sequential
from keras.preprocessing import sequence
from sklearn.model_selection import train_test_split
from keras.models import load_model
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Dropout
from numpy import ndarray
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from tensorflow import keras
from keras.layers import Dense,Dropout,Flatten,Conv1D,MaxPooling1D
from tensorflow.keras.callbacks import TensorBoard
import tensorflow as tf

df = pd.read_excel(“demo.xlsx”)
df.shape
(1224,11)

df_num = df.shape[0]
indexes = np.random.permutation(df_num)
train_indexes = indexes[:int(df_num *0.8)]
test_indexes = indexes[int(df_num *0.8):]
train_df = df.loc[train_indexes]
test_df= df.loc[test_indexes]

scaler = MinMaxScaler(feature_range=(0, 1))
df= scaler.fit_transform(df)

x_train = np.array(train_df.drop(‘Y’,axis=‘columns’))
y_train = np.array(train_df[‘Y’])
x_test = np.array(test_df.drop(‘Y’,axis=‘columns’))
y_test = np.array(test_df[‘Y’])

x_train = np.reshape(x_train, (x_train.shape[0],1,
x_train.shape[1]))
x_test = np.reshape(x_test, (x_test.shape[0],1, x_test.shape[1]))

model = Sequential()
model.add(Conv1D(filters=11,
kernel_size=(1),
padding=‘same’,
input_shape=(x_train.shape[1], x_train.shape[2]),
activation=‘relu’))
model.add(MaxPooling1D(pool_size=(1)))
model.add(Flatten())
model.add(Dense(1,activation=‘sigmoid’))
model.compile(loss=‘binary_crossentropy’,
optimizer=‘adam’,
metrics=[‘accuracy’])
print(model.summary())
Model: “sequential”


Layer (type) Output Shape Param #

conv1d (Conv1D) (None, 1, 11) 121


max_pooling1d (MaxPooling1D) (None, 1, 11) 0


flatten (Flatten) (None, 11) 0


dense (Dense) (None, 1) 12

Total params: 133
Trainable params: 133
Non-trainable params: 0


None

checkpoint_filepath = ‘.\tmp\checkpoint’
model_checkpoint_callback = [tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_filepath, save_weights_only=True)]

history = model.fit(x_train, y_train, epochs=200,
batch_size=3,
callbacks=model_checkpoint_callback)
Epoch 1/200
327/327 [==============================] - 0s 477us/step - loss: nan - accuracy: 0.6588
Epoch 2/200
327/327 [==============================] - 0s 483us/step - loss: nan - accuracy: 0.6609
Epoch 3/200
327/327 [==============================] - 0s 481us/step - loss: nan - accuracy: 0.6609
Epoch 4/200
327/327 [==============================] - 0s 476us/step - loss: nan - accuracy: 0.6609
Epoch 5/200
327/327 [==============================] - 0s 480us/step - loss: nan - accuracy: 0.6609
Epoch 6/200
327/327 [==============================] - 0s 477us/step - loss: nan - accuracy: 0.6609
Epoch 7/200
327/327 [==============================] - 0s 470us/step - loss: nan - accuracy: 0.6609
Epoch 8/200
327/327 [==============================] - 0s 477us/step - loss: nan - accuracy: 0.6609
Epoch 9/200