def epoch(eta=0.04, penalty=0.4, epochs=200, mini_batch_size=100, t0=5, t1=50, create_conf=False): layer1 = DenseLayer(features, 100, sigmoid()) #layer2 = DenseLayer(100, 50, sigmoid()) #layer3 = DenseLayer(100, 50, sigmoid()) layer4 = DenseLayer(100, 10, softmax()) layers = [layer1, layer4] network = NN(layers) cost_array = np.zeros((epochs, 2)) def learning_schedule(t): return 0.04 #t0/(t+t1) for i in range(epochs): random.shuffle(batch) X_train_shuffle = X_train[batch] one_hot_shuffle = one_hot[batch] Y_train_shuffle = Y_train[batch] #eta = learning_schedule(i) network.SGD(ce, 100, X_train_shuffle, one_hot_shuffle, eta, penalty) Y_pred = np.argmax(network.feedforward(X_test), axis=1) Y_pred_train = np.argmax(network.feedforward(X_train_shuffle), axis=1) cost_array[i, 0] = accuracy()(Y_test.ravel(), Y_pred) cost_array[i, 1] = accuracy()(Y_train_shuffle.ravel(), Y_pred_train) print("accuracy on train data = %.3f" % cost_array[-1, 1]) print("accuracy on test data = %.3f" % cost_array[-1, 0]) if create_conf == True: #creating confusion matrix numbers = np.arange(0, 10) conf_matrix = confusion_matrix(Y_pred, Y_test, normalize="true") heatmap = sb.heatmap(conf_matrix, cmap="viridis", xticklabels=["%d" % i for i in numbers], yticklabels=["%d" % i for i in numbers], cbar_kws={'label': 'Accuracy'}, fmt=".2", edgecolor="none", annot=True) heatmap.set_xlabel("pred") heatmap.set_ylabel("true") heatmap.set_title(r"FFNN prediction accuracy with $\lambda$ = {:.1e} $\eta$ = {:.1e}"\ .format(penalty, eta)) fig = heatmap.get_figure() fig.savefig("../figures/MNIST_confusion_net.pdf", bbox_inches='tight', pad_inches=0.1, dpi=1200) plt.show() return cost_array[-1]
trainingSet = [] ### each training iteration trains the network on 3 chorales at a time for j in range(3): randomChoraleNo = i * 3 + j choraleArray = MusicHandler.getChoraleArray( i, 'jsbach_chorals_harmony.data') trainingSet.extend(getTrainingSet(choraleArray)) print("Length of training set: " + str(len(trainingSet))) ### crop training data to a multiple of batchSize trainingDataLength = (len(trainingSet) // batchSize) * batchSize print(trainingDataLength) trainingData = trainingSet[:trainingDataLength] print("Length of cropped training data: " + str(len(trainingData))) ### do the network training network.SGD(trainingData, 100, batchSize, 1, None) network.saveToFile('BachGenerator1') ### generate a piece of music ---------------------------------------------- choraleArray = MusicHandler.getChoraleArray(0, 'jsbach_chorals_harmony.data') choraleLength = 40 chords = [] chords.append(choraleArray[0]) chords.append(choraleArray[1]) chords.append(choraleArray[2]) chords.append(choraleArray[3]) for chord in range(choraleLength - 4): input = [note for note in chords[chord]] for i in range(1, 4): input = input + chords[chord + i]