Ejemplo n.º 1
0
    def dycosine(query_vec, question_vec):
        num = dy.transpose(query_vec) * question_vec
        dem1 = dy.sqrt(dy.transpose(query_vec) * query_vec)
        dem2 = dy.sqrt(dy.transpose(question_vec) * question_vec)
        dem = dem1 * dem2

        return dy.cdiv(num, dem)
Ejemplo n.º 2
0
def eval_dict_dataset(dataset, net, shortlist, proj, parsed):
    ranks = []
    num_batches = len(dataset)
    if parsed:
        dim = dataset[0][0].shape[0]
        batch_size = 1
    else:
        dim, batch_size = dataset[0][0].shape
    for batch_num, data in enumerate(dataset):
        if parsed:
            words, definitions, _ = data
        else:
            words, definitions = data
        words = np.reshape(np.transpose(words), (batch_size, dim))
        dy.renew_cg()
        P = dy.parameter(proj)
        if parsed:
            outputs = net.do_parse_tree(definitions)
        else:
            outputs, _ = net(definitions)
        outputs = P * outputs
        normalised_outputs = outputs * dy.cdiv(dy.inputTensor([1]), dy.sqrt(dy.squared_norm(outputs)))
        normalised_outputs = np.reshape(np.transpose(normalised_outputs.npvalue()), (batch_size, dim))
        for output, word in zip(normalised_outputs, words):
            target_similarity = np.dot(word, output)
            similarities = np.dot(shortlist, output)
            rank = (similarities > target_similarity).sum()
            ranks.append(rank)
    total = len(ranks)
    accuracy10 = float(sum(int(r <= 10) for r in ranks))/total
    accuracy100 = float(sum(int(r <= 100) for r in ranks))/total
    return np.median(ranks), accuracy10, accuracy100
Ejemplo n.º 3
0
 def l2_normalize(vector):
     square_sum = dy.sqrt(
         dy.bmax(
             dy.sum_elems(dy.square(vector)),
             np.finfo(float).eps * dy.ones((1))[0],
         ))
     return dy.cdiv(vector, square_sum)
Ejemplo n.º 4
0
  def transduce(self, src: ExpressionSequence) -> ExpressionSequence:
    src = src.as_tensor()

    src_height = src.dim()[0][0]
    src_width = 1
    batch_size = src.dim()[1]

    W = dy.parameter(self.pW)
    b = dy.parameter(self.pb)

    src = dy.reshape(src, (src_height, src_width), batch_size=batch_size) # ((276, 80, 3), 1)
    # convolution and pooling layers
    l1 = (W*src)+b
    output = dy.cdiv(l1,dy.sqrt(dy.squared_norm(l1)))
    return ExpressionSequence(expr_tensor=output)
Ejemplo n.º 5
0
    def eval(self, mini_batch):
        words, pos_tags, chars, langs, signs, masks = mini_batch
        h_out = self.rnn_mlp(mini_batch, False)[-1]
        t_out = dy.transpose(
            dy.reshape(h_out, (h_out.dim()[0][0], h_out.dim()[1])))

        sims = []
        for i in range(len(langs)):
            for j in range(i + 1, len(langs)):
                sims.append(dy.sqrt(dy.squared_distance(t_out[i], t_out[j])))
        sim = dy.esum(sims)
        sim.forward()
        sim_value = sim.value() / len(sims)
        dy.renew_cg()
        return sim_value
Ejemplo n.º 6
0
    def transduce(self, src):
        src = src.as_tensor()

        src_height = src.dim()[0][0]
        src_width = src.dim()[0][1]
        src_channels = 1
        batch_size = src.dim()[1]

        src = dy.reshape(src, (src_height, src_width, src_channels),
                         batch_size=batch_size)  # ((276, 80, 3), 1)
        # print(self.filters1)
        # convolution and pooling layers
        l1 = dy.rectify(
            dy.conv2d(src,
                      dy.parameter(self.filters1),
                      stride=[self.stride[0], self.stride[0]],
                      is_valid=True))
        pool1 = dy.maxpooling2d(l1, (1, 4), (1, 2), is_valid=True)

        l2 = dy.rectify(
            dy.conv2d(pool1,
                      dy.parameter(self.filters2),
                      stride=[self.stride[1], self.stride[1]],
                      is_valid=True))
        pool2 = dy.maxpooling2d(l2, (1, 4), (1, 2), is_valid=True)

        l3 = dy.rectify(
            dy.conv2d(pool2,
                      dy.parameter(self.filters3),
                      stride=[self.stride[2], self.stride[2]],
                      is_valid=True))

        pool3 = dy.kmax_pooling(l3, 1, d=1)
        # print(pool3.dim())
        output = dy.cdiv(pool3, dy.sqrt(dy.squared_norm(pool3)))
        output = dy.reshape(output, (self.num_filters[2], ),
                            batch_size=batch_size)
        # print("my dim: ", output.dim())

        return ExpressionSequence(expr_tensor=output)
Ejemplo n.º 7
0
def cosine_similarity(a, b):
    # FIXME do I really need to do this for scalar division? :(
    norm = dy.cdiv(dy.inputTensor([1]), dy.sqrt(dy.squared_norm(a)))
    return dy.dot_product(a, b) * norm 
Ejemplo n.º 8
0
 def score_constituent(self, constituent):
     w_score = dy.parameter(self.w_score)
     w_score = dy.cdiv(w_score, dy.sqrt(dy.squared_norm(w_score)))
     h = dy.cdiv(constituent.h, dy.sqrt(dy.squared_norm(constituent.h)))
     return dy.dot_product(w_score, h)*self.inv_temp
Ejemplo n.º 9
0
 def l2_normalize(x):
     square_sum = dynet.sqrt(dynet.bmax(dynet.sum_elems(dynet.square(x)), np.finfo(float).eps * dynet.ones((1))[0]))
     return dynet.cdiv(x, square_sum)
Ejemplo n.º 10
0
 def l2_normalize(x):
     epsilon = np.finfo(float).eps * dy.ones(pred.dim()[0])
     norm = dy.sqrt(dy.sum_elems(dy.square(x)))
     sign = dy.cdiv(x, dy.bmax(dy.abs(x), epsilon))
     return dy.cdiv(dy.cmult(sign, dy.bmax(dy.abs(x), epsilon)), dy.bmax(norm, epsilon[0]))