python类linear()的实例源码

layers.py 文件源码 项目:SMASH 作者: ajbrock 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def forward(self, input):
        return F.linear(input, self.weight[:, :input.size(1)], bias=self.bias)


# Simple class that dynamically inserts a nonlinearity between a batchnorm and a conv,
# using SMASH convs (and potentially SMASH BatchNorms)
affine.py 文件源码 项目:torchsample 作者: ncullen93 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def F_affine3d(x, matrix, center=True):
    A = matrix[:3,:3]
    b = matrix[:3,3]

    # make a meshgrid of normal coordinates
    coords = Variable(th_iterproduct(x.size(1),x.size(2),x.size(3)).float(),
                requires_grad=False)

    if center:
        # shift the coordinates so center is the origin
        coords[:,0] = coords[:,0] - (x.size(1) / 2. + 0.5)
        coords[:,1] = coords[:,1] - (x.size(2) / 2. + 0.5)
        coords[:,2] = coords[:,2] - (x.size(3) / 2. + 0.5)


    # apply the coordinate transformation
    new_coords = F.linear(coords, A, b)

    if center:
        # shift the coordinates back so origin is origin
        new_coords[:,0] = new_coords[:,0] + (x.size(1) / 2. + 0.5)
        new_coords[:,1] = new_coords[:,1] + (x.size(2) / 2. + 0.5)
        new_coords[:,2] = new_coords[:,2] + (x.size(3) / 2. + 0.5)

    # map new coordinates using bilinear interpolation
    x_transformed = F_trilinear_interp3d(x, new_coords)

    return x_transformed
mnist.py 文件源码 项目:pyscatwave 作者: edouardoyallon 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def f(o, params, stats, mode):
    o = F.batch_norm(o, running_mean=stats['bn.running_mean'],
                     running_var=stats['bn.running_var'],
                     weight=params['bn.weight'],
                     bias=params['bn.bias'], training=mode)
    o = F.conv2d(o, params['conv1.weight'], params['conv1.bias'])
    o = F.relu(o)
    o = o.view(o.size(0), -1)
    o = F.linear(o, params['linear2.weight'], params['linear2.bias'])
    o = F.relu(o)
    o = F.linear(o, params['linear3.weight'], params['linear3.bias'])
    return o
model.py 文件源码 项目:NoisyNet-A3C 作者: Kaixhin 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def forward(self, input):
    return F.linear(input, self.weight + self.sigma_weight * Variable(self.epsilon_weight), self.bias + self.sigma_bias * Variable(self.epsilon_bias))
test_jit.py 文件源码 项目:pytorch 作者: pytorch 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def LSTMCell(input, hidden, w_ih, w_hh, b_ih=None, b_hh=None):
    hx, cx = hidden
    gates = F.linear(input, w_ih, b_ih) + F.linear(hx, w_hh, b_hh)

    ingate, forgetgate, cellgate, outgate = gates.chunk(4, 1)
    ingate = F.sigmoid(ingate)
    forgetgate = F.sigmoid(forgetgate)
    cellgate = F.tanh(cellgate)
    outgate = F.sigmoid(outgate)

    cy = (forgetgate * cx) + (ingate * cellgate)
    hy = outgate * F.tanh(cy)
    return hy, cy
