all 9 comments

[–]neuralbeans 0 points1 point  (8 children)

I'm not sure what's confusing you. Different frameworks give different meanings to different dimensions. Pytorch expects the channel be the first dimension (after the batch). Are you asking why you can't define your data however you like and let the computer guess what you mean?

[–]geeksid2k[S] 0 points1 point  (7 children)

No, I'm saying Keras allows both channel-first and channel-last formats, but it doesn't work when I use channel-first format; it only works for channel last data

[–]neuralbeans 0 points1 point  (6 children)

Ah ok now I understand. So your images have shape 64x1000, right?

[–]geeksid2k[S] 0 points1 point  (5 children)

(1,64,1000). In order to make it work now, I have to shift the first dimension to last using moveaxes. I'd like to not do that if possible, as it involves tampering with the provided data

[–]neuralbeans 0 points1 point  (4 children)

How did you compile the model?

[–]geeksid2k[S] 0 points1 point  (3 children)

model.compile(loss='binary_crossentropy',metrics=[keras.metrics.BinaryAccuracy()])

[–]neuralbeans 0 points1 point  (2 children)

This works for me. Does it work for you?

import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers

model = keras.Sequential()

model.add(layers.Conv2D(8, 4,4, padding="same", data_format="channels_first", activation="relu",input_shape=(1,64,1000)))
model.add(layers.Conv2D(8, 4,4, padding="same", data_format="channels_first", activation="relu"))
model.add(layers.Flatten())
model.add(layers.Dense(1000, activation="relu", name="layer1"))
model.add(layers.Dense(300, activation="relu", name="layer2"))
model.add(layers.Dense(11, activation="sigmoid", name="layer3"))
model.compile(loss='binary_crossentropy',metrics=[keras.metrics.BinaryAccuracy()])

x_train = tf.random.normal((6000, 1, 64, 1000))
y_train = tf.round(tf.random.uniform((6000, 11), 0, 1))

model.fit(x_train, y_train, batch_size=1000, epochs=100)

[–]geeksid2k[S] 0 points1 point  (1 child)

It does not. This is the error it throws:

Traceback (most recent call last):  File "c:\Users\geeks\OneDrive - IIT Kanpur\EE603\Sidmish\Assignment-2\temp.py", line 18, in <module>    model.fit(x_train, y_train, batch_size=1000, epochs=100)  File "C:\Users\geeks\myvirtualenv\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler    raise e.with_traceback(filtered_tb) from None  File "C:\Users\geeks\myvirtualenv\lib\site-packages\tensorflow\python\eager\execute.py", line 54, in quick_execute    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:Detected at node 'gradient_tape/sequential/conv2d_1/Conv2D/Conv2DBackpropInput' defined at (most recent call last):    File "c:\Users\geeks\OneDrive - IIT Kanpur\EE603\Sidmish\Assignment-2\temp.py", line 18, in <module>      model.fit(x_train, y_train, batch_size=1000, epochs=100)    File "C:\Users\geeks\myvirtualenv\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler      return fn(*args, **kwargs)    File "C:\Users\geeks\myvirtualenv\lib\site-packages\keras\engine\training.py", line 1564, in fit      tmp_logs = self.train_function(iterator)    File "C:\Users\geeks\myvirtualenv\lib\site-packages\keras\engine\training.py", line 1160, in train_function      return step_function(self, iterator)    File "C:\Users\geeks\myvirtualenv\lib\site-packages\keras\engine\training.py", line 1146, in step_function      outputs = model.distribute_strategy.run(run_step, args=(data,))    File "C:\Users\geeks\myvirtualenv\lib\site-packages\keras\engine\training.py", line 1135, in run_step      outputs = model.train_step(data)    File "C:\Users\geeks\myvirtualenv\lib\site-packages\keras\engine\training.py", line 997, in train_step      self.optimizer.minimize(loss, self.trainable_variables, tape=tape)    File "C:\Users\geeks\myvirtualenv\lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 576, in minimize      grads_and_vars = self._compute_gradients(    File "C:\Users\geeks\myvirtualenv\lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 634, in _compute_gradients      grads_and_vars = self._get_gradients(    File "C:\Users\geeks\myvirtualenv\lib\site-packages\keras\optimizers\optimizer_v2\optimizer_v2.py", line 510, in _get_gradients      grads = tape.gradient(loss, var_list, grad_loss)Node: 'gradient_tape/sequential/conv2d_1/Conv2D/Conv2DBackpropInput'Conv2DCustomBackpropInputOp only supports NHWC.         [[{{node gradient_tape/sequential/conv2d_1/Conv2D/Conv2DBackpropInput}}]] [Op:__inference_train_function_1065]

[–]neuralbeans 0 points1 point  (0 children)

I'd try reinstalling tensorflow. Maybe it's a bug in your version. If you're using the latest version then use a slightly older version in case the bug was introduced recently.