예제 #1
0
파일: gomoku_ai.py 프로젝트: longern/Gomoku
def naive_ai(board: Board, feature=None):
    if feature is None:
        feature = get_features(board)
    score = lambda x: naive_score(x, board.current_player)
    result = numpy.apply_along_axis(score, 2, feature) + gauss_noise(
        10e-6, board.shape)
    mask = (board.data == 0)
    return numpy.unravel_index((result * mask).argmax(), board.shape)
def main():
    lc_super_data = {}
    for grade in valid_grades:
        lc_super_data[grade] = {
            'training': {
                'data': [],
                'targets': []
            },
            'testing': {
                'data': [],
                'targets': []
            }
        }

    loans = storage.load_from_file(config.StorageFile.LC_LOAN_EXTENDED_DATA)
    feature.print_feature_name_idx(loans[0])
    for loan in loans:
        if need_to_drop(loan):
            continue

        grade = loan['grade'].upper()
        if loan['loanStatus'] in good_status_list:
            target = 1
        elif loan['loanStatus'] in bad_status_list:
            target = 0
        else:
            raise Exception("bad loan status input: " + loan['loanStatus'])

        features = feature.get_features(loan)
        feature.validate(features)

        if random.random() >= 0.1:
            key = 'training'
        else:
            key = 'testing'

        if target == 1:
            if grade == 'A' and random.random() >= 0.301:
                key = 'testing'
            if grade == 'B' and random.random() >= 0.706:
                key = 'testing'
            if grade == 'C' and random.random() >= 0.5:
                key = 'testing'
            if grade == 'D' and random.random() >= 0.8:
                key = 'testing'
            if grade == 'E' and random.random() >= 0.8:
                key = 'testing'

        lc_super_data[grade][key]['data'].append(features)
        lc_super_data[grade][key]['targets'].append(target)

    print_training_data_stats(lc_super_data)
    storage.save_to_file(lc_super_data, config.StorageFile.model_training_file)
    print "Saved model training data to %s" % config.StorageFile.model_training_file
예제 #3
0
파일: gomoku_ai.py 프로젝트: longern/Gomoku
def ai_move(board: Board, feature=None, policy: str = "max"):
    if board.turn == 1:
        return naive_ai(board)
    if feature is None:
        feature = get_features(board)
    score = lambda x: naive_score(x, board.current_player)
    result = model_manager["reinforce"].predict(numpy.array(
        [feature])).reshape(15, 15)
    if policy != "sample":
        result += numpy.apply_along_axis(score, 2, feature)
    result = result * (board.data == 0)
    result /= result.sum()
    if policy == "max":
        choice = numpy.unravel_index(result.argmax(), board.shape)
    elif policy == "sample":
        choice = numpy.unravel_index(
            numpy.random.choice(225, p=result.flatten()), board.shape)
    return choice
    def setup_architecture(self):
        expected_output = []
        feature_vector = []
        count = 0
        for f in os.listdir(self.training_folder):
            if count > 0:
                break
            if '.png' in f:
                feature_vector = feature.get_features('train/'+f)
                f = f.split('.')
                f = f[0]
                theline = linecache.getline('trainLabels.csv', int(f) + 1)
                theline = theline.strip(' ')
                theline = theline.strip('\n')
                theline = theline.split(',')
                theline = theline[1]
                expected_output = self.get_expected_output(theline)
                count += 1

        self.input_layer = layers.InputLayer(len(feature_vector))
        self.get_hidden_layers(self.hidden_layer_sizes)
        self.output_layer = layers.OutputLayer(len(expected_output))
        if len(self.hidden_layer_sizes) == 0:
            self.input_layer.set_architecture(self.output_layer)
        else:
            self.input_layer.set_architecture(self.hidden_layers[0])    
        for i, hidden_layer in enumerate(self.hidden_layers):
            prevLayer = None
            nextLayer = None
            if i == 0:
                prevLayer = self.input_layer
            else:
                prevLayer = self.hidden_layers[i-1]
            if i == len(self.hidden_layers) - 1:
                nextLayer = self.output_layer
            else:
                nextLayer = self.hidden_layers[i+1]        
            hidden_layer.set_architecture(prevLayer, nextLayer)    
        self.output_layer.set_architecture(self.hidden_layers[-1])                
    def train(self):
        count = 0
        for f in os.listdir(self.training_folder):
            count += 1
            if count%100 == 0:
                print count
            if '.png' in f:
                f = f.split('.')
                f = f[0]
                if int(f) < 30000:
                    feature_vector = feature.get_features('train/'+f+'.png')
                    theline = linecache.getline('trainLabels.csv', int(f) + 1)
                    theline = theline.strip(' ')
                    theline = theline.strip('\n')
                    theline = theline.split(',')
                    theline = theline[1]
                    expected_output = self.get_expected_output(theline)

                    self.input_layer.put_values(feature_vector)
                    for hidden_layer in self.hidden_layers:
                        hidden_layer.calc_neuron_vals()
                    self.output_layer.calc_neuron_vals()
                    self.output_layer.put_values(expected_output)
                    self.back_propagate()
    def test(self):
        for f in os.listdir(self.training_folder):
            if '.png' in f:
                f = f.split('.')
                f = f[0]
                if int(f) >= 30000:
                    feature_vector = feature.get_features('train/'+f + '.png')
                    theline = linecache.getline('trainLabels.csv', int(f) + 1)
                    theline = theline.strip(' ')
                    theline = theline.strip('\n')
                    theline = theline.split(',')
                    theline = theline[1]
                    expected_output = self.get_expected_output(theline)

                    self.input_layer.put_values(feature_vector)
                    for hidden_layer in self.hidden_layers:
                        hidden_layer.calc_neuron_vals()
                    self.output_layer.calc_neuron_vals()
                    self.output_layer.put_values(expected_output)
                    output = self.output_layer.get_output()
                    if output == theline:
                        self.p += 1
                    else:
                        self.f += 1    