wideresnet.py 文件源码 项目:pretrained-models.pytorch 作者: Cadene 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def define_model(params):
    def conv2d(input, params, base, stride=1, pad=0):
        return F.conv2d(input, params[base + '.weight'],
                        params[base + '.bias'], stride, pad)

    def group(input, params, base, stride, n):
        o = input
        for i in range(0,n):
            b_base = ('%s.block%d.conv') % (base, i)
            x = o
            o = conv2d(x, params, b_base + '0')
            o = F.relu(o)
            o = conv2d(o, params, b_base + '1', stride=i==0 and stride or 1, pad=1)
            o = F.relu(o)
            o = conv2d(o, params, b_base + '2')
            if i == 0:
                o += conv2d(x, params, b_base + '_dim', stride=stride)
            else:
                o += x
            o = F.relu(o)
        return o

    # determine network size by parameters
    blocks = [sum([re.match('group%d.block\d+.conv0.weight'%j, k) is not None
                   for k in params.keys()]) for j in range(4)]

    def f(input, params, pooling_classif=True):
        o = F.conv2d(input, params['conv0.weight'], params['conv0.bias'], 2, 3)
        o = F.relu(o)
        o = F.max_pool2d(o, 3, 2, 1)
        o_g0 = group(o, params, 'group0', 1, blocks[0])
        o_g1 = group(o_g0, params, 'group1', 2, blocks[1])
        o_g2 = group(o_g1, params, 'group2', 2, blocks[2])
        o_g3 = group(o_g2, params, 'group3', 2, blocks[3])
        if pooling_classif:
            o = F.avg_pool2d(o_g3, 7, 1, 0)
            o = o.view(o.size(0), -1)
            o = F.linear(o, params['fc.weight'], params['fc.bias'])
        return o

    return f
modules.py 文件源码 项目:pytorch_workplace 作者: DingKe 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def forward(self, input):
        output = F.linear(input, self.weight, self.bias)
        return sparsify_grad(output, self.k, self.simplified)
modules.py 文件源码 项目:pytorch_workplace 作者: DingKe 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def forward(self, input):
        binary_weight = binarize(self.weight)
        if self.bias is None:
            return F.linear(input, binary_weight)
        else:
            return F.linear(input, binary_weight, self.bias)
modules.py 文件源码 项目:pytorch_workplace 作者: DingKe 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def forward(self, input, h):
        output = F.linear(input, self.weight_ih, self.bias) + F.linear(h, self.weight_hh)
        if self.grad_clip:
            output = clip_grad(output, -self.grad_clip, self.grad_clip) # avoid explosive gradient
        output = F.relu(output)

        return output
modules.py 文件源码 项目:pytorch_workplace 作者: DingKe 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def forward(self, input, h):
        ih = F.linear(input, self.weight_ih, self.bias)
        hh = F.linear(h, self.weight_hh)

        if self.grad_clip:
            ih = clip_grad(ih, -self.grad_clip, self.grad_clip)
            hh = clip_grad(hh, -self.grad_clip, self.grad_clip)

        z = F.sigmoid(ih[:, :self.hidden_size] + hh[:, :self.hidden_size])
        n = F.relu(ih[:, self.hidden_size:] + hh[:, self.hidden_size:])
        h = (1 - z) * n + z * h

        return h
utils.py 文件源码 项目:restricted-boltzmann-machine-deep-belief-network-deep-boltzmann-machine-in-pytorch 作者: wmingwei 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def v_to_h(v, W, h_bias):
    # p_h = F.sigmoid(v.mm(self.W.t()) + self.h_bias.repeat(v.size()[0],1))
    p_h = torch.sigmoid(F.linear(v,W,h_bias))
    h = torch.bernoulli(p_h)
    return p_h,h
utils.py 文件源码 项目:restricted-boltzmann-machine-deep-belief-network-deep-boltzmann-machine-in-pytorch 作者: wmingwei 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def h_to_v(h, W, v_bias):
    p_v = torch.sigmoid(F.linear(h,W.t(),v_bias))
    v = torch.bernoulli(p_v)
    return p_v,v
model_DBN.py 文件源码 项目:restricted-boltzmann-machine-deep-belief-network-deep-boltzmann-machine-in-pytorch 作者: wmingwei 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def v_to_h(self,v):

        # p_h = F.sigmoid(v.mm(self.W.t()) + self.h_bias.repeat(v.size()[0],1))
        p_h = F.sigmoid(F.linear(v,self.W,self.h_bias))
        sample_h = self.sample_from_p(p_h)
        return p_h,sample_h
