While working on a school project, there was a problem that the cosine similarity was always measured as 1. I had no choice but to question that the cosine similarity was always measured as 1 because I confirmed that the tensor1 and tensor2 were different. Why is the cosine similarity always measured as 1?
- training code
for i, (_image1, _label1) in enumerate(train_loader):
image1 = _image1.to(DEVICE)
label1 = _label1[0]
vector1_tensor = model(image1)
if (i == 0): #Exception Case
image2 = image1
label2 = label1
vector2_tensor = vector1_tensor
#PROBLEM LOCATION
similarity = F.cosine_similarity(vector1_tensor, vector2_tensor, dim = -1)
scaled_similarity = torch.sigmoid(similarity)
if label1 == label2:
target_vector = [1]
else :
target_vector = [0]
target_tensor = torch.tensor(target_vector).float()
target_tensor = target_tensor.to(DEVICE)
optimizer.zero_grad()
cost = loss(scaled_similarity, target_tensor)
cost.backward()
optimizer.step()
if not i % 40:
print (f'Epoch: {epoch:03d}/{EPOCH:03d} | '
f'Batch {i:03d}/{len(train_loader):03d} |'
f' Cost: {cost:.4f}')
#Recycle tensor for reduced computation
image2 = image1.clone()
label2 = label1
vector2_tensor = vector1_tensor.detach()
- model define code
class trans_VGG(nn.Module):
def __init__(self, base_dim):
super(trans_VGG, self).__init__()
self.feature = nn.Sequential(
conv_2(3, base_dim),
conv_2(base_dim, base_dim*2),
conv_2(base_dim*2, base_dim*4),
conv_3(base_dim*4, base_dim*8),
conv_3(base_dim*8, base_dim*8)
)
self.fc_layer = nn.Sequential(
nn.Linear(base_dim*8*7*7, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 1000),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(1000, 800)
)
for param in self.parameters():
param.requires_grad = True
def forward(self, x):
x = self.feature(x)
x = x.view(x.size(0), -1)
x = self.fc_layer(x)
return x
- Instead of using Pytorch's cosine similarity function, we created and calculated the cosine similarity function ourselves and found that the cosine similarity was still 1.
- We found that the values of sensor1 and sensor2 are different.
- This model is a VGG model, which obtains the similarity between the two tensors by embedding the two tensors that passed through the VGG model.
Cosine similarity measures the angle between vectors. Vectors that have different magnitudes but still point in the same direction will have a cosine similarity of 1.