예제 #7
0
def main():
    lc_super_data = {}
    for grade in valid_grades:
        lc_super_data[grade] = {
            'training': {
                'data': [],
                'targets': []
            },
            'testing': {
                'data': [],
                'targets': []
            }
        }
    train_data = storage.load_from_file(config.StorageFile.model_training_file)
    for grade, lc_data in train_data.iteritems():
        if not grade in valid_grades:
            continue
        for i in xrange(len(lc_data['testing']['targets'])):
            if lc_data['testing']['targets'][i] == 0:
                lc_super_data[grade]['testing']['targets'].append(0)
                lc_super_data[grade]['testing']['data'].append(
                    lc_data['testing']['data'][i])
    print_training_data_stats(lc_super_data)

    loans = storage.load_from_file(config.StorageFile.LC_LOAN_EXTENDED_DATA)
    feature.print_feature_name_idx(loans[0])

    for loan in loans:
        if need_to_drop(loan):
            continue

        grade = loan['grade'].upper()
        if loan['loanStatus'] in good_status_list:
            target = 1
        elif loan['loanStatus'] in bad_status_list:
            target = 0
        else:
            raise Exception("bad loan status input: " + loan['loanStatus'])

        if target == 0:
            raise Exception("Not possible")

        features = feature.get_features(loan)
        feature.validate(features)

        if target == 1:
            if grade == 'A' and random.random() >= 0.1:
                continue
            if grade == 'B' and random.random() >= 0.2:
                continue
            # if grade == 'C' and random.random() >= 0.05:
            #   continue
            # if grade == 'D' and random.random() >= 0.1:
            #   continue
            # if grade == 'E' and random.random() >= 0.1:
            #   continue

        lc_super_data[grade]['testing']['data'].append(features)
        lc_super_data[grade]['testing']['targets'].append(target)

    print_training_data_stats(lc_super_data)
    storage.save_to_file(lc_super_data,
                         config.StorageFile.model_evaluating_file)
    print "Saved model training data to %s" % config.StorageFile.model_evaluating_file
