keras - loss: nan and accuracy is 0.000 - keras

dataset = data.values
dataset[:, [0,2,3,4,5,6,7,8,9]]
Y = dataset[:, 1]
X_train, X_test, y_train, y_test = train_test_split(X, Y, stratify=Y, random_state=0)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit(X_train).transform(X_train)
X_test_scaled = scaler.fit(X_test).transform(X_test)
model = Sequential()
model.add(Dense(50, input_dim = 9, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error', optimizer='Adam', metrics=['accuracy'])
model.summary()
model.fit(X_train_scaled, y_train, epochs=50)
print('\n accuracy : {:.4f}'.format(model.evaluate(X_test_scaled, y_test)[1]))
enter image description here
i think the problem is Scaler.. but i don't know what i do...
why the loss function has nan output, and why the accuracy is 0?

Related

How do I display metrics after fitting a Keras model

I am learning about neural networks with Kaggle tutorials. I have made a neural net to predict concrete strength and I want to display the MSE (for starters) metric after fitting the model. I have failed both with print(metrics) and plotting the metrics (displays an empty graph).
df = concrete.copy()
df_train = df.sample(frac=0.7, random_state=0)
df_valid = df.drop(df_train.index)
X_train = df_train.drop('CompressiveStrength', axis=1)
X_valid = df_valid.drop('CompressiveStrength', axis=1)
y_train = df_train['CompressiveStrength']
y_valid = df_valid['CompressiveStrength']
model = keras.Sequential([
layers.BatchNormalization(),
layers.Dense(512, activation='relu', input_shape=input_shape),
layers.BatchNormalization(),
layers.Dense(512, activation='relu'),
layers.Dropout(rate=0.3), # apply 30% dropout to the next layer
layers.Dense(512, activation='relu'),
layers.BatchNormalization(),
layers.Dense(512, activation='relu'),
layers.BatchNormalization(),
layers.Dense(1),
])
model.compile(
optimizer='sgd', # SGD is more sensitive to differences of scale
loss='mse',
metrics=[tf.keras.metrics.MeanSquaredError()]
)
history = model.fit(
X_train, y_train,
validation_data=(X_valid, y_valid),
batch_size=64,
epochs=100,
verbose=0,
callbacks=[early_stopping],
)
print(history)
pyplot.plot(history.history['mean_squared_error'])

CNN for numeric data

I wanted to use CNN for the classification of my dataset which is numerical dataset. My dataset is 3200x36 size. Whenever I used the following code and passed my data, I did not get any result. For the accuracy, it just runs but do not output anything. What did I do wrong, Please explain.
x_train, x_test, y_train, y_test = train_test_split(feature, target, train_size=0.75)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=3, activation='relu'))
model.add(MaxPooling2D(pool_size=4))
model.add(Dense(3, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=100, batch_size=8149, verbose=1)
accuracy = model.evaluate(x_test, y_test)
print( model.evaluate(x_test, y_test))

InvalidArgumentError: indices[13,9] = 197491 is not in [0, 189722) [[node sequential_7

MODEL CNN
create a list of the target columns
target_cols = [y_toxic,y_severe_toxic,y_obscene,y_threat,y_insult,y_identity_hate]
preds = []
for col in target_cols:
print('\n')
# set the value of y
y = col
# create a stratified split
X_train, X_eval, y_train ,y_eval = train_test_split(X, y,test_size=0.25,shuffle=True,
random_state=5,stratify=y)
# cnn model
model = Sequential()
e = Embedding(189722, 100, weights=[embedding_matrix],
input_length=500, trainable=False)
model.add(e)
model.add(Conv1D(128, 3, activation='relu'))
model.add(MaxPooling1D(3))
model.add(Dropout(0.2))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(3))
model.add(Dropout(0.2))
model.add(Conv1D(64, 3, activation='relu'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
# compile the model
Adam_opt = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(optimizer=Adam_opt, loss='binary_crossentropy', metrics=['acc'])
early_stopping = EarlyStopping(monitor='val_loss', patience=5, mode='min')
save_best = ModelCheckpoint('toxic.hdf', save_best_only=True,
monitor='val_loss', mode='min')
history = model.fit(X_train, y_train, validation_data=(X_eval, y_eval),
epochs=100, verbose=1,callbacks=[early_stopping,save_best])
# make a prediction on y (target column)
model.load_weights(filepath = 'toxic.hdf')
predictions = model.predict(X_test)
y_preds = predictions[:,0]
# append the prediction to a python list
preds.append(y_preds)
let me know this why it getting alike.... I tried ot reshape it still the same error

Error when checking input: expected conv1d_11_input to have shape (6700, 1) but got array with shape (1, 1)

Iam using 1dCNN for time series data but following error occur in model.fit line.
The error is as follows :
Error when checking input: expected conv1d_11_input to have shape (6700, 1)
but got array with shape (1, 1).
any one plz help
code portion is as below
dataframe = pd.read_excel("file path", header=None,delim_whitespace=True)
dataset = dataframe.values
X=dataframe.values[:,0]
Y=dataframe.values[:,2]
X = np.expand_dims(X, axis=1)
Y = np.expand_dims(Y, axis=1)
(X_train, X_test, Y_train, Y_test) = train_test_split(X, Y, test_size=0.33, random_state=seed)
X_train = np.reshape(X_train, (-1, X_train.shape[1],1))
Y_train = np.reshape(Y_train, (Y_train.shape[0], 1, Y_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
print(X_train.shape)
print(Y_train.shape)
n_timesteps, n_features, n_outputs = X_train.shape[0], X_train.shape[1], Y_train.shape[1]
verbose, epochs, batch_size = 0, 100, 32
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,1)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=verbose)'
Use:
n_timesteps, n_outputs = X_train.shape[1], Y_train.shape[1]

Matrix size-incompatible problem in Keras

Recently I just started learning to implement neural network using Keras, and I tried to implement Le-Net5 NN for MNIST problem based on the structure listed as below.
Code:
# Load the data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Parameter set up
input_shape = (32,32,1)
batch_size = 128
# Format the image info
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
x_train = np.pad(x_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')
x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant')
# Encode label
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Construct the model
model = keras.Sequential()
conv_stride = 1
pooling_stride = 2
model.add(layers.Conv2D(filters=6, kernel_size=[5,5], input_shape=input_shape, padding="valid", strides=[conv_stride,conv_stride], activation='tanh'))
model.add(layers.AveragePooling2D(pool_size=[2,2], padding="valid", strides=pooling_stride))
model.add(layers.Conv2D(filters=16, kernel_size=[5,5], padding="valid", strides=[conv_stride,conv_stride],activation='tanh'))
model.add(layers.AveragePooling2D(pool_size=[2,2], padding="valid", strides=pooling_stride))
model.add(layers.Conv2D(filters=120, kernel_size=[5,5], padding="valid", strides=[conv_stride,conv_stride], activation='tanh'))
model.add(layers.Flatten())
model.add(layers.Dense(84, activation='tanh'))
model.add(layers.Dense(10, activation=tf.nn.softmax))
print(model.summary())
model.compile(optimizer="sgd", loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=batch_size, verbose=1, epochs=12)
score = model.evaluate(x_test, y_test, verbose=1)
print("Test Accuracy: ", score[1])
However, there's an error when I run the program, the error message is:
Matrix size-incompatible: In[0]: [128,1536], In[1]: [1176,200]
[[{{node dense/MatMul}} = MatMul[T=DT_FLOAT, _class=["loc:#training/Adam/gradients/dense/MatMul_grad/MatMul"], transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](flatten/Reshape, dense/MatMul/ReadVariableOp)]]
I have checked the structure using model.summary() and it seems like the neural network's structure is correct. Can anybody tell me which part leads to the error?

Resources