示例#1
0
    def __call__(self, x, soft_labels=False, temperature=None, train=False):
        if self.mlp:
            act = self.mlp_activation
            x_in = act(self.W_mlp * x + self.b_mlp)
        else:
            x_in = x

        logits = self.W*x_in + self.b
        if soft_labels and temperature:
            # calculate the soft labels smoothed with the temperature
            # see Distilling the Knowledge in a Neural Network
            elems = dynet.exp(logits / temperature)
            return dynet.cdiv(elems, dynet.sum_elems(elems))
        if self.act:
            return self.act(logits)
        return logits
示例#2
0
    def __call__(self, x, soft_labels=False, temperature=None):
        if self.mlp:
            W_mlp = dynet.parameter(self.W_mlp)
            b_mlp = dynet.parameter(self.b_mlp)
            act = self.mlp_activation
            x_in = act(W_mlp * x + b_mlp)
        else:
            x_in = x
        # from params to expressions
        W = dynet.parameter(self.W)
        b = dynet.parameter(self.b)

        logits = (W * x_in + b) + dynet.scalarInput(1e-15)
        if soft_labels and temperature:
            # calculate the soft labels smoothed with the temperature
            # see Distilling the Knowledge in a Neural Network
            elems = dynet.exp(logits / temperature)
            return dynet.cdiv(elems, dynet.sum_elems(elems))
        return self.act(logits)
示例#3
0
文件: mnnl.py 项目: bplank/bilstm-aux
    def __call__(self, x, soft_labels=False, temperature=None, train=False):
        if self.mlp:
            W_mlp = dynet.parameter(self.W_mlp)
            b_mlp = dynet.parameter(self.b_mlp)
            act = self.mlp_activation
            x_in = act(W_mlp * x + b_mlp)
        else:
            x_in = x
        # from params to expressions
        W = dynet.parameter(self.W)
        b = dynet.parameter(self.b)

        logits = W*x_in + b
        if soft_labels and temperature:
            # calculate the soft labels smoothed with the temperature
            # see Distilling the Knowledge in a Neural Network
            elems = dynet.exp(logits / temperature)
            return dynet.cdiv(elems, dynet.sum_elems(elems))
        if self.act:
            return self.act(logits)
        return logits
示例#4
0
 def cosine(self, e1, e2):
     return dynet.cdiv(
         dynet.dot_product(e1, e2),
         (dynet.cmult(dynet.squared_norm(e1), dynet.squared_norm(e2))))