示例#1
0
def v1_9_pos_log(output, eye_mask, eps, neg_freq, pos_freq):
    output_len = len(output)
    output_mat = torch.cat(output)
    output_mat = normalize(output_mat, dim=1)
    output_mat_sim = output_mat.mm(output_mat.t())
    dt_size = output_mat.shape[0]
    pred = (output_mat_sim * eye_mask[:dt_size, :dt_size]).argmax(1)
    label_range = (torch.from_numpy(np.cumsum(
        [i.shape[0] for i in output])).to("cuda").repeat([dt_size, 1]).t())
    pred = (pred > label_range).sum(0)
    label = torch.cat([
        torch.ones(o.shape[0], device="cuda") * l
        for l, o in zip(range(output_len), output)
    ])
    acc = (pred == label).float().mean()
    # concept_mask = torch.stack([label != i for i in label]).float()
    concept_mask = torch.stack([label != i for i in label])
    concept_mask_not = torch.logical_not(concept_mask)
    pos_score = output_mat_sim * concept_mask_not + concept_mask
    pos_loss = -((1 - pos_score) * torch.log((1 + pos_score) / 2)).sum(0)
    pos_loss = pos_loss / (concept_mask_not.sum(0) - 1 + eps)
    pos_loss = pos_loss.sum()
    neg_score = output_mat_sim * concept_mask
    neg_score = torch.cos_(neg_score * neg_freq)
    neg_loss = -(0.5 * (2 - neg_score) * torch.log(
        (1 + neg_score) / 2)).sum(0) / (concept_mask.sum(0) + eps)
    neg_loss = neg_loss.sum()
    loss = pos_loss + neg_loss
    result = {
        "loss": loss,
        "pos_loss": pos_loss,
        "neg_loss": neg_loss,
        "acc": acc,
    }
    return result
示例#2
0
print ("El valor de b[1, 1] es de ", b[1, 1])
c = b[1:, 1:]
print ("El tamaño de c es de", c.size())
print ("El stride de c es de ", c.stride())
print ("El offset de c es de ", c.storage_offset())


# In[6]:


#Ejercicio 2
torchcos = torch.Tensor([0,1,2])
torchcos_1 = torch.cos(torchcos)
print(torchcos_1)
#In place
torch.cos_(torch.Tensor([0,1,2]))


# In[20]:


#Ejercicio 3
d = torch.Tensor([[0,1,2],[0,1,2]])
d.unsqueeze_(0).shape


# In[15]:


#Ejercicio 4
d.squeeze_(0).shape