def main(): args = parse_args() if len(args.x_path) > 1: x = data.load_multiple(args.x_path) else: x = data.load_data(args.x_path[0]) if len(args.y_path) > 1: y = data.load_multiple(args.y_path) else: y = data.load_data(args.y_path[0]) assert len(x) == len(y) p = np.random.permutation(len(x)) x, y = x[p], y[p] # Scale images xmax = x.max() ymax = y.max() print('x_max: {}, y_max: {}'.format(xmax, ymax)) x /= xmax y /= ymax if not os.path.isdir(args.save_dir): os.makedirs(args.save_dir) np.save(os.path.join(args.save_dir, 'xymax.npy'), np.array([xmax, ymax])) if args.model is None: model = nn.build(feat_maps=args.feat_maps, growth_rate=args.growth_rate, blocks=args.blocks, dropout=args.dropout, reduction=args.reduction, bottleneck=args.bottleneck) else: model = keras.models.load_model(args.model, custom_objects={'npcc': nn.npcc}) nn.train(model, x, y, args.save_dir, batch_size=args.batch_size, max_epochs=args.epochs, validation_split=args.validation_split, patience=args.patience, lr0=args.learning_rate, decay=args.decay) if args.x_test_path is not None and args.y_test_path is not None: x_test = data.load_data(args.x_test_path) y_test = data.load_data(args.y_test_path) assert len(x_test) == len(y_test) x_test /= xmax y_test /= ymax test_loss = model.evaluate(x_test, y_test) print('Test loss:', test_loss)
def updateQ(): global trainingStarted global totalEpisodes if (trainingStarted): X = [] Y = [] newlist = random.sample(replaymemory, batchSize) for i in range(0, len(newlist)): state, action, nextState, reward = newlist[i] y = reward + gamma * bestQ(nextState) X.append(encodeInput(state, action)) Y.append(y) if totalEpisodes == 1: nn.train(model, X, Y)
def train(train_x, train_y): ''' this is a function to train all classifiers ''' tree_start = time.time() tree_clf = tree.train(train_x, train_y) print('Decision Tree - Training Time: ', round(time.time() - tree_start, 3), 's') svm_start = time.time() svm_clf = svm.train(train_x, train_y) print('SVM - Training Time: ', round(time.time() - svm_start, 3), 's') knn_start = time.time() knn_clf = knn.train(train_x, train_y) print('k-NN - Training Time: ', round(time.time() - knn_start, 3), 's') nn_start = time.time() nn_clf = nn.train(train_x, train_y) print('Neural Network - Training Time: ', round(time.time() - nn_start, 3), 's') boost_start = time.time() boost_clf = boost.train(train_x, train_y) print('Boosted Tree - Training Time: ', round(time.time() - boost_start, 3), 's') return [tree_clf, svm_clf, knn_clf, nn_clf, boost_clf]
def trainModels(train, groundtruth, name, useDistance=False, useNeighbours=False, usePatch=False, layers=[10, 5, 1], k=5): n = len(train) // k for i in range(k): out_path = local_folder + "pixelwise/models/" + name + "_" + str( i) + ".h5" if not os.path.isfile(out_path): print("Training model...", name, i) (XX, YY) = getXX_YY(train, groundtruth, i, n, useDistance, useNeighbours, usePatch) model = nn.train(XX, YY, layers) model.save(out_path) del model del XX del YY
def train(folds_x, folds_y): ''' this is a function to train all classifiers ''' tree_clf = tree.train(folds_x, folds_y) svm_clf = svm.train(folds_x, folds_y) knn_clf = knn.train(folds_x, folds_y) nn_clf = nn.train(folds_x, folds_y) boost_clf = boost.train(folds_x, folds_y) return [tree_clf, svm_clf, knn_clf, nn_clf, boost_clf] #
def main(): # Setup matplotlib x_bounds = [-10, 10] y_bounds = [-10, 10] x_bounds = [-10, 10] y_bounds = [-10, 10] fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(111) ax.set_xlim(x_bounds) ax.set_ylim(y_bounds) ax.set_aspect('equal') # Setup and train the neural net training_sample_size = 500000 test_sample_size = 10 print("Generating training data...") # TODO This could be more efficient train_data = generate_box_samples_fast(training_sample_size, x_bounds, y_bounds) test_data = generate_box_samples_fast(test_sample_size, x_bounds, y_bounds) # TODO test data should be from a different part of the plane to make sure we are generalizing print("Done.") # Layer sizes vec_size = len(train_data[0]) assert vec_size == 8 layer_widths = [vec_size, 200, 3, 200, vec_size] # Optimization hyperparameters param_scale = 0.1 #?? batch_size = 512 # TODO does batch size effect learning rate? num_epochs = 20 step_size = 0.01 params = nn.init_params(layer_widths, param_scale) print("Training") params = nn.train(train_data, test_data, layer_widths, step_size, num_epochs, batch_size) with open("./models/my-nn-model.bin", 'wb') as f: np.save(f, params) print("Done.") # Draw some output # Draw some output decoded_boxes = nn.evaluate_net(test_data, params) num_to_show = 15 draw_boxes_from_samples(ax, test_data, 'r', linewidth=5) draw_boxes_from_samples(ax, decoded_boxes, 'b') plt.show()
def main(): mode = sys.argv[1] print "mode = ", mode train_data = np.array([[0, 0], [0, 1], [1,0], [1,1]]) train_label = np.array([[1, 0], [0, 1], [0, 1], [1, 0]]) wHid, wOut = train(train_data, train_label, mode) test_data = np.array([[0,0], [0,1], [1,0], [1,1]]) test(test_data, wHid, wOut)
def main(): X_train, X_test, Y_train, Y_test = dp.get_data() train_info = nn.train(X_train, Y_train, X_test, Y_test, n_iter=1_000, test_every=100, learning_rate=0.001, hidden_size=13, batch_size=23, return_losses=True, return_weights=True, return_scores=True, seed=80718) losses = train_info[0] weights = train_info[1] val_scores = train_info[2] print(f'val_scores: {[round(s, 2) for s in val_scores]}') plt.xlabel('iteration') plt.ylabel('loss (RMSE)') plt.plot(losses) plt.show()
def main(): if len(sys.argv) == 1: print("USAGE: python3", sys.argv[0], " num-games") sys.exit(1) try: games = int(sys.argv[1]) if games <= 0: raise ValueError("number of games must be positive") except ValueError as ex: print("USAGE: python3", sys.argv[0], " num-games") sys.exit(1) model = nn.train() strat = nn.NNStrategy(model) print( "RESULT:", yahtzee.evaluate_strategy(games, strat.choose_dice, strat.choose_category))
def runTests(X, Y): Xtr, Xte, Ytr, Yte = utils.splitData(X, Y, 0.9) print("X and Y shapes =", X.shape, Y.shape) results, estimator = nn.train(Xtr, Ytr) print(results) estimator.fit(Xtr, Ytr) Yhat = estimator.predict(Xte) mse = utils.mse(Yte, Yhat) print("mse on testing data is", mse) return ( results, mse, )
(training_label[0:first], training_label[last:training_size])) tr_size = training_size - q result.append((vl_data, vl_label, vl_size, tr_data, tr_label, tr_size)) return result K = 10 data = K_fold(training_data, training_size, training_label, K) layers = [] vl_acc = [] num_nodes = 20 print "Training is starting" for i in xrange(K): print " Case #" + str(i + 1) + ":" layer = train(data[i][3], data[i][4], data[i][5], feature_size, num_nodes, 0.005, 300) accur = accuracy(data[i][0], data[i][1], data[i][2], layer, num_nodes) layers.append(layer) vl_acc.append(accur) print " Validation accuracy: " + str(accur) tr_acc = [ accuracy(training_data, training_label, training_size, x, num_nodes) for x in layers ] print "Training is done" print "Testing is starting" max_ac = max(vl_acc) ind_ac = 0 for i in xrange(K): if (vl_acc[i] == max_ac):
def trainModel(layerSizes, X, y, lmbda, maxIter): start_time = time.time() model = nn.train(layerSizes, X, y, lmbda, maxIter) print "Model trained in %s seconds" % (time.time() - start_time) return model
import time import mxnet as mx from mxnet import nd import nn import data_gen num_sets = 64 num_assoc = 16 batch_size = 64 num_epochs = 100 num_train_batches = 100 num_test_batches = 10 train_data, train_labels = data_gen.GenData(num_train_batches, batch_size, num_sets, speed=2, coverage=1) test_data, test_labels = data_gen.GenData(num_test_batches, batch_size, num_sets, speed=2, coverage=1) net = nn.train(train_data, train_labels, test_data, test_labels, batch_size, num_epochs)
# print inputnodes # print outputnodes # print categories.index('workforce of the future') nn = nn.neuralnetwork(inputnodes,hiddennodes,outputnodes,learningrate) for x in range(0,train_num): nn_input = getNN_input(inputs[x], nn_cat) nn_output = getNN_output(targets[x],categories) # print (nn_input.shape) # print (nn_output.shape) nn.train(nn_input,nn_output) pass for test_num in range(3000, 3200): test = getNN_input(inputs[test_num], nn_cat) actual_result = targets[test_num] result_matrix = nn.query(test) predicted_result = categories[np.argsort(result_matrix)[0]] print "%s > %s > %s > %s" % (inputs[test_num], categories[np.argsort(result_matrix)[-1]],categories[np.argsort(result_matrix)[-2]], categories[np.argsort(result_matrix)[-3]]) # print "%s was tested with result %s, with the actual label being %s" % (inputs[test_num], actual_result, predicted_result) pass # print result_matrix
vl_size = q; tr_data = np.vstack((training_data[0:first],training_data[last:training_size])); tr_label = np.hstack((training_label[0:first],training_label[last:training_size])); tr_size = training_size - q; result.append((vl_data, vl_label, vl_size, tr_data, tr_label, tr_size)); return result; K = 10; data = K_fold(training_data, training_size, training_label, K); layers = []; vl_acc = []; num_nodes = 20; print "Training is starting"; for i in xrange(K): print " Case #"+str(i+1)+":"; layer = train(data[i][3], data[i][4], data[i][5], feature_size, num_nodes, 0.005, 300); accur = accuracy(data[i][0], data[i][1], data[i][2], layer, num_nodes); layers.append(layer); vl_acc.append(accur); print " Validation accuracy: "+str(accur); tr_acc = [accuracy(training_data, training_label, training_size, x, num_nodes) for x in layers]; print "Training is done"; print "Testing is starting"; max_ac = max(vl_acc); ind_ac = 0; for i in xrange(K): if (vl_acc[i] == max_ac): ind_ac = i; break; training_accuracy = accuracy(training_data, training_label, training_size, layers[ind_ac], num_nodes);
def main(datasource, feature_extractor, target, chroma_network, optimiser, training, regularisation, augmentation, testing): err = False if chroma_network is None: print(Colors.red('ERROR: Specify a chroma extractor!')) err = True if feature_extractor is None: print(Colors.red('ERROR: Specify a feature extractor!')) err = True if target is None: print(Colors.red('ERROR: Specify a target!')) err = True if chroma_network is None: print(Colors.red('ERROR: Specify a chroma extractor!')) err = True if err: return 1 # intermediate target is always chroma vectors target_chroma = targets.ChromaTarget( feature_extractor['params']['fps']) target_chords = targets.create_target( feature_extractor['params']['fps'], target ) if not isinstance(datasource['test_fold'], list): datasource['test_fold'] = [datasource['test_fold']] if not isinstance(datasource['val_fold'], list): datasource['val_fold'] = [datasource['val_fold']] # if no validation folds are specified, always use the # 'None' and determine validation fold automatically if datasource['val_fold'][0] is None: datasource['val_fold'] *= len(datasource['test_fold']) if len(datasource['test_fold']) != len(datasource['val_fold']): print(Colors.red('ERROR: Need same number of validation and ' 'test folds')) return 1 all_pred_files = [] all_gt_files = [] print(Colors.magenta('\nStarting experiment ' + ex.observers[0].hash())) with TempDir() as exp_dir: for test_fold, val_fold in zip(datasource['test_fold'], datasource['val_fold']): print('') print(Colors.yellow( '=' * 20 + ' FOLD {} '.format(test_fold) + '=' * 20)) # Load data sets print(Colors.red('\nLoading data...\n')) feature_ext = features.create_extractor(feature_extractor, test_fold) train_set, val_set, test_set, gt_files = data.create_datasources( dataset_names=datasource['datasets'], preprocessors=datasource['preprocessors'], compute_features=feature_ext, compute_targets=target_chroma, context_size=datasource['context_size'], test_fold=test_fold, val_fold=val_fold, cached=datasource['cached'] ) if testing['test_on_val']: test_set = val_set print(Colors.blue('Train Set:')) print('\t', train_set) print(Colors.blue('Validation Set:')) print('\t', val_set) print(Colors.blue('Test Set:')) print('\t', test_set) print('') # build network print(Colors.red('Building network...\n')) model_type = globals()[chroma_network['model']['type']] mdl = model_type.build_model(in_shape=train_set.dshape, out_size_chroma=train_set.tshape[0], out_size=target_chords.num_classes, model=chroma_network['model']) chroma_neural_net = mdl['chroma_network'] chord_neural_net = mdl['chord_network'] input_var = mdl['input_var'] chroma_target_var = mdl['chroma_target_var'] chord_target_var = mdl['chord_target_var'] chroma_loss_fn = mdl['chroma_loss_fn'] chord_loss_fn = mdl['chord_loss_fn'] chroma_opt, chroma_lrs = create_optimiser(chroma_network['optimiser']) chord_opt, chord_lrs = create_optimiser(optimiser) chroma_train_fn = nn.compile_train_fn( chroma_neural_net, input_var, chroma_target_var, loss_fn=chroma_loss_fn, opt_fn=chroma_opt, **chroma_network['regularisation'] ) chroma_test_fn = nn.compile_test_func( chroma_neural_net, input_var, chroma_target_var, loss_fn=chroma_loss_fn, **chroma_network['regularisation'] ) chroma_process_fn = nn.compile_process_func( chroma_neural_net, input_var ) chord_train_fn = nn.compile_train_fn( chord_neural_net, input_var, chord_target_var, loss_fn=chord_loss_fn, opt_fn=chord_opt, tags={'chord': True}, **regularisation ) chord_test_fn = nn.compile_test_func( chord_neural_net, input_var, chord_target_var, loss_fn=chord_loss_fn, tags={'chord': True}, **regularisation ) chord_process_fn = nn.compile_process_func( chord_neural_net, input_var ) print(Colors.blue('Chroma Network:')) print(nn.to_string(chroma_neural_net)) print('') print(Colors.blue('Chords Network:')) print(nn.to_string(chord_neural_net)) print('') print(Colors.red('Starting training chroma network...\n')) chroma_training = chroma_network['training'] chroma_train_batches, chroma_validation_batches = \ model_type.create_iterators(train_set, val_set, chroma_training, augmentation) crm_train_losses, crm_val_losses, _, crm_val_accs = nn.train( network=chroma_neural_net, train_fn=chroma_train_fn, train_batches=chroma_train_batches, test_fn=chroma_test_fn, validation_batches=chroma_validation_batches, threads=10, callbacks=[chroma_lrs] if chroma_lrs else [], num_epochs=chroma_training['num_epochs'], early_stop=chroma_training['early_stop'], early_stop_acc=chroma_training['early_stop_acc'], acc_func=nn.nn.elemwise_acc ) # we need to create a new dataset with a new target (chords) del train_set del val_set del test_set del gt_files train_set, val_set, test_set, gt_files = data.create_datasources( dataset_names=datasource['datasets'], preprocessors=datasource['preprocessors'], compute_features=feature_ext, compute_targets=target_chords, context_size=datasource['context_size'], test_fold=test_fold, val_fold=val_fold, cached=datasource['cached'] ) if testing['test_on_val']: test_set = val_set print(Colors.blue('Train Set:')) print('\t', train_set) print(Colors.blue('Validation Set:')) print('\t', val_set) print(Colors.blue('Test Set:')) print('\t', test_set) print('') print(Colors.red('Starting training chord network...\n')) chord_train_batches, chord_validation_batches = \ model_type.create_iterators(train_set, val_set, training, augmentation) crd_train_losses, crd_val_losses, _, crd_val_accs = nn.train( network=chord_neural_net, train_fn=chord_train_fn, train_batches=chord_train_batches, test_fn=chord_test_fn, validation_batches=chord_validation_batches, threads=10, callbacks=[chord_lrs] if chord_lrs else [], num_epochs=training['num_epochs'], early_stop=training['early_stop'], early_stop_acc=training['early_stop_acc'], ) print(Colors.red('\nStarting testing...\n')) param_file = os.path.join( exp_dir, 'params_fold_{}.pkl'.format(test_fold)) nn.save_params(chord_neural_net, param_file) ex.add_artifact(param_file) pred_files = test.compute_labeling( chord_process_fn, target_chords, test_set, dest_dir=exp_dir, use_mask=False, batch_size=testing['batch_size'] ) # compute chroma vectors for the test set # TODO: replace this with experiment.compute_features for cf in compute_chroma(chroma_process_fn, test_set, batch_size=training['batch_size'], dest_dir=exp_dir): ex.add_artifact(cf) test_gt_files = dmgr.files.match_files( pred_files, test.PREDICTION_EXT, gt_files, data.GT_EXT ) all_pred_files += pred_files all_gt_files += test_gt_files print(Colors.blue('Results:')) scores = test.compute_average_scores(test_gt_files, pred_files) test.print_scores(scores) result_file = os.path.join( exp_dir, 'results_fold_{}.yaml'.format(test_fold)) yaml.dump(dict(scores=scores, chord_train_losses=map(float, crd_train_losses), chord_val_losses=map(float, crd_val_losses), chord_val_accs=map(float, crd_val_accs), chroma_train_losses=map(float, crm_train_losses), chroma_val_losses=map(float, crm_val_losses), chroma_val_accs=map(float, crm_val_accs)), open(result_file, 'w')) ex.add_artifact(result_file) # close all files del train_set del val_set del test_set del gt_files # if there is something to aggregate if len(datasource['test_fold']) > 1: print(Colors.yellow('\nAggregated Results:\n')) scores = test.compute_average_scores(all_gt_files, all_pred_files) test.print_scores(scores) result_file = os.path.join(exp_dir, 'results.yaml') yaml.dump(dict(scores=scores), open(result_file, 'w')) ex.add_artifact(result_file) for pf in all_pred_files: ex.add_artifact(pf) print(Colors.magenta('Stopping experiment ' + ex.observers[0].hash()))
def main(_log, datasource, feature_extractor, target, model, optimiser, training, regularisation, augmentation, testing): err = False if model is None or not model or 'type' not in model: _log.error(Colors.red('Specify a model!')) err = True if feature_extractor is None: _log.error(Colors.red('Specify a feature extractor!')) err = True if target is None: _log.error(Colors.red('Specify a target!')) err = True if err: return 1 target_computer = targets.create_target( feature_extractor['params']['fps'], target ) if not isinstance(datasource['test_fold'], list): datasource['test_fold'] = [datasource['test_fold']] if not isinstance(datasource['val_fold'], list): datasource['val_fold'] = [datasource['val_fold']] # if no validation folds are specified, always use the # 'None' and determine validation fold automatically if datasource['val_fold'][0] is None: datasource['val_fold'] *= len(datasource['test_fold']) if len(datasource['test_fold']) != len(datasource['val_fold']): _log.error(Colors.red('Need same number of validation and test folds')) return 1 all_pred_files = [] all_gt_files = [] print(Colors.magenta('\nStarting experiment ' + ex.observers[0].hash())) with TempDir() as exp_dir: for test_fold, val_fold in zip(datasource['test_fold'], datasource['val_fold']): print('') print(Colors.yellow( '=' * 20 + ' FOLD {} '.format(test_fold) + '=' * 20)) # Load data sets print(Colors.red('\nLoading data...\n')) train_set, val_set, test_set, gt_files = data.create_datasources( dataset_names=datasource['datasets'], preprocessors=datasource['preprocessors'], compute_features=features.create_extractor(feature_extractor, test_fold), compute_targets=target_computer, context_size=datasource['context_size'], test_fold=test_fold, val_fold=val_fold, cached=datasource['cached'], ) if testing['test_on_val']: test_set = val_set print(Colors.blue('Train Set:')) print('\t', train_set) print(Colors.blue('Validation Set:')) print('\t', val_set) print(Colors.blue('Test Set:')) print('\t', test_set) print('') # build network print(Colors.red('Building network...\n')) model_type = globals()[model['type']] mdl = model_type.build_model(in_shape=train_set.dshape, out_size=train_set.tshape[0], model=model) # mandatory parts of the model neural_net = mdl['network'] input_var = mdl['input_var'] target_var = mdl['target_var'] loss_fn = mdl['loss_fn'] # optional parts mask_var = mdl.get('mask_var') feature_out = mdl.get('feature_out') train_batches, validation_batches = model_type.create_iterators( train_set, val_set, training, augmentation ) opt, lrs = create_optimiser(optimiser) train_fn = nn.compile_train_fn( neural_net, input_var, target_var, loss_fn=loss_fn, opt_fn=opt, mask_var=mask_var, **regularisation ) test_fn = nn.compile_test_func( neural_net, input_var, target_var, loss_fn=loss_fn, mask_var=mask_var, **regularisation ) process_fn = nn.compile_process_func( neural_net, input_var, mask_var=mask_var) if feature_out is not None: feature_fn = nn.compile_process_func( feature_out, input_var, mask_var=mask_var ) else: feature_fn = None print(Colors.blue('Neural Network:')) print(nn.to_string(neural_net)) print('') if 'param_file' in training: nn.load_params(neural_net, training['param_file'].format(test_fold)) train_losses = [] val_losses = [] val_accs = [] else: if 'init_file' in training: print('initialising') nn.load_params(neural_net, training['init_file'].format(test_fold)) print(Colors.red('Starting training...\n')) train_losses, val_losses, _, val_accs = nn.train( network=neural_net, train_fn=train_fn, train_batches=train_batches, test_fn=test_fn, validation_batches=validation_batches, threads=10, callbacks=[lrs] if lrs else [], num_epochs=training['num_epochs'], early_stop=training['early_stop'], early_stop_acc=training['early_stop_acc'] ) param_file = os.path.join( exp_dir, 'params_fold_{}.pkl'.format(test_fold)) nn.save_params(neural_net, param_file) ex.add_artifact(param_file) print(Colors.red('\nStarting testing...\n')) if feature_fn is not None: dest_dir = os.path.join(exp_dir, 'features_fold_{}'.format(test_fold)) compute_features( feature_fn, train_set, batch_size=testing['batch_size'], dest_dir=dest_dir, extension='.features.npy', use_mask=mask_var is not None) compute_features( feature_fn, val_set, batch_size=testing['batch_size'], dest_dir=dest_dir, extension='.features.npy', use_mask=mask_var is not None) compute_features( feature_fn, test_set, batch_size=testing['batch_size'], dest_dir=dest_dir, extension='.features.npy', use_mask=mask_var is not None) ex.add_artifact(dest_dir) pred_files = test.compute_labeling( process_fn, target_computer, test_set, dest_dir=exp_dir, use_mask=mask_var is not None, batch_size=testing['batch_size'] ) test_gt_files = dmgr.files.match_files( pred_files, test.PREDICTION_EXT, gt_files, data.GT_EXT ) all_pred_files += pred_files all_gt_files += test_gt_files print(Colors.blue('Results:')) scores = test.compute_average_scores(test_gt_files, pred_files) test.print_scores(scores) result_file = os.path.join( exp_dir, 'results_fold_{}.yaml'.format(test_fold)) yaml.dump(dict(scores=scores, train_losses=map(float, train_losses), val_losses=map(float, val_losses), val_accs=map(float, val_accs)), open(result_file, 'w')) ex.add_artifact(result_file) # delete datasets so disk space is free del train_set del val_set del test_set # if there is something to aggregate if len(datasource['test_fold']) > 1: print(Colors.yellow('\nAggregated Results:\n')) scores = test.compute_average_scores(all_gt_files, all_pred_files) test.print_scores(scores) result_file = os.path.join(exp_dir, 'results.yaml') yaml.dump(dict(scores=scores), open(result_file, 'w')) ex.add_artifact(result_file) for pf in all_pred_files: ex.add_artifact(pf) print(Colors.magenta('Stopping experiment ' + ex.observers[0].hash()))
import nn import numpy as np predictions = nn.train( xFile='../BizFeatures/resnet/BizFeature_resnet_together_train_pca256_cluster64.csv', yFile='../BizFeatures/labels/biz_labels.csv', testFile='../BizFeatures/resnet/BizFeature_resnet_together_test_pca256_cluster64.csv', colNumber = 256*64, classNumber = 9, learningRate = 1e-4, training_epochs = 700, batch_size = 100, display_step = 5, labelThreshold = 0.3, returnProb = True) print(predictions.shape) #np.save('../LabelConvert/OneHot/nnPredictions.npy', predictions) np.save('../Ensemble/data/nnPredictionsProb.npy', predictions)
def varyTrainingData(): ngames, epochs, upperbonus, upperbonusthreshold, nSteps, nTrains, nGenerations, test_NN_size = parseInput( ) y_axis = [] state_potentials = None dict_name = "dict_UPT" + str(upperbonusthreshold) + "_UP" + str(upperbonus) optimal_strategy.initGame(upperbonusthreshold, upperbonus) YahtzeeScoresheet.UPPER_BONUS = upperbonus YahtzeeScoresheet.UPPER_BONUS_THRESHOLD = upperbonusthreshold dataout = "dataset_UPT" + str(upperbonusthreshold) + "_UP" + str( upperbonus) dataout += "_testNNsize" if test_NN_size else "" try: state_potentials = optimal_strategy.loadDict(dict_name) print("Value: " + str( optimal_strategy.queryValueOfModifiedGame(upperbonusthreshold, upperbonus))) except FileNotFoundError: print("Dictionary " + dict_name + " not found, generating now") state_potentials = optimal_strategy.buildDict() optimal_strategy.saveDict(dict_name, state_potentials) for trial in range(1, nSteps + 1): dataout = dataout.split('-') dataout = dataout[0] dataout += '-' dataout += str(trial * 2500) if not test_NN_size else str(trial) global_mean = 0 for generation in range(0, nGenerations): filename = dataout + "_" + str(generation) try: f = open("training/" + filename, "r") f.close() except FileNotFoundError: n_training_examples = trial * 2500 if not test_NN_size else 55000 print("Training set " + filename + " not found, generating now.") manager = mp.Manager() all_data = manager.list() jobs = [] for procid in range(0, N_PROCESSES): proc_dict = deepcopy(state_potentials) proc_policy = OptimalPolicy(proc_dict) proc = mp.Process( target=proc_policy.generate_training_data, args=(n_training_examples // N_PROCESSES, all_data)) jobs.append(proc) proc.start() for proc in jobs: proc.join() labelout = open("training/" + filename, "a") for line in all_data: labelout.write(line) labelout.close() mean = 0 size = 100 if not test_NN_size else trial * 5 for training_session in range(0, nTrains): trial_model = nn.train(filename, size, epochs) trial_policy = nn.NNPolicy(trial_model) mean += evaluate_policy(ngames, trial_policy.choose_dice, trial_policy.choose_category) global_mean += mean / nTrains y_axis.append(global_mean / nGenerations) print(y_axis) dataoutresults = open("training/" + dataout.split("-")[0] + "_final", "a") dataoutresults.write(str(y_axis)) dataoutresults.close()
weight_decay = config['weight_decay'] net = [] regularizers = [] inputs = np.random.randn(config['batch_size'], 1, 28, 28) net += [layers.Convolution(inputs, 16, 5, "conv1")] regularizers += [ layers.L2Regularizer(net[-1].weights, weight_decay, 'conv1_l2reg') ] net += [layers.MaxPooling(net[-1], "pool1")] net += [layers.ReLU(net[-1], "relu1")] net += [layers.Convolution(net[-1], 32, 5, "conv2")] regularizers += [ layers.L2Regularizer(net[-1].weights, weight_decay, 'conv2_l2reg') ] net += [layers.MaxPooling(net[-1], "pool2")] net += [layers.ReLU(net[-1], "relu2")] ## 7x7 net += [layers.Flatten(net[-1], "flatten3")] net += [layers.FC(net[-1], 512, "fc3")] regularizers += [ layers.L2Regularizer(net[-1].weights, weight_decay, 'fc3_l2reg') ] net += [layers.ReLU(net[-1], "relu3")] net += [layers.FC(net[-1], 10, "logits")] data_loss = layers.SoftmaxCrossEntropyWithLogits() loss = layers.RegularizedLoss(data_loss, regularizers) nn.train(train_x, train_y, valid_x, valid_y, net, loss, config) nn.evaluate("Test", test_x, test_y, net, loss, config)
}, { "input_dim": 50, "output_dim": 25, "activation": "relu" }, { "input_dim": 25, "output_dim": 1, "activation": "sigmoid" }, ] # Training params_values = nn.train(np.transpose(X_train), np.transpose(y_train.reshape((y_train.shape[0], 1))), NN_ARCHITECTURE, 10000, 0.01) # Prediction Y_test_hat, _ = nn.full_forward_propagation(np.transpose(X_test), params_values, NN_ARCHITECTURE) # Accuracy achieved on the test set acc_test = nn.get_accuracy_value( Y_test_hat, np.transpose(y_test.reshape((y_test.shape[0], 1)))) print("Test set accuracy: {:.2f} - David".format(acc_test)) # karas # Building a model model = Sequential() model.add(Dense(25, input_dim=2, activation='relu'))
def main(): parser = argparse.ArgumentParser(description="Command Line Parser") parser.add_argument("--train-data", dest="train_data", default='$', help="Give path to the training data set.") parser.add_argument("--test-data", dest="test_data", required=True, help="Give path to the testing data set.") parser.add_argument("--dataset", dest="dataset", required=True, help="Give Name of data set.") parser.add_argument("--configuration", dest="configuration", default='$', help="Give structure of Neural Net.") args = parser.parse_args() train_data = args.train_data test_data = args.test_data dataset = args.dataset configuration = args.configuration if configuration != '$': configuration = list(map(int, configuration.strip('[]').split(','))) #Test mode if train_data == '$': test_filename = test_data testdata = list() if dataset == 'MNIST': num_classes = 10 with open("../mem/weight_mnist.pkl", 'r') as saved: w = pkl.load(saved) with open("../mem/bias_mnist.pkl", 'r') as saved: b = pkl.load(saved) with open("../mem/configuration_mnist.pkl", 'r') as saved: nn_structure = pkl.load(saved) files = os.listdir(test_filename) for f in files: #print "Reading File", f file_path = os.path.join(test_filename, f) img = PIL.Image.open(file_path).convert("L") matrix = np.array(img) shape = matrix.shape reshaped_matrix = np.reshape(matrix, [ shape[0] * shape[1], ]) testdata.append(reshaped_matrix) e = float(files.index(f) + 1) / len(files) sys.stdout.write('{}% File Reading Completed .....\r'.format( int(e * 100))) test_scaler = StandardScaler() testdata = test_scaler.fit_transform(np.array(testdata)) n_components = 64 pca_test = PCA(n_components) testdata = pca_test.fit_transform(testdata) predicted_y = nn.predict(np.array(testdata), w, b, len(nn_structure)) print "Prediction is: ", predicted_y print len(nn_structure) else: num_classes = 2 with open("../mem/weight_catdog.pkl", 'r') as saved: w = pkl.load(saved) with open("../mem/bias_catdog.pkl", 'r') as saved: b = pkl.load(saved) with open("../mem/configuration_catdog.pkl", 'r') as saved: nn_structure = pkl.load(saved) files = os.listdir(test_filename) for f in files: #print "Reading File", f file_path = os.path.join(test_filename, f) img = PIL.Image.open(file_path).convert("L") matrix = np.array(img) shape = matrix.shape reshaped_matrix = np.reshape(matrix, [ shape[0] * shape[1], ]) testdata.append(reshaped_matrix) e = float(files.index(f) + 1) / len(files) sys.stdout.write('{}% File Reading Completed .....\r'.format( int(e * 100))) test_scaler = StandardScaler() testdata = test_scaler.fit_transform(np.array(testdata)) n_components = 64 pca_test = PCA(n_components) testdata = pca_test.fit_transform(testdata) predicted_y = nn.predict(np.array(testdata), w, b, len(nn_structure)) predicted = ['cat' if x == 0 else 'dog' for x in predicted_y] print "Prediction is:", predicted else: train_filename = train_data test_filename = test_data raw_data, raw_labels, num_classes = nn.read_data(train_filename) #nn_structure = configuration testdata = list() if dataset == 'MNIST': #configuration = [64]+configuration+[10] #print configuration nn_structure = [64] + configuration + [10] scaler = StandardScaler() scaled_data = scaler.fit_transform(raw_data) print "Scaling Completed ....." n_components = 64 pca = PCA(n_components) data = pca.fit_transform(scaled_data) print "Dimensionaly Reduced ....." labels = np.array(list(map(int, raw_labels))) onehot_labels = nn.one_hot(labels, num_classes) w, b, cost_list = nn.train(nn_structure, data, onehot_labels) with open("../mem/weight_mnist.pkl", 'w') as saver: pkl.dump(w, saver) with open("../mem/bias_mnist.pkl", 'w') as saver: pkl.dump(b, saver) with open("../mem/configuration_mnist.pkl", 'w') as saver: pkl.dump(nn_structure, saver) #num_classes = 10 files = os.listdir(test_filename) for f in files: #print "Reading File", f file_path = os.path.join(test_filename, f) img = PIL.Image.open(file_path).convert("L") matrix = np.array(img) shape = matrix.shape reshaped_matrix = np.reshape(matrix, [ shape[0] * shape[1], ]) testdata.append(reshaped_matrix) e = float(files.index(f) + 1) / len(files) sys.stdout.write('{}% File Reading Completed .....\r'.format( int(e * 100))) test_scaler = StandardScaler() testdata = test_scaler.fit_transform(np.array(testdata)) n_components = 64 pca_test = PCA(n_components) testdata = pca_test.fit_transform(testdata) predicted_y = nn.predict(np.array(testdata), w, b, len(nn_structure)) print "Prediction is:", predicted_y else: nn_structure = [64] + configuration + [2] scaler = StandardScaler() scaled_data = scaler.fit_transform(raw_data) print "Scaling Completed ....." n_components = 64 pca = PCA(n_components) data = pca.fit_transform(scaled_data) print "Dimensionaly Reduced ....." print "Cat is class 0 and Dog is class 1 ....." labels = np.array([0 if x == 'cat' else 1 for x in raw_labels]) onehot_labels = nn.one_hot(labels, num_classes) w, b, cost_list = nn.train(nn_structure, data, onehot_labels) with open("../mem/weight_catdog.pkl", 'w') as saver: pkl.dump(w, saver) with open("../mem/bias_catdog.pkl", 'w') as saver: pkl.dump(b, saver) with open("../mem/configuration_catdog.pkl", 'w') as saver: pkl.dump(nn_structure, saver) files = os.listdir(test_filename) for f in files: #print "Reading File", f file_path = os.path.join(test_filename, f) img = PIL.Image.open(file_path).convert("L") matrix = np.array(img) shape = matrix.shape reshaped_matrix = np.reshape(matrix, [ shape[0] * shape[1], ]) testdata.append(reshaped_matrix) e = float(files.index(f) + 1) / len(files) sys.stdout.write('{}% File Reading Completed .....\r'.format( int(e * 100))) test_scaler = StandardScaler() testdata = test_scaler.fit_transform(np.array(testdata)) n_components = 64 pca_test = PCA(n_components) testdata = pca_test.fit_transform(testdata) predicted_y = nn.predict(np.array(testdata), w, b, len(nn_structure)) predicted = ['cat' if x == 0 else 'dog' for x in predicted_y] print "Prediction is:", predicted
trainProbabilities = np.zeros((10, trainLabels.size)); for a in range(trainLabels.size): trainProbabilities[trainLabels[a], a] = 1 testProbabilities = np.zeros((10, testLabels.size)); for a in range(testLabels.size): testProbabilities[testLabels[a], a] = 1 estimatedTrainProbabilities = nn.ff(model, trainData) trainLoss = np.zeros(epochMax+1) trainAcc = np.zeros(epochMax+1) trainLoss[0], trainAcc[0] = nn.evaluate(model, opt, trainData, trainProbabilities) testLoss = np.zeros(epochMax+1) testAcc = np.zeros(epochMax+1) testLoss[0], testAcc[0] = nn.evaluate(model, opt, testData, testProbabilities) for epoch in range(1, epochMax+1): nn.train(model, opt, trainData, trainProbabilities) trainLoss[epoch], trainAcc[epoch] = nn.evaluate(model, opt, trainData, trainProbabilities) testLoss[epoch], testAcc[epoch] = nn.evaluate(model, opt, testData, testProbabilities) print('Epoch:', epoch, ', Learning Rate:', opt.lr, ', Loss:', trainLoss[epoch], '/', testLoss[epoch], 'Acc:', trainAcc[epoch], '/', testAcc[epoch]) if trainLoss[epoch]>trainLoss[epoch-1]: opt.lrDecay(); time_end = time.time() print('Elapsed time:', time_end-time_start, 'seconds.') print('Training accuracy:', trainAcc[-1]*100, '%;') print('Test accuracy:', testAcc[-1]*100, '%.') fig1 = pyplot.figure() ax1 = fig1.add_subplot(1, 1, 1) ax1.plot(range(epochMax+1), 100*trainAcc, label='Training') ax1.plot(range(epochMax+1), 100*testAcc, label='Test')
# coding: utf-8 # neural_network/test_handwritten_digits.py """手写字符集 """ import nn import numpy as np from sklearn import datasets from scipy.io import loadmat # digits = datasets.load_digits() # # # X = digits.images.reshape((len(digits.images), -1)) # y = digits.target.reshape(-1, 1) data = loadmat('data/handwritten_digits.mat') Thetas = loadmat('data/ex4weights.mat') Thetas = [Thetas['Theta1'], Thetas['Theta2']] X = np.mat(data['X']) y = np.mat(data['y']) res = nn.train(X,y,hiddenNum=1,unitNum=25,Thetas=Thetas, precision = 0.5) print 'Error is: %.4f'%res['error']
from nn import train params = { "batch_size": 5, "epochs": 1000, #"learning_rate" : 0.0005, "learning_rate": 0.009, "beta1": 0.5, "print_interval": 10, "save_interval": 100, "save_path": "models/" } train("test_data/train", "test_data/valid", params)
NeuralNetwork = nn.NeuralNetwork nn = NeuralNetwork(2, 2, 1) training = [ { 'inputs': [1, 0], 'targets': [1] }, { 'inputs': [0, 1], 'targets': [1] }, { 'inputs': [1, 1], 'targets': [0] }, { 'inputs': [0, 0], 'targets': [0] }, ] for i in range(100): data = training[random.randint(0, 3)] nn.train(data['inputs'], data['targets']) answer = nn.predict([1, 0]) print(answer)
batchImagePixels = [ traindata[i] for i in range(batchstartIndex, batchendIndex) ] batchImageLabels = [ trainlabel[i] for i in range(batchstartIndex, batchendIndex) ] X = np.asarray(batchImagePixels, dtype=None, order=None) y = [] for i in range(len(batchImageLabels)): labellist = [0 for i in range(opdim)] labellist[int(batchImageLabels[i])] = 1 y.append(labellist) Y = np.asarray(y, dtype=None, order=None) weights = nn.train(model, X, Y, weights, learning_rate) batchstartIndex = batchendIndex batchendIndex = batchstartIndex + batchsize X_test = np.asarray(testdata, dtype=None, order=None) accuracyOfMyCode, f1_score_macro, f1_score_micro = nn.predict( X_test, testlabel, weights) print("Test Accuracy ", accuracyOfMyCode) f.write("Test Accuracy " + str(accuracyOfMyCode)) f.write("\n") print("Test F1 Score(Macro) ", f1_score_macro) f.write("Test F1 Score(Macro) " + str(f1_score_macro)) f.write("\n") print("Test F1 Score(Micro) ", f1_score_micro) f.write("Test F1 Score(Micro) " + str(f1_score_micro)) f.write("\n") f.close()
def train_model(models_dir, training_data_dir, num_epochs, batch_size, auto_save_interval, max_to_keep, ckpt_n_hours, model_name, train_percent, val_percent, test_percent, keep_percent, mean, weight_decay, learning_rate, dropout): logger.info("Fetching data.") # NOTE: There is a potential bug here where the sizes of augmented/nonaugmented data do not match, i.e. one group is > 512 and one is < 512. This case is (probably) not handled properly. raw_data_lst_nonaug, seg_data_lst_nonaug, orig_dims = pipeline.load_all_data( training_data_dir, no_empty=True, reorient=True, predicting=False, include_lower=False, load_augmented=False) raw_data_lst_aug, seg_data_lst_aug, aug_dims = pipeline.load_all_data( training_data_dir, no_empty=True, reorient=True, predicting=False, include_lower=False, load_augmented=True) training_height, training_width = raw_data_lst_nonaug[0].shape[ 0], raw_data_lst_nonaug[0].shape[1] print("type(seg_data_lst_nonaug):", type(seg_data_lst_nonaug)) print("type(seg_data_lst_nonaug[0]):", type(seg_data_lst_nonaug[0])) print("len(raw_data_lst_nonaug), len(seg_data_lst_nonaug): ", len(raw_data_lst_nonaug), len(seg_data_lst_nonaug)) print("training_height, training_width: ", training_height, training_width) print("raw_data_lst_nonaug[0].shape: ", raw_data_lst_nonaug[0].shape) print("seg_data_lst_nonaug[0].shape: ", seg_data_lst_nonaug[0].shape) logger.debug("ORIG DIMS: %s", orig_dims) logger.info("Initializing model.") tf.reset_default_graph() sess = tf.Session() model = Unet.Unet(mean, weight_decay, learning_rate, dropout, h=training_height, w=training_width) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(max_to_keep=max_to_keep, keep_checkpoint_every_n_hours=ckpt_n_hours) x_train, x_val, x_test, y_train, y_val, y_test = pipeline.split_data( raw_data_lst_nonaug, seg_data_lst_nonaug, train_percent, val_percent, test_percent, keep_percent, total_keep=500) if len(raw_data_lst_aug) > 0: logger.debug("Splitting augmented data.") x_train_aug, x_val_aug, x_test_aug, y_train_aug, y_val_aug, y_test_aug = pipeline.split_data( raw_data_lst_aug, seg_data_lst_aug, percent_train=90, percent_val=5, percent_test=5, percent_keep=100, total_keep=1000) logger.debug("Extending training set.") x_train = np.concatenate((x_train, x_train_aug)) y_train = np.concatenate((y_train, y_train_aug)) logger.debug("Generated random indices.") indices = np.arange(x_train.shape[0]) np.random.shuffle(indices) logger.debug("Shuffling training set.") x_train = x_train[indices] y_train = y_train[indices] logger.info("Training model. Details:") logger.info(" * Model name: %s", model_name) logger.info(" * Epochs: %d", num_epochs) logger.info(" * Batch size: %d", batch_size) logger.info(" * Max checkpoints to keep: %d", max_to_keep) logger.info(" * Keeping checkpoint every n hours: %d", ckpt_n_hours) logger.info(" * Keeping checkpoint every n epochs: %d", auto_save_interval) logger.info(" * Model directory save destination: %s", models_dir) logger.info(" * Training data directory: %s", training_data_dir) try: losses, accs = nn.train(sess, model, saver, x_train, y_train, x_val, y_val, num_epochs, batch_size, auto_save_interval, models_dir=models_dir, model_name=model_name) except KeyboardInterrupt: logger.info("Training interrupted.") logger.info("Training done. Saving model.") pipeline.save_model(models_dir, model_name, saver, sess) logger.info("Computing accuracy on test set.") test_acc = nn.validate(sess, model, x_test, y_test, verbose=True) logger.info("Test accuracy: %s", test_acc) return losses, accs, test_acc
import preprocessing import nn import pickle from data_utils import pearsonsR if __name__ == '__main__': trainPosts, testPosts, devPosts, devTestPosts = preprocessing.prepare() countNot1 = 0 for i in trainPosts[0]: if i[-1] != -1: countNot1 +=1 print('Count not -1 is ', countNot1) pr = pearsonsR(trainPosts[0]) for item in pr: print(item) with open('pearsonsR.p', 'wb') as f: pickle.dump(pr, file=f) pickle.dump print('Beginning nn') # nn = nn.simple_feed_forward() nn.train(trainPosts[0]) nn.test(devTestPosts[0])
from preprocessing import Preprocessor from nn import train import torch import torch.nn as nn p = Preprocessor(dimensions=2) data, focus_words, contexts = p.run() model = train(input_dimension=len(p.vocabulary), embedding_dimension=2, learning_rate=0.1, focus_words=focus_words, contexts=contexts) # check for similarity between batman and wayne vs joker and wayne def who_is_wayne(m): w1 = model.layer1.weight # stack to merge rows like row_1 -> col_1 with row_2 -> col_2 as a single row # print w1 in case you need an explanation! w1 = torch.stack((w1[0], w1[1]), dim=1) b1 = model.layer1.bias # word vectors we're looking for from word2vec # are actually the backprop updated weights and biases of the # hidden layer vectors = w1 + b1 print("") print("Preparing vectors")
#convert into numpy array X, y, le = get_numpy_array(features_df) # split into training and testing data X_train, X_test, y_train, y_test = get_train_test(X, y) num_labels = y.shape[1] X_train = np.expand_dims(X_train, axis=2) X_test = np.expand_dims(X_test, axis=2) # create model architecture model = nn.create_cnn(num_labels) # train model print("Training..") nn.train(model, X_train, X_test, y_train, y_test, "trained_cnn.h5") # compute test loss and accuracy test_loss, test_accuracy = nn.compute(X_test, y_test, "trained_cnn.h5") print("Test loss", test_loss) print("Test accuracy", test_accuracy) # predicting using trained model with any test file in dataset nn.predict("sample_wav/new_file_Chainsaw.wav", le, "trained_cnn.h5") elif sys.argv[1] == "mlp": #convert into numpy array X, y, le = get_numpy_array(features_df) # split into training and testing data
x_data = [] for input_value in input_data: x_data.append(input_value[0]) x_data.append(input_value[1]) train_x.append(x_data) train_y.append(output) train_x = np.array(train_x) train_y = np.array(train_y) net = [] inputs = np.random.randn(config['batch_size'], len(train_x)) net += [layers.FC(inputs, 50, "fc1")] net += [layers.Sigmoid(net[-1], "sg1")] net += [layers.FC(net[-1], 10, "fc2")] net += [layers.Sigmoid(net[-1], "sg2")] net += [layers.FC(net[-1], 5, "fc3")] loss = layers.MeanSquareError() nn.train(train_x, train_y, net, loss, config) while True: pointsFilename = input("Enter file name:") with open(pointsFilename) as f: points = json.load(f) points = np.array(points).reshape(1, 100) nn.evaluate(net, np.array(points)) print('DONE')