Exemplo n.º 1
0
def analogy(pos1, neg1, pos2, neg2, word2idx, idx2word, W, is_printing, max_distance):
    V, D = W.shape

    # don't actually use pos2 in calculation, just print what's expected

    for w in (pos1, neg1, pos2, neg2):
        if w not in word2idx:
            print("Sorry, %s not in word2idx" % w)
            return max_distance

    p1 = W[word2idx[pos1]]
    n1 = W[word2idx[neg1]]
    p2 = W[word2idx[pos2]]
    n2 = W[word2idx[neg2]]

    vec = p1 - n1 + n2
    distances = pairwise_distances(vec.reshape(1, D), W, metric='cosine').reshape(V)
    idx = distances.argsort()[:10]
    # pick one that's not p1, n1, or n2
    best_idx = -1
    keep_out = [word2idx[w] for w in (pos1, neg1, neg2)]
    for i in idx:
        if i not in keep_out:
            best_idx = i
            break
    if is_printing:
        print("got: %s - %s = %s - %s" % (pos1, neg1, idx2word[idx[0]], neg2))
        print("closest 10:")
        for i in idx:
            print(idx2word[i], distances[i])

        print("dist to %s:" % pos2, cos_dist(p2, vec))
    print("testing: %s - %s = %s - %s: distance = %s " % (pos1, neg1, pos2, neg2, abs(1 - cos_dist(p2, vec))))
    return abs(1 - cos_dist(p2, vec))
Exemplo n.º 2
0
def assign_bert_mentions(entities, doc, label_prefix = 'NER', \
		max_dist = 0.10, add_unlinked_entities = True):
	for t in 'io':
		valid_entities = [e for e in entities if e.type == t]
		valid_mentions = get_doc_spans(doc, label_prefix + '_'+ t)
		valid_mentions = [m for m in valid_mentions if m.text and not m.text.isspace()]
		if not valid_mentions:
			print('Warning! No valid mentions for {} ({})'.format(doc.id, t))
			continue
		try:
			entity_embs = list(encode([e.text for e in valid_entities]))
			mention_embs = encode([m.text for m in valid_mentions])
		except ValueError:
			print(doc.id)
			print(t)
			print(valid_mentions)
			raise
		for m, m_emb in zip(valid_mentions, mention_embs):
			dists = [cos_dist(m_emb, e_emb) for e_emb in entity_embs]
			# explicitly sort on first element - builtin comparator breaks when dists are tied!
			sorted_dists = sorted(zip(dists, valid_entities), key = itemgetter(0))
			# require a minimum similarity between mention and entity
			if sorted_dists[0][0] <= max_dist:
				sorted_dists[0][1].mentions.append(m)
			else:
				if add_unlinked_entities:
					# ooohhh sheeeeeit create a new entity
					unlinked_e = classes.Entity(m, t)
					unlinked_e.mentions.append(m)
					entities.append(unlinked_e)
					valid_entities.append(unlinked_e)
					entity_embs.append(encode([unlinked_e.text])[0])
def analogy(pos1, neg1, pos2, neg2):
    # don't actually use pos2 in calculation, just print what's expected
    print("testing: %s - %s = %s - %s" % (pos1, neg1, pos2, neg2))
    for w in (pos1, neg1, pos2, neg2):
        if w not in word2idx:
            print("Sorry, %s not in word2idx" % w)
            return

    p1 = W[word2idx[pos1]]
    n1 = W[word2idx[neg1]]
    p2 = W[word2idx[pos2]]
    n2 = W[word2idx[neg2]]

    vec = p1 - n1 + n2

    distances = pairwise_distances(vec.reshape(1, D), W,
                                   metric='cosine').reshape(V)
    idx = distances.argsort()[:10]

    # pick the best that's not p1, n1, or n2
    best_idx = -1
    keep_out = [word2idx[w] for w in (pos1, neg1, neg2)]
    for i in idx:
        if i not in keep_out:
            best_idx = i
            break

    print("got: %s - %s = %s - %s" % (pos1, neg1, top_words[best_idx], neg2))
    print("closest 10:")
    for i in idx:
        print(top_words[i], distances[i])

    print("dist to %s:" % pos2, cos_dist(p2, vec))