model_DBN.py 文件源码 项目:restricted-boltzmann-machine-deep-belief-network-deep-boltzmann-machine-in-pytorch 作者: wmingwei 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def h_to_v(self,h):
        p_v = F.sigmoid(F.linear(h,self.W.t(),self.v_bias))
        sample_v = self.sample_from_p(p_v)
        return p_v,sample_v
model_DBN.py 文件源码 项目:restricted-boltzmann-machine-deep-belief-network-deep-boltzmann-machine-in-pytorch 作者: wmingwei 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def free_energy(self,v):
        vbias_term = v.mv(self.v_bias)
        wx_b = torch.clamp(F.linear(v,self.W,self.h_bias),-80,80)
        hidden_term = wx_b.exp().add(1).log().sum(1)
        return (-hidden_term - vbias_term).mean()
model_DBM.py 文件源码 项目:restricted-boltzmann-machine-deep-belief-network-deep-boltzmann-machine-in-pytorch 作者: wmingwei 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def v_to_h(self,v):

        # p_h = F.sigmoid(v.mm(self.W.t()) + self.h_bias.repeat(v.size()[0],1))
        p_h = torch.sigmoid(F.linear(v,self.W,self.h_bias))
        sample_h = self.sample_from_p(p_h)
        return p_h,sample_h
model_DBM.py 文件源码 项目:restricted-boltzmann-machine-deep-belief-network-deep-boltzmann-machine-in-pytorch 作者: wmingwei 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def h_to_v(self,h):
        p_v = torch.sigmoid(F.linear(h,self.W.t(),self.v_bias))
        sample_v = self.sample_from_p(p_v)
        return p_v,sample_v
model_DBM.py 文件源码 项目:restricted-boltzmann-machine-deep-belief-network-deep-boltzmann-machine-in-pytorch 作者: wmingwei 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def free_energy(self,v):
        vbias_term = v.mv(self.v_bias)
        wx_b = torch.clamp(F.linear(v,self.W,self.h_bias),-80,80)
        hidden_term = wx_b.exp().add(1).log().sum(1)
        return (-hidden_term - vbias_term).mean()
model_DBM.py 文件源码 项目:restricted-boltzmann-machine-deep-belief-network-deep-boltzmann-machine-in-pytorch 作者: wmingwei 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def odd_to_even(self, odd_input = None):
        even_p_output= []
        even_output= []
        for i in range(self.n_even_layers):
            if i == 0:
                even_p_output.append(torch.sigmoid(F.linear(odd_input[i],self.W[2*i].t(),self.bias[2*i])))
            elif (self.n_even_layers > self.n_odd_layers) and i == self.n_even_layers - 1:
                even_p_output.append(torch.sigmoid(F.linear(odd_input[i-1],self.W[2*i-1],self.bias[2*i])))
            else:
                even_p_output.append(torch.sigmoid(F.linear(odd_input[i-1],self.W[2*i-1],self.bias[2*i]) + F.linear(odd_input[i],self.W[2*i].t())))
        for i in even_p_output:
            even_output.append(torch.bernoulli(i))

        return even_p_output, even_output
model_DBM.py 文件源码 项目:restricted-boltzmann-machine-deep-belief-network-deep-boltzmann-machine-in-pytorch 作者: wmingwei 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def even_to_odd(self, even_input = None):
        odd_p_output = [] 
        odd_output = []
        for i in range(self.n_odd_layers):
            if (self.n_even_layers == self.n_odd_layers) and i == self.n_odd_layers - 1:
                odd_p_output.append(torch.sigmoid(F.linear(even_input[i],self.W[2*i],self.bias[2*i+1])))
            else:
                odd_p_output.append(torch.sigmoid(F.linear(even_input[i],self.W[2*i],self.bias[2*i+1]) + F.linear(even_input[i+1],self.W[2*i+1].t())))

        for i in odd_p_output:
            odd_output.append(torch.bernoulli(i))

        return odd_p_output, odd_output


问题


面经


文章

微信
公众号

扫码关注公众号