Ejemplo n.º 1
0
def add_hypernyms(string: str, p: float = 0.01) -> str:
    """Replace word with a higher-level category.

    A common negative sampling technique involves replacing words
    in a sentence with a word that has the same general meaning, but
    is too general for the context, e.g.:

    "all dogs go to heaven" -> "all quadrupeds go to place"

    The replacement words are drawn from wordnet (wordnet_). For
    words with more than one possible replacement, one is selected using
    ``random.choice``.

    Args:
        string: text
        p: conditional probability of replacing a word

    Returns:
        enriched text

    .. _wordnet: https://wordnet.princeton.edu/
    """
    wn = _get_wordnet()
    words = [wn.lemmatize(w) for w in string.split()]
    for index, word in enumerate(words):
        if (word in HYPERNYMS) and random.binomial(1, p):
            words[index] = random.choice(HYPERNYMS[word])
    return " ".join(words)
Ejemplo n.º 2
0
    def __init__(self, seqs, pred_dict, use_best_site=True):
        """
        :param seqs: list[str]
        :param pred_dict: dict
        :return:
        """
        super(GetScore, self).__init__()
        width = len(next(pred_dict.iterkeys()))

        self.ind = []
        self.dists = []
        for seq in seqs:
            contigs = Subset(seq, width)
            contigs.extend(RC(contigs))

            scores = [pred_dict[key] for key in contigs]  # len(key) == width
            max_score = max(scores)
            if use_best_site:
                self.append(max_score)
            else:
                self.append(np.mean(scores))

            ind = [i for i, x in enumerate(scores) if x == max_score]
            idx = random.choice(ind)
            self.ind.append(idx)

            n = len(scores) / 2  # binding sites positions #
            self.dists.append(idx - n / 2 if idx < n else idx - n - n / 2)
Ejemplo n.º 3
0
def add_synonyms(string: str, p: float = 0.01) -> str:
    """Replace word with one that has a close meaning.

    A common data augmentation technique involves replacing words
    in a sentence with a word that has the same general meaning
    (arxiv:1509.01626_), e.g.:

    "all dogs go to heaven" -> "all domestic dog depart to heaven"

    The replacement words are drawn from wordnet (wordnet_). For
    words with more than one possible replacement, one is selected using
    ``random.choice``.

    Args:
        string: text
        p: conditional probability of replacing a word

    Returns:
        enriched text

    .. _arxiv:1509.01626 : https://arxiv.org/abs/1509.01626
    .. _wordnet: https://wordnet.princeton.edu/
    """
    wn = _get_wordnet()
    words = [wn.lemmatize(w) for w in string.split()]
    for index, word in enumerate(words):
        if (word in SYNONYMS) and random.binomial(1, p):
            words[index] = random.choice(SYNONYMS[word])
    return " ".join(words)
Ejemplo n.º 4
0
Archivo: lstm.py Proyecto: Yevgnen/LSTM
    def sampling(self, x, len):
        cell = self.create_cell()
        sample = []
        for t in range(len):
            cell = self.forward_time(x, cell)
            x = sprd.choice(range(self.vocab_size), p=cell[-1].y)
            sample.append(x)

        return sample
Ejemplo n.º 5
0
Archivo: lstm.py Proyecto: Yevgnen/LSTM
    def sampling(self, x, len):
        cell = self.create_cell()
        sample = []
        for t in range(len):
            cell = self.forward_time(x, cell)
            x = sprd.choice(range(self.vocab_size), p=cell[-1].y)
            sample.append(x)

        return sample
Ejemplo n.º 6
0
    def draw(self):
        """
        Draws the best arm with probability (1 - epsilon)
        Draws any arm at random with probility epsilon

        :return: The numerical index of the selected arm
        """
        if random.rand() < self.epsilon:
            return random.choice(self.n_arms)
        else:
            return argmax(self._metric_fn())