Exemplo n.º 4
0
def analogy(p1, n1, p2, n2, word2idx, idx2word, WordEmbeddings):
    V, D = WordEmbeddings.shape
    for w in (p1, n1, p2, n2):
        if w not in word2idx:
            print("%s not found in word2idx" % w)
            return

    print("testing %s - %s = %s - %s" % (p1, n1, p2, n2))
    vec1 = WordEmbeddings[word2idx[p1]]
    vec2 = WordEmbeddings[word2idx[n2]]
    vec3 = WordEmbeddings[word2idx[p2]]
    vec4 = WordEmbeddings[word2idx[n2]]

    vec = vec1 - vec2 + vec4

    closest_neighbours = pairwise_distances(vec.reshape(1, D),
                                            WordEmbeddings,
                                            metric='cosine').reshape(V)
    top_ten = closest_neighbours.argsort()[:10]

    best_pick = -1
    keep_out = [word2idx[w] for w in (p1, n1, n2)]

    for idx in top_ten:
        if idx not in keep_out:
            best_pick = idx
            break

    print("got %s - %s = %s - %s" % (p1, n1, idx2word[best_pick], n2))

    print("distance to %s", p2, cos_dist(vec3, vec))
Exemplo n.º 5
0
def analogy(pos1, neg1, pos2, neg2):
  # don't actually use pos2 in calculation, just print what's expected
  print("testing: %s - %s = %s - %s" % (pos1, neg1, pos2, neg2))
  for w in (pos1, neg1, pos2, neg2):
    if w not in word2idx:
      print("Sorry, %s not in word2idx" % w)
      return

  p1 = W[word2idx[pos1]]
  n1 = W[word2idx[neg1]]
  p2 = W[word2idx[pos2]]
  n2 = W[word2idx[neg2]]

  vec = p1 - n1 + n2

  distances = pairwise_distances(vec.reshape(1, D), W, metric='cosine').reshape(V)
  idx = distances.argsort()[:10]

  # pick the best that's not p1, n1, or n2
  best_idx = -1
  keep_out = [word2idx[w] for w in (pos1, neg1, neg2)]
  for i in idx:
    if i not in keep_out:
      best_idx = i
      break

  print("got: %s - %s = %s - %s" % (pos1, neg1, top_words[best_idx], neg2))
  print("closest 10:")
  for i in idx:
    print(top_words[i], distances[i])

  print("dist to %s:" % pos2, cos_dist(p2, vec))
Exemplo n.º 6
0
def analogy(pos1, neg1, pos2, neg2, word2idx, idx2word, W):
    V, D = W.shape

    print("testing: %s - %s = %s - %s" % (pos1, neg1, pos2, neg2))
    for w in (pos1, neg1, pos2, neg2):
        if w not in word2idx:
            print("sorry, %s not in word2idx" % w)
            return

    p1 = W[word2idx[pos1]]
    n1 = W[word2idx[neg1]]
    p2 = W[word2idx[pos2]]
    n2 = W[word2idx[neg2]]

    vec = p1 - n1 + n2

    distances = pairwise_distances(vec.reshape(1, D), W, metric='cosine').reshape(V)
    idx = distances.argsort()[:10]

    best_idx = -1
    keep_out = [word2idx[w] for w in (pos1, neg1, neg2)]

    for i in idx:
        if i not in keep_out:
            best_idx = i
            break
    print("got: %s - %s = %s - %s" % (pos1, neg1, idx2word[best_idx], neg2))
    print('closest 10:')
    for i in idx:
        print(idx2word[i], distances[i])

    print('dist to %s:' % pos2, cos_dist(p2, vec))
Exemplo n.º 7
0
def cos_predict(cut_text, match_sents, match_labels, thre):
    vec = bow.transform([cut_text])
    vec = svd.transform(vec)
    match_texts, dists = list(), list()
    for sent_ind, label in zip(match_sents, match_labels):
        match_texts.append(texts[sent_ind])
        match_vec = sent_vec[sent_ind]
        dists.append(cos_dist(vec, match_vec))
    return sort(dists, match_texts, match_labels, thre, cand=5)
