W 3 | Create and Train the Model Error soft_dice_loss

Hi!

While doing this assignment, all exercises pass the tests. However, when trying to create the model, which is a given line in the lab, there is an error.

This is the line provided by the lab to define the model:

model = util.unet_model_3d(loss_function=soft_dice_loss, metrics=[dice_coefficient])

All the functions before this line are working perfectly. However, while defining the model, the soft-dice-loss seems to be throwing an error, even though the function passed the test.

This is the log I am getting:


NotImplementedError Traceback (most recent call last)
in ()
----> 1 model = util.unet_model_3d(loss_function=soft_dice_loss, metrics=[dice_coefficient])

~/work/W3A1/util.py in unet_model_3d(loss_function, input_shape, pool_size, n_labels, initial_learning_rate, deconvolution, depth, n_base_filters, include_label_wise_dice_coefficients, metrics, batch_normalization, activation_name)
190
191 model.compile(optimizer=Adam(lr=initial_learning_rate), loss=loss_function,
→ 192 metrics=metrics)
193 return model
194

/opt/conda/lib/python3.6/site-packages/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
227 # loss_weight_2 * output_2_loss_fn(…) +
228 # layer losses.
→ 229 self.total_loss = self._prepare_total_loss(masks)
230
231 # Functions for train, test and predict will

/opt/conda/lib/python3.6/site-packages/keras/engine/training.py in _prepare_total_loss(self, masks)
690
691 output_loss = loss_fn(
→ 692 y_true, y_pred, sample_weight=sample_weight)
693
694 if len(self.outputs) > 1:

/opt/conda/lib/python3.6/site-packages/keras/losses.py in call(self, y_true, y_pred, sample_weight)
69 scope_name = ‘lambda’ if self.name == ‘’ else self.name
70 with K.name_scope(scope_name):
—> 71 losses = self.call(y_true, y_pred)
72 return losses_utils.compute_weighted_loss(
73 losses, sample_weight, reduction=self.reduction)

/opt/conda/lib/python3.6/site-packages/keras/losses.py in call(self, y_true, y_pred)
130 Loss values per sample.
131 “”"
→ 132 return self.fn(y_true, y_pred, **self._fn_kwargs)
133
134 def get_config(self):

in soft_dice_loss(y_true, y_pred, axis, epsilon)
22
23 dice_numerator = 2* K.sum((y_true*y_pred),axis=axis) + epsilon
—> 24 dice_denominator = K.sum(np.square(y_true),axis=axis) + K.sum(np.square(y_pred),axis=axis) + epsilon
25 dice_loss = 1 - (K.mean(dice_numerator/dice_denominator))
26

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in array(self)
734 def array(self):
735 raise NotImplementedError(“Cannot convert a symbolic Tensor ({}) to a numpy”
→ 736 " array.".format(self.name))
737
738 def len(self):

NotImplementedError: Cannot convert a symbolic Tensor (activation_15_target:0) to a numpy array.

Any clue?

Thank you!

Juan

SOLVED!

Instead of using numpy.square, I used keras.square, and the algorithm are happy campers now.

Thanks!

I think you posted this in the wrong course area.
Your assignment is from Course 1, but you posted it in Course 5.
I’ll move it to the correct area.

Hey Juan Olano.

T Mosh sir is right. You need to be very specific about the courses while posting your queries as posting at wrong places could lead the queries unattended.

I guess its from ‘AI for Everyone’. Please check it once. Although I have done that for you. Still you need to check that.