def make_model(h_layers, h_nodes): # create an ANN with specified parameters net = ANN(p=Parameter(num_input=len(sample), num_hidden_layers=h_layers, nodes_per_hidden=h_nodes, num_output=NUM_GENRES, hidden_activation=DEFAULT_H_ACTIVATION, output_activation=DEFAULT_O_ACTIVATION, initialize=False, loss_function=DEFAULT_LOSS, features=indepent_features)) return net
X.values, encode(Y), # one hot encoder, see ANN_encode.py test_size=VALIDATION_PERCENT, # validation size random_state=EXPERIMENT_SEED) sample = trainx[0].copy() print('Data done!\n\n********') net = 0 history = 0 callback = 0 # Use this for pre trained models net = ANN(trained_model=MODEL_NAME) if (MODEL_NAME_2 != ''): net2 = ANN(trained_model=MODEL_NAME_2) samples = 0 # The number of test samples to check samples = int(input('Begin prediction on test set.\nNumber of samples:\t')) if samples > valx.shape[0]: samples = valx.shape[0] print('Too bad... you wanted too many samples. Using the max:\t{}'.format( samples)) input('Press enter to continue...') print('\n')
def getHistory(data_set_size): # set your experiment seed for train test split EXPERIMENT_SEED = 42 FEATURE_COUNT = 200 VALIDATION_PERCENT = 0.1 DEFAULT_LAYERS = 1 DEFAULT_NODES = len(classes) + 1 DEFAULT_H_ACTIVATION = 'relu' DEFAULT_O_ACTIVATION = 'softmax' DEFAULT_LOSS = 'categorical_crossentropy' DEFAULT_BATCH = 200 DEFAULT_EPOCHS = 200 TEST_RATIO = 0.34 DATA_SET = data_set_size # use the best model MODEL_NAME = 'matt' ## Process Data # Load the Data Management's interface import sys sys.path.append('../Back_End/') sys.path.append('../Data_Management/') import CSVInterface import song_result_interface import pandasDB print('Initializing Data Management interface...') # reads the data from the csv reader = CSVInterface.featRead() DB = pandasDB.DataBase() D = {} D['X'] = { 'small' : reader.getSubset( reader.getFrame('features'), sub='small' ), 'medium': reader.getSubset( reader.getFrame('features'), sub='medium' ), 'cleanLarge' : reader.getSubset( reader.getFrame('features'), sub='cleanLarge' ) } D['Y'] = { 'small' : reader.getSubset( reader.getFrame('track')['genre_top'], sub='small' ), 'medium': reader.getSubset( reader.getFrame('track')['genre_top'], sub='medium' ), 'cleanLarge': reader.getSubset( reader.getFrame('track')['genre_top'], sub='cleanLarge' ), } # Show all the weights prior to training # net.show_weights(net.num_hidden_layers + 1) # The data after removing outliers # data = outlier_method(RawData) #get the features # indepent_features = reader.selectN(n=FEATURE_COUNT) indepent_features = ['mfcc', 'spectral_contrast'] print('Constructing datasets') print('X') # the ind vars # X = pd.DataFrame(D['X'][DATA_SET].iloc[:, indepent_features]) X = pd.DataFrame(D['X'][DATA_SET][indepent_features]) print('Y') # the dependent var Y = pd.DataFrame(D['Y'][DATA_SET], columns=['genre_top']) print('train/validation split') # Test and train split using encoded Y labels (vector of 0s with one 1) trainx, testx, trainy, testy = train_test_split( X.values, encode(Y), # one hot encoder, see ANN_encode.py test_size=VALIDATION_PERCENT, # validation size random_state=EXPERIMENT_SEED ) sample = trainx[0].copy() print('Data done!\n\n********') ## Build the neural network print('\nBuilding neural net') print('input : {}'.format(len(sample))) print('output: {}\n'.format(NUM_GENRES)) net = 0 history = 0 callback = 0 # Use this for pre trained models net = ANN(p=Parameter( num_input=len(sample), num_hidden_layers=1, nodes_per_hidden=len(sample) + 1, num_output=NUM_GENRES, hidden_activation=DEFAULT_H_ACTIVATION, output_activation=DEFAULT_O_ACTIVATION, initialize=False, loss_function=DEFAULT_LOSS, features = indepent_features )) # Show the weights # net.show_weights(net.num_hidden_layers + 1) # Train the network # returns history of training process, and a callback object that can # extract information about the model at the end of events ANN_callbacks.py h, callback = net.train( trainx, trainy, num_iter=DEFAULT_EPOCHS, testing=(testx, np.array(testy)), batch=DEFAULT_BATCH, interactive=False ) return h
## Build the neural network print('\nBuilding neural net') print('input : {}'.format(len(sample))) print('output: {}\n'.format(NUM_GENRES)) # the neural network net = 0 # the history object from training history = 0 # the callback function from training callback = 0 # Use this for pre trained models if MODEL_NAME != '': net = ANN(trained_model=MODEL_NAME) else: # Use this to test your own architecture net = ANN(p=Parameter(num_input=len(sample), num_hidden_layers=DEFAULT_LAYERS, nodes_per_hidden=DEFAULT_NODES, num_output=NUM_GENRES, hidden_activation=DEFAULT_H_ACTIVATION, output_activation=DEFAULT_O_ACTIVATION, initialize=False, loss_function=DEFAULT_LOSS, features=indepent_features)) # Train the network # returns history of training process, and a callback object that can # extract information about the model at the end of events ANN_callbacks.py
from ANN_class import ANN import mnistHandwriting import matplotlib.pyplot as plt import numpy as np print "Loading Data" T = mnistHandwriting.MNISTexample(0, 10000) Test = mnistHandwriting.MNISTexample(0, 10000, bTrain=True) ANN_network = ANN([784, 30, 30, 10]) ANN_network.mini_batch_training(T, 50, 0.3, 50) plt.plot(ANN_network.cost_result) plt.plot(ANN_network.accuracy_result, 'r-') plt.show() correct = 0.0 total = 0.0 wrong = [] for i in Test: network_result = ANN_network.propagate_result(i[0]) if np.argmax(network_result) == np.argmax(i[1]): correct += 1 #print network_result else: wrong.append(i) total += 1 #mnistHandwriting.writeMNISTimage(wrong) print correct / total
from ANN_class import ANN import mnistHandwriting import matplotlib.pyplot as plt import numpy as np print("Loading Data") T = mnistHandwriting.MNISTexample(0, 60000) Test = mnistHandwriting.MNISTexample(0, 1000, bTrain=True) ANN_network = ANN([784, 20, 25, 10]) ANN_network.mini_batch_training(T, 300, 3, 250) plt.plot(ANN_network.cost_result) plt.plot(ANN_network.accuracy_result, 'r-') correct = 0.0 total = 0.0 wrong = [] for i in Test: network_result = ANN_network.propagate_result(i[0]) if np.argmax(network_result) == np.argmax(i[1]): correct += 1 #print network_result else: wrong.append(i) total += 1 #mnistHandwriting.writeMNISTimage(wrong) print(correct / total) plt.show()
import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split import itertools import time import seaborn as sns from ANN_class import ANN df = pd.read_csv('./train.csv') X = np.asmatrix(df.drop('label', axis=1)) / 255 y = np.asarray(df['label']) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0) ann = ANN(2, [64, 64]) ALPHA = 0.1 EPOCH = 20 start_time = time.time() ann.fit(X_train, y_train, ALPHA, EPOCH) print("--- %s seconds ---" % (time.time() - start_time)) res = ann.predict(X_test).reshape(1, -1) y = y_test.reshape(1, -1) print(f'Test Accuracy: {np.sum(res == y) / len(y_test)}') # plot some number num = plt.imshow(X_train[1000, :].reshape(28, 28)) # plot the Training cost and Validation cost
# the dependent var Y = pd.DataFrame(D['Y'][DATA_SET], columns=['genre_top']) print('train/validation split') # Test and train split using encoded Y labels (vector of 0s with one 1) trainx, valx, trainy, valy = train_test_split( X.values, encode(Y), # one hot encoder, see ANN_encode.py test_size=VALIDATION_PERCENT, # validation size random_state=EXPERIMENT_SEED) print('Data done!\n\n********') # Use this for pre trained models net = ANN(trained_model=MODEL_NAME) if (MODEL_NAME_2 != ''): net2 = ANN(trained_model=MODEL_NAME_2) # The number of test samples to check samples = 1000 print('\n') val_scores = [] val_scores2 = [] naive = [] #calculate the avg rank of all predictions every time a new prediction is made for index in range(0, samples): song = DB.query()['track_data']