예제 #8
0
def model_handler(features, labels, mode, params, config):
    extractor = tf.make_template(
        'extractor',
        get_model(params),
        create_scope_now_=True,
    )

    feature_fn = get_features(params)
    model_input = features['data']

    predictions = extractor(model_input, params,
                            mode == tf.estimator.ModeKeys.TRAIN)

    if mode == tf.estimator.ModeKeys.TRAIN:
        loss = tf.losses.absolute_difference(labels=labels,
                                             predictions=predictions)

        # some lr tuner, you could use move interesting functions
        def learning_rate_decay_fn(learning_rate, global_step):
            return tf.train.exponential_decay(learning_rate,
                                              global_step,
                                              decay_steps=5000,
                                              decay_rate=0.97)

        train_op = tf.contrib.layers.optimize_loss(
            loss=loss,
            global_step=tf.contrib.framework.get_global_step(),
            learning_rate=params.learning_rate,
            #optimizer=lambda lr: tf.train.MomentumOptimizer(lr, 0.9, use_nesterov=True),
            optimizer='Adam',
            #learning_rate_decay_fn=learning_rate_decay_fn,
            clip_gradients=params.clip_gradients,
            variables=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES))

        specs = dict(
            mode=mode,
            loss=loss,
            train_op=train_op,
        )

    if mode == tf.estimator.ModeKeys.EVAL:
        loss = tf.losses.absolute_difference(labels=labels,
                                             predictions=predictions)
        specs = dict(
            mode=mode,
            loss=loss,
        )

    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {
            'pred': predictions,
            'target': features['target'],
            'cur': features['cur'],
            'future': features['future'],
            'buy': features['buy'],
            'sell': features['sell'],
        }
        specs = dict(
            mode=mode,
            predictions=predictions,
        )
    return tf.estimator.EstimatorSpec(**specs)
import data_loader
import feature
import adaboost
import numpy as np
import pickle
import matplotlib.pyplot as plt
import visulization
import metrics
import classifiers
import cascade

train_x, train_y, test_x, test_y = data_loader.load()

train_f, i_f = feature.get_features(train_x)
test_selection_index = np.concatenate(
    [range(472),
     np.random.choice(19572, 2000, replace=False) + 472])
test_x = test_x[test_selection_index]
test_y = test_y[test_selection_index]

try:
    with open("classifier.pkl", "rb") as f:
        classifier = pickle.load(f)
except:
    classifier = adaboost.train(train_f, train_y, 10)
    with open("classifier.pkl", "wb") as f:
        pickle.dump(classifier, f, protocol=pickle.HIGHEST_PROTOCOL)

# 0.99, 0.4, 0.01
try:
    with open("cascade.pkl", "rb") as f:
예제 #10
0
	def expand(self, board: easyBoard):
		sz = board.board_size

		if self.is_root:
			features = get_features(board)
			result = policy_network.predict(np.array([features])).reshape(15, 15) * (board.data == 0)
			k_select = 5
			k_argmax = self.kth(result, sz, k=k_select)
			#print(k_argmax)

			value_me = []
			value_op = []
			vme_max = -1
			vop_max = -1
			for k_pos in k_argmax:
				if k_pos[0] == -1 and k_pos[1] == -1:
					continue
				vme = self.check_win(board, k_pos, 3 - board.current_player)
				vop = self.check_win(board, k_pos, board.current_player)
				value_me.append(vme)
				value_op.append(vop)
				if vme > vme_max:
					vme_max = vme
				if vop > vop_max:
					vop_max = vop

			if vme_max >= vop_max:
				for i in range(k_select):
					if k_argmax[i][0] == -1 and k_argmax[i][1] == -1:
						continue
					if value_me[i] == vme_max:
						self.childs.append(mcts_node(False, self, tuple(k_argmax[i])))
						if len(self.childs) == 3:
							break
			else:
				for i in range(k_select):
					if k_argmax[i][0] == -1 and k_argmax[i][1] == -1:
						continue
					if value_op[i] == vop_max:
						self.childs.append(mcts_node(False, self, tuple(k_argmax[i])))
						if len(self.childs) == 3:
							break
			#print(k_argmax)
			#for k_pos in k_argmax:
			#	self.childs.append(mcts_node(False, self, tuple(k_pos)))
		else:
			available = np.zeros((sz, sz), int)
			dirs = np.array([(-2, 0), (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1), (2, 0)])
			locations = []
			for i in range(sz):
				for j in range(sz):
					if board.data[i][j] != 0:
						for dir in dirs:
							ask = (i, j) + dir
							if board.is_empty(ask):
								available[tuple(ask)] = 1
			for i in range(sz):
				for j in range(sz):
					if available[i][j] == 1:
						locations.append((i, j))
			random.shuffle(locations)
			for location in locations:
				self.childs.append(mcts_node(False, self, location))