美文网首页
操练代码之损失函数

操练代码之损失函数

作者: 万州客 | 来源:发表于2024-08-17 23:11 被阅读0次

    同一系列。

    一,代码

    import torch
    import numpy as np
    import matplotlib.pyplot as plt
    
    
    loss = torch.nn.L1Loss(reduction='mean')
    input = torch.tensor([1., 2., 3., 4.])
    target = torch.tensor([4., 5., 6., 7.])
    output = loss(input, target)
    print(output)
    
    loss_fn = torch.nn.MSELoss(reduction='mean')
    loss = loss_fn(input, output)
    print(loss)
    
    entroy = torch.nn.CrossEntropyLoss()
    input = torch.Tensor([[-0.1181, -0.3682, -0.2209]])
    target = torch.tensor([0])
    output = entroy(input, target)
    print(output)
    
    a = torch.tensor([1., 2., 3., 4.])
    b = torch.tensor([4.1, 51., 6.1, 7.1])
    similarity = torch.cosine_similarity(a, b, dim=0)
    loss = 1 - similarity
    print(loss)
    

    二,输出

    2024-08-18 22_59_38-悬浮球.png

    相关文章

      网友评论

          本文标题:操练代码之损失函数

          本文链接:https://www.haomeiwen.com/subject/tttpkjtx.html