Hi fyy!
First, I don’t think the overall structure of what you are doing is
correct.  More on that, below.
Some specific comments, in line:



 fyy:

1.I use Rounding_output_c1 = torch.round(output_c1)
because I want to make my network’s output is integer,
and use these code to caculate my training_acc
…
and if I don’t use Rounding_output_c1 = torch.round(output_c1),  training_acc will be 0.


Accuracy and loss are two different things.  Accuracy is the count
of how many right answers you get, and doesn’t need to be
differentiable.
Loss is the quantity the optimizer attempts to minimize while
training, and does need to be differentiable in order for pytorch’s
gradient-descent optimization algorithms to work.
Of course, your loss function should have some relationship to
your accuracy for it to make sense, but they aren’t the same.
Even if it did make sense to use torch.round() to calculate
your accuracy (and I don’t think it does in your case), you can’t
use it for your loss function because it’s not usefully differentiable.


2 I use loss.requires_grad = True   because when I debug my code I observed that  loss requires_grad is False


This means that something in your code is breaking your autograd
computation graph.  (Although torch.round() won’t work for
your loss function, it is technically differentiable, and I don’t think
it breaks the computation graph.)
I’m not sure, but maybe this for loop breaks the graph?
Rounding_output_c11 = torch.stack([(Rounding_output_c1 == i).float() for i in range(256)]) #[4,224,224]->[256,4,224,224] 256 is the number of classes, means pixel from 0-255

Perhaps @ptrblck might be able to spot where the graph is getting
broken.
Some comments about your overall approach:



 fyy:

I use crosssentropy loss for my network , outputs size is [4,2,224,224] where 4 means batchsize, 2 means channels, 224 means h and w .output_c1 size is [4,224,224] ,labels size is [4,224,224] too.


From this I speculate that you are performing multi-class image
segmentation, that is, that you are classifying each pixel in an
image, assigning them nClass different class labels.
The question is what is nClass, that is, what are the possible
values of each element of your [nBatch, 244, 244] labels
tensor?
You do say # 256 is the number of classes, but that seems
rather large.  It is the number of possible values of an 8-bit pixel, but
do all such values really occur in your labels tensor?
Anyway, let’s call it nClass, and assume that each value in labels
is an integer class label in the range [0, nClass - 1], inclusive.
(I am assuming that nClass != 2, that is, that this is not a binary
segmentation / classification problem.)
For this you want to use CrossEntropyLoss, and structure your
network output to have shape [nBatch, nClass, 244, 244].
(These are your predictions and are the input to the loss function.)
Note that your number of channels (2) does not show up in the
shape your your output (nor in the shape of labels).
Your labels, again, have shape [nBatch, 244, 244] and are
the target passed to CrossEntropyLoss.  (nClass enters into
labels not in its shape, but  in the fact the values in labels
range from 0 to nClass - 1.)
If the above makes sense for your use case, then to calculate your
accuracy you would test input.argmax (dim = 1) for equality
against labels (the target you pass to CrossEntropyLoss),
and count the matches.
Good luck.
K. Frank