Exemplo n.º 8
0
def weights_true_cossim(estr, X, Y, Xorig, Yorig, x_align_ref, y_align_ref,
                        results, **kwargs):
    """Calculates cosine-distance between estimated and true weights.

    Provides outcome metrics ``x_weights_true_cossim`` and
    ``y_weights_true_cossim``

    Parameters
    ----------
    estr : **sklearn**-style estimator
        fitted estimator
    X : np.ndarray (n_samples, n_features)
        dataset `X`
    Y : np.ndarray (n_samples, n_features)
        dataset `Y`
    Xorig : ``None`` or np.ndarray (n_samples, n_orig_features)
        if ``None`` set to ``X``. Allows to provide an alternative set of `X`
        features for calculating loadings. I.e. an implicit assumption is that
        the rows in ``X`` and ``Xorig`` correspond to the same samples
        (subjects).
    Yorig : ``None`` or np.ndarray (n_samples, n_orig_features)
        if ``None`` set to ``Y``. Allows to provide an alternative set of `Y`
        features for calculating loadings. I.e. an implicit assumption is that
        the rows in ``Y`` and ``Yorig`` correspond to the same samples
        (subjects).
    x_align_ref : (n_features,)
        the sign of `X` weights is chosen such that the cosine-distance between
        fitted `X` weights and ``x_align_ref`` is positive
    y_align_ref : (n_features,)
        the sign of `Y` weights is chosen such that the cosine-distance between
        fitted `Y` weights and ``y_align_ref`` is positive
    results : xr.Dataset
        containing outcome features computed so far, and is modified with
        outcomes of this function
    kwargs : dict
        keyword arguments
    """
    results['x_weights_true_cossim'] = 1 - cos_dist(
        results['x_weights'].values, x_align_ref)
    results['y_weights_true_cossim'] = 1 - cos_dist(
        results['y_weights'].values, y_align_ref)
Exemplo n.º 9
0
 def get_weighted_nn(self, word):
     nn_w_dists = {}
     if word in self.word_vectors:
         all_vectors = (self.word_vectors, )
         for clue in self.cm_cluelist:
             b_dist = cos_dist(self.concatenate(clue, all_vectors),
                               self.concatenate(word, all_vectors))
             nn_w_dists[clue] = b_dist
     else:
         if self.configuration.verbose:
             print(word, "not in word_vectors")
     self.word_dists[word] = nn_w_dists
     return {k: 1 - v for k, v in nn_w_dists.items()}
Exemplo n.º 10
0
    def get_similarities(self, data):
        edge_list = []
        for i, node in enumerate(data):
            for j, other_node in enumerate(data):
                best_node_dist = (None, 1)
                qualifying_edges = []
                if i != j:
                    dist = cos_dist(node[self.qa:], other_node[:self.qa])
                    if dist < self.threshold:
                        qualifying_edges.append((i, j, dist))
                        self.distances.append(dist)

        return edge_list
