tex: Motoki Noguchi 7-dan
[gostyle.git] / make_train_set.py
blob741df74b54c8faba66af8f0cf2ba0793ff6b4432
1 #!/usr/bin/python
2 import sys
3 from gostyle import print_vector, print_set_to_file, dump_object_to_file, load_object_from_file
4 from gostyle import InputVectorGenerator, PlanarOutputVectorGenerator, StrategyOutputVectorGenerator, PlayerStrategyIdentificator
5 from gostyle import Compose, PCA, Combinator, Rescale
7 from data_about_players import Data
9 if __name__ == '__main__':
10 pickle_filename = 'input_gen.pickle'
11 train_set_filename = 'train_set.data'
12 main_pat_filename = Data.main_pat_filename
13 num_features = 300
15 ### Objects creating input vector when called
16 print >>sys.stderr, "Creating input vector generator from main pat file:", main_pat_filename
17 i = InputVectorGenerator(main_pat_filename, num_features)
19 ### Objects creating output vector when called
20 o = PlanarOutputVectorGenerator(Data.player_vector)
21 ### List of players
22 players = Data.player_vector.keys()
24 #o = StrategyOutputVectorGenerator(Data.strategy_players, valid_strategies=["moyo","territorial"])
25 #o = StrategyOutputVectorGenerator(Data.strategy_players)
26 #players = Data.players_all
28 # Create list of input vectors
29 input_vectors = []
30 for name in players:
31 input_vectors += [i(Data.pat_files_folder + name)]
33 if len(input_vectors) == 0:
34 print >>sys.stderr, "No input vectors."
35 sys.exit()
37 ### PCA example usage
38 # Change this to False, if you do not want to use PCA
39 use_pca = False
40 if use_pca:
41 # Create PCA object, trained on input_vectors
42 print >>sys.stderr, "Running PCA."
43 pca = PCA(input_vectors, reduce=True)
44 # Perform a PCA on input vectors
45 input_vectors = pca.process_list_of_vectors(input_vectors)
46 # Creates a Composed object that first generates an input vector
47 # and then performs a PCA analysis on it.
48 i = Compose(i, pca)
50 ### We now save the InputGenerator to a file for later use.
51 # This is especially feasible when we use the PCA, since thus we may use
52 # the once trained object again.
53 print "Saving the input generator object to file:", pickle_filename
54 dump_object_to_file(i, pickle_filename)
56 # Create list of output vectors
57 output_vectors = [ o(name) for name in players ]
58 # Create list of pairs: [ (input_vec_1, output_vec_1), (input_vec_2, output_vec_2), ... ]
59 data = zip(input_vectors, output_vectors)
60 # And filter out players with no output vector
61 # (since PlanarOutputVectorGenerator returns None if the player does not have defined output vector)
62 # Note: This would not be needed if we set `players = PlanarOutputVectorGenerator.players' in the beggining
63 data = filter(lambda x: x[1] != None, data)
65 if len(data) == 0:
66 print >>sys.stderr, "No data."
67 sys.exit()
69 ### We can enlarge the data set by adding linear combinations of input and output vectors
70 use_lin_combinations = True
71 if use_lin_combinations:
72 data += Combinator().combine(data)
74 # Save the result
75 # Create the neural network train set
76 print >>sys.stderr, "Saving neural network train set to file:", train_set_filename
77 print_set_to_file(data,train_set_filename)
78 print >>sys.stderr, "Printed %d pairs of dimensions (%d,%d)."%(len(data), len(data[0][0]), len(data[0][1]))
80 #for i,o in data:
81 # print_vector(i)
82 # print_vector(o)