Related
I tried to do experiments in different datasets using this model, it works fine for univariate time series. However, I get an issue when trying to do it for multivariate time series and I think it's due to Time Distributed layer but I am not sure. I tried to read different posts about the same question with no luck.
trainx shape: (38100, 100, 4) | trainy shape: (38100, 4)
testx shape: (12230, 100, 4) | testy shape: (12230, 4)
(samples, timestamps, features)
The model is as follows:
def build_model(X):
'''
Builds an autoencoder model.
#params: X input array
#return: autoencoder full model, encoder model part
'''
encoder_inputs = keras.layers.Input(shape=(X.shape[1], X.shape[2]), name='Input_Layer')
L1 = keras.layers.LSTM(64, return_sequences=True, name='Encoder_1')(encoder_inputs)
L2 = keras.layers.LSTM(32, return_sequences=True, name='Encoder_2')(L1)
code = keras.layers.LSTM(2, return_sequences=False, name='code_vector')(L2)
L3 = keras.layers.RepeatVector(X.shape[1], name='Repeat_Vector')(code)
L4 = keras.layers.LSTM(32, return_sequences=True, name='Decoder_1')(L3)
L5 = keras.layers.LSTM(64, return_sequences=True, name='Decoder_2')(L4)
decoder_outputs = keras.layers.TimeDistributed(keras.layers.Dense(X.shape[2]), name='Time_Distrubted')(L5)
encoder = keras.Model(inputs=encoder_inputs, outputs=code, name='Encoder')
autoencoder = keras.Model(inputs=encoder_inputs, outputs=decoder_outputs, name='Autoencoder')
return autoencoder, code
Then I build the model and compile and fit it as follows:
model, code = build_model(trainx)
model.compile('adam', loss='mae')
history = model.fit(x=trainx, y=trainy, epochs=100, validation_split=0.1, batch_size=32, callbacks=callbacks, shuffle=False)
I get the following error trace:
<ipython-input-246-e01fa31bc39d> in <module>
----> 1 history = model.fit(x=trainx, y=trainy, epochs=100, validation_split=0.1, batch_size=32, callbacks=callbacks, shuffle=False)
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1181 _r=1):
1182 callbacks.on_train_batch_begin(step)
-> 1183 tmp_logs = self.train_function(iterator)
1184 if data_handler.should_sync:
1185 context.async_wait()
~\Anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
887
888 with OptionalXlaContext(self._jit_compile):
--> 889 result = self._call(*args, **kwds)
890
891 new_tracing_count = self.experimental_get_tracing_count()
~\Anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
948 # Lifting succeeded, so variables are initialized and we can run the
949 # stateless function.
--> 950 return self._stateless_fn(*args, **kwds)
951 else:
952 _, _, _, filtered_flat_args = \
~\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
3021 (graph_function,
3022 filtered_flat_args) = self._maybe_define_function(args, kwargs)
-> 3023 return graph_function._call_flat(
3024 filtered_flat_args, captured_inputs=graph_function.captured_inputs) # pylint: disable=protected-access
3025
~\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1958 and executing_eagerly):
1959 # No tape is watching; skip to running the function.
-> 1960 return self._build_call_outputs(self._inference_function.call(
1961 ctx, args, cancellation_manager=cancellation_manager))
1962 forward_backward = self._select_forward_and_backward_functions(
~\Anaconda3\lib\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
589 with _InterpolateFunctionError(self):
590 if cancellation_manager is None:
--> 591 outputs = execute.execute(
592 str(self.signature.name),
593 num_outputs=self._num_outputs,
~\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
57 try:
58 ctx.ensure_initialized()
---> 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
InvalidArgumentError: Incompatible shapes: [32,100,4] vs. [32,4]
[[node gradient_tape/mean_absolute_error/BroadcastGradientArgs (defined at <ipython-input-246-e01fa31bc39d>:1) ]] [Op:__inference_train_function_110609]
Function call stack:
train_function
As I mentioned I think it is probably related to time distributed layer. However, if it helps, the model can run when batch_size=1. Other than that it does not.
From comments
The network output must match your target shape. If you have a 2D
target your network must produce 2D and not 3D. simply setting
return_sequences=False produces 2D output.
def build_model(X):
'''
Builds an autoencoder model.
#params: X input array
#return: autoencoder full model, encoder model part
'''
encoder_inputs = keras.layers.Input(shape=(X.shape[1], X.shape[2]), name='Input_Layer')
L1 = keras.layers.LSTM(64, return_sequences=True, name='Encoder_1')(encoder_inputs)
L2 = keras.layers.LSTM(32, return_sequences=True, name='Encoder_2')(L1)
code = keras.layers.LSTM(2, return_sequences=False, name='code_vector')(L2)
L3 = keras.layers.RepeatVector(X.shape[1], name='Repeat_Vector')(code)
L4 = keras.layers.LSTM(32, return_sequences=True, name='Decoder_1')(L3)
L5 = keras.layers.LSTM(64, name='Decoder_2')(L4)
decoder_outputs = keras.layers.Dense(X.shape[2], name='Time_Distrubted')(L5)
encoder = keras.Model(inputs=encoder_inputs, outputs=code, name='Encoder')
autoencoder = keras.Model(inputs=encoder_inputs, outputs=decoder_outputs, name='Autoencoder')
return autoencoder, code
model, code = build_model(trainx)
model.compile('adam', loss='mae')
history = model.fit(x=trainx, y=trainy, epochs=100, validation_split=0.1, batch_size=32, callbacks=callbacks, shuffle=False)
(paraphrased from Marco Cerliani)
I have a a predicament with the model I am developing based on a popular dataset of skin-cancer images.
I have to points I'd like some guidance on -
A.
The original dataset is over +10K images with which almost 7000 images belong to one of the seven classes. I've created a subset of 4948 random images with which I ran a function to convert the images into a list of lists - first list contains the image and the latter the class as well as dismiss any images that are of class (5 - the class with the +6800K images). Thought process was to normalise the distribution across the classes.
Re-running the original model with an output (Dense layer of 6 neurons instead of 7) - retrieves an error.
Am I missing a step to 'indicate' to the model that there are only six possible classes? The model runs only when the output layer has seven neurons.
error:
Train on 1245 samples, validate on 312 samples
Epoch 1/30
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-138-8a3b40a69e37> in <module>
25 metrics=["accuracy"])
26
---> 27 model.fit(X_train, y_train, batch_size=32, epochs=30, validation_split=0.2)
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
778 validation_steps=validation_steps,
779 validation_freq=validation_freq,
--> 780 steps_name='steps_per_epoch')
781
782 def evaluate(self,
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs)
361
362 # Get outputs.
--> 363 batch_outs = f(ins_batch)
364 if not isinstance(batch_outs, list):
365 batch_outs = [batch_outs]
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
3290
3291 fetched = self._callable_fn(*array_vals,
-> 3292 run_metadata=self.run_metadata)
3293 self._call_fetch_callbacks(fetched[-len(self._fetches):])
3294 output_structure = nest.pack_sequence_as(
/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1456 ret = tf_session.TF_SessionRunCallable(self._session._session,
1457 self._handle, args,
-> 1458 run_metadata_ptr)
1459 if run_metadata:
1460 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
InvalidArgumentError: Received a label value of 6 which is outside the valid range of [0, 6). Label values: 1 1 2 4 2 1 2 1 2 1 2 2 4 2 2 1 3 1 4 6 0 2 4 2 0 4 2 4 4 0 2 4
[[{{node loss_15/activation_63_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]
B.
I'm attempting to add Data Augmentation as the dataset is relatively small taking into account the number of classes and the sparsity of the images across the classes. Once I try to run the generator I receive the error message below which suggests that there is something wrong with one of the variables in the validation_data tuple. I cannot understand what the issue is.
Example values of the test set look like:
[[[[0.41568627]
[0.4 ]
[0.43137255]
...
[0.54509804]
[0.54901961]
[0.54509804]]
[[0.42352941]
[0.43137255]
[0.43921569]
...
[0.56078431]
[0.54117647]
[0.55294118]]
[[0.41960784]
[0.41960784]
[0.45490196]
...
[0.51764706]
[0.57254902]
[0.50588235]]
...
[[0.30980392]
[0.36470588]
[0.36470588]
...
[0.47058824]
[0.44705882]
[0.41960784]]
[[0.29803922]
[0.31764706]
[0.34509804]
...
[0.45098039]
[0.43921569]
[0.4 ]]
[[0.25882353]
[0.30196078]
[0.31764706]
...
[0.45490196]
[0.42745098]
[0.36078431]]]
[[[0.60784314]
[0.59215686]
[0.56862745]
...
[0.59607843]
[0.63921569]
[0.63529412]]
[[0.6627451 ]
[0.63137255]
[0.62352941]
...
[0.67843137]
[0.60784314]
[0.63529412]]
[[0.62745098]
[0.65098039]
[0.6 ]
...
[0.61568627]
[0.63921569]
[0.67058824]]
...
[[0.62352941]
[0.6 ]
[0.59607843]
...
[0.6627451 ]
[0.71372549]
[0.6745098 ]]
[[0.61568627]
[0.58431373]
[0.61568627]
...
[0.67058824]
[0.65882353]
[0.68235294]]
[[0.61176471]
[0.60392157]
[0.61960784]
...
[0.65490196]
[0.6627451 ]
[0.66666667]]]]
[2, 1, 4, 4, 2]
Error:
Epoch 1/10
1/155 [..............................] - ETA: 11s - loss: 1.7916 - acc: 0.3000
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-139-8f19a958861f> in <module>
12 history = model.fit_generator(trainAug.flow(X_train, y_train, batch_size=batch_size)
13 ,epochs = 10, validation_data = (X_test, y_test),
---> 14 steps_per_epoch= X_train.shape[0]// batch_size
15 )
16 #epochs = epochs, validation_data = (X_test, y_test),
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1431 shuffle=shuffle,
1432 initial_epoch=initial_epoch,
-> 1433 steps_name='steps_per_epoch')
1434
1435 def evaluate_generator(self,
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, steps_name, **kwargs)
262
263 is_deferred = not model._is_compiled
--> 264 batch_outs = batch_function(*batch_data)
265 if not isinstance(batch_outs, list):
266 batch_outs = [batch_outs]
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
1173 self._update_sample_weight_modes(sample_weights=sample_weights)
1174 self._make_train_function()
-> 1175 outputs = self.train_function(ins) # pylint: disable=not-callable
1176
1177 if reset_metrics:
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
3290
3291 fetched = self._callable_fn(*array_vals,
-> 3292 run_metadata=self.run_metadata)
3293 self._call_fetch_callbacks(fetched[-len(self._fetches):])
3294 output_structure = nest.pack_sequence_as(
/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1456 ret = tf_session.TF_SessionRunCallable(self._session._session,
1457 self._handle, args,
-> 1458 run_metadata_ptr)
1459 if run_metadata:
1460 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
InvalidArgumentError: Received a label value of 6 which is outside the valid range of [0, 6). Label values: 0 1 6 4 2 4 2 0 1 2
[[{{node loss_15/activation_63_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]
Code:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sys
import os
import cv2
DATA_DIR = "/Users/namefolder/PycharmProjects/skin-cancer/HAM10000_images_part_1"
metadata = pd.read_csv(os.path.join(DATA_DIR, 'HAM10000_metadata.csv'))
lesion_type_dict = {'nv': 'Melanocytic nevi',
'mel': 'Melanoma',
'bkl': 'Benign keratosis-like lesions ',
'bcc': 'Basal cell carcinoma',
'akiec': 'Actinic keratoses',
'vasc': 'Vascular lesions',
'df': 'Dermatofibroma'}
metadata['cell_type'] = metadata['dx'].map(lesion_type_dict.get)
metadata['dx_code'] = pd.Categorical(metadata['dx']).codes
# save array of image-id and diagnosis-type (categorical)
metadata = metadata[['image_id', 'dx', 'dx_type', 'dx_code']]
training_data = []
IMG_SIZE=50
# preparing training data
def creating_training_data(path):
for img in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
for index, row in metadata.iterrows():
if (img == row['image_id']+'.jpg') & (row['dx_code'] != 5):
try:
training_data.append([new_array, row['dx_code']])
except Exception as ee:
pass
except Exception as e:
pass
return training_data
training_data = creating_training_data(DATA_DIR)
import random
random.shuffle(training_data)
# Splitting data into X features and Y label
X_train = []
y_train = []
for features, label in training_data:
X_train.append(features)
y_train.append(label)
# Reshaping of the data - required by Tensorflow and Keras (*necessary step of deep-learning using these repos)
X_train = np.array(X_train).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
# Normalize data - to reduce processing requirements
X_train = X_train/255.0
# model configuration
model = Sequential()
model.add(Conv2D(64, (3,3), input_shape = X_train.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Dense(6))
model.add(Activation("softmax"))
model.compile(loss="mean_squared_error",
optimizer="adam",
metrics=["accuracy"])
# Data Augmentation - Repo enabler
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
# initialize the training training data augmentation object
trainAug = ImageDataGenerator(
rescale=1 / 255.0,
rotation_range=20,
zoom_range=0.05,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
horizontal_flip=True,
fill_mode="nearest")
# initialize the validation (and testing) data augmentation object
valAug = ImageDataGenerator(rescale=1 / 255.0)
#set a leraning rate annealer
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
#Augmented Images model development
)
trainAug.fit(X_train)
#Fit the model
epochs = 10
batch_size= 10
history = model.fit_generator(trainAug.flow(X_train, y_train, batch_size=batch_size),epochs = 10, validation_data = (X_test, y_test), steps_per_epoch= X_train.shape[0]// batch_size)
Initially you had 7 labels: your code was then expecting labels 0, 1, 2, 3, 4, 5, 6
You removed label 5 from the dataset, ok. Now you have 6 labels in total.
Your code is expecting: 0, 1, 2, 3, 4, 5
But what you have in your data is: 0, 1, 2, 3, 4, 6
After removing the label 5, you need to transform the labels 6 into 5.
Something in the lines of:
if (img == row['image_id']+'.jpg') & (row['dx_code'] > 5):
try:
training_data.append([new_array, row['dx_code'] - 1])
except Exception as ee:
pass
elif (img == row['image_id']+'.jpg') & (row['dx_code'] < 5):
try:
training_data.append([new_array, row['dx_code']])
except Exception as ee:
pass
I am new to Keras and I am trying to build a recurrent neural network to classify Audio files.
During the training, I am receiving an InvalidArgumentError: indices[28,0] = -711 is not in [0, 20000).
I have found various topics talking about this error but, to be honest, I did not understand what I have to change in the parameters I am passing to the network in order to help it managing the negative values I have in the training array.
Below the code:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional
max_features = 20000
maxlen = 40
batch_size = 32
model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen))
model.add(Bidirectional(LSTM(64)))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
# try using different optimizers and different optimizer configs
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
print('Train...')
model.fit(X_train, y_train,
batch_size=batch_size,
epochs=4,
validation_data=[X_test, y_test])
Error below:
Train...
Train on 964 samples, validate on 476 samples
Epoch 1/4
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-22-3452d23cb8b5> in <module>()
12 batch_size=batch_size,
13 epochs=4,
---> 14 validation_data=[X_test, y_test])
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1037 initial_epoch=initial_epoch,
1038 steps_per_epoch=steps_per_epoch,
-> 1039 validation_steps=validation_steps)
1040
1041 def evaluate(self, x=None, y=None,
/usr/local/lib/python3.6/dist-packages/keras/engine/training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
197 ins_batch[i] = ins_batch[i].toarray()
198
--> 199 outs = f(ins_batch)
200 outs = to_list(outs)
201 for l, o in zip(out_labels, outs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs)
2714
-> 2715 return self._call(inputs)
2716 else:
2717 if py_any(is_tensor(x) for x in inputs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
2674 else:
-> 2675 fetched = self._callable_fn(*array_vals)
2676 return fetched[:len(self.outputs)]
2677
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1437 ret = tf_session.TF_SessionRunCallable(
1438 self._session._session, self._handle, args, status,
-> 1439 run_metadata_ptr)
1440 if run_metadata:
1441 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it stays alive
530 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: indices[28,0] = -711 is not in [0, 20000)
[[{{node embedding_3/embedding_lookup}} = GatherV2[Taxis=DT_INT32, Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:#training_1/Adam/Assign_2"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_3/embeddings/read, embedding_3/Cast, training_1/Adam/gradients/embedding_3/embedding_lookup_grad/concat/axis)]]
EDIT1: X_train is the float32 array below
array([[-5.79938449e+02, 6.63875936e+01, -6.75054944e+00, ...,
-2.89458464e+00, -2.30009868e+00, -2.34216322e+00],
[-3.38924973e+02, 1.60668197e+01, -5.39871140e+01, ...,
1.27180395e+00, 4.28090614e+00, 2.01538667e+00],
[-5.53199739e+02, 3.45314936e+01, -1.68711443e+01, ...,
-9.47345310e-02, -1.04780706e-02, 1.69060756e-01],
...,
[-5.91902354e+02, 6.14329122e+01, 1.43761675e+00, ...,
-4.38644438e+00, -3.67977820e+00, -1.89899207e+00],
[-7.04889969e+02, 6.24931510e+01, 1.90338300e+01, ...,
-1.47540089e+00, -1.75498741e+00, -4.55713837e-01],
[-8.24296641e+02, 7.43124586e+01, 1.43319513e+01, ...,
-7.60749297e-01, -1.05324700e+00, -8.54044186e-01]])
When I run the below code:
model = Sequential()
model.add(Conv2D(64,(3,3),input_shape = X.shape[1:],data_format = "channels_last"))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size = (2,2)))
model.add(Conv2D(64,(3,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics = ['accuracy'])
model.fit(X, y, batch_size=32, epochs=1,
validation_split=0.3)
I'm getting:
InvalidArgumentError Traceback (most recent call
last) in ()
3 batch_size=32,
4 epochs=1,
----> 5 validation_split=0.3)
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py
in fit(self, x, y, batch_size, epochs, verbose, callbacks,
validation_split, validation_data, shuffle, class_weight,
sample_weight, initial_epoch, steps_per_epoch, validation_steps,
**kwargs) 1361 initial_epoch=initial_epoch, 1362 steps_per_epoch=steps_per_epoch,
-> 1363 validation_steps=validation_steps) 1364 1365 def evaluate(self,
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py
in fit_loop(model, inputs, targets, sample_weights, batch_size,
epochs, verbose, callbacks, val_inputs, val_targets,
val_sample_weights, shuffle, callback_metrics, initial_epoch,
steps_per_epoch, validation_steps)
262 ins_batch[i] = ins_batch[i].toarray()
263
--> 264 outs = f(ins_batch)
265 if not isinstance(outs, list):
266 outs = [outs]
~\Anaconda3\lib\site-packages\tensorflow\python\keras\backend.py in
call(self, inputs) 2910 feed_symbols != self._feed_symbols or self.fetches != self._fetches or 2911
session != self._session):
-> 2912 self._make_callable(feed_arrays, feed_symbols, symbol_vals, session) 2913 2914 fetched =
self._callable_fn(*array_vals)
~\Anaconda3\lib\site-packages\tensorflow\python\keras\backend.py in
_make_callable(self, feed_arrays, feed_symbols, symbol_vals, session) 2855 callable_opts.target.append(self.updates_op.name) 2856
Create callable.
-> 2857 callable_fn = session._make_callable_from_options(callable_opts) 2858 # Cache
parameters corresponding to the generated callable, so that 2859
we can detect future mismatches and refresh the callable.
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
_make_callable_from_options(self, callable_options) 1412 """ 1413 self._extend_graph()
-> 1414 return BaseSession._Callable(self, callable_options) 1415 1416
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
init(self, session, callable_options) 1366 with errors.raise_exception_on_not_ok_status() as status: 1367
self._handle = tf_session.TF_SessionMakeCallable(
-> 1368 session._session, options_ptr, status) 1369 finally: 1370 tf_session.TF_DeleteBuffer(options_ptr)
~\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py
in exit(self, type_arg, value_arg, traceback_arg)
517 None, None,
518 compat.as_text(c_api.TF_Message(self.status.status)),
--> 519 c_api.TF_GetCode(self.status.status))
520 # Delete the underlying status object from memory otherwise it stays alive
521 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: Default MaxPoolingOp only supports NHWC on
device type CPU [[Node: max_pooling2d_2/MaxPool =
MaxPoolT=DT_FLOAT,
_class=["loc:#training_3/Adam/gradients/max_pooling2d_2/MaxPool_grad/MaxPoolGrad"],
data_format="NCHW", ksize=[1, 1, 2, 2], padding="VALID", strides=[1,
1, 2, 2],
_device="/job:localhost/replica:0/task:0/device:CPU:0"]]
X.shape ----> (8000, 50, 50, 1)
Also I'm using 4GB RAM CPU. I've run only on one epoch. Does this have anything to do with the memory?
I would like to implement a convolutional recurrent neural net with lstm in keras. I am a only a beginner in Machine Learning therefore I struggle understanding everything. Here is my code:
def initialize_model():
'Function to initialize the Convolutional Neural Network'
seed=123
# define CNN model
model = Sequential()
model.add(TimeDistributed(Conv2D(input_shape=(None,1,2048,1),kernel_size=(256,1),strides=(32,1),filters=16
,activation=LeakyReLU(0.01),data_format='channels_first')))
#cnn.add(MaxPooling2D(...))
model.add(TimeDistributed(Flatten()))
# define LSTM model
#model = Sequential()
#model.add(TimeDistribu(cnn))
model.add(LSTM(1024,recurrent_activation=LeakyReLU(0.01),recurrent_initializer=glorot_normal(seed)
,recurrent_regularizer=regularizers.l2(0.005)))
model.add(Dense(2048))
return model
def compile_model(model,learning_rate,loss_function):
#optimizer
optimizer = Adam(lr=learning_rate,beta_1=0.9,beta_2=0.999,epsilon=1e-8)
#Compile the model
model.compile(optimizer=optimizer,loss=loss_function,metrics=["accuracy"])
return model
# import and split data
data = get_data_from_files(10)
print(data.shape)
X_train, X_test, y_train, y_test = train_test_split(data[0],data[1], test_size=0.2, random_state=123)
print(X_train.shape) -> 160000,2048
print(y_train.shape) -> 160000,2048
print(X_test.shape) -> 40000,2048
print(y_test.shape) -> 40000,2048
X_tr = np.expand_dims(np.expand_dims(X_train,axis=2),axis=3)
X_te = np.expand_dims(np.expand_dims(X_test,axis=2),axis=3)
#X_tr shape -->(160000, 2048, 1, 1)
#X_te shape -->(40000, 2048, 1, 1)
model = initialize_model()
model =compile_model(model,0.001,"cosine_proximity")
hist = model.fit(X_tr,y_train
,validation_data=[X_te,y_test]
,epochs=5,batch_size=1000)
It returns the following error:
ValueError Traceback (most recent call last)
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other)
666 try:
--> 667 self.assert_same_rank(other)
668 new_dims = []
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in assert_same_rank(self, other)
711 raise ValueError("Shapes %s and %s must have the same rank" % (self,
--> 712 other))
713
ValueError: Shapes (256, 1, 2048, 16) and (?, ?, ?) must have the same rank
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in with_rank(self, rank)
741 try:
--> 742 return self.merge_with(unknown_shape(ndims=rank))
743 except ValueError:
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other)
672 except ValueError:
--> 673 raise ValueError("Shapes %s and %s are not compatible" % (self, other))
674
ValueError: Shapes (256, 1, 2048, 16) and (?, ?, ?) are not compatible
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-36-1633649265a0> in <module>()
6 hist = model.fit(X_tr,y_train
7 ,validation_data=[X_te,y_test]
----> 8 ,epochs=5,batch_size=1000)
9
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
953 sample_weight=sample_weight,
954 class_weight=class_weight,
--> 955 batch_size=batch_size)
956 # Prepare validation data.
957 do_validation = False
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
674 # to match the value shapes.
675 if not self.inputs:
--> 676 self._set_inputs(x)
677
678 if y is not None:
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/training.py in _set_inputs(self, inputs, outputs, training)
574 assert len(inputs) == 1
575 inputs = inputs[0]
--> 576 self.build(input_shape=(None,) + inputs.shape[1:])
577 return
578
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/sequential.py in build(self, input_shape)
225 self.inputs = [x]
226 for layer in self._layers:
--> 227 x = layer(x)
228 self.outputs = [x]
229
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
458 # Actually call the layer,
459 # collecting output(s), mask(s), and shape(s).
--> 460 output = self.call(inputs, **kwargs)
461 output_mask = self.compute_mask(inputs, previous_mask)
462
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/layers/wrappers.py in call(self, inputs, training, mask)
246 inner_mask_shape = self._get_shape_tuple((-1,), mask, 2)
247 kwargs['mask'] = K.reshape(mask, inner_mask_shape)
--> 248 y = self.layer.call(inputs, **kwargs)
249 if hasattr(y, '_uses_learning_phase'):
250 uses_learning_phase = y._uses_learning_phase
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/layers/convolutional.py in call(self, inputs)
166 padding=self.padding,
167 data_format=self.data_format,
--> 168 dilation_rate=self.dilation_rate)
169 if self.rank == 3:
170 outputs = K.conv3d(
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in conv2d(x, kernel, strides, padding, data_format, dilation_rate)
3564 strides=strides,
3565 padding=padding,
-> 3566 data_format=tf_data_format)
3567
3568 if data_format == 'channels_first' and tf_data_format == 'NHWC':
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in convolution(input, filter, padding, strides, dilation_rate, name, data_format)
777 dilation_rate=dilation_rate,
778 name=name,
--> 779 data_format=data_format)
780 return op(input, filter)
781
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in __init__(self, input_shape, filter_shape, padding, strides, dilation_rate, name, data_format)
854 filter_shape=filter_shape,
855 spatial_dims=spatial_dims,
--> 856 data_format=data_format)
857
858 def _build_op(self, _, padding):
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in __init__(self, input_shape, dilation_rate, padding, build_op, filter_shape, spatial_dims, data_format)
437 raise ValueError("dilation_rate must be positive")
438 if np.all(const_rate == 1):
--> 439 self.call = build_op(num_spatial_dims, padding)
440 return
441
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in _build_op(self, _, padding)
863 data_format=self.data_format,
864 strides=self.strides,
--> 865 name=self.name)
866
867 def __call__(self, inp, filter): # pylint: disable=redefined-builtin
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in __init__(self, input_shape, filter_shape, padding, data_format, strides, name)
134 strides=None,
135 name=None):
--> 136 filter_shape = filter_shape.with_rank(input_shape.ndims)
137 self.padding = padding
138 self.name = name
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in with_rank(self, rank)
742 return self.merge_with(unknown_shape(ndims=rank))
743 except ValueError:
--> 744 raise ValueError("Shape %s must have rank %d" % (self, rank))
745
746 def with_rank_at_least(self, rank):
ValueError: Shape (256, 1, 2048, 16) must have rank 3
I am completely stuck and have no idea how to resolve the problem.
EDIT:
I updated my code with the new error which is now a error of shape.
Fo anyone with a similar problem, I changed the trained and test data shape as follow :
X_tr = np.expand_dims(np.expand_dims(np.expand_dims(X_train,axis=1),axis=3),axis=1)
X_te = np.expand_dims(np.expand_dims(np.expand_dims(X_test,axis=1),axis=3),axis=1)
and the first layer :
model.add(TimeDistributed(Conv2D(kernel_size=(256,1),strides=(32,1),filters=16,activation=LeakyReLU(0.01),data_format='channels_first'),input_shape=(None,1,2048,1)))
It know run very well.
I am fairly confident that I have found the solution so this answer is edited to reflect that. According to keras documentation, when ising TimeDistribution with conv2d the input_shape must be a touple of length 4
Below is a part of the keras documentation.
TimeDistributed can be used with arbitrary layers, not just Dense, for instance with a Conv2D layer:
model = Sequential()
model.add(TimeDistributed(Conv2D(64, (3, 3)),
input_shape=(10, 299, 299, 3)))