I am seeking for help of the error I am getting. I want output as 3 multi-class-category. So what I did to append those output and created a custom loss function to handle it. But it seems I am getting stuck with the shape.
class Model(keras.Model):
def __init__(self):
super(Model, self).__init__()
self.dense1 = layers.Dense(10, input_shape=(1, 5), activation="relu")
self.l = []
self.u = layers.Dense(4, activation="softmax")
self.l.append(self.u)
self.c = layers.Dense(4, activation="softmax")
self.l.append(self.c)
self.k = layers.Dense(4, activation="softmax")
self.l.append(self.k)
self.outputs = layers.Concatenate()
def call(self, inputs):
x = tf.convert_to_tensor(inputs)
x = self.dense1(x)
ls = []
u = self.u(x)
ls.append(u)
c = self.c(x)
ls.append(c)
k = self.k(x)
ls.append(k)
return self.outputs(ls)
def process(self, observations):
action_probs = self.predict_on_batch(observations)
return action_probs
This is the custom_cross_entropy I used which is concatination of 3 sparse_categorical_crossentropy:
def custom_cross_entropy(y_true, y_pred):
l = []
for i in range(3):
l.append(tf.keras.metrics.sparse_categorical_crossentropy(y_true[:,i].reshape((-1,1)), y_pred[:, 4 * i: 4 * (i 1)].reshape(-1, 1, 4), from_logits=False, axis=-1))
return K.concatenate(l).reshape((-1,1))
I have created a dummy data to show the output.
X = [[[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]]]
y_t = [[0, 1, 3], [0, 2, 1], [2, 1, 1], [1, 2, 0], [1, 2, 3], [1, 2, 1]]
This is the error I am getting. It seems the model is trying to use each value as y_true value which I did not understand why.
model = Model()
model.compile(loss=custom_cross_entropy, optimizer='adam', metrics=['accuracy'])
model.fit(X, y_t, epochs=5)
Console Showing:
Epoch 1/5
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_37044/2147060259.py in <module>
----> 1 model.fit(X, y_t, epochs=5)
~\anaconda3\lib\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, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1182 _r=1):
1183 callbacks.on_train_batch_begin(step)
-> 1184 tmp_logs = self.train_function(iterator)
1185 if data_handler.should_sync:
1186 context.async_wait()
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
883
884 with OptionalXlaContext(self._jit_compile):
--> 885 result = self._call(*args, **kwds)
886
887 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)
3037 (graph_function,
3038 filtered_flat_args) = self._maybe_define_function(args, kwargs)
-> 3039 return graph_function._call_flat(
3040 filtered_flat_args, captured_inputs=graph_function.captured_inputs) # pylint: disable=protected-access
3041
~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1961 and executing_eagerly):
1962 # No tape is watching; skip to running the function.
-> 1963 return self._build_call_outputs(self._inference_function.call(
1964 ctx, args, cancellation_manager=cancellation_manager))
1965 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: assertion failed: [Condition x == y did not hold element-wise:] [x (custom_cross_entropy/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [6 1] [y (custom_cross_entropy/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [18 1]
[[node custom_cross_entropy/SparseSoftmaxCrossEntropyWithLogits/assert_equal_1/Assert/Assert (defined at \AppData\Local\Temp/ipykernel_37044/2925072780.py:6) ]] [Op:__inference_train_function_51479]
Function call stack:
train_function
Any suggestion to fix the issue is welcome.
CodePudding user response:
I think you are addressing the wrong dimension in your custom_cross_entropy
function. Maybe try something like this:
import tensorflow as tf
class Model(tf.keras.Model):
def __init__(self):
super(Model, self).__init__()
self.dense1 = tf.keras.layers.Dense(10, input_shape=(1, 5), activation="relu")
self.l = []
self.u = tf.keras.layers.Dense(4, activation="softmax")
self.l.append(self.u)
self.c = tf.keras.layers.Dense(4, activation="softmax")
self.l.append(self.c)
self.k = tf.keras.layers.Dense(4, activation="softmax")
self.l.append(self.k)
self.outputs = tf.keras.layers.Concatenate()
def call(self, inputs):
x = tf.convert_to_tensor(inputs)
x = self.dense1(x)
ls = []
u = self.u(x)
ls.append(u)
c = self.c(x)
ls.append(c)
k = self.k(x)
ls.append(k)
return self.outputs(ls)
def process(self, observations):
action_probs = self.predict_on_batch(observations)
return action_probs
def custom_cross_entropy(y_true, y_pred):
l = []
for i in range(3):
temp_y_true = tf.reshape(y_true[:,i], (-1,1))
temp_y_pred = tf.reshape(y_pred[:, :, 4 * i: 4 * (i 1)],(-1, 1, 4))
tf.print('Fixed solution ::: Y_true -->', tf.shape(temp_y_true), 'Y_pred -->', tf.shape(temp_y_pred))
temp_y_true_wrong = tf.reshape(y_true[:,i], (-1,1))
temp_y_pred_wrong = tf.reshape(y_pred[:, 4 * i: 4 * (i 1)],(-1, 1, 4))
tf.print('Wrong solution ::: Y_true -->', tf.shape(temp_y_true_wrong), 'Y_pred -->', tf.shape(temp_y_pred_wrong))
l.append(tf.keras.metrics.sparse_categorical_crossentropy(temp_y_true, temp_y_pred, from_logits=False, axis=-1))
return tf.reshape(tf.keras.backend.concatenate(l), (-1,1))
X = [[[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]]]
y_t = [[0, 1, 3], [0, 2, 1], [2, 1, 1], [1, 2, 0], [1, 2, 3], [1, 2, 1]]
model = Model()
model.compile(loss=custom_cross_entropy, optimizer='adam', metrics=['accuracy'])
model.fit(X, y_t, epochs=5)
Epoch 1/5
Fixed solution ::: Y_true --> [6 1] Y_pred --> [6 1 4]
Wrong solution ::: Y_true --> [6 1] Y_pred --> [18 1 4]
...