Exemplo n.º 11
0
    def step(self, a):
        posbefore = np.copy(self.get_body_com("torso")[:2])
        self.do_simulation(a, self.frame_skip)
        posafter = self.get_body_com("torso")[:2]

        self.ant_pos_before = posbefore.copy()
        self.ant_pos_after = posafter.copy()

        # original
        # forward_reward = np.sum(self.goal_direction * (posafter - posbefore))/self.dt
        # ctrl_cost = .5 * np.square(a).sum()
        # contact_cost = 0.5 * 1e-3 * np.sum(
        #     np.square(np.clip(self.sim.data.cfrc_ext, -1, 1)))
        # survive_reward = 1.0

        # # new try v1: cosine similarlity
        # forward_reward = 1.0 - cos_dist(self.goal_direction, posafter - posbefore)
        # ctrl_cost = 0.0
        # contact_cost = 0.0
        # survive_reward = 0.0

        # new try v2: just region clipped forward cost
        forward_reward = np.sum(self.goal_direction *
                                (posafter - posbefore)) / self.dt
        ctrl_cost = 0.0
        contact_cost = 0.0
        survive_reward = 0.0

        # clipping based on region
        # for this we also have to clip the rewards from below by 0 so that the agent won't try to do weird stuff
        # clip min by zero
        forward_reward = max(forward_reward, 0.0)
        # clip by region
        if 1.0 - cos_dist(self.goal_direction, posafter) < 0.96:
            forward_reward = 0.0

        reward = forward_reward - ctrl_cost - contact_cost + survive_reward
        state = self.state_vector()
        notdone = np.isfinite(state).all() and 1.0 >= state[2] >= 0.
        done = not notdone
        ob = self._get_obs()
        return ob, reward, done, dict(
            reward_forward=forward_reward,
            reward_ctrl=-ctrl_cost,
            reward_contact=-contact_cost,
            reward_survive=survive_reward,
            goal_direction=self.goal_direction.copy(),
            projected_dist=np.sum(posafter * self.goal_direction),
            debug_target_dist=np.linalg.norm(posafter - self.goal_direction))
Exemplo n.º 12
0
def assign_best_mention(entities, doc):
	mentions = { \
			'i': [m for m in get_doc_spans(doc, 'NER_i') if m.text and not m.text.isspace()],
			'o': [m for m in get_doc_spans(doc, 'NER_o') if m.text and not m.text.isspace()]
	}
	embs = { \
			'i': encode([m.text for m in mentions['i']]),
			'o': encode([m.text for m in mentions['o']])
	}
	for e in entities:
		e_emb = encode([e.text])[0]
		dists = [cos_dist(e_emb, m_emb) for m_emb in embs[e.type]]
		sorted_dists = sorted(zip(dists, mentions[e.type]), key = itemgetter(0))
		if sorted_dists[0][0] >= 0.15:
			continue
		e.mentions = [sorted_dists[0][1]]
 def simC(self, user_i, user_j, vector_type):
     '''
     :param user_i:
     :param user_j:
     :return: simC
     '''
     if vector_type == 'svd':
         ui = self.vectorize_users(user_i)
         uj = self.vectorize_users(user_j)
         sim = 1 - cos_dist(ui, uj)
         return sim
     elif vector_type == 'chen':
         num = 0
         sum1 = 0
         sum2 = 0
         for it in self.items:
             num += self.R[it - 1001][user_i - 1] * self.R[it -
                                                           1001][user_j - 1]
             sum1 += np.power(self.R[it - 1001][user_i - 1], 2)
             sum2 += np.power(self.R[it - 1001][user_j - 1], 2)
         den = np.sqrt(sum1) * np.sqrt(sum2)
         return num / den
Exemplo n.º 14
0
    def analogy(self, pos1, neg1, pos2, neg2):

        N, D = self.W.shape
        assert (N == self.vocab_size)

        # don't actually use pos2 in calculation, just print what's expected
        print("testing: %s - %s = %s - %s" % (pos1, neg1, pos2, neg2))
        for w in (pos1, neg1, pos2, neg2):
            if w not in self.word2idx:
                print("Sorry, %s not in pre-trained word2idx" % w)
                return

        p1_w2v = self.wv[self.word2idx[pos1]]
        n1_w2v = self.wv[self.word2idx[neg1]]
        p2_w2v = self.wv[self.word2idx[pos2]]
        n2_w2v = self.wv[self.word2idx[neg2]]

        w2v = p1_w2v - n1_w2v + n2_w2v  # (D,)

        distances = pairwise_distances(w2v.reshape(1, D),
                                       self.W,
                                       metric='cosine').reshape(N)
        idx = distances.argsort()[:10]  # smaller distance, more similar

        # pick one that's not p1, n1, or n2
        best_idx = -1
        idx_not_use = [self.word2idx[w] for w in (pos1, neg1, neg2)]
        for i in idx:
            if i not in idx_not_use:
                best_idx = i
                break

        print("analogy results: %s - %s = %s - %s" %
              (pos1, neg1, self.idx2word[best_idx], neg2))
        print("closest top 10:")
        for i in idx:
            print(self.idx2word[i], distances[i])

        print("cosine distance to %s:" % pos2, cos_dist(p2_w2v, w2v))
