I want to create a binary linear layer where the weights would be binary however during the backward pass i want to retain the real valued weights. Something like this one in torch:

  
      github.com
  
  
    itayhubara/BinaryNet/blob/master/Models/BinaryLinear.lua 8
--require 'randomkit'

local BinaryLinear, parent = torch.class('BinaryLinear', 'nn.Linear')

function BinaryLinear:__init(inputSize, outputSize,stcWeights)
   local delayedReset = self.reset
   self.reset = function() end
   parent.__init(self, inputSize, outputSize)
   self.reset = delayedReset

   self.weight = torch.Tensor(outputSize, inputSize)
   self.weightB = torch.Tensor(outputSize, inputSize)
   self.weightOrg = torch.Tensor(outputSize, inputSize)
   self.maskStc = torch.Tensor(outputSize, inputSize)
   self.randmat = torch.Tensor(outputSize, inputSize)
   self.bias = torch.Tensor(outputSize)
   self.gradWeight = torch.Tensor(outputSize, inputSize)
   self.gradBias = torch.Tensor(outputSize)
   self.stcWeights=stcWeights
   self:reset()


  This file has been truncated. show original

  
  
    
    
  
  


How do i handle the weight binary weight during only forward pass and reall weights during the backward pass?