Exemple #1
0
    def forward(self, input):
        w = self.weight
        a = self.bias

        if self.has_sq == True and self.quan == True:
            nw = QuantizeNbit.apply(w, self.quan_bit, self.w_scale,
                                    self.w_offset)
            na = QuantizeNbit.apply(a, self.quan_bit, self.a_scale,
                                    self.a_offset)
        else:
            nw = w
            na = a

        output = F.linear(input, nw, na)
        return output
    def true_weight(self):
        w = self.weight
        w_bias = self.bias

        nw = 2*(w - (w.max()+w.min())/2)/(w.max()-w.min())
        if(w_bias.max()-w_bias.min() == 0):
            nw_bias = w_bias
        else:
            nw_bias = 2*(w_bias - (w_bias.max()+w_bias.min())/2) / \
                (w_bias.max()-w_bias.min())

        if self.quan == True:
            nw = QuantizeNbit.apply(nw, self.quan_bit)
            nw_bias = QuantizeNbit.apply(nw_bias, self.quan_bit)
        return nw, nw_bias
    def forward(self, input):
        w = self.weight
        w_bias = self.bias

        nw = 2*(w - (w.max()+w.min())/2)/(w.max()-w.min())
        if(w_bias.max()-w_bias.min() == 0):
            nw_bias = w_bias
        else:
            nw_bias = 2*(w_bias - (w_bias.max()+w_bias.min())/2) / \
                (w_bias.max()-w_bias.min())

        if self.quan == True:
            nw = QuantizeNbit.apply(nw, self.quan_bit)
            nw_bias = QuantizeNbit.apply(nw_bias, self.quan_bit)

        output = F.linear(input, nw, nw_bias)
        return output
    def true_weight(self):
        w = self.weight
        a = self.bias

        if self.quan == False:
            print('quan=False')
            return w, w_bias

        if self.has_sq == True and self.quan == True:
            nw = QuantizeNbit.apply(
                w, self.quan_bit, self.w_scale, self.w_offset)
            na = QuantizeNbit.apply(
                a, self.quan_bit, self.a_scale, self.a_offset)
            return nw, na
        else:
            print('have not cal the scale and offset, weight is origin weight.')
            return w, a
Exemple #5
0
    def forward(self, input):
        w = self.weight
        a = input

        if self.has_sq == True and self.quan == True:
            nw = QuantizeNbit.apply(w, self.quan_bit, self.scale, self.offset)
        else:
            nw = w
        output = F.conv2d(a, nw, self.bias, self.stride, self.padding,
                          self.dilation, self.groups)
        return output
 def true_weight(self):
     w = self.weight
     #nw = 2*(w - (w.max()+w.min())/2)/(w.max()-w.min())
     nw = w - ((w.view(w.size(0), -1).max(-1)[0].view(w.size(0), 1, 1, 1)) +
               (w.view(w.size(0), -1).min(-1)[0].view(w.size(0), 1, 1, 1)))/2
     nw = 2 * nw
     nw = nw / ((w.view(w.size(0), -1).max(-1)[0].view(w.size(0), 1, 1, 1)) -
                (w.view(w.size(0), -1).min(-1)[0].view(w.size(0), 1, 1, 1)))
     # print(nw)
     if self.quan == True:
         nw = QuantizeNbit.apply(nw, self.quan_bit)
     return nw
    def true_weight(self):
        w = self.weight
        if self.quan == False:
            print('quan=False')
            return w

        if self.has_sq == True and self.quan == True:
            nw = QuantizeNbit.apply(w, self.quan_bit, self.scale, self.offset)
            return nw
        else:
            print('have not cal the scale and offset, weight is origin weight.')
            return w
    def forward(self, input):
        w = self.weight
        a = input

        # 将权重从min,max映射到-1,1
        #nw = 2*(w - (w.max()+w.min())/2)/(w.max()-w.min())
        nw = w - ((w.view(w.size(0), -1).max(-1)[0].view(w.size(0), 1, 1, 1)) +
                  (w.view(w.size(0), -1).min(-1)[0].view(w.size(0), 1, 1, 1)))/2
        nw = 2 * nw
        nw = nw / ((w.view(w.size(0), -1).max(-1)[0].view(w.size(0), 1, 1, 1)) -
                   (w.view(w.size(0), -1).min(-1)[0].view(w.size(0), 1, 1, 1)))
        if self.quan == True:
            nw = QuantizeNbit.apply(nw, self.quan_bit)

        output = F.conv2d(a, nw, self.bias,
                          self.stride, self.padding,
                          self.dilation, self.groups)
        return output