Exemplo n.º 15
0
def analogy(pos1, neg1, pos2, neg2, word2index, index2word, W):
    V, D = W.shape

    # do not actually use pos2 in calculation just print what's excepted
    print("testing: %s - %s = %s - %s" % (pos1, neg1, pos2, neg2))
    for w in (pos1, neg1, pos2, neg2):
        if w not in word2index:
            print("Sorry, %s not in word2index" % w)
            return
    p1 = W[word2index[pos1]]
    n1 = W[word2index[neg1]]
    p2 = W[word2index[pos2]]
    n2 = W[word2index[neg2]]

    vec = p1 - n1 + n2

    distances = pairwise_distances(vec.reshape(1, D), W,
                                   metric='cosine').reshape(V)
    index = distances.argsort()[:10]

    # pick one that's not p1, n1 or n2
    best_index = -1
    keep_out = [word2index[w] for w in (pos1, neg1, neg2)]
    print("keep out:", keep_out)
    for i in index:
        if i not in keep_out:
            best_index = i
            break
    print("best index:", best_index)

    print("got: %s - %s = %s - %s" %
          (pos1, neg1, index2word[best_index], neg2))
    print("closest 10:")
    for i in index:
        print(index2word[i], distances[i])

    print("dist to %s:" % pos2, cos_dist(p2, vec))
    print("\n")
Exemplo n.º 16
0
        ("yc, sn + tn", yc, sn + tn),
        ("yc, sh + th", yc, sh + th),
        ("yv + yc, sn", yv + yc, sn),
        ("yv + yc, sh", yv + yc, sh),
        ("yv + yc, tn", yv + yc, tn),
        ("yv + yc, th", yv + yc, th),
        ("yv + yc, sn + sh", yv + yc, sn + sh),
        ("yv + yc, tn + th", yv + yc, tn + th),
        ("yv + yc, sn + tn", yv + yc, sn + tn),
        ("yv + yc, sh + th", yv + yc, sh + th),
    ]

    scores = np.zeros((4, len(pairs)))
    for i, (s, x, y) in enumerate(pairs):
        scores[0][i] = norm_dist(x, y)
        scores[2][i] = cos_dist(x, y)

    scores[1] = scores[0] / max(scores[0])
    scores[3] = scores[2] / max(scores[2])

    print("Distances between normalized vectors")
    for count, i in enumerate(np.argsort(scores[3]), 1):
        print(f"{pairs[i][0]} ({count}/{len(pairs)})")
        print("norm\t{0:.4f}\t{1:.4f}".format(scores[0][i], scores[1][i]))
        print("cos\t{0:.4f}\t{1:.4f}\n".format(scores[2][i], scores[3][i]))

    print(
        "Ready! Go to http://projector.tensorflow.org/ and upload the files in the "
        "`projector` directory to visualize the embeddings.\n")

    pearson = pearsonr(scores[1], scores[3])
