# Trying to build a custom loss function in Keras/Tensorflow but get an error

I am using tensorflow 2.0 and I built a loss function that has two inputs, y_pred and y_true. I made sure to use only tensorflow operations throughout the entire function (it's long) and to return a final tensor of one floating number.

I ran the loss function with two tensors with the relevant sizes and it worked, no bugs, got a loss. But when I try to compile my model, it won't work and I get a dimensionality error, I'm not sure where it's coming from.

    inputs_shape_0=np.shape(np.array(pic))[0] #100
inputs_shape_1=np.shape(np.array(pic))[1] #100
last_layer_size=np.shape(np.array(x0))[1] #100*100

model = tf.keras.Sequential()

(inputs_shape_0,inputs_shape_1,1)))

,activation='linear'))


Summary of model:

    Model: "sequential_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d_14 (Conv2D)           (None, 100, 100, 64)      320
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 50, 50, 64)        0
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 50, 50, 32)        8224
_________________________________________________________________
conv2d_16 (Conv2D)           (None, 50, 50, 1)         129
_________________________________________________________________
up_sampling2d_3 (UpSampling2 (None, 100, 100, 1)       0
=================================================================
Total params: 8,673
Trainable params: 8,673
Non-trainable params: 0
_____________________________


and I have a custom loss function:

   model.compile(optimizer='rmsprop',loss=dl_tf_loss, loss_weights=
[None],metrics=
['accuracy'])


and I'm getting the following error:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call
last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py
in
_create_c_op(graph, node_def, inputs, control_inputs)
1550   try:
-> 1551     c_op = c_api.TF_FinishOperation(op_desc)
1552   except errors.InvalidArgumentError as e:

InvalidArgumentError: Dimension size must be evenly divisible by 1000000
but is
10000 for 'loss_6/up_sampling2d_1_loss/Reshape' (op: 'Reshape') with input
shapes: [?,100,100,100], [2] and with input tensors computed as partial
shapes:
input[1] = [10000,1].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call
last)
15 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py
in
_create_c_op(graph, node_def, inputs, control_inputs)
1552   except errors.InvalidArgumentError as e:
1553     # Convert to ValueError for backwards compatibility.
-> 1554     raise ValueError(str(e))
1555
1556   return c_op

ValueError: Dimension size must be evenly divisible by 1000000 but is
10000 for
'loss_6/up_sampling2d_1_loss/Reshape' (op: 'Reshape') with input shapes:
[?,100,100,100], [2] and with input tensors computed as partial shapes:
input[1]
= [10000,1].


I have no idea where it's getting the 1,000,000 from. In my custom loss function that doesn't exist but if you guys think that it's probably from there then I'll go back and look at it again.

• Okay, I understood that the problem does have to do with my loss function. If I won't succeed at fixing it, I will update. – Keren Jun 28 '19 at 10:44