Ejemplo n.º 7
0
    def draw(self):
        """
        Draws the best arm with probability (1 - epsilon * temp)
        Draws any arm with probability epsilon * temp

        :return: The numerical index of the selected arm
        """
        temp = self._schedule_fn(self.total_draws)
        if random.rand() < self.epsilon * temp:
            return random.choice(self.n_arms)
        else:
            return argmax(self._metric_fn())
Ejemplo n.º 8
0
    def _forwardImplementation(self, inbuf, outbuf):
        """ Draws a random number between 0 and 1. If the number is less
            than epsilon, a random action is chosen. If it is equal or
            larger than epsilon, the greedy action is returned.
        """
        assert self.module

        if random.random() < self.epsilon:
            legalMoves = self.module.legalMoves.nonzero()[0]
            outbuf[:] = array([random.choice(legalMoves)])
        else:
            outbuf[:] = inbuf

        self.epsilon *= self.decay
Ejemplo n.º 9
0
def split_online_offline(dataset, online_ratio):
    online_size = int(len(dataset) * online_ratio)
    online_inds = random.choice(list(range(len(dataset))),
                                online_size,
                                replace=False)
    online_mask = np.zeros(len(dataset))
    online_mask[online_inds] = 1

    offline_mask = np.ones(len(dataset))
    offline_mask[online_inds] = 0.0

    dataset = np.asarray(dataset)
    online_dataset = dataset[online_mask == 1.0]
    offline_dataset = dataset[offline_mask == 1.0]

    return online_dataset, offline_dataset
Ejemplo n.º 10
0
    def _forwardImplementation(self, inbuf, outbuf):
        """ Draws a random number between 0 and 1. If the number is less
            than epsilon, a random action is chosen. If it is equal or
            larger than epsilon, the greedy action is returned.
        """
        assert self.module

        if random.random() < self.epsilon:
            #only the actions that have a Q value > -infinity are valid
            actionValues = self.module.getActionValues(self.state)
            #print(actionValues)
            actions = [a for a in xrange(len(actionValues)) if actionValues[a] > float("-inf")]
            outbuf[:] = random.choice(actions)
        else:
            outbuf[:] = self.module.getMaxAction(self.state)

        self.epsilon *= self.decay
Ejemplo n.º 11
0
def add_characters(string: str, p: float = 0.01) -> str:
    """Insert individual characters with probability p.

    These are chosen randomly from the ascii alphabet (including
    both upper and lower cases).

    Args:
        string: text
        p: probability of removing a character

    Returns:
        enriched text
    """
    for index in reversed(range(len(string))):
        if random.binomial(1, p):
            new_char = random.choice(list(ascii_letters))
            string = string[:index] + new_char + string[index:]
    return string
Ejemplo n.º 12
0
 def sorted(self, size=None, step=1, reverse=True, rand=False):
     """
     :param size: int | None
     :param step: int
     :param reverse: bool
     :param rand: bool
     :return: list[Peak]
     """
     if rand:
         out = random.choice(self, size=size, replace=False)
     else:
         sort_pks = sorted(self, reverse=reverse)
         if size is None:
             stop = None
         else:
             stop = step * size
             assert stop <= len(self)
         out = sort_pks[0:stop:step]
     return Peaks(out)
Ejemplo n.º 13
0
    def _sample_angles(self,N):
        if (self.verbose): print "   sample angles ..."
        R = random.rand(N)

        if self.ani:
            # Anisotropic systems sample angles: cdf(q) = erfi(a*q)/erfi(a), a = sqrt(k)*p
            p = self.r/self.ra
            a = p*sqrt(self.k)
            if self.mod.multi:
                a *= sqrt(self.mod.s2/self.s2j)
            self.q = numpy.zeros(N)

            for j in range(N):
                self.q[j] = optimize.brentq(self._pdf_angle, 0, 1, args=(a[j], R[j]))
        else:
            # Isotropic: cdf(q) = q
            self.q = R

        self.vr = self.v*self.q*random.choice((-1,1),size=self.N)
        self.vt = self.v*sqrt(1-self.q**2)