Exemplo n.º 17
0
def herify(sarsd, dyn_vals, obs_vectorizer, FLAGS):
    """return new sarsd's that came from HER

    NOTE: this requires that sarsd not be flattened yet. a bit hacky
    
    """
    her_sarsd = []
    legit_reached_goals = []
    no_move_reach = []
    moves = []

    for ridx in range(len(sarsd['s'])):
        all_s = sarsd['s'][ridx]
        all_s_next = sarsd['s_next'][ridx]
        phi_s_next = dyn_vals['phi_s_next'][ridx]
        if FLAGS['aac']:
            value_phi_s_next = dyn_vals['value_phi_s_next'][ridx]

        # We are kind of keeping this in format of rollouts
        for k in range(FLAGS['her_k']):
            new_sarsd = {}
            new_sarsd['d'] = np.zeros_like(sarsd['r'][ridx])
            new_sarsd['r'] = np.zeros_like(sarsd['r'][ridx])
            num_tidx = len(new_sarsd['r'])
            new_sarsd['r'] += FLAGS['old_weight'] * sarsd['og_reward'][
                ridx][:num_tidx]

            new_s = obs_vectorizer.to_np_dict(copy.deepcopy(all_s))
            new_s_next = obs_vectorizer.to_np_dict(copy.deepcopy(all_s_next))

            next_obs = obs_vectorizer.to_np_dict(all_s_next)
            next_array = next_obs['array']
            if FLAGS['use_image'] and not FLAGS['use_embed']:
                next_image = next_obs['image']

            if FLAGS['use_embed']:
                new_sarsd['phi_s_pi'] = sarsd['phi_s_pi'][ridx].copy()
                new_sarsd['phi_s_next_pi'] = sarsd['phi_s_next_pi'][ridx].copy(
                )
                new_sarsd['phi_g_pi'] = np.full_like(new_sarsd['phi_s_pi'],
                                                     np.nan)
                if FLAGS['aac']:
                    new_sarsd['phi_s_vf'] = sarsd['phi_s_vf'][ridx].copy()
                    new_sarsd['phi_s_next_vf'] = sarsd['phi_s_next_vf'][
                        ridx].copy()
                    new_sarsd['phi_g_vf'] = np.full_like(
                        new_sarsd['phi_s_vf'], np.nan)

            for tidx in range(num_tidx):
                # sample random new future
                new_idx = np.random.randint(tidx, num_tidx)

                new_goal_array = next_array[new_idx].copy()
                new_s['goal_array'][tidx] = new_goal_array
                new_s_next['goal_array'][tidx] = new_goal_array

                # Replace observations
                if FLAGS['use_embed']:
                    new_sarsd['phi_g_pi'][tidx] = phi_s_next[new_idx].copy()
                    if FLAGS['aac']:
                        new_sarsd['phi_g_vf'][tidx] = value_phi_s_next[
                            new_idx].copy()

                if FLAGS['use_image'] and not FLAGS['use_embed']:
                    new_goal_image = next_image[new_idx].copy()
                    new_s['goal_image'][tidx] = new_goal_image
                    new_s_next['goal_image'][tidx] = new_goal_image

                # Compute replaced rewards
                moved = True
                if FLAGS['penalize_stasis']:
                    # Penalize the agent if it does not move
                    curr_s, curr_s_next = new_s['array'][tidx], new_s_next[
                        'array'][tidx]
                    deltas = (curr_s_next - curr_s)
                    sum_deltas = np.sum(np.abs(deltas))
                    moved = sum_deltas >= FLAGS['stasis_threshold']
                    if moved:
                        new_sarsd['r'][tidx] += FLAGS['stasis_rew']
                    else:
                        new_sarsd['r'][tidx] += -FLAGS['stasis_rew']
                moves.append(1 if moved else 0)

                # Reward is sparse random new goal in future
                if FLAGS['aac']:
                    # compute goal using value (GN) because it is probably a bit better
                    #dgoal = cos_dist(value_phi_s_next[tidx], value_phi_s_next[new_idx])
                    dgoal = cos_dist(
                        dyn_vals['value_reward_phi_s_next'][ridx][tidx],
                        dyn_vals['value_reward_phi_s_next'][ridx][new_idx])
                else:
                    #goal = cos_dist(phi_s_next[tidx], phi_s_next[new_idx])
                    dgoal = cos_dist(
                        dyn_vals['reward_phi_s_next'][ridx][tidx],
                        dyn_vals['reward_phi_s_next'][ridx][new_idx])

                if dgoal < FLAGS['goal_threshold']:
                    if moved:
                        new_sarsd['r'][tidx] += 1.0
                        legit_reached_goals.append(1)
                        no_move_reach.append(0)
                    else:
                        no_move_reach.append(1)
                        legit_reached_goals.append(0)
                    # DONE
                    new_sarsd['d'][tidx] = 1.0  # done if we reach goal
                else:
                    new_sarsd['r'][tidx] += -1.0  # else -1
                    no_move_reach.append(0)
                    legit_reached_goals.append(0)

            # combine new stuff
            if not FLAGS['use_embed']:
                new_sarsd['s'] = obs_vectorizer.to_vecs(
                    [ns for ns in zip(*new_s.values())])
                new_sarsd['s_next'] = obs_vectorizer.to_vecs(
                    [ns_next for ns_next in zip(*new_s_next.values())])
            new_sarsd['a'] = sarsd['a'][ridx]
            her_sarsd.append(new_sarsd)

            # TODO: add all new stuff to a sarsd or rollout
    out_sarsd = {}
    for key in her_sarsd[0].keys():
        out_sarsd[key] = np.concatenate([hs[key] for hs in her_sarsd])
    extra_info = {
        'her_reach_frac': np.mean(legit_reached_goals),
        'her_nomove_reach_frac': np.mean(no_move_reach),
        'her_move_frac': np.mean(moves)
    }

    return out_sarsd, extra_info
