I'm trying to create a model for time series analysis using LSTM layer, however accuracy is very low even when using Dense layers and no LSTM.
The data is time series (synthetic spectrum), which depends on 4 parameters. Changing the parameters enables to use different size datasets, where each sample is more or less different from the other. But no matter the size of dataset accuracy is always as low as 0.0 - 0.32 %.
Model with LSTM:
print(trainset.shape)
print(testset.shape)
print(trainlabels.shape)
model = Sequential()
model.add(Masking(mask_value=0.0, input_shape=(trainset.shape[1], trainset.shape[2])))
model.add(LSTM(10, activation='relu', stateful=False, return_sequences=False))
model.add(Dropout(0.3))
model.add(Dense(len(trainlabels), activation='relu'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='Adam', metrics=['accuracy'])
print(model.summary())
model.fit(trainset, trainlabels, validation_data=(testset, testlabels),
epochs=3, batch_size=10)
scores = model.evaluate(testset, testlabels, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
OUTPUT:
(2478, 600, 1)
(620, 600, 1)
(2478,)
Model: "sequential_7"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
masking_7 (Masking) (None, 600, 1) 0
_________________________________________________________________
lstm_7 (LSTM) (None, 10) 480
_________________________________________________________________
dropout_7 (Dropout) (None, 10) 0
_________________________________________________________________
dense_7 (Dense) (None, 2478) 27258
=================================================================
Total params: 27,738
Trainable params: 27,738
Non-trainable params: 0
_________________________________________________________________
None
Train on 2478 samples, validate on 620 samples
Epoch 1/3
2478/2478 [==============================] - 53s 22ms/step - loss: 8.9022 - accuracy: 4.0355e-04 - val_loss: 7.8152 - val_accuracy: 0.0016
Epoch 2/3
2478/2478 [==============================] - 54s 22ms/step - loss: 7.8152 - accuracy: 4.0355e-04 - val_loss: 7.8152 - val_accuracy: 0.0016
Epoch 3/3
2478/2478 [==============================] - 53s 21ms/step - loss: 7.8152 - accuracy: 4.0355e-04 - val_loss: 7.8152 - val_accuracy: 0.0016
Accuracy: 0.16%
Some values in training data are 0.0, therefore Masking is used.
I have tried playing around with different loss, optimizer, activations, Dropout and layer parameters. The result is always the same even after adding more Dense layers, or changing batch size.
Model with Dense:
Data format is 2D instead of 3D.
model=keras.Sequential([
keras.layers.Masking(mask_value=0.0, input_shape=(trainset.shape[1],)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dropout(0.05),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dropout(0.05),
keras.layers.Dense(len(labels), activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(np.uint8(trainset), np.uint8(trainlabels), epochs=100)
test_loss, test_acc=model.evaluate(np.uint8(testset), np.uint8(testlabels),
verbose=2)
print(test_acc)
OUTPUT:
Train on 1239 samples
Epoch 1/100
1239/1239 [==============================] - 1s 1ms/sample - loss: 8.5421 - accuracy: 0.0033
Epoch 2/100
1239/1239 [==============================] - 0s 371us/sample - loss: 6.2039 - accuracy: 0.0025
Epoch 3/100
1239/1239 [==============================] - 0s 347us/sample - loss: 5.6502 - accuracy: 0.0033
****
Epoch 97/100
1239/1239 [==============================] - 0s 380us/sample - loss: 0.1472 - accuracy: 0.9746
Epoch 98/100
1239/1239 [==============================] - 0s 364us/sample - loss: 0.1562 - accuracy: 0.9680
Epoch 99/100
1239/1239 [==============================] - 1s 408us/sample - loss: 0.1511 - accuracy: 0.9721
Epoch 100/100
1239/1239 [==============================] - 0s 378us/sample - loss: 0.1719 - accuracy: 0.9680
310/1 - 0s - loss: 18.6845 - accuracy: 0.0000e+00
0.0
With this model loss is very low but so is accuracy.
What kind of model architecture should be used for my data?
Thanks in advance for helping to learn this stuff!
Related
I'm trying to train a neural network for classification. The target labels are -1 or 1.
The neural network is as follows:
def build_nn(window,n_features,lr = 0.001):
_input = Input(shape = (window,n_features),name = 'input')
x1 = LSTM(100, input_shape = (window,n_features), return_sequences = True,activation = 'relu')(_input)
x2 = Dropout(0.5)(x1)
x3 = LSTM(50 , return_sequences = False, activation = 'relu')(x2)
x4 = Dropout(0.5)(x3)
x5 = Dense(25,kernel_initializer = 'uniform', activation = 'relu')(x4)
x6 = Dense(10,kernel_initializer = 'uniform', activation = 'relu')(x5)
nn = Model(inputs = [_input],outputs = [x6])
nn.compile(loss='binary_crossentropy',optimizer=Adam(lr = lr),metrics=['accuracy'])
return nn
The inputs are windowed (generally the actual value plus 1-2 past values) and when trying to fit the model, I get that the accuracy and val_accuracy are both 0 for almost like 100 epochs. Sometimes I see a little variation in the accuracy (sometimes even in the val_accuracy) but it goes back to 0.
I'm also using EarlyStopping and ModelCheckpoint (I know that setting patience to 500 will make no difference because the number of epochs are also 500):
early_stopping = EarlyStopping(patience = 500,verbose = True,monitor='val_accuracy')
check_point = ModelCheckpoint('lstm_classifier.hdf5',verbose=1,save_best_only=True,monitor = 'val_accuracy')
This is the model summary:
Model: "model_18"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input (InputLayer) [(None, 2, 17)] 0
_________________________________________________________________
lstm_38 (LSTM) (None, 2, 100) 47200
_________________________________________________________________
dropout_38 (Dropout) (None, 2, 100) 0
_________________________________________________________________
lstm_39 (LSTM) (None, 50) 30200
_________________________________________________________________
dropout_39 (Dropout) (None, 50) 0
_________________________________________________________________
dense_36 (Dense) (None, 25) 1275
_________________________________________________________________
dense_37 (Dense) (None, 10) 260
=================================================================
Total params: 78,935
Trainable params: 78,935
Non-trainable params: 0
_________________________________________________________________
And this is a sample of the output when trying to fit the model.
Epoch 1/500
148/148 [==============================] - 10s 32ms/step - loss: 0.5422 - accuracy: 0.0000e+00 - val_loss: 0.2783 - val_accuracy: 0.0000e+00
Epoch 00001: val_accuracy improved from -inf to 0.00000, saving model to lstm_classifier.hdf5
Epoch 2/500
148/148 [==============================] - 4s 29ms/step - loss: 0.4661 - accuracy: 0.0000e+00 - val_loss: 0.2778 - val_accuracy: 0.0000e+00
Epoch 00002: val_accuracy did not improve from 0.00000
Epoch 3/500
148/148 [==============================] - 4s 24ms/step - loss: 0.4656 - accuracy: 0.0000e+00 - val_loss: 0.2772 - val_accuracy: 0.0000e+00
Epoch 00003: val_accuracy did not improve from 0.00000
Epoch 4/500
148/148 [==============================] - 4s 26ms/step - loss: 0.4631 - accuracy: 0.0000e+00 - val_loss: 0.2766 - val_accuracy: 0.0000e+00
Epoch 00004: val_accuracy did not improve from 0.00000
Epoch 5/500
148/148 [==============================] - 4s 29ms/step - loss: 0.4628 - accuracy: 0.0000e+00 - val_loss: 0.2763 - val_accuracy: 0.0000e+00
Epoch 00005: val_accuracy did not improve from 0.00000
Epoch 6/500
148/148 [==============================] - 5s 31ms/step - loss: 0.4608 - accuracy: 0.0000e+00 - val_loss: 0.2759 - val_accuracy: 0.0000e+00
Epoch 00006: val_accuracy did not improve from 0.00000
Epoch 7/500
148/148 [==============================] - 4s 25ms/step - loss: 0.4622 - accuracy: 0.0000e+00 - val_loss: 0.2754 - val_accuracy: 0.0000e+00
Epoch 00007: val_accuracy did not improve from 0.00000
Epoch 8/500
148/148 [==============================] - 4s 30ms/step - loss: 0.4583 - accuracy: 0.0000e+00 - val_loss: 0.2749 - val_accuracy: 0.0000e+00
Epoch 00008: val_accuracy did not improve from 0.00000
Epoch 9/500
148/148 [==============================] - 4s 30ms/step - loss: 0.4596 - accuracy: 0.0000e+00 - val_loss: 0.2748 - val_accuracy: 0.0000e+00
Epoch 00009: val_accuracy did not improve from 0.00000
Epoch 10/500
148/148 [==============================] - 4s 26ms/step - loss: 0.4570 - accuracy: 0.0000e+00 - val_loss: 0.2740 - val_accuracy: 0.0000e+00
Epoch 00010: val_accuracy did not improve from 0.00000
Epoch 11/500
148/148 [==============================] - 4s 27ms/step - loss: 0.4569 - accuracy: 0.0000e+00 - val_loss: 0.2921 - val_accuracy: 0.0000e+00
As you can see, the accuracy and val_accuracy are both stuck at 0, despite the fact that the loss and val_loss are changing. Generally, the accuracy and val_accuracy stay both at 0. Sometimes the model finds a better val_accuracy, but then it goes back to 0.
Epoch 00274: val_accuracy did not improve from 0.46822
Epoch 275/500
148/148 [==============================] - 4s 27ms/step - loss: 0.5844 - accuracy: 0.0000e+00 - val_loss: 0.3316 - val_accuracy: 0.0000e+00
And also noticed that the loss and val_loss tends to go bigger.
My question is: why I'm seeing no changes (o very small and difficult changes) in the accuracy and val_accuracy and what can I do to correct/improve this? Is something wrong with the model? I'm also scaling the features using MinMaxScaler(feature_range=(0,1))
This is my code for this program. it is working correctly but suddenly not work please anyone can solve this problem
model = Sequential()
print(nb_filters[0], 'filters')
print('input shape', img_rows, 'rows', img_cols, 'cols', patch_size, 'patchsize')
model.add(Convolution3D(
nb_filters[0],
kernel_dim1=1, # depth
kernel_dim2=nb_conv[0], # rows
kernel_dim3=nb_conv[1], # cols
input_shape=(1, img_rows, img_cols, patch_size),
activation='relu'
))
model.add(MaxPooling3D(pool_size=(1, nb_pool[0], nb_pool[0])))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, init='normal', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(nb_classes,init='normal'))
model.add(Activation('softmax'))
#optimizer adam,sgd,RMSprop,Adagrad,Adadelta,Nadam,
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
This error creates in my program. what is the problem I don't understand to solve this I search many times but not solve this problem?
--------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-112-671e85975992> in <module>
13 x = Dense(nb_classes, activation='softmax')(x)
14
---> 15 custom_model = Model(input=resnet_model.input, output=x)
16
17 for layer in custom_model.layers[:7]:
/usr/local/lib/python3.8/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
--> 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/engine/training.py in __init__(self, *args, **kwargs)
259 # self.trainable_weights
260 # self.non_trainable_weights
--> 261 generic_utils.validate_kwargs(kwargs, {'trainable', 'dtype', 'dynamic',
262 'name', 'autocast'})
263 super(Model, self).__init__(**kwargs)
/usr/local/lib/python3.8/dist-packages/tensorflow/python/keras/utils/generic_utils.py in validate_kwargs(kwargs, allowed_kwargs, error_message)
776 for kwarg in kwargs:
777 if kwarg not in allowed_kwargs:
--> 778 raise TypeError(error_message, kwarg)
779
780
TypeError: ('Keyword argument not understood:', 'input')
As suggested by Dr. Snoopy, the arguments to the tf.keras.Model are inputs and outputs but you are passing it as input and output respectively in custom_model = Model(input=resnet_model.input, output=x).
Code to reproduce the error -
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
X1 = tf.constant([2, 3, 4, 5, 6, 7])
X2 = tf.constant([2, 3, 4, 5, 6, 7])
yTrain = tf.constant([4, 6, 8, 10, 12, 14])
input1 = keras.Input(shape=(1,))
input2 = keras.Input(shape=(1,))
x = layers.concatenate([input1, input2])
x = layers.Dense(8, activation='relu')(x)
outputs = layers.Dense(2)(x)
mlp = keras.Model(input = [input1, input2], output = outputs)
mlp.summary()
mlp.compile(loss='mean_squared_error',
optimizer='adam', metrics=['accuracy'])
mlp.fit([X1, X2], yTrain, batch_size=1, epochs=10, validation_split=0.2,
shuffle=True)
mlp.evaluate([X1, X2], yTrain)
Output -
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-bec9ebbd1faf> in <module>()
14 x = layers.Dense(8, activation='relu')(x)
15 outputs = layers.Dense(2)(x)
---> 16 mlp = keras.Model(input = [input1, input2], output = outputs)
17
18 mlp.summary()
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py in validate_kwargs(kwargs, allowed_kwargs, error_message)
776 for kwarg in kwargs:
777 if kwarg not in allowed_kwargs:
--> 778 raise TypeError(error_message, kwarg)
779
780
TypeError: ('Keyword argument not understood:', 'input')
To fix the error, change the arguments as inputs and outputs.
Fixed Code -
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
X1 = tf.constant([2, 3, 4, 5, 6, 7])
X2 = tf.constant([2, 3, 4, 5, 6, 7])
yTrain = tf.constant([4, 6, 8, 10, 12, 14])
input1 = keras.Input(shape=(1,))
input2 = keras.Input(shape=(1,))
x = layers.concatenate([input1, input2])
x = layers.Dense(8, activation='relu')(x)
outputs = layers.Dense(2)(x)
mlp = keras.Model(inputs = [input1, input2], outputs = outputs)
mlp.summary()
mlp.compile(loss='mean_squared_error',
optimizer='adam', metrics=['accuracy'])
mlp.fit([X1, X2], yTrain, batch_size=1, epochs=10, validation_split=0.2,
shuffle=True)
mlp.evaluate([X1, X2], yTrain)
Output -
Model: "functional_5"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_6 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
input_7 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
concatenate_34 (Concatenate) (None, 2) 0 input_6[0][0]
input_7[0][0]
__________________________________________________________________________________________________
dense_4 (Dense) (None, 8) 24 concatenate_34[0][0]
__________________________________________________________________________________________________
dense_5 (Dense) (None, 2) 18 dense_4[0][0]
==================================================================================================
Total params: 42
Trainable params: 42
Non-trainable params: 0
__________________________________________________________________________________________________
Epoch 1/10
4/4 [==============================] - 0s 32ms/step - loss: 54.3236 - accuracy: 0.0000e+00 - val_loss: 169.3114 - val_accuracy: 0.0000e+00
Epoch 2/10
4/4 [==============================] - 0s 6ms/step - loss: 53.4965 - accuracy: 0.0000e+00 - val_loss: 167.0008 - val_accuracy: 0.0000e+00
Epoch 3/10
4/4 [==============================] - 0s 6ms/step - loss: 52.7413 - accuracy: 0.0000e+00 - val_loss: 164.6473 - val_accuracy: 0.0000e+00
Epoch 4/10
4/4 [==============================] - 0s 6ms/step - loss: 51.8159 - accuracy: 0.0000e+00 - val_loss: 162.4427 - val_accuracy: 0.0000e+00
Epoch 5/10
4/4 [==============================] - 0s 6ms/step - loss: 51.0917 - accuracy: 0.0000e+00 - val_loss: 160.1798 - val_accuracy: 0.0000e+00
Epoch 6/10
4/4 [==============================] - 0s 6ms/step - loss: 50.4425 - accuracy: 0.0000e+00 - val_loss: 157.8355 - val_accuracy: 0.0000e+00
Epoch 7/10
4/4 [==============================] - 0s 6ms/step - loss: 49.5709 - accuracy: 0.0000e+00 - val_loss: 155.6147 - val_accuracy: 0.0000e+00
Epoch 8/10
4/4 [==============================] - 0s 6ms/step - loss: 48.7816 - accuracy: 0.0000e+00 - val_loss: 153.4298 - val_accuracy: 0.0000e+00
Epoch 9/10
4/4 [==============================] - 0s 6ms/step - loss: 47.9975 - accuracy: 0.0000e+00 - val_loss: 151.2858 - val_accuracy: 0.0000e+00
Epoch 10/10
4/4 [==============================] - 0s 6ms/step - loss: 47.3943 - accuracy: 0.0000e+00 - val_loss: 149.0254 - val_accuracy: 0.0000e+00
1/1 [==============================] - 0s 2ms/step - loss: 80.9333 - accuracy: 0.0000e+00
[80.93333435058594, 0.0]
I am trying to train on gray images. The batch_size = 32, image size = (48*48).
I define my network input_shape = (48,48,1). I get an error like below when I train the network.
Error :
ValueError: Error when checking input: expected conv2d_17_input to have 4 dimensions, but got array with shape (32, 48, 48)
model.add(Conv2D(32, kernel_size=(5, 5),
activation='relu',
input_shape=(48,48,1)
)
)
Let's say you have 1000 training images where each image is 48x48 greyscale. After you have loaded the images into a numpy array, you will end up with the shape : (1000, 48, 48).
This essentially means you have 1000 elements in your array and each element is a 48x48 matrix.
Now in order to feed this data to train a CNN, you have to reshape this list to (1000, 48, 48, 1) where 1 stands for channel dimension. Since you are having greyscaled images you have to use 1. If it was RGB it will be 3.
Consider the toy example given below,
x_train = np.random.rand(1000, 48, 48) #images
y_train = np.array([np.random.randint(0, 2) for x in range(1000)]) # labels
# simple model
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5),
activation='relu',
input_shape=(48,48,1)
)
)
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
# fitting model
model.fit(x_train, y_train, epochs=10, batch_size=32)
This will throw an error,
Error when checking input: expected conv2d_3_input to have 4 dimensions, but got array with shape (1000, 48, 48)
To fix it reshape the x_train like this,
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)
Now fit the model,
model.fit(x_train, y_train, epochs=10, batch_size=32)
Epoch 1/10
1000/1000 [==============================] - 1s 1ms/step - loss: 0.7177
Epoch 2/10
1000/1000 [==============================] - 1s 882us/step - loss: 0.6762
Epoch 3/10
1000/1000 [==============================] - 1s 870us/step - loss: 0.5882
Epoch 4/10
1000/1000 [==============================] - 1s 888us/step - loss: 0.4588
Epoch 5/10
1000/1000 [==============================] - 1s 906us/step - loss: 0.3272
Epoch 6/10
1000/1000 [==============================] - 1s 910us/step - loss: 0.2228
Epoch 7/10
1000/1000 [==============================] - 1s 895us/step - loss: 0.1607
Epoch 8/10
1000/1000 [==============================] - 1s 879us/step - loss: 0.1172
Epoch 9/10
1000/1000 [==============================] - 1s 886us/step - loss: 0.0935
Epoch 10/10
1000/1000 [==============================] - 1s 888us/step - loss: 0.0638
Your input should have 4 dimenstions even if it is gray scale. So, you can use np.reshape(input,(32,48,48,1)) or np.expand_dims(input,axis=3).
Your images should be reshaped to (sample_length, 48, 48, 1) and your input_shape = (48, 48, 1)
x_train = x_train.reshape(x_train.shape[0], 48, 48, 1)
x_test = x_test.reshape(x_test.shape[0], 48, 48, 1)
input_shape = (48, 48, 1)
You can look at the MNIST example here, which is similar to your case.
I'm trying to build model to do activity recognition.
Using InceptionV3 and backbone and LSTM for the detection, using pre-trained weights.
train_generator = datagen.flow_from_directory(
'dataset/train',
target_size=(1,224, 224),
batch_size=batch_size,
class_mode='categorical', # this means our generator will only yield batches of data, no labels
shuffle=True,
classes=['PlayingPiano','HorseRiding','Skiing', 'Basketball','BaseballPitch'])
validation_generator = datagen.flow_from_directory(
'dataset/validate',
target_size=(1,224, 224),
batch_size=batch_size,
class_mode='categorical', # this means our generator will only yield batches of data, no labels
shuffle=True,
classes=['PlayingPiano','HorseRiding','Skiing', 'Basketball','BaseballPitch'])
return train_generator,validation_generator
I train 5 classes so split my data into folders for train and validate.
This is my CNN+LSTM architecture
image = Input(shape=(None,224,224,3),name='image_input')
cnn = applications.inception_v3.InceptionV3(
weights='imagenet',
include_top=False,
pooling='avg')
cnn.trainable = False
encoded_frame = TimeDistributed(Lambda(lambda x: cnn(x)))(image)
encoded_vid = LSTM(256)(encoded_frame)
layer1 = Dense(512, activation='relu')(encoded_vid)
dropout1 = Dropout(0.5)(layer1)
layer2 = Dense(256, activation='relu')(dropout1)
dropout2 = Dropout(0.5)(layer2)
layer3 = Dense(64, activation='relu')(dropout2)
dropout3 = Dropout(0.5)(layer3)
outputs = Dense(5, activation='softmax')(dropout3)
model = Model(inputs=[image],outputs=outputs)
sgd = SGD(lr=0.001, decay = 1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd,loss='categorical_crossentropy', metrics=['accuracy'])
model.fit_generator(train_generator,validation_data = validation_generator,steps_per_epoch=300, epochs=nb_epoch,callbacks=callbacks,shuffle=True,verbose=1)
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
image_input (InputLayer) (None, None, 224, 224, 3) 0
_________________________________________________________________
time_distributed_1 (TimeDist (None, None, 2048) 0
_________________________________________________________________
lstm_1 (LSTM) (None, 256) 2360320
_________________________________________________________________
dense_1 (Dense) (None, 512) 131584
_________________________________________________________________
dropout_1 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 256) 131328
_________________________________________________________________
dropout_2 (Dropout) (None, 256) 0
_________________________________________________________________
dense_3 (Dense) (None, 64) 16448
_________________________________________________________________
dropout_3 (Dropout) (None, 64) 0
_________________________________________________________________
dense_4 (Dense) (None, 5) 325
_________________________________________________________________
Model compiles normally without problem.
Problem starts during the training. It reaches val_acc=0.50 and then drops back to val_acc=0.30 and the loss just freeze on 0.80 and mostly don't move.
Here the logs from training, as you see the model for some tome improves and then just slowly drops down and later just freeze.
Any idea what can be the reason?
Epoch 00002: val_loss improved from 1.56471 to 1.55652, saving model to ./weights_inception/Inception_V3.02-0.28.h5
Epoch 3/500
300/300 [==============================] - 66s 219ms/step - loss: 1.5436 - acc: 0.3281 - val_loss: 1.5476 - val_acc: 0.2981
Epoch 00003: val_loss improved from 1.55652 to 1.54757, saving model to ./weights_inception/Inception_V3.03-0.30.h5
Epoch 4/500
300/300 [==============================] - 66s 220ms/step - loss: 1.5109 - acc: 0.3593 - val_loss: 1.5284 - val_acc: 0.3588
Epoch 00004: val_loss improved from 1.54757 to 1.52841, saving model to ./weights_inception/Inception_V3.04-0.36.h5
Epoch 5/500
300/300 [==============================] - 66s 221ms/step - loss: 1.4167 - acc: 0.4167 - val_loss: 1.4945 - val_acc: 0.3553
Epoch 00005: val_loss improved from 1.52841 to 1.49446, saving model to ./weights_inception/Inception_V3.05-0.36.h5
Epoch 6/500
300/300 [==============================] - 66s 221ms/step - loss: 1.2941 - acc: 0.4683 - val_loss: 1.4735 - val_acc: 0.4443
Epoch 00006: val_loss improved from 1.49446 to 1.47345, saving model to ./weights_inception/Inception_V3.06-0.44.h5
Epoch 7/500
300/300 [==============================] - 66s 221ms/step - loss: 1.2096 - acc: 0.5116 - val_loss: 1.3738 - val_acc: 0.5186
Epoch 00007: val_loss improved from 1.47345 to 1.37381, saving model to ./weights_inception/Inception_V3.07-0.52.h5
Epoch 8/500
300/300 [==============================] - 66s 221ms/step - loss: 1.1477 - acc: 0.5487 - val_loss: 1.2337 - val_acc: 0.5788
Epoch 00008: val_loss improved from 1.37381 to 1.23367, saving model to ./weights_inception/Inception_V3.08-0.58.h5
Epoch 9/500
300/300 [==============================] - 66s 221ms/step - loss: 1.0809 - acc: 0.5831 - val_loss: 1.2247 - val_acc: 0.5658
Epoch 00009: val_loss improved from 1.23367 to 1.22473, saving model to ./weights_inception/Inception_V3.09-0.57.h5
Epoch 10/500
300/300 [==============================] - 66s 221ms/step - loss: 1.0362 - acc: 0.6089 - val_loss: 1.1704 - val_acc: 0.5774
Epoch 00010: val_loss improved from 1.22473 to 1.17035, saving model to ./weights_inception/Inception_V3.10-0.58.h5
Epoch 11/500
300/300 [==============================] - 66s 221ms/step - loss: 0.9811 - acc: 0.6317 - val_loss: 1.1612 - val_acc: 0.5616
Epoch 00011: val_loss improved from 1.17035 to 1.16121, saving model to ./weights_inception/Inception_V3.11-0.56.h5
Epoch 12/500
300/300 [==============================] - 66s 221ms/step - loss: 0.9444 - acc: 0.6471 - val_loss: 1.1533 - val_acc: 0.5613
Epoch 00012: val_loss improved from 1.16121 to 1.15330, saving model to ./weights_inception/Inception_V3.12-0.56.h5
Epoch 13/500
300/300 [==============================] - 66s 221ms/step - loss: 0.9072 - acc: 0.6650 - val_loss: 1.1843 - val_acc: 0.5361
Epoch 00013: val_loss did not improve from 1.15330
Epoch 14/500
300/300 [==============================] - 66s 221ms/step - loss: 0.8747 - acc: 0.6744 - val_loss: 1.2135 - val_acc: 0.5258
Epoch 00014: val_loss did not improve from 1.15330
Epoch 15/500
300/300 [==============================] - 67s 222ms/step - loss: 0.8666 - acc: 0.6829 - val_loss: 1.1585 - val_acc: 0.5443
Epoch 00015: val_loss did not improve from 1.15330
Epoch 16/500
300/300 [==============================] - 66s 222ms/step - loss: 0.8386 - acc: 0.6926 - val_loss: 1.1503 - val_acc: 0.5482
Epoch 00016: val_loss improved from 1.15330 to 1.15026, saving model to ./weights_inception/Inception_V3.16-0.55.h5
Epoch 17/500
300/300 [==============================] - 66s 221ms/step - loss: 0.8199 - acc: 0.7023 - val_loss: 1.2162 - val_acc: 0.5288
Epoch 00017: val_loss did not improve from 1.15026
Epoch 18/500
300/300 [==============================] - 66s 222ms/step - loss: 0.8018 - acc: 0.7150 - val_loss: 1.1995 - val_acc: 0.5179
Epoch 00018: val_loss did not improve from 1.15026
Epoch 19/500
300/300 [==============================] - 66s 221ms/step - loss: 0.7923 - acc: 0.7186 - val_loss: 1.2218 - val_acc: 0.5137
Epoch 00019: val_loss did not improve from 1.15026
Epoch 20/500
300/300 [==============================] - 67s 222ms/step - loss: 0.7748 - acc: 0.7268 - val_loss: 1.2880 - val_acc: 0.4574
Epoch 00020: val_loss did not improve from 1.15026
Epoch 21/500
300/300 [==============================] - 66s 221ms/step - loss: 0.7604 - acc: 0.7330 - val_loss: 1.2658 - val_acc: 0.4861
The model is starting to overfit. Ideally as you increase number of epochs training loss will decrease(depends on learning rate), if its not able to decrease may be your model can have a high bias for the data. You can use bigger model(more parameters or deeper model).
you can also to reduce the learning rate, if it still freezes then model may have a low bias.
Thank you for the help. Yes, the problem was overfitting, so i made more aggresive dropout on LSTM, and it helped. But the accuracy on val_loss and acc_val still very low
video = Input(shape=(None, 224,224,3))
cnn_base = VGG16(input_shape=(224,224,3),
weights="imagenet",
include_top=False)
cnn_out = GlobalAveragePooling2D()(cnn_base.output)
cnn = Model(inputs=cnn_base.input, outputs=cnn_out)
cnn.trainable = False
encoded_frames = TimeDistributed(cnn)(video)
encoded_sequence = LSTM(32, dropout=0.5, W_regularizer=l2(0.01), recurrent_dropout=0.5)(encoded_frames)
hidden_layer = Dense(units=64, activation="relu")(encoded_sequence)
dropout = Dropout(0.2)(hidden_layer)
outputs = Dense(5, activation="softmax")(dropout)
model = Model([video], outputs)
Here the logs
Epoch 00033: val_loss improved from 1.62041 to 1.57951, saving model to
./weights_inception/Inception_V3.33-0.76.h5
Epoch 34/500
100/100 [==============================] - 54s 537ms/step - loss: 0.6301 - acc:
0.9764 - val_loss: 1.6190 - val_acc: 0.7627
Epoch 00034: val_loss did not improve from 1.57951
Epoch 35/500
100/100 [==============================] - 54s 537ms/step - loss: 0.5907 - acc:
0.9840 - val_loss: 1.5927 - val_acc: 0.7608
Epoch 00035: val_loss did not improve from 1.57951
Epoch 36/500
100/100 [==============================] - 54s 537ms/step - loss: 0.5783 - acc:
0.9812 - val_loss: 1.3477 - val_acc: 0.7769
Epoch 00036: val_loss improved from 1.57951 to 1.34772, saving model to
./weights_inception/Inception_V3.36-0.78.h5
Epoch 37/500
100/100 [==============================] - 54s 537ms/step - loss: 0.5618 - acc:
0.9802 - val_loss: 1.6545 - val_acc: 0.7384
Epoch 00037: val_loss did not improve from 1.34772
Epoch 38/500
100/100 [==============================] - 54s 537ms/step - loss: 0.5382 - acc:
0.9818 - val_loss: 1.8298 - val_acc: 0.7421
Epoch 00038: val_loss did not improve from 1.34772
Epoch 39/500
100/100 [==============================] - 54s 536ms/step - loss: 0.5080 - acc:
0.9844 - val_loss: 1.7948 - val_acc: 0.7290
Epoch 00039: val_loss did not improve from 1.34772
Epoch 40/500
100/100 [==============================] - 54s 537ms/step - loss: 0.4800 - acc:
0.9892 - val_loss: 1.8036 - val_acc: 0.7522
i'm beginner studying neural network.
i'm doing kaggle project - bike sharing demand.
i want to use simple neural network by keras, but loss is not decreasing.
what should i do?
-----------code--------------
# dataset from pandas
feature_names = ["season", "holiday", "workingday", "weather",
"temp", "atemp", "humidity", "windspeed",
"datetime_year", "datetime_hour", "datetime_dayofweek"]
label_name = ["count"]
X_train = train[feature_names] #shape (10886, 11)
Y_train = train[label_name] #shape (10886, 1)
X_test = test[feature_names]
# layers
model = Sequential()
model.add(Dense(units = 50, kernel_initializer = 'uniform', activation = 'relu', input_dim=11))
model.add(Dropout(0.3))
model.add(Dense(units = 50, kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(units = 5, kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
model.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['accuracy'])
# Train
model.fit(X_train, Y_train, batch_size = 100, epochs = 200)
---------result------------
Epoch 1/200
10886/10886 [==============================] - 4s 325us/step - loss: 69206.2478 - acc: 0.0094
Epoch 2/200
10886/10886 [==============================] - 1s 93us/step - loss: 69184.5435 - acc: 0.0096
Epoch 3/200
10886/10886 [==============================] - 1s 89us/step - loss: 69181.6330 - acc: 0.0096
Epoch 4/200
10886/10886 [==============================] - 1s 93us/step - loss: 69179.0222 - acc: 0.0096
Epoch 5/200
10886/10886 [==============================] - 1s 91us/step - loss: 69175.7442 - acc: 0.0096
Epoch 6/200
10886/10886 [==============================] - 1s 109us/step - loss: 69171.9052 - acc: 0.0096
Epoch 7/200
10886/10886 [==============================] - 1s 122us/step - loss: 69171.6164 - acc: 0.0096
Epoch 8/200
10886/10886 [==============================] - 1s 92us/step - loss: 69167.6923 - acc: 0.0096
Epoch 9/200
10886/10886 [==============================] - 1s 91us/step - loss: 69166.2911 - acc: 0.0096
Epoch 10/200
10886/10886 [==============================] - 1s 94us/step - loss: 69164.1145 - acc: 0.0096
...
Try setting the learning rate in the model.compile section. Start with 0.0001, then 0.001 and 0.01 and see what happens.