def train_model_scenario_2(n, model_fname, opponet_model_fname, alpha=0.1, iterations=5000): alpha0 = alpha decay_rate = 0.01 modelInstance = model.load(opponet_model_fname) W0 = modelInstance['W'] b0 = modelInstance['b'] if model_fname == opponet_model_fname: W = W0 b = b0 else: make_movement_fn = lambda x: model.predict2(W0, b0, x) (W, b) = model.initialize_weights(n) for i in range(0, iterations): if model_fname == opponet_model_fname: make_movement_fn = lambda x: model.predict2(W, b, x) ex = training.make_training_examples(make_movement_fn) X = ex['X'] Y = ex['Y'] # displayTrainingExamples(X, Y) # (dW, db) = model.calcGradients(W, b, X, Y) (dW, db, _) = model.back_propagation(n, W, b, X, Y) alpha = alpha0 / (1 + decay_rate * i) model.update_weights(W, dW, b, db, alpha) if i % 100 == 0: print('iteration ' + str(i)) (aL, _) = model.forward_propagation(W, b, X) cost = model.cost_function(Y, aL) print('cost') print(cost) print('alpha') print(alpha) # if i % 1000 == 0: # is_back_prop_correct = model.checkBackPropagation(n, W, b, X, Y) # if not is_back_prop_correct: # print("BP is not correct") # exit() print('------ end -------') model.save(n, W, b, model_fname)
def spy_on_training_process(model_fname): model_instance = model.load(model_fname) n = model_instance['n'] W = model_instance['W'] b = model_instance['b'] vdW = np.zeros(W.shape) vdb = np.zeros(b.shape) alpha0 = 0.3 beta=0.9 iterations = 100000 decay_rate = 4.0 / iterations pos_trains = 0 x_to_investigate = None for i in range(0, iterations): make_movement_fn = lambda x: model.predict2(W, b, x) ex = make_training_examples(make_movement_fn) X = ex['X'] Y = ex['Y'] x = X[:, 0].reshape(9, 1) if x_to_investigate == None: x_to_investigate = x position_to_investigate = x_to_investigate.reshape(3, 3) if (x == x_to_investigate).all(): position.print_position(position_to_investigate) (_, _, aLbefore) = model.predict3(W, b, x_to_investigate) (dW, db, _) = model.back_propagation(n, W, b, X, Y) alpha = alpha0 / (1.0 + decay_rate * i) vdW = beta * vdW + (1 - beta) * dW vdb = beta * vdb + (1 - beta) * db W = W - alpha * dW b = b - alpha * db if i > 0 and i % 1000 == 0: model.save(n, W, b, model_fname) print('========saved=======') if (x == x_to_investigate).all(): pos_trains += 1 position.print_position(position_to_investigate) (_, _, aLafter) = model.predict3(W, b, x_to_investigate) print('\niteration: %d, position trained times: %d' % (i, pos_trains)) y = Y[:, 0].reshape(9, 1) table = np.concatenate((aLbefore, aLafter, y), axis = 1) print(table)
def generate_and_save_m_training_examples(m, make_movement_fn=None, model_fname=None, traing_fname=None): if make_movement_fn == None and model_fname != None: modelInstance = model.load(model_fname) W = modelInstance['W'] b = modelInstance['b'] make_movement_fn = lambda x: model.predict2(W, b, x) trainingExamples = make_training_examples(make_movement_fn) X = trainingExamples['X'] Y = trainingExamples['Y'] for i in range(0, m): trainingExamples = make_training_examples(make_movement_fn) X = np.append(X, trainingExamples['X'], axis=1) Y = np.append(Y, trainingExamples['Y'], axis=1) if len(X[0]) >= m: break if i % 100 == 0: print("Done: " + str(len(X[0]))) X = X[:, 0:m] Y = Y[:, 0:m] if traing_fname == None: save_training_examples({ 'X': X, 'Y': Y, }) else: save_training_examples({ 'X': X, 'Y': Y, }, file_name=traing_fname)
def train_model_scenario_4(model_fname, alpha0=1, iterations=500000, beta=0.9): debug = False model_instance = model.load(model_fname) n = model_instance['n'] W = model_instance['W'] b = model_instance['b'] vdW = np.zeros(W.shape) vdb = np.zeros(b.shape) decay_rate = 9.0 / iterations # it will reduce final alpha 10 times for i in range(0, iterations): if i % 1000 == 0: print('i: %d' % (i)) # debug = True if i % 500 == 0 else False make_movement_fn = lambda x: model.predict2(W, b, x) ex = training.make_training_examples(make_movement_fn) X = ex['X'] Y = ex['Y'] if debug: print(X) print(Y) for j in range(len(X.T) - 1, -1, -1): x = X.T[j] nextPosition = position.transform_vector_into_position(x) x = position.transform_position_into_vector(nextPosition) position.print_position(nextPosition) (aL, _) = model.forward_propagation(W, b, x) print("\naL") print(aL) print('\n predicted ') movement = model.predict(W, b, x) position.print_movement(movement) print(' expected ') y = Y.T[j] position.print_movement(y.reshape(9, 1)) raw_input("Press Enter to continue...") # displayTrainingExamples(X, Y) # (dW, db) = model.calcGradients(W, b, X, Y) (dW, db, _) = model.back_propagation(n, W, b, X, Y) if debug: if i > 0 and i % 3000 == 0: is_back_prop_correct = model.check_back_propagation(n, W, b, X, Y) if is_back_prop_correct: print("BP is OK") else: print("BP is not correct") exit() alpha = alpha0 / (1.0 + decay_rate * i) # if debug: if i % 1000 == 0: print("alpha:") print(alpha) vdW = beta * vdW + (1 - beta) * dW vdb = beta * vdb + (1 - beta) * db # model.updateWeights(W, dW, b, db, alpha) # W = W - alpha * vdW # b = b - alpha * vdb W = W - alpha * dW b = b - alpha * db if i > 0 and i % 50 == 0: model.save(n, W, b, model_fname) if debug: print("model saved") print('------ end -------') model.save(n, W, b, model_fname)
import model #Test 1 model.load_data('data/train.csv', 'data/test.csv') train_tweets, test_tweets = model.preprocess_data() X,y,test, feature_names = model.feature_extraction(train_tweets, test_tweets) clfs = model.train2(X,y) test_prediction = model.predict2(clfs, test) model.saveResults('output/somePrediction.csv', test_prediction)