コード例 #1
0
def main():
    #make a neural network with set architecture
    arch = (2,4,1)
    nn = Neural_Network(arch)

    #XOR input data
    X_train = np.array( [ [0,0], [0,1], [1,0], [1,1] ] )
    #XOR output data
    y_train = np.array( [[0],[1],[1],[0]] )

    #set max iterations, learning rate, and convergence threshold
    iters, lr, threshold = 5000, 1, 0.00001
    #train the network
    J_Hist = nn.train(X_train, y_train, alpha = lr, maxIter = iters, convergenceThreshold = threshold)

    #forward propagate to get a prediction from the network
    result = nn.forwardProp(X_train)

    #print some nice information
    print("\nUnfiltered Prediction:\n", result)
    print("Final Prediction:\n", result >= 0.5, '\n')
    print("Random init cost: ", round(J_Hist[0], 5), ", Final cost: ", round(J_Hist[-1], 5))
    print("Cost reduction from random init: ", round(J_Hist[0] - J_Hist[-1], 5), '\n')

    #set up subplots for the cost history and decision boundary
    figure, plots = plt.subplots(ncols=2)
    figure.suptitle('Neural Network Learning of XOR') #supertitle
    figure.tight_layout(pad=2.5, w_pad=1.5, h_pad=0) #fix margins
    drawCostHistory(J_Hist, plots[0])
    drawDecisionBoundary(nn, plots[1], seperation_coefficient = 50, square_size = 1, allowNegatives = False)
    #show the cool graphs :)
    plt.show()