# Soft\_Dice\_Loss and Tensorflow v1 /v2 MRI segmentation

**URL:** <https://community.deeplearning.ai/t/soft-dice-loss-and-tensorflow-v1-v2-mri-segmentation/890289>\
**Category:** AI for Medical Diagnosis\
**Tags:** week-module-3, dl-ai-learning-platform\
**Created:** [March 13, 2026, 9:14pm UTC](https://community.deeplearning.ai/t/soft-dice-loss-and-tensorflow-v1-v2-mri-segmentation/890289 "2026-03-13T21:14:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![user440](https://avatars.discourse-cdn.com/v4/letter/u/ad7895/32.png) [@user440](https://community.deeplearning.ai/u/user440)\
**Post date:** [March 13, 2026, 9:14pm UTC](https://community.deeplearning.ai/t/soft-dice-loss-and-tensorflow-v1-v2-mri-segmentation/890289/1 "2026-03-13T21:14:17Z")

</div>

Hi,  
I’m trying to implement the multi-class version of soft\_dice\_loss but facing the difficulty that test cases include both single-class and multi-class inputs, for example :  
print(“Case 4:\n”)  
print(“Pred:\n”)  
print(pred\_4[0, :, :, 0])  
print(“\nLabel:\n”)  
print(label\_4[0, :, :, 0]) .

The function expects tensors (num\_classes, x\_dim, y\_dim, z\_dim) but the test cases use a mixture . I need a way to handle both 3D and 4D inputs with the same function. Do I need to add 1 dim to the 3D tensors ?

2dly , the code seems to run in Tensorflow 1 , with sessions and tf.compat.v1.logging.set\_verbosity(tf.compat.v1.logging.ERROR)  
Is it compatible with Tensorflow 2 code we might use somewhere in the notebook ?  
@Deepti_Prasad  
Thanks

---

<div class="post-metadata">

**Author:** ![Deepti\_Prasad](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/deepti_prasad/32/165831_2.png) [@Deepti\_Prasad](https://community.deeplearning.ai/u/Deepti_Prasad)\
**Post date:** [March 14, 2026, 5:00am UTC](https://community.deeplearning.ai/t/soft-dice-loss-and-tensorflow-v1-v2-mri-segmentation/890289/2 "2026-03-14T05:00:23Z")

</div>

hi @user440

is this assignment codes you are talking about?

can you post screenshot of your failed unittest so I can understand what are you trying?

I also didn’t understand tensorflow 1 and tensorflow 2 question.

Please post screenshot of your unittest output and/or error you have encountered. Keep in mind while you post screenshot, no part of grade function codes should be posted on public post here as it is against community guidelines.

You can refer to this thread which explains hard-coding implementation can cause issues in implementation, also about adding dim, the comment has explain ation, that each value added is about not adding dim, but selective value mentioning, adds the correct dimention to the output.

> [@W3 assignment - Grading](https://community.deeplearning.ai/t/w3-assignment-grading/703076/11):
>
> @ardonp you didn’t do this correction mentioned in my first comment response to your debugging. Please read the comments carefully and follow each of them one by one. I understand the discomfort, but debugging requires patience, attention to detail and more self-reflection. The instructions mentions to change the dimension of X and y from the x dim, y dim, z dim, number of channels to num , x dim, y dim, z dim So mention X, (3, 0, 1, 2) is incorrect Instructions before grade cell mentions …

regards

Dr. Deepti

---

<div class="post-metadata">

**Author:** ![user440](https://avatars.discourse-cdn.com/v4/letter/u/ad7895/32.png) [@user440](https://community.deeplearning.ai/u/user440)\
**Post date:** [March 16, 2026, 4:39pm UTC](https://community.deeplearning.ai/t/soft-dice-loss-and-tensorflow-v1-v2-mri-segmentation/890289/3 "2026-03-16T16:39:18Z")

</div>

Hello Dr Deepti, @Deepti_Prasad  
Indeed, this is about C1W3 assignment.  
Re the multi class version for Dice\_coefficient (Exercise 4) and Soft\_Dice\_loss (Exercise 5), I encountered a problem due partly to the instructions : “Compute mean soft dice loss over all abnormality classes.” This made me write the function a certain way but resulted in errors.  
I tried another way and managed to get the expected results. But I don’t quite understand, so I will send you a screenshot of the results with 1st method I used.  
Regarding Tensorflow, in the import packages section we have import support\_files.keras\_sessions as keras\_sessions and tf.compat.v1.logging.set\_verbosity(tf.compat.v1.logging.ERROR) and in Exercise 1 we have “Please use the [Keras.sum(x,axis=)]” which links to [https://www.tensorflow.org/versions/r1.15/api\_docs/python/tf/keras/backend/sum](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/keras/backend/sum) so Tensorflow 1  
The current version is Tensorflow 2. If we were to rewrite the # Verification of implementation in Tensorflow 2 ,would it require a lot of changes? I think sessions have been deprecated in TF2.

---

<div class="post-metadata">

**Author:** ![Deepti\_Prasad](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/deepti_prasad/32/165831_2.png) [@Deepti\_Prasad](https://community.deeplearning.ai/u/Deepti_Prasad)\
**Post date:** [March 17, 2026, 2:25pm UTC](https://community.deeplearning.ai/t/soft-dice-loss-and-tensorflow-v1-v2-mri-segmentation/890289/4 "2026-03-17T14:25:37Z")

</div>

@user440

The labs of this course probably uses only some part of tensorflow version, limited to categorical choice of labels and shouldn’t be issue as long as you are running the codes in course provided platform.

Ofcourse one will have to make many changes with many of tensorflow libraries being deprecated.

You can try and let me know how was the experience 🤪

Regards  
Dr. Deepti

---

<div class="post-metadata">

**Author:** ![Mohammed\_Eltayeb1](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@Mohammed\_Eltayeb1](https://community.deeplearning.ai/u/Mohammed_Eltayeb1)\
**Post date:** [July 5, 2026, 5:19am UTC](https://community.deeplearning.ai/t/soft-dice-loss-and-tensorflow-v1-v2-mri-segmentation/890289/5 "2026-07-05T05:19:08Z")

</div>

Ok