Ejemplo n.º 14
0
def add_fat_thumbs(string: str, p: float = 0.01) -> str:
    """Replace characters with QWERTY neighbors.

    One source of typographic mistakes comes from pressing a nearby key
    on a keyboard (or on a touchscreen). With probability p, replace each
    character is a string with one from a set of its neighbors. The
    replacement is chosen using ``random.choice``.

    Args:
        string: text
        p: probability of replacing a character

    Returns:
        enriched text
    """
    for index, char in enumerate(string):
        if char in NEIGHBORS and random.binomial(1, p):
            new_char = random.choice(NEIGHBORS[char])
            string = string[:index] + new_char + string[index + 1:]
    return string
Ejemplo n.º 15
0
    def _forwardImplementation(self, inbuf, outbuf):
        """ Draws a random number between 0 and 1. If the number is less
            than epsilon, a random action is chosen. If it is equal or
            larger than epsilon, the greedy action is returned.
        """
        assert self.module

        if random.random() < self.epsilon:
            #only the actions that have a Q value > -infinity are valid
            actionValues = self.module.getActionValues(self.state)
            #print(actionValues)
            actions = [
                a for a in xrange(len(actionValues))
                if actionValues[a] > float("-inf")
            ]
            outbuf[:] = random.choice(actions)
        else:
            outbuf[:] = self.module.getMaxAction(self.state)

        self.epsilon *= self.decay
Ejemplo n.º 16
0
    def _forwardImplementation(self, inbuf, outbuf):
        """ Draws a random number between 0 and 1. If the number is less
            than epsilon, a random action is chosen. If it is equal or
            larger than epsilon, the greedy action is returned.
        """
        assert self.module

        # Choose action from allowed actions.
        if random.random() < self.epsilon:

            # Only select allowed actions
            allowed_actions = np.where(
                np.array(self.env.visited_states) == 0)[0]
            act = array([random.choice(allowed_actions)])
            outbuf[:] = act

        else:
            outbuf[:] = inbuf

        self.epsilon *= self.decay
Ejemplo n.º 17
0
    def _forwardImplementation(self, inbuf, outbuf):
        """ Draws a random number between 0 and 1. If the number is less
            than epsilon, a random action is chosen. If it is equal or
            larger than epsilon, the greedy action is returned.
        """
        assert self.module

        #print np.nonzero(self.env.actions_index[self.env.current_state])[0]

        # Choose action from allowed actions.
        if random.random() < self.epsilon:
            act = array(
                [random.choice(self.env.mods[self.env.current_state]) - 1])
            outbuf[:] = act
            #print "current_state", self.env.current_state, "exploration action", act
            #print "allowed actions", self.env.mods[self.env.current_state]
            #outbuf[:] = array([random.randint(self.module.numActions)])
        else:
            outbuf[:] = inbuf

        self.epsilon *= self.decay
Ejemplo n.º 18
0
def add_misspelling(string: str, p: float = 0.1) -> str:
    """Replace words with common misspellings.

    Replaces a word with a common way that word is mispelled, given one or
    more known, common misspellings taken from the Wikipedia spelling
    correction corpus (wikipedia_). For words with more than one common
    misspelling, one is chosen using ``random.choice``.

    Args:
        string: text
        p: conditional probability of replacing a word

    Returns:
        enriched text

    .. _wikipedia: https://en.wikipedia.org/wiki/Wikipedia:Lists_of_common_misspellings
    """
    words = string.split()
    for index, word in enumerate(words):
        if (word in MISSPELLINGS) and random.binomial(1, p):
            words[index] = random.choice(MISSPELLINGS[word])
    return " ".join(words)
Ejemplo n.º 19
0
 def _createorderArray(totalCount, selectedCount):
     ordering  = random.choice(list(range(1,int(totalCount+1))) , selectedCount)
     return ordering