Many thanks for your reply. Yes, it worked and loss function decreased. But I still have a problem. It seems that on my main dataset, mmd_loss doesn’t work. My goal is to train the model on the source dataset and test this model on the target one. Source and target datasets are from two different domains. As they have different distributions. I’ve used mmd_loss in network training to minimize the discrepancy of source and target datasets. While when I train the network, from the first epoch, the value of mmd_loss is minimized! Although the mmd_loss value is minimized, the results in the classification of the target dataset are not satisfactory.
I don’t know where the problem is! The network structure is as follows:
class ConvNet(nn.Module):
    def __init__(self, num_conv1 =4, size_conv1 =64, num_conv2 =8, size_conv2 =32, len_flatten =num_conv2*out_conv2, num_fc1 =500, num_fc2 = 50, n_class =8):
        super(ConvNet, self).__init__()
        self.conv1 = nn.Conv1d(1, num_conv1, size_conv1)
        self.conv2 = nn.Conv1d(num_conv1, num_conv2, size_conv2)
        self.pool = nn.MaxPool1d(2)
        self.fc1 = nn.Linear(len_flatten, num_fc1)
        self.fc2 = nn.Linear(num_fc1, num_fc2)
        self.fc3 = nn.Linear(num_fc2, n_class)
        self.lrelu = nn.LeakyReLU(0.01)
        self.drop = nn.Dropout(p = 0.2)
       
    def forward (self, Source, Target):
        x_src = self.drop(self.pool(self.lrelu(self.conv1(Source))))
        x_tar = self.drop(self.pool(self.lrelu(self.conv1(Target))))
        x_src = self.drop(self.pool(self.lrelu(self.conv2(x_src))))
        x_tar = self.drop(self.pool(self.lrelu(self.conv2(x_tar))))
        source_mmd = x_src.view(-1,x_src.shape[1]*x_src.shape[2])
        target_mmd = x_tar.view(-1,x_tar.shape[1]*x_tar.shape[2])           
        x_src = self.lrelu(self.fc1(source_mmd)) 
        x_src = self.lrelu(self.fc2(x_src))
        src_output = self.fc3(x_src)
        return src_output, source_mmd,target_mmd