quant.py 文件源码

python
阅读 40 收藏 0 点赞 0 评论 0

项目:pytorch-playground 作者: aaron-xichen 项目源码 文件源码
def tanh_quantize(input, bits):
    assert bits >= 1, bits
    if bits == 1:
        return torch.sign(input)
    input = torch.tanh(input) # [-1, 1]
    input_rescale = (input + 1.0) / 2 #[0, 1]
    n = math.pow(2.0, bits) - 1
    v = torch.floor(input_rescale * n + 0.5) / n
    v = 2 * v - 1 # [-1, 1]

    v = 0.5 * torch.log((1 + v) / (1 - v)) # arctanh
    return v
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号