I am trying to create a basic, custom model together with a custom tf.data.Dataset. However, running my code gives me a “InvalidArgumentError”.
The full error message:
InvalidArgumentError: Matrix size-incompatible: In[0]: [26,599], In[1]: [15574,2048] [Op:MatMul]
So I have a signal on which I do a certain calculation. This gives me a 2D matrix of 26 rows and 599 columns. I want to group these matrices in a tf.data.Dataset, so that I can use them later to build a model.
The full code and error message are given below. TensorFlow version is 2.3.1.
Could someone please help me identify the issue?
Thanks in advance.
Dataset creation (tf.data.Dataset)
""" Create separate tf.data.Dataset's for the training and test set. """ # Create train labels signal_matrices_train = signal_matrices.get_xval_set(fold=0, set='train') matrices_list = [] label1_list = [] label2_list = [] for i in range(len(signal_matrices_train)): matrices_list.append(signal_matrices_train[i]['preprocessed']['matrix']) label1_list.append(signal_matrices_train[i]['label1'] label2_list.append(signal_matrices_train[i]['label2']) train_array = np.dstack(matrices_list) train_array = np.swapaxes(train_array, 1, 0) # To get (26, 599) for the signal matrices train_label1_array = np.array(label1_list) train_label2_array = np.array(label2_list) if(train_label1_array.shape == train_label2_array.shape): temparr = np.column_stack((train_label1_array, train_label2_array)) train_dataset_tf = tf.data.Dataset.from_tensor_slices((train_array.T, temparr)) else: print('Train: Label1 and label2 array do not have same length.') train_dataset_tf = tf.data.Dataset.from_tensor_slices((train_array.T, train_label1_array, train_label2_array)) # Same for test labels
After the dataset declarations, I try to check if things are as I would expect:
""" Print info about train_dataset_tf """ print('Training dataset:') pprint(train_dataset_tf.element_spec) print('Length of train_dataset_tf: ', len(train_dataset_tf)) print('nTest dataset:') pprint(test_dataset_tf.element_spec) print('Length of test_dataset_tf: ', len(test_dataset_tf)) Output: Training dataset: (TensorSpec(shape=(26, 599), dtype=tf.float64, name=None), TensorSpec(shape=(2,), dtype=tf.int64, name=None)) Length of train_dataset_tf: 2351 Test dataset: (TensorSpec(shape=(26, 599), dtype=tf.float64, name=None), TensorSpec(shape=(2,), dtype=tf.int64, name=None)) Length of test_dataset_tf: 1541
I indeed have matrices of [26×599] containing floats and I have two binary classes (one-hot encoded).
Next, I print the first element of the training dataset:
""" Give an example of an element in the training dataset """ it = iter(train_dataset_tf) print(next(it)) Output: (<tf.Tensor: shape=(26, 599), dtype=float64, numpy= array([[ 0.0072, 0.0100 , 0.0108, ..., 0.0097, 0.0070, 0.0091 ], [ 0.0070, 0.0092, 0.0099, ..., 0.0090, 0.0069, 0.0085 ], [ 0.0029 , 0.0041, 0.0044, ..., 0.0039, 0.0030, 0.0040], ..., [-0.0047, -0.0049, -0.0047, ..., -0.0054, -0.0057, -0.0047], [-0.0045, -0.0050 , -0.0049, ..., -0.0056, -0.0057, -0.0046], [-0.0042, -0.0049, -0.0047, ..., -0.0056, -0.0059, -0.0043]])>, <tf.Tensor: shape=(2,), dtype=int64, numpy=array([0, 1])>)
The last step of the dataset creation is to generate batches with a size of 32:
""" Create batches of training data """ train_batches = train_dataset_tf.shuffle(5000, seed=17, reshuffle_each_iteration=True).batch(BATCH_SIZE, drop_remainder=True) # Buffer size = 5000 and batch size = 32 (buffer size greater or equal to dataset size is recommended)
Model creation
""" Model declaration """ # Source: https://www.tensorflow.org/tutorials/keras/classification#preprocess_the_data model = Sequential() model.add(Input(shape=[26,599], batch_size=BATCH_SIZE, name='input')) model.add(Flatten(input_shape=[26, 599], data_format=None, name='flatten1')) # Go from 2D [26x599] to 1D [15 574] model.add(Dense(2048, activation='relu', name='dense1', input_shape=[15574])) model.add(Dense(512, activation='relu', name='dense2')) model.add(Dense(128, activation='relu', name='dense3')) model.add(Dense(NUM_CLASSES, activation='sigmoid', name='dense_classification')) # Output layer that does classification model.compile('sgd', 'binary_crossentropy', metrics=['accuracy'])
Next, I try to check if the model declaration is correct. I do this with model.summary():
model.summary() Output: Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= flatten1 (Flatten) (32, 15574) 0 _________________________________________________________________ dense1 (Dense) (32, 2048) 31897600 _________________________________________________________________ dense2 (Dense) (32, 512) 1049088 _________________________________________________________________ dense3 (Dense) (32, 128) 65664 _________________________________________________________________ dense_classification (Dense) (32, 2) 258 ================================================================= Total params: 33,012,610 Trainable params: 33,012,610 Non-trainable params: 0
In my eyes, this seems to be correct.
The final step is to train the model. This part also contains the full error message that is thrown:
# Definition of callbacks reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, min_lr=0, mode='auto', verbose=1) early_stop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=14, verbose=0, mode='auto', restore_best_weights=True) csv_logger = CSVLogger(LOGFILE, separator=',', append=True) # Train the model history = model.fit( train_dataset_tf, batch_size=BATCH_SIZE, epochs=5, verbose=2, callbacks = [reduce_lr, csv_logger, early_stop], shuffle=True ) Output: Epoch 1/5 --------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) <ipython-input-39-9d9a4ccaf472> in <module> 35 callbacks = [reduce_lr, csv_logger, early_stop], 36 class_weight = CLASS_WEIGHTS, ---> 37 shuffle=True 38 ) ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs) 106 def _method_wrapper(self, *args, **kwargs): 107 if not self._in_multi_worker_mode(): # pylint: disable=protected-access --> 108 return method(self, *args, **kwargs) 109 110 # Running inside `run_distribute_coordinator` already. ~/myvenv/project/lib/python3.6/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) 1096 batch_size=batch_size): 1097 callbacks.on_train_batch_begin(step) -> 1098 tmp_logs = train_function(iterator) 1099 if data_handler.should_sync: 1100 context.async_wait() ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in train_function(iterator) 804 def train_function(iterator): 805 """Runs a training execution with one step.""" --> 806 return step_function(self, iterator) 807 808 else: ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in step_function(model, iterator) 794 795 data = next(iterator) --> 796 outputs = model.distribute_strategy.run(run_step, args=(data,)) 797 outputs = reduce_per_replica( 798 outputs, self.distribute_strategy, reduction='first') ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py in run(***failed resolving arguments***) 1209 fn = autograph.tf_convert( 1210 fn, autograph_ctx.control_status_ctx(), convert_by_default=False) -> 1211 return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) 1212 1213 # TODO(b/151224785): Remove deprecated alias. ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py in call_for_each_replica(self, fn, args, kwargs) 2583 kwargs = {} 2584 with self._container_strategy().scope(): -> 2585 return self._call_for_each_replica(fn, args, kwargs) 2586 2587 def _call_for_each_replica(self, fn, args, kwargs): ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py in _call_for_each_replica(self, fn, args, kwargs) 2943 self._container_strategy(), 2944 replica_id_in_sync_group=constant_op.constant(0, dtypes.int32)): -> 2945 return fn(*args, **kwargs) 2946 2947 def _reduce_to(self, reduce_op, value, destinations, experimental_hints): ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs) 273 def wrapper(*args, **kwargs): 274 with ag_ctx.ControlStatusCtx(status=ag_ctx.Status.UNSPECIFIED): --> 275 return func(*args, **kwargs) 276 277 if inspect.isfunction(func) or inspect.ismethod(func): ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in run_step(data) 787 788 def run_step(data): --> 789 outputs = model.train_step(data) 790 # Ensure counter is updated only if `train_step` succeeds. 791 with ops.control_dependencies(_minimum_control_deps(outputs)): ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in train_step(self, data) 745 746 with backprop.GradientTape() as tape: --> 747 y_pred = self(x, training=True) 748 loss = self.compiled_loss( 749 y, y_pred, sample_weight, regularization_losses=self.losses) ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs) 983 984 with ops.enable_auto_cast_variables(self._compute_dtype_object): --> 985 outputs = call_fn(inputs, *args, **kwargs) 986 987 if self._activity_regularizer: ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py in call(self, inputs, training, mask) 370 if not self.built: 371 self._init_graph_network(self.inputs, self.outputs) --> 372 return super(Sequential, self).call(inputs, training=training, mask=mask) 373 374 outputs = inputs # handle the corner case where self.layers is empty ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/engine/functional.py in call(self, inputs, training, mask) 384 """ 385 return self._run_internal_graph( --> 386 inputs, training=training, mask=mask) 387 388 def compute_output_shape(self, input_shape): ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/engine/functional.py in _run_internal_graph(self, inputs, training, mask) 506 507 args, kwargs = node.map_arguments(tensor_dict) --> 508 outputs = node.layer(*args, **kwargs) 509 510 # Update tensor_dict. ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs) 983 984 with ops.enable_auto_cast_variables(self._compute_dtype_object): --> 985 outputs = call_fn(inputs, *args, **kwargs) 986 987 if self._activity_regularizer: ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py in call(self, inputs) 1196 self.bias, 1197 self.activation, -> 1198 dtype=self._compute_dtype_object) 1199 1200 def compute_output_shape(self, input_shape): ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/keras/layers/ops/core.py in dense(inputs, kernel, bias, activation, dtype) 51 outputs = sparse_ops.sparse_tensor_dense_matmul(inputs, kernel) 52 else: ---> 53 outputs = gen_math_ops.mat_mul(inputs, kernel) 54 # Broadcast kernel to inputs. 55 else: ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py in mat_mul(a, b, transpose_a, transpose_b, name) 5622 return _result 5623 except _core._NotOkStatusException as e: -> 5624 _ops.raise_from_not_ok_status(e, name) 5625 except _core._FallbackException: 5626 pass ~/myvenv/project/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name) 6841 message = e.message + (" name: " + name if name is not None else "") 6842 # pylint: disable=protected-access -> 6843 six.raise_from(core._status_to_exception(e.code, message), None) 6844 # pylint: enable=protected-access 6845 ~/myvenv/project/lib/python3.6/site-packages/six.py in raise_from(value, from_value) InvalidArgumentError: Matrix size-incompatible: In[0]: [26,599], In[1]: [15574,2048] [Op:MatMul]
submitted by /u/mich2000222
[visit reddit] [comments]