Hi, I created a custom loss function in which the loss is calculated based on silhouette score. Here is my loss function:
class ClusteringLoss(torch.nn.Module):
def init(self):
super(ClusteringLoss,self).init()
def forward(self, embeddingData, origData, k):
repData = embeddingData.detach().numpy();
kmeansModel = KMeans(n_clusters=k).fit(repData);
yhat = kmeansModel.labels_;
silhouetteScore = metrics.silhouette_score(origData, yhat);
print("SilhouetteScore1: {}".format(silhouetteScore));
loss = Variable( torch.tensor(silhouetteScore), requires_grad = True);
loss = alpha*(1-loss);
return loss;
The argument ‘embeddingData’ is the output from the last layer of the model and it is a part of the gradient flow. I needed to use it to run Kmeans clustering and then calculate the silhouette score. Since the computed silhouette score can indicate how well the clustering result is, so I used it as the loss for my loss function.
However, I noticed that the gradients would not be updated after running a few epochs. I think the gradient flow was cut off, but I do not know which part of the code causes the problem. Any help will be appreciated. Thank you so much.