# Create the target array for net_x with value 0.01 for each element.
    x_target = np.zeros(onodes_x)
    # Set the value of the index which results from the correct x-value to 0.99.
    x_target[int(x_value * 100 + 200)] = 0.99
    # Create the target array for net_y with value 0.01 for each element.
    y_target = np.zeros(onodes_y)
    # Set the value of the index which results from the correct y-value to 0.99.
    y_target[int(y_value * 100 + 200)] = 0.99
    # Create the target array for net_z with value 0.01 for each element.
    z_target = np.zeros(onodes_z)
    # Set the value of the index which results from the correct z-value to 0.99.
    z_target[int(z_value * 100 + 100)] = 0.99

    # Train the neural networks.
    net_x.train(traininginput, x_target)
    net_y.train(traininginput, y_target)
    net_z.train(traininginput, z_target)

    # After 'test'-number of trainings, test the network.
    if e % test == 0:

        # Create an empty array to save all errors of one training and calculate mean error and
        # standard deviation in the end.
        errorlist = []

        # Test the networks for 500 angle combinations
        for i in range(500):

            # Depending on the degrees of freedom, 1, 2 or 3 angles are drawn randomly from the list
            # containing all integer numbers from -180 to 180.
    # and 3 are random numbers out of the array angles. Theta 1 is 0.
    trainingangles = np.asarray(
        [0, random.choice(angles),
         random.choice(angles)])
    # Set the input to a range between 0.01 and 1.00.
    traininginput = (((trainingangles + 180) / 360) * 0.99) + 0.01

    # Calculate the real result for the randomly chosen input with homogenous transformation matrices.
    result = manipulator.calculate(trainingangles[0], trainingangles[1],
                                   trainingangles[2])
    # Set the result to a range between 0.2 and 0.8 for the targets of the net.
    target = (((result[:3] + 2) / 4) * 0.6) + 0.2
    target[2] = (((result[2] + 1) / 4) * 0.6) + 0.2

    # Train the neural network.
    net.train(traininginput, target)

    # After test-number of trainings, test the network.
    if e % test == 0:

        # Create an empty array to save all errors of one training and calculate mean error and
        # standard deviation in the end.
        errorlist = []

        # Test the network for 500 angle combinations
        for i in range(500):
            # An array of 3 angles as input. Depending on the degrees of freedom, 1, 2 or 3 angles
            # are randomly chosen.
            testangles = np.asarray(
                [0, random.choice(angles),
                 random.choice(angles)])
epochs = 60000
# Number of trainings, after which the network is tested.
test = epochs / 5

for e in range(epochs+1):

    # Set the input to a range between 0.01 and 1.00.
    x = ((random.random()) * 0.99) + 0.01
    y = 3 * x * (1 - x)

    # Define the target for the network avoiding 0 because it can not be reached by the
    # sigmoid activation function.
    target = y + 0.1

    # Train the neural network.
    net.train(x, target)

    # After 'test'-number of trainings, test the network.
    if e % test == 0:

        # Create empty arrays for each test to plot the function calculated by the network
        # and calculate mean error and deviation.
        outputarray = []
        errorlist = []

        # Test the network for 300 input values between 0 and 1.
        input = (np.linspace(0, 1, num=300))
        for i in range(len(input)):
            # Subtract 0.1 from the output, to compensate the previously set target.
            output = float(net.query(input[i])) - 0.1
            # Save the outputs in an array.
    x = random.choice(np.linspace(0, 1, num=101))
    # Create the input array with value 0.01 for each element
    xlist = np.zeros(inodes) + 0.01
    # Set the value of the index which results from the multiplication of x with 100 to 1.0.
    xlist[int(x * 100)] = 1.00

    # Calculate the result for the given input with the function. Round the result to 2 decimals
    # which is the accuracy, the network is limited to due to the discretization of the output.
    y = (3 * x * (1 - x)).__round__(2)
    # Create the target array with value 0.01.
    targetlist = np.zeros(onodes) + 0.01
    # Set the value of the index which results from the multiplication of y with 100 to 0.99.
    targetlist[int(y * 100)] = 0.99

    # Train the neural network.
    net.train(xlist, targetlist)

    # After 'test'-number of trainings, test the network.
    if e % test == 0:

        # Create empty arrays for each test to plot the function calculated by the network
        # and calculate mean error and deviation.
        outputarray = []
        errorlist = []

        # Test the network for 300 input values between 0 and 1.
        input = np.linspace(0, 1, num=300)
        for i in range(len(input)):
            # Create the input array the same way as in training.
            inputlist = np.zeros(inodes)
            inputlist[int(input[i] * 100)] = 1.00
Пример #5
0
for e in range(epochs + 1):

    # Set the input to a range between 0.01 and 1.00.
    x = ((random.random()) * 0.99) + 0.01

    # Calculate the result for the given input with the function. Round the result to 2 decimals
    # which is the accuracy, the network is limited to due to the discretization of the output.
    y = (3 * x * (1 - x)).__round__(2)
    # Create the target array with value 0.01.
    targetlist = np.zeros(onodes) + 0.01
    # Set the value of the index which results from the multiplication of y with 100 to 0.99.
    targetlist[int(y * 100)] = 0.99

    # Train the neural network.
    net.train(x, targetlist)

    # After 'test'-number of trainings, test the network.
    if e % test == 0:

        # Create empty arrays for each test to plot the function calculated by the network
        # and calculate mean error and deviation.
        outputarray = []
        errorlist = []

        # Test the network for 300 input values between 0 and 1.
        input = (np.linspace(0, 1, num=300))
        for i in range(len(input)):
            # Ask the network for the result, being an array similar to the target array.
            result = net.query(input[i])
            # The value being compared to the result of the function is the index of the result