Exemplo n.º 18
0
def swap_rewards(rollouts, sarsd, dyn_vals, obs_vectorizer, FLAGS):
    """
    Swap rewards in rollouts for rewards in new_rewards and replace information to make things consistent.
    NOTE: this reduces the lengths of every rollout by 1

    Also does some other reward post-processing

    Args:
        add_old (bool): True to combine the two rewards into a single signal
    """
    new_rewards = [
        np.zeros(rs.shape[0], np.float32) for rs in dyn_vals['phi_s']
    ]

    assert len(sarsd['s']) == len(new_rewards)

    new_sarsd = {
        key: []
        for key in list(sarsd.keys()) + ['og_reward', 'dgoal']
    }

    new_dyn_vals = {key: [] for key in dyn_vals.keys()}
    new_rollouts = []
    reached_goals = []

    for ridx in range(len(sarsd['s'])):
        og_rewards = np.array(sarsd['r'][ridx][:len(new_rewards[ridx])])
        new_rewards[ridx] += FLAGS['old_weight'] * og_rewards

        s_array, s_next_array, goal_array = obs_vectorizer.to_np_dict(
            sarsd['s'][ridx])['array'], obs_vectorizer.to_np_dict(
                sarsd['s_next'][ridx])['array'], obs_vectorizer.to_np_dict(
                    sarsd['s'][ridx])['goal_array']
        if FLAGS['penalize_stasis']:
            # Penalize the agent if it does not move
            deltas = (s_next_array - s_array)
            sum_deltas = np.sum(np.abs(deltas), axis=(-1, -2))
            assert sum_deltas.shape == new_rewards[ridx].shape
            stasis_mask = (sum_deltas < FLAGS['stasis_threshold'])
            move_mask = np.logical_not(stasis_mask)
            new_rewards[ridx][move_mask] += FLAGS['stasis_rew']
            new_rewards[ridx][stasis_mask] += -FLAGS['stasis_rew']

        if FLAGS['goal_conditioned'] and not FLAGS['only_stasis']:
            if FLAGS['aac']:
                # compute rewards using value (GN) based embedding.
                # i think this would help the reward signal be a bit smoother than image
                phi_s = dyn_vals['value_reward_phi_s'][ridx]
                phi_s_next = dyn_vals['value_reward_phi_s_next'][ridx]
                phi_g = dyn_vals['value_reward_phi_g'][ridx]
            else:
                phi_s = dyn_vals['reward_phi_s'][ridx]
                phi_s_next = dyn_vals['reward_phi_s_next'][ridx]
                phi_g = dyn_vals['reward_phi_g'][ridx]

            dgoal = np.array(
                [cos_dist(phi_g[i], phi_s_next[i]) for i in range(len(phi_g))])

            new_sarsd['dgoal'].append(dgoal)
            achieved_goal = dgoal < FLAGS['goal_threshold']
            done_idx = np.argmax(achieved_goal)

            # TODO: really fix this so that we have a consistent reward function.  It should
            # just be a function that takes in certain things so that we can reuse it.

            # Everything before goal is -1 (including if we don't reach goal)
            # at goal, reward = 0.0
            if done_idx == 0:
                if achieved_goal[0] > 0.0:
                    # This means get rid of rollouts if we are already at the goal
                    continue
                else:
                    # This means we never reached it, so all steps get -1
                    new_rewards[ridx][:] += -1.0
                reached_goals.append(0)
            else:
                # This means we reached goal at some point
                # Shorten rollout to end at goal and give rewards out
                new_rewards[ridx] = new_rewards[ridx][:done_idx + 1]
                new_rewards[ridx][:done_idx] += -1.0
                # Test here to only give good reward if they actually moved
                # NOTE: this probably is not required since I think it is impossible to get here without HER, but at least this makes things consisten
                if new_rewards[ridx][done_idx] > 0.0:
                    new_rewards[ridx][done_idx] += 1.0
                reached_goals.append(1)

            sarsd['r'][ridx] = new_rewards[ridx]

        new_len = len(new_rewards[ridx])

        # store new rewards in sarsd
        sarsd['r'][ridx] = new_rewards[ridx]

        # Adapt length of sarsd and dyn vals so they will match up downstream
        for key in sarsd:
            new_sarsd[key].append(sarsd[key][ridx][:new_len])
        new_sarsd['og_reward'].append(og_rewards)
        for key in new_dyn_vals:
            new_dyn_vals[key].append(dyn_vals[key][ridx][:new_len])

        # Fix the shape of rollouts
        rollouts[ridx].rewards = new_rewards[ridx]
        # correct the length of the other settings so anyrl.rollouts doesn't bark at us
        rollouts[ridx].infos = rollouts[ridx].infos[:new_len]
        rollouts[ridx].model_outs = rollouts[ridx].model_outs[:new_len]
        rollouts[ridx].observations = rollouts[ridx].observations[:new_len]
        # reset this because it does not make sense for endless envs
        rollouts[ridx].prev_reward = 0.0
        new_rollouts.append(rollouts[ridx])

    return new_rollouts, new_sarsd, new_dyn_vals, {
        'reached_frac': np.mean(reached_goals)
    }
