About task : I have class distances as input and want to get class confidences (number between 0.0 and 1.0).
So I have something like :
[
[
0.0,
0.0,
0.0,
6.371921190238224,
0.0,
3.3287083713830516,
7.085957828217146,
7.747408965761948,
5.498717498872398,
5.498717498872398,
5.498717498872398,
5.498717498872398,
8.529725281060978
],
[
6.396501448825533,
0.0,
0.0,
5.217483270813266,
0.0,
5.319046151560534,
5.823161030197735,
3.8991256371824976,
6.269856323952211,
5.517874167220461,
6.396501448825533,
5.328678274963717,
3.8991256371824976
],
]
And as result
[
[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
...
]
I have about 200 examples. My network building code is next :
def train(self, distances, classes):
"""
Train network
:param distances: array of distances to classes
:type distances: list[list[float]]
:param classes: array of class indicators
:type classes: list[list[float]]
"""
example_count, class_count = self._dimensions(distances, classes)
self.model = Sequential()
self.model.add(Dense(128, input_dim=class_count))
self.model.add(Dense(class_count))
self.model.compile(optimizer=SGD(), loss='mse')
self.model.fit(array(distances), array(classes))
But during training I get next output :
Epoch 1/10
425/425 [==============================] - 0s - loss: nan
Epoch 2/10
425/425 [==============================] - 0s - loss: nan
Epoch 3/10
425/425 [==============================] - 0s - loss: nan
Epoch 4/10
425/425 [==============================] - 0s - loss: nan
Epoch 5/10
425/425 [==============================] - 0s - loss: nan
Epoch 6/10
425/425 [==============================] - 0s - loss: nan
Epoch 7/10
425/425 [==============================] - 0s - loss: nan
Epoch 8/10
425/425 [==============================] - 0s - loss: nan
Epoch 9/10
425/425 [==============================] - 0s - loss: nan
Epoch 10/10
425/425 [==============================] - 0s - loss: nan
And when I trying to use model.predict(numpy.array([[ 0.0, 0.0, 0.0, 6.371921190238224, 0.0, 3.3287083713830516, 7.085957828217146, 7.747408965761948, 5.498717498872398, 5.498717498872398, 5.498717498872398, 5.498717498872398, 8.529725281060978]])) (example from train set) - I getting [[ nan nan nan nan nan nan nan nan nan nan nan nan nan]]
What can be wrong in data or building code?
Seems like I had wrong fit parameters (learning rate and other). Now I have next code (yes, I added neurons to hidden layer and increased train epochscount during testing):
example_count, class_count = self._dimensions(distances, classes)
self.model = Sequential()
self.model.add(Dense(1024, input_dim=class_count))
self.model.add(Dense(class_count))
self.model.compile(optimizer=SGD(lr=0.002, momentum=0.0, decay=0.0, nesterov=True), loss='mse', metrics=['accuracy'])
self.model.fit(array(distances), array(classes), nb_epoch=80)
And it gives
...
Epoch 79/80
425/425 [==============================] - 0s - loss: 0.0381 - acc: 0.6729
Epoch 80/80
425/425 [==============================] - 0s - loss: 0.0382 - acc: 0.6871
[[ 0.19048974 0.1585739 0.28798762 -0.23555818 0.4293299 0.10981751
-0.08614585 -0.06363138 0.05927059 0.07283521 -0.07852616 -0.02396417
-0.28515971]]
Not a good accuracity, but topic problem solved
Related
My model giving this answer while training. The accuracy is coming only in binary - 0 or 1. Also, my model is predicting only D label([0 0 0 1]).
WARNING:absl:Found untraced functions such as lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses, lstm_cell_2_layer_call_fn while saving (showing 5 of 15). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: /content/drive/MyDrive/SavedModels/OpenfaceRNN_final_7/assets
INFO:tensorflow:Assets written to: /content/drive/MyDrive/SavedModels/OpenfaceRNN_final_7/assets
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
[[0. 0. 1. 0.]]
(1, 2362, 715)
Epoch 1/50
1/1 [==============================] - 1s 729ms/step - loss: 1.4395 - accuracy: 0.0000e+00
Epoch 2/50
1/1 [==============================] - 1s 696ms/step - loss: 1.4322 - accuracy: 0.0000e+00
Epoch 3/50
1/1 [==============================] - 1s 705ms/step - loss: 1.4250 - accuracy: 0.0000e+00
Epoch 4/50
1/1 [==============================] - 1s 707ms/step - loss: 1.4178 - accuracy: 0.0000e+00
Epoch 5/50
1/1 [==============================] - 1s 699ms/step - loss: 1.4107 - accuracy: 0.0000e+00
Epoch 6/50
1/1 [==============================] - 1s 703ms/step - loss: 1.4036 - accuracy: 0.0000e+00
Epoch 7/50
1/1 [==============================] - 1s 708ms/step - loss: 1.3965 - accuracy: 0.0000e+00
Epoch 8/50
1/1 [==============================] - 1s 712ms/step - loss: 1.3895 - accuracy: 0.0000e+00
Epoch 9/50
1/1 [==============================] - 1s 700ms/step - loss: 1.3826 - accuracy: 0.0000e+00
Epoch 10/50
1/1 [==============================] - 1s 709ms/step - loss: 1.3756 - accuracy: 0.0000e+00
Epoch 11/50
1/1 [==============================] - 1s 697ms/step - loss: 1.3688 - accuracy: 0.0000e+00
Epoch 12/50
1/1 [==============================] - 1s 698ms/step - loss: 1.3619 - accuracy: 0.0000e+00
Epoch 13/50
1/1 [==============================] - 1s 796ms/step - loss: 1.3551 - accuracy: 0.0000e+00
Epoch 14/50
1/1 [==============================] - 1s 783ms/step - loss: 1.3483 - accuracy: 0.0000e+00
Epoch 15/50
1/1 [==============================] - 1s 727ms/step - loss: 1.3416 - accuracy: 0.0000e+00
Epoch 16/50
1/1 [==============================] - 1s 705ms/step - loss: 1.3349 - accuracy: 0.0000e+00
Epoch 17/50
1/1 [==============================] - 1s 702ms/step - loss: 1.3282 - accuracy: 0.0000e+00
Epoch 18/50
1/1 [==============================] - 1s 709ms/step - loss: 1.3215 - accuracy: 0.0000e+00
Epoch 19/50
1/1 [==============================] - 1s 695ms/step - loss: 1.3149 - accuracy: 0.0000e+00
Epoch 20/50
1/1 [==============================] - 1s 700ms/step - loss: 1.3083 - accuracy: 0.0000e+00
Epoch 21/50
1/1 [==============================] - 1s 706ms/step - loss: 1.3017 - accuracy: 0.0000e+00
Epoch 22/50
1/1 [==============================] - 1s 703ms/step - loss: 1.2952 - accuracy: 0.0000e+00
Epoch 23/50
1/1 [==============================] - 1s 701ms/step - loss: 1.2887 - accuracy: 0.0000e+00
Epoch 24/50
1/1 [==============================] - 1s 717ms/step - loss: 1.2822 - accuracy: 0.0000e+00
Epoch 25/50
1/1 [==============================] - 1s 709ms/step - loss: 1.2757 - accuracy: 0.0000e+00
Epoch 26/50
1/1 [==============================] - 1s 702ms/step - loss: 1.2692 - accuracy: 0.0000e+00
Epoch 27/50
1/1 [==============================] - 1s 707ms/step - loss: 1.2628 - accuracy: 0.0000e+00
Epoch 28/50
1/1 [==============================] - 1s 732ms/step - loss: 1.2564 - accuracy: 0.0000e+00
Epoch 29/50
1/1 [==============================] - 1s 705ms/step - loss: 1.2500 - accuracy: 0.0000e+00
Epoch 30/50
1/1 [==============================] - 1s 702ms/step - loss: 1.2436 - accuracy: 0.0000e+00
Epoch 31/50
1/1 [==============================] - 1s 707ms/step - loss: 1.2373 - accuracy: 0.0000e+00
Epoch 32/50
1/1 [==============================] - 1s 708ms/step - loss: 1.2309 - accuracy: 0.0000e+00
Epoch 33/50
1/1 [==============================] - 1s 696ms/step - loss: 1.2246 - accuracy: 0.0000e+00
Epoch 34/50
1/1 [==============================] - 1s 710ms/step - loss: 1.2183 - accuracy: 0.0000e+00
Epoch 35/50
1/1 [==============================] - 1s 703ms/step - loss: 1.2120 - accuracy: 0.0000e+00
Epoch 36/50
1/1 [==============================] - 1s 715ms/step - loss: 1.2058 - accuracy: 1.0000
Epoch 37/50
1/1 [==============================] - 1s 711ms/step - loss: 1.1995 - accuracy: 1.0000
Epoch 38/50
1/1 [==============================] - 1s 709ms/step - loss: 1.1933 - accuracy: 1.0000
Epoch 39/50
1/1 [==============================] - 1s 701ms/step - loss: 1.1870 - accuracy: 1.0000
Epoch 40/50
1/1 [==============================] - 1s 706ms/step - loss: 1.1808 - accuracy: 1.0000
Epoch 41/50
1/1 [==============================] - 1s 725ms/step - loss: 1.1746 - accuracy: 1.0000
Epoch 42/50
1/1 [==============================] - 1s 710ms/step - loss: 1.1685 - accuracy: 1.0000
Epoch 43/50
1/1 [==============================] - 1s 697ms/step - loss: 1.1623 - accuracy: 1.0000
Epoch 44/50
1/1 [==============================] - 1s 697ms/step - loss: 1.1561 - accuracy: 1.0000
Epoch 45/50
1/1 [==============================] - 1s 712ms/step - loss: 1.1500 - accuracy: 1.0000
Epoch 46/50
1/1 [==============================] - 1s 701ms/step - loss: 1.1439 - accuracy: 1.0000
Epoch 47/50
1/1 [==============================] - 1s 710ms/step - loss: 1.1378 - accuracy: 1.0000
Epoch 48/50
1/1 [==============================] - 1s 711ms/step - loss: 1.1317 - accuracy: 1.0000
Epoch 49/50
1/1 [==============================] - 1s 699ms/step - loss: 1.1256 - accuracy: 1.0000
Epoch 50/50
1/1 [==============================] - 1s 704ms/step - loss: 1.1195 - accuracy: 1.0000
This is my model:
classifier = Sequential()
#input_tensor = Input(shape=(236115, 714))
classifier.add(LSTM(units=1024, return_sequences=True, dropout=0.1,input_shape = (2362, 715) ))
classifier.add(Dense(1024, activation='relu'))
classifier.add(BatchNormalization())
classifier.add((Dropout(0.1)))
classifier.add(LSTM(units=512, return_sequences=True, dropout=0.1))
classifier.add(Dense(512, activation='relu'))
classifier.add(BatchNormalization())
classifier.add((Dropout(0.1)))
classifier.add(LSTM(units=256, dropout=0.1))
classifier.add(Dense(256, activation='relu'))
classifier.add(BatchNormalization())
classifier.add((Dropout(0.1)))
classifier.add(Dense(128, activation='relu'))
classifier.add(BatchNormalization())
classifier.add((Dropout(0.1)))
classifier.add(Dense(64, activation='relu'))
classifier.add(BatchNormalization())
classifier.add(Dropout(0.1))
classifier.add(Dense(32, activation='relu'))
classifier.add(BatchNormalization())
classifier.add(Dense(16, activation='relu'))
classifier.add(BatchNormalization())
classifier.add(Dense(8, activation='relu'))
classifier.add(BatchNormalization())
classifier.add(Dense(4, activation='softmax'))
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]
Keras predicted output for binary classification is probabilities. Not classes, i.e., 1 or 0.
for example the following code generates probabilities.
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout
# Generate dummy data
x_train = np.random.random((100, 20))
y_train = np.random.randint(2, size=(100, 1))
x_test = np.random.random((10, 20))
y_test = np.random.randint(2, size=(10, 1))
model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
model.fit(x_train, y_train, epochs=20, batch_size=128)
y_predicted = model.predict(x_test)
print(y_predicted)
and the output is:
Epoch 1/20
100/100 [==============================] - 1s 5ms/step - loss: 0.8134 - acc: 0.4300
Epoch 2/20
100/100 [==============================] - 0s 17us/step - loss: 0.7429 - acc: 0.4600
Epoch 3/20
100/100 [==============================] - 0s 20us/step - loss: 0.7511 - acc: 0.4300
Epoch 4/20
100/100 [==============================] - 0s 18us/step - loss: 0.7408 - acc: 0.5000
Epoch 5/20
100/100 [==============================] - 0s 21us/step - loss: 0.6922 - acc: 0.5700
Epoch 6/20
100/100 [==============================] - 0s 31us/step - loss: 0.6874 - acc: 0.5600
Epoch 7/20
100/100 [==============================] - 0s 29us/step - loss: 0.7005 - acc: 0.5600
Epoch 8/20
100/100 [==============================] - 0s 23us/step - loss: 0.6960 - acc: 0.5200
Epoch 9/20
100/100 [==============================] - 0s 24us/step - loss: 0.6988 - acc: 0.5200
Epoch 10/20
100/100 [==============================] - 0s 26us/step - loss: 0.7276 - acc: 0.4000
Epoch 11/20
100/100 [==============================] - 0s 20us/step - loss: 0.6967 - acc: 0.5000
Epoch 12/20
100/100 [==============================] - 0s 30us/step - loss: 0.7085 - acc: 0.5000
Epoch 13/20
100/100 [==============================] - 0s 24us/step - loss: 0.6993 - acc: 0.5500
Epoch 14/20
100/100 [==============================] - 0s 26us/step - loss: 0.7278 - acc: 0.4600
Epoch 15/20
100/100 [==============================] - 0s 27us/step - loss: 0.6665 - acc: 0.5500
Epoch 16/20
100/100 [==============================] - 0s 24us/step - loss: 0.6784 - acc: 0.5500
Epoch 17/20
100/100 [==============================] - 0s 24us/step - loss: 0.7259 - acc: 0.4800
Epoch 18/20
100/100 [==============================] - 0s 26us/step - loss: 0.7093 - acc: 0.5500
Epoch 19/20
100/100 [==============================] - 0s 28us/step - loss: 0.6911 - acc: 0.5700
Epoch 20/20
100/100 [==============================] - 0s 34us/step - loss: 0.6771 - acc: 0.5500
[[0.4875336 ]
[0.47847825]
[0.4808622 ]
[0.5032022 ]
[0.4556646 ]
[0.48644704]
[0.4600153 ]
[0.47782585]
[0.49664593]
[0.5001673 ]]
Now, how can I get the classes from that probabilities? I tried manually setting a threshold like this:
print([1 if x >0.4 else 0 for x in y_predicted])
Is there any other method to do that from Keras API? I could not find any.
Yes, model.predict_classes.
model.predict_classes(x_test)
https://github.com/keras-team/keras/blob/f0eb8d538c82798944346b4b2df917a06bf5e9d4/keras/engine/sequential.py#L254 (predict_classes)
which uses a threshold of 0.5 in case of binary classification or argmax in case of multi-class.
I am trying to train model to play Chrome Dino (the offline game).
The idea was to have 6 last screenshots of the game, use CNN on each separately (to extract features) and then put those features as timesteps into LSTM.
My training data is X = [6 timestep game screenshots] -> y=[1,0] (keep running, jump)
Timestep example
I have even split the database so it has 50% jump examples and 50% keep running examples.
Sadly I am stuck at 50% accuracy and loss is stuck too.
198/198 [==============================] - 0s - loss: 0.6944 - acc: 0.4596
Epoch 91/100
198/198 [==============================] - 0s - loss: 0.6932 - acc: 0.5000
Epoch 92/100
198/198 [==============================] - 0s - loss: 0.6932 - acc: 0.5000
Epoch 93/100
198/198 [==============================] - 0s - loss: 0.6932 - acc: 0.5000
Epoch 94/100
198/198 [==============================] - 0s - loss: 0.6933 - acc: 0.5000
Epoch 95/100
198/198 [==============================] - 0s - loss: 0.6942 - acc: 0.5000
Epoch 96/100
198/198 [==============================] - 0s - loss: 0.6939 - acc: 0.5000
Epoch 97/100
198/198 [==============================] - 0s - loss: 0.6935 - acc: 0.5000
I have tried many model hyperparams with different layers, but I always get the same result.
Current model
model = Sequential()
model.add(TimeDistributed(Convolution2D(64, 3, 3, activation='relu'), input_shape=(FRAMES_TO_PROCESS, FRAME_HEIGHT,FRAME_WIDTH, FRAME_FILTERS )))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(TimeDistributed(ZeroPadding2D((1,1))))
model.add(TimeDistributed(Convolution2D(64, 3, 3, activation='relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=(2,2))))
model.add(TimeDistributed(ZeroPadding2D((1,1))))
model.add(TimeDistributed(Convolution2D(128, 3, 3, activation='relu')))
model.add(TimeDistributed(ZeroPadding2D((1,1))))
model.add(TimeDistributed(Convolution2D(128, 3, 3, activation='relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=(2,2))))
model.add(Activation('relu'))
model.add(TimeDistributed(Flatten()))
model.add(Dropout(0.1))
model.add(LSTM(120, return_sequences=False))
model.add(Dense(2, activation='softmax'))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
Any idea what went wrong?
I'm trying to learn Keras and are using LSTM for a classification problem. I want to be able to plot the
accuracy and loss and update the plot during training. For that I'm using the callback function.
For some reason the accuracy and loss I receive in the callbacks does not match with
the accuracy and loss printed by the fit function.
Here are the relevant lines of my code:
class PlotCallbacks(Callback):
def on_batch_end(self, batch, logs={}):
print(logs)
return
# Create the model
model = Sequential()
model.add(Embedding(top_words, embedding_vector_length,input_length=max_conv_length))
model.add(LSTM(300))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, callbacks=[PlotCallbacks()], nb_epoch=1, batch_size=3, verbose=1)
When running the program I get this output (first row of each batch is printed by callback, second is printed by model.fit):
Epoch 1/1
{'acc': 0.0, 'loss': 1.1038421, 'batch': 0, 'size': 3}
3/25 [==>...........................] - ETA: 27s - loss: 1.1038 - acc: 0.0000e+00
{'acc': 1.0, 'loss': 1.0622898, 'batch': 1, 'size': 3}
6/25 [======>.......................] - ETA: 19s - loss: 1.0831 - acc: 0.5000
{'acc': 1.0, 'loss': 0.91526389, 'batch': 2, 'size': 3}
9/25 [=========>....................] - ETA: 13s - loss: 1.0271 - acc: 0.6667
{'acc': 1.0, 'loss': 0.36570337, 'batch': 3, 'size': 3}
12/25 [=============>................] - ETA: 11s - loss: 0.8618 - acc: 0.7500
{'acc': 1.0, 'loss': 0.1433304, 'batch': 4, 'size': 3}
15/25 [=================>............] - ETA: 9s - loss: 0.7181 - acc: 0.8000
{'acc': 1.0, 'loss': 0.041385528, 'batch': 5, 'size': 3}
18/25 [====================>.........] - ETA: 6s - loss: 0.6053 - acc: 0.8333
{'acc': 1.0, 'loss': 0.011424608, 'batch': 6, 'size': 3}
21/25 [========================>.....] - ETA: 3s - loss: 0.5205 - acc: 0.8571
{'acc': 1.0, 'loss': 0.0034991663, 'batch': 7, 'size': 3}
24/25 [===========================>..] - ETA: 1s - loss: 0.4558 - acc: 0.8750
{'acc': 1.0, 'loss': 0.0012318328, 'batch': 8, 'size': 1}
25/25 [==============================] - 26s - loss: 0.4377 - acc: 0.8800
I have tried to print logs.get('acc'), as well as saved the accuracies to a list in the PlotCallbacks object and print the list, but the problem remains.
Does anyone have a clue what the problem may be?
Thanks
on_batch_end() type callback function gets the accuracy of the batch that just got trained. Whereas the logs printed by keras is the average over all the batches that it has seen in the current epoch. You can easily observe that in your logs.. say in first 2 batches one accuracy was 0.0 and 1.0, which made the overall accuracy over 2 batches seen as 0.5000. here is exactly where the average is calculated.
Also accuracy as a metric is usually reported from epoch to epoch so you can change the callback to on_epoch_end().
Here is a simple callback that records the accuracy in the way defined by Keras' progbar (average over all the batches seen so far in the current epoch):
class AccHistory(Callback):
def on_train_begin(self, logs={}):
self.accs = []
self.acc_avg = []
def on_batch_end(self, batch, logs={}):
self.accs.append(round(1e2*float(logs.get('acc')),4))
self.acc_avg.append(round(np.mean(self.accs,dtype=np.float64),4))
def on_epoch_end(self, batch, logs={}):
self.accs = []