Example #1
0
        def _match_when_rows_are_non_empty():
            """Performs matching when the rows of similarity matrix are non empty.

      Returns:
        matches:  int32 tensor indicating the row each column matches to.
      """
            # Matches for each column
            matches = int(np.argmax(similarity_matrix, 0))

            # Deal with matched and unmatched threshold
            if self._matched_threshold is not None:
                # Get logical indices of ignored and unmatched columns as np.int64
                matched_vals = np.max(similarity_matrix, 0)
                below_unmatched_threshold = np.greater(
                    self._unmatched_threshold, matched_vals)
                between_thresholds = np.logical_and(
                    np.greater_equal(matched_vals, self._unmatched_threshold),
                    np.greater(self._matched_threshold, matched_vals))

                if self._negatives_lower_than_unmatched:
                    matches = self._set_values_using_indicator(
                        matches, below_unmatched_threshold, -1)
                    matches = self._set_values_using_indicator(
                        matches, between_thresholds, -2)
                else:
                    matches = self._set_values_using_indicator(
                        matches, below_unmatched_threshold, -2)
                    matches = self._set_values_using_indicator(
                        matches, between_thresholds, -1)

            if self._force_match_for_each_row:
                similarity_matrix_shape = shape_utils.combined_static_and_dynamic_shape(
                    similarity_matrix)
                force_match_column_ids = np.argmax(similarity_matrix, 1)

                force_match_column_indicators = np.one_hot(
                    force_match_column_ids, depth=similarity_matrix_shape[1])

                force_match_row_ids = np.argmax(force_match_column_indicators,
                                                0)

                force_match_column_mask = np.cast(
                    np.max(force_match_column_indicators, 0), np.bool)
                final_matches = np.where(force_match_column_mask,
                                         force_match_row_ids, matches)
                return final_matches
            else:
                return matches
Example #2
0
 def batch_iter(self,gen):
     i=0
     _ids,_tags,_words,_lbs=[],[],[],[]
     while(1):
         _id,_tag,_word,_lb=gen.__next__()
         self._vali_equal(len(_id), len(_word))
         self._vali_equal(len(_id), 200)
         self._vali_equal(len(_tag), self.btsize)
         _ids.extend(_id)
         _tags.extend(_tag)
         _words.extend(_word)
         _lbs.extend(_lb)
         i+=1
         print("\n>counter:",i)
         if i==self.btsize:
             yield self.toArr(_ids,self.btsize,200), self.toArr(np.one_hot(_tags),self.btsize,18)
             #yield self.toArr(_ids,self.btsize,200), self.toArr(np.one_hot(_tags),self.btsize,18), self.toArr(_words,self.btsize,200), self.toArr(_lbs,self.btsize,1)
             _ids,_tags,_words,_lbs=[],[],[],[]
             i=0
Example #3
0
 def next_batch_text_classify_train(self,gen):
     i=0
     _ids,_tags,_words,_lbs=[],[],[],[]
     while(1):
         _id,_tag,_word,_lb=gen.__next__()
         self._vali_equal(len(_id), len(_word))
         self._vali_equal(len(_id), 200)
         self._vali_equal(len(_tag), self.btsize)
         _ids.extend(_id)
         _tags.extend(_tag)
         _words.extend(_word)
         _lbs.extend(_lb)
         i+=1
         print("\n>counter:",i)
         if i==self.btsize:
             yield self.toArr(_ids,self.btsize,200), self.toArr(np.one_hot(_tags),self.btsize,18), self.toArr(_words,self.btsize,200), self.toArr(_lbs,self.btsize,1)
             _ids,_tags,_words,_lbs=[],[],[],[]
             i=0
         #import pdb
         pass#pdb.set_trace()
Example #4
0
 def call(self, inputs):
     one_hot = tf.one_hot(inputs, self.n_tokens)
     return tf.reduce_sum(one_hot, axis=1)[:, 1:]
Example #5
0
max_vocabulary_size = 1000
n_oov_buckets = 100

sample_review_batches = train_set.map(lambda review, label: review)
sample_reviews = np.concatenate(list(
    sample_review_batches.as_numpy_iterator()),
                                axis=0)

text_vectorization = TextVectorization(max_vocabulary_size,
                                       n_oov_buckets,
                                       input_shape=[])
text_vectorization.adapt(sample_reviews)
text_vectorization(X_example)

simple_example = tf.constant([[1, 3, 1, 0, 0], [2, 2, 0, 0, 0]])
print(tf.reduce_sum(tf.one_hot(simple_example, 4), axis=1))


class BagOfWords(tf.keras.layers.Layer):
    def __init__(self, n_tokens, dtype=tf.int32, **kwargs):
        super().__init__(dtype=tf.int32, **kwargs)
        self.n_tokens = n_tokens

    def call(self, inputs):
        one_hot = tf.one_hot(inputs, self.n_tokens)
        return tf.reduce_sum(one_hot, axis=1)[:, 1:]


bag_of_words = BagOfWords(n_tokens=4)
print(bag_of_words(simple_example))
Example #6
0
def play_one_game(model):
    game = Game()
    while True:
        winner = game.put_stone_by_model(model)
        if winner != None:
            #print(winner.value)
            #game.print_board()
            break

    A_x = []
    A_y = []
    for board, choice in zip(game.players.A.board_log,
                             game.players.A.choice_log):
        A_x.append([
            board[game.players.A],
            board[game.players.B],
            np.full([Constant.Board.ROW_SIZE, Constant.Board.COL_SIZE],
                    Constant.Player.A),
        ])

        temp_A_y = np.one_hot(
            [Constant.Board.ROW_SIZE, Constant.Board.COL_SIZE], choice)

        if winner == game.players.A:
            temp_A_y *= Constant.Model.Reward.A_WIN_BONUS
        elif winner == game.players.B:
            temp_A_y *= Constant.Model.Reward.A_LOSE_BONUS
        else:  # draw
            temp_A_y *= Constant.Model.Reward.A_DRAW_BONUS

        temp_A_y += game.get_available_location(board)
        temp_A_y = temp_A_y / temp_A_y.sum()
        A_y.append(temp_A_y)

    B_x = []
    B_y = []
    for board, choice in zip(game.players.B.board_log,
                             game.players.B.choice_log):
        B_x.append([
            board[game.players.A], board[game.players.B],
            np.full([Constant.Board.ROW_SIZE, Constant.Board.COL_SIZE],
                    Constant.Player.B)
        ])

        temp_B_y = np.one_hot(
            [Constant.Board.ROW_SIZE, Constant.Board.COL_SIZE], choice)

        if winner == game.players.A:
            temp_B_y *= Constant.Model.Reward.B_LOSE_BONUS
        elif winner == game.players.B:
            temp_B_y *= Constant.Model.Reward.B_WIN_BONUS
        else:  # draw
            temp_B_y *= Constant.Model.Reward.B_DRAW_BONUS

        temp_B_y += game.get_available_location(board)
        temp_B_y = temp_B_y / temp_B_y.sum()
        B_y.append(temp_B_y)

    x = np.swapaxes(np.swapaxes(np.append(A_x, B_x, axis=0), 1, 2), 2, 3)
    y = np.append(A_y, B_y, axis=0)

    model.fit(x, y, verbose=0)