#   if dist < min_dist:
#     closest = i
#     min_dist = dist

# set word embedding matrix
# W = (W + U) / 2

distances = pairwise_distances(vec.reshape(1, D), W,
                               metric='cosine').reshape(V)
idx = distances.argsort()[:10]

print("closest 10:")
for i in idx:
    print(top_words[i], distances[i])

print("dist to queen:", cos_dist(W[word2idx['queen']], vec))


def analogy(pos1, neg1, pos2, neg2):
    # don't actually use pos2 in calculation, just print what's expected
    print("testing: %s - %s = %s - %s" % (pos1, neg1, pos2, neg2))
    for w in (pos1, neg1, pos2, neg2):
        if w not in word2idx:
            print("Sorry, %s not in word2idx" % w)
            return

    p1 = W[word2idx[pos1]]
    n1 = W[word2idx[neg1]]
    p2 = W[word2idx[pos2]]
    n2 = W[word2idx[neg2]]
Exemplo n.º 20
0
#   dist = cos_dist(W[i], vec)
#   if dist < min_dist:
#     closest = i
#     min_dist = dist

# set word embedding matrix
# W = (W + U) / 2

distances = pairwise_distances(vec.reshape(1, D), W, metric='cosine').reshape(V)
idx = distances.argsort()[:10]

print("closest 10:")
for i in idx:
  print(top_words[i], distances[i])

print("dist to queen:", cos_dist(W[word2idx['queen']], vec))



def analogy(pos1, neg1, pos2, neg2):
  # don't actually use pos2 in calculation, just print what's expected
  print("testing: %s - %s = %s - %s" % (pos1, neg1, pos2, neg2))
  for w in (pos1, neg1, pos2, neg2):
    if w not in word2idx:
      print("Sorry, %s not in word2idx" % w)
      return

  p1 = W[word2idx[pos1]]
  n1 = W[word2idx[neg1]]
  p2 = W[word2idx[pos2]]
  n2 = W[word2idx[neg2]]
Exemplo n.º 21
0
 def get_similarity(self, w1, w2):
     if w1 not in self.wv or w2 not in self.wv: return -0.5
     sim = 1.0 - cos_dist(self.wv[w1], self.wv[w2])
     return sim