Keras loss: 0.0000e+00 and accuracy stays constant - python

I have 101 folders from 0-100 containing synthetic training images.
This is my code:
dataset = tf.keras.utils.image_dataset_from_directory(
'Pictures/synthdataset5', labels='inferred', label_mode='int', class_names=None, color_mode='rgb', batch_size=32, image_size=(128,128), shuffle=True, seed=None, validation_split=None, subset=None,interpolation='bilinear', follow_links=False,crop_to_aspect_ratio=False
)
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
model = Sequential()
model.add(Conv2D(32, kernel_size=5, activation='relu', input_shape=(128,128,3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=5, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, kernel_size=3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, kernel_size=3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(dataset,epochs=75)
And I always get the same result for every epoch:
Epoch 1/75
469/469 [==============================] - 632s 1s/step - loss: 0.0000e+00 - accuracy: 0.0098
What's wrong???

So turns out your loss might be the problem after all.
If you use SparseCategoricalCrossentropy instead as loss it should work.
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
After this you should adjust the last layer to:
model.add(Dense(101, activation='softmax'))
Also don't forget to import import tensorflow as tf
Let me know if this solves the issue.

Related

Why is the value of my loss so huge? Running an MLP on Tensorflow

I am running a plain Sequential API MLP and encounter a huge loss in my output. I am not sure what is wrong and how to fix. I am using Batch Normalization layers as I read that would help and it actually does but my output is still ridiculous. Any other advice on the architecture is more than welcome.
RMSE loss is about 56000
from tensorflow.keras.models import Sequential
model = Sequential()
model.add(Dense(3, input_dim=X_train_enc.shape[1], activation='relu', kernel_initializer='he_normal'))
model.add(BatchNormalization())
model.add(Dense(10, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(20, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(30, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(20, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(10, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(3, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(1, activation = 'linear'))
# # summarize layers
print(model.summary())
from tensorflow.keras import backend
from tensorflow.keras.losses import mean_squared_error
#Creating/Defining our own metrics
def mean_absolute_percentage_error(y_true, y_pred):
diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),
K.epsilon(),
None))
return 100. * K.mean(diff, axis=-1)
tf.keras.backend.set_epsilon(1) #https://stackoverflow.com/questions/49729522/why-is-the-mean-average-percentage-errormape-extremely-high
def rmse(y_true, y_pred):
return backend.sqrt(mean_squared_error(y_true, y_pred))
#def rmse(y_true, y_pred):
# return backend.sqrt(backend.mean(backend.square(y_pred - y_true), axis=-1))
Adamax = tf.keras.optimizers.Adamax(lr=0.02, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
#Compiling model
model.compile(optimizer='adam',loss=rmse, metrics=['accuracy',rmse, 'mae', 'mape'])#Metric functions are similar to loss functions, except that the results from evaluating a metric are not used when training the model
# # Creating a checkpoint
filepath="weights-improvement-{epoch:02d}-{val_rmse:.9f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_rmse', verbose=1, save_best_only=True, mode='min')
# # monitor = "val_loss", save_best_only=False
callbacks_list = [checkpoint]
history = model.fit(X_train_enc, y_train, epochs=200, batch_size=16, validation_split=0.2681867, callbacks=callbacks_list, verbose=1)

Issue Tensorflow 2.2 with model.fit and model.evaluate

When I whant to train my model in tf it seems like tf don't get right values (cf screen).
I expect to have 21759 and not 680
It's appening since I changed of OS (fedora 30 xfce -> fedora 32 gnome) and on others laptops there is not this issue.
I am using Tf 2.2.
My dataset is made by somes csv created by tshark: A screen of my DS
Here is few lines of my code:
My model:
model = Sequential()
model.add(LSTM(9, input_shape=dataset[0].shape, activation='relu', return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(9, input_shape=dataset[0].shape, activation='relu', return_sequences=True))
model.add(Dropout(0.3))
model.add(Dense(9, activation='relu'))
model.add(Flatten())
model.add(Dense(2, activation='softmax'))
opt = tf.keras.optimizers.Adam(lr=1e-4, decay=1e-5)
model.compile(loss='sparse_categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
Do you have any ideas ?
PS: It happens too with this .PY
import tensorflow as tf
dataset = [[1, 1],[2, 2]] * 50
label = [0, 1] * 50
print(len(dataset))
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, activation="relu", input_shape=(2,)),
tf.keras.layers.Dense(2, activation="softmax")
])
model.compile(
loss="sparse_categorical_crossentropy",
optimizer="sgd",
metrics=["accuracy"]
)
history = model.fit(dataset, label, epochs=1)
Ouput:
100
4/4 [==============================] - 0s 611us/step - loss: 0.6578 - accuracy: 0.5000
Like Koralp Catalsakal said it was just an "configuration difference" issue.
So I just had to set up manually the batch_size.

How do I reconcile predictions from keras flow_from_dataframe to actual labels?

I've been researching this issue and while there seems to be references to it, I wasn't able to to get it to work.
I'm using keras flow_from_dataframe. My file names and labels are stored in two dataframes, one for train (df) and one for validation (dfv).
As I train my model the validation accuracy is at .75. I can verify that looking at model.history. Even model.evaluate_generator will confirm the exact same values.
But when I do a model.predict_generator using the exact same validation dataset provided to .fit and model.evaluate_generator, I get essentially randomness (.5), which tells me that either something with the order of observation is going on, or that there might be parts of the pipeline that are not carried on to the evaluation part (like rescaling).
Here's my code:
datagen=ImageDataGenerator(rescale=1./255.)
train_generator=datagen.flow_from_dataframe(
dataframe=df,
directory=path+'\\img.tar\\img',
x_col="loc",
y_col="target",
weight_col=None,
target_size=(32, 32),
color_mode="rgb",
classes=None,
class_mode="binary",
batch_size=100,
shuffle=True,
seed=SEED,
save_to_dir=None,
save_prefix="",
save_format="png",
subset=None,
interpolation="nearest",
validate_filenames=True
)
valid_generator=datagen.flow_from_dataframe(
dataframe=dfv,
directory=path+'\\img.tar\\img',
x_col="loc",
y_col="target",
weight_col=None,
target_size=(32, 32),
color_mode="rgb",
classes=None,
class_mode="binary",
batch_size=100,
shuffle=False,
seed=SEED,
save_to_dir=None,
save_prefix="",
save_format="png",
subset=None,
interpolation="nearest",
validate_filenames=True
)
#Found 8500 validated image filenames belonging to 2 classes.
#Found 500 validated image filenames belonging to 2 classes.
from keras.models import Sequential
#Import from keras_preprocessing not from keras.preprocessing
from keras_preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D
from keras import regularizers, optimizers
import pandas as pd
import numpy as np
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=(32,32,3)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam',metrics=["accuracy"])
STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size
#STEP_SIZE_TEST=test_generator.n//test_generator.batch_size
hx = model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=1
)
#Epoch 1/1
#85/85 [==============================] - 74s 871ms/step - loss: 0.6584 - accuracy: 0.6398 - val_loss: 0.6059 - val_accuracy: 0.7500
hx.history
#{'val_loss': [0.6059464812278748],
#'val_accuracy': [0.75],
#'loss': [0.6583675097016727],
#'accuracy': [0.6397647]}
model.evaluate_generator(generator=valid_generator,
steps=STEP_SIZE_VALID)
#[0.6059464812278748, 0.75]
pred=model.predict_generator(valid_generator,verbose=1,steps=STEP_SIZE_VALID)
roc_auc_score(dfv.label.values,pred.ravel())
#0.51
print(roc_auc_score(dfv.label.values,pred.ravel()>df['label'].mean()))
print(roc_auc_score(dfv.label.values,pred.ravel()>0.5))
#0.5
#0.5

Why the acc of DNN is less than 1%

Im a new in deeplearning. I have a X with 34 dimension, which are some stock technical indicator data. And Y is label, which is binary(1,-1) represent the stock is uptrend or downtrend.Here is my code.
import pandas as pd
import numpy as np
data = pd.read_csv('data/data_week.csv')
data.dropna(inplace=True)
x = data.loc[:, 'bbands_upperband':'turn_std_5']
y = data['label']
from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential()
model.add(Dense(512, activation='relu', input_dim=34))
model.add(Dense(200, activation='relu'))
model.add(Dense(200, activation='relu'))
model.add(Dense(200, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='relu'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(x, y, epochs=2, batch_size=200)
231200/1041021 [=====>........................] - ETA: 59s - loss: 0.7098 - acc: 0.0086
232000/1041021 [=====>........................] - ETA: 59s - loss: 0.7087 - acc: 0.0086
However, the accuracy is less than 1%. I think this must has something wrong.
If you know please tell me and thank you very much!
For a binary classification model you should use sigmoid activation function in your last dense layer
model.add(Dense(1, activation='sigmoid'))
also, your classes must be (0,1) not (-1,1)

Convolutional Neural Network with Non-Existent Loss and Accuracy of 0

I am attempting to train a simple convolutional neural network shown below.
model= Sequential()
model.add(Conv1D(32, 3, padding='same', input_shape=(700,7))
model.add(Activation('relu'))
model.add(Conv1D(32,3))
model.add(Activation('relu'))
model.add(MaxPooling1D())
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
I fit it using 100 epochs and a validation training split 0.2 on input data shaped [1000L, 700L, 7L]. Every single one of my epochs displaed the following:
loss: nan - acc:0.0000e+00 - val_loss: nan - val_acc: 0.0000e+00
So my question is, what went wrong and how do I fix it? Is the problem with the network or how my data is being inputed and fitted to the model?

Categories

Resources