Exemplo n.º 1
0
def train_lt_network():
    """Train Long-Term Network

    Trains an Evolino LSTM neural network for long-term path planning for
    use in the surgical simulator.

    Returns:
        A copy of the fully-trained path planning neural network.
    """
    # Build up the list of files to use as training set
    filepath = '../../results/'

    training_filenames = [
        'sample1.dat',
        'sample2.dat',
        'sample3.dat',
        'sample4.dat',
    ]

    testing_filename = 'sample5.dat'

    # Get the training data and place it into a dataset
    training_dataset = None

    # Store all training set ratings
    ratings = np.array([])

    for training_filename in training_filenames:
        training_file = filepath + training_filename
        training_data = datastore.retrieve(training_file)

        # Normalize the time input of the data
        training_data = pathutils.normalize_time(training_data,
                                                 t_col=constants.G_TIME_IDX)

        # Add this data sample to the training dataset
        training_dataset = datastore.list_to_dataset(
            training_data[:,
                          constants.G_LT_INPUT_IDX:constants.G_LT_INPUT_IDX +
                          constants.G_LT_NUM_INPUTS],
            training_data[:,
                          constants.G_LT_OUTPUT_IDX:constants.G_LT_OUTPUT_IDX +
                          constants.G_LT_NUM_OUTPUTS],
            dataset=training_dataset)

        # Store the rating of the data
        this_rating = training_data[1:, constants.G_RATING_IDX]
        ratings = np.hstack((ratings, this_rating))

    # Get testing data
    testing_file = filepath + testing_filename
    testing_data = datastore.retrieve(testing_file)

    testing_data = pathutils.normalize_time(testing_data,
                                            t_col=constants.G_TIME_IDX)

    # Store the testing data in a datastore object
    testing_dataset = datastore.list_to_dataset(
        testing_data[:, constants.G_LT_INPUT_IDX:constants.G_LT_INPUT_IDX +
                     constants.G_LT_NUM_INPUTS],
        testing_data[:, constants.G_LT_OUTPUT_IDX:constants.G_LT_OUTPUT_IDX +
                     constants.G_LT_NUM_OUTPUTS],
        dataset=None)

    # Build up a full ratings matrix
    nd_ratings = None

    for rating in ratings:
        this_rating = rating * np.ones((1, constants.G_LT_NUM_OUTPUTS))

        if nd_ratings is None:
            nd_ratings = this_rating
        else:
            nd_ratings = np.vstack((nd_ratings, this_rating))

    # Create network and trainer
    print('>>> Building Network...')
    net = network.LongTermPlanningNetwork()

    print('>>> Initializing Trainer...')
    trainer = network.LongTermPlanningTrainer(evolino_network=net,
                                              dataset=training_dataset,
                                              nBurstMutationEpochs=20,
                                              importance=nd_ratings)

    # Begin the training iterations
    fitness_list = []
    max_fitness = None
    max_fitness_epoch = None

    # Draw the generated path plot
    fig = plt.figure(1, facecolor='white')
    testing_axis = fig.add_subplot(111, projection='3d')

    fig.show()

    # Define paramters for convergence
    CONVERGENCE_THRESHOLD = -0.000005
    REQUIRED_CONVERGENCE_STREAK = 20

    idx = 0
    current_convergence_streak = 0

    while True:
        print('>>> Training Network (Iteration: %3d)...' % (idx + 1))
        trainer.train()

        # Determine fitness of this network
        current_fitness = trainer.evaluation.max_fitness
        fitness_list.append(current_fitness)

        print('>>> FITNESS: %f' % current_fitness)

        # Determine if this is the minimal error network
        if max_fitness is None or max_fitness < current_fitness:
            # This is the minimum, record it
            max_fitness = current_fitness
            max_fitness_epoch = idx

        # Draw the generated path after training
        print('>>> Testing Network...')
        washout_ratio = 1.0 / len(testing_data)
        _, generated_output, _ = net.calculateOutput(
            testing_dataset, washout_ratio=washout_ratio)

        generated_input = testing_data[:len(generated_output), :constants.
                                       G_TOTAL_NUM_INPUTS]

        # Smash together the input and output
        generated_data = np.hstack((generated_input, generated_output))

        print('>>> Drawing Generated Path...')
        pathutils.display_path(testing_axis,
                               generated_data,
                               testing_data,
                               title='Generated Testing Path')

        plt.draw()

        if current_fitness > CONVERGENCE_THRESHOLD:
            # We've encountered a fitness higher than threshold
            current_convergence_streak += 1
        else:
            # Streak ended. Reset the streak counter
            current_convergence_streak = 0

        if current_convergence_streak == REQUIRED_CONVERGENCE_STREAK:
            print('>>> Convergence Achieved: %d Iterations' % idx)
            break

        idx += 1

    # Draw the iteration fitness plot
    plt.figure(facecolor='white')
    plt.cla()
    plt.title('Fitness of RNN over %d Iterations' % (idx - 1))
    plt.xlabel('Training Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)

    plt.plot(range(len(fitness_list)), fitness_list, 'r-')

    plt.annotate('local max',
                 xy=(max_fitness_epoch, fitness_list[max_fitness_epoch]),
                 xytext=(max_fitness_epoch,
                         fitness_list[max_fitness_epoch] + 0.01),
                 arrowprops=dict(facecolor='black', shrink=0.05))

    plt.show()

    # Return a full copy of the trained neural network
    return copy.deepcopy(net)
Exemplo n.º 2
0
def train_path_planning_network():
    """Train Path Planning Network

    Trains an Evolino LSTM neural network for long-term path planning for
    use in the surgical simulator.

    Returns:
        A copy of the fully-trained path planning neural network.
    """
    # Build up the list of files to use as training set
    training_dir = constants.G_TRAINING_DATA_DIR

    # Find all data files in the training data directory
    training_files = pathutils.list_data_files(training_dir)

    # Get the training data and place it into a dataset
    training_dataset = None

    # Store all training set ratings
    ratings = np.array([])

    for training_file in training_files:
        training_data = datastore.retrieve(training_file)

        # Normalize the time input of the data
        training_data = pathutils.normalize_time(training_data, t_col=constants.G_TIME_IDX)

        # Add this data sample to the training dataset
        training_dataset = datastore.list_to_dataset(
            training_data[:,constants.G_RNN_INPUT_IDX:constants.G_RNN_INPUT_IDX+constants.G_RNN_NUM_INPUTS],
            training_data[:,constants.G_RNN_OUTPUT_IDX:constants.G_RNN_OUTPUT_IDX+constants.G_RNN_NUM_OUTPUTS],
            dataset=training_dataset
        )

        # Store the rating of the data
        this_rating = training_data[1:,constants.G_RATING_IDX]
        ratings = np.hstack((ratings, this_rating))


    # Get the starting point information for testing
    output_start_idx = constants.G_RNN_OUTPUT_IDX
    output_end_idx = output_start_idx + constants.G_RNN_NUM_OUTPUTS

    output_initial_condition = training_data[0,output_start_idx:output_end_idx]
    
    # Generate the time sequence input data for testing
    time_steps = constants.G_RNN_GENERATED_TIME_STEPS
    t_input = np.linspace(start=0.0, stop=1.0, num=time_steps)
    t_input = np.reshape(t_input, (len(t_input), 1))

    gate_start_idx = constants.G_GATE_IDX
    gate_end_idx = gate_start_idx + constants.G_NUM_GATE_INPUTS

    # Pull the gate data from the last training dataset
    gate_data = training_data[0:1,gate_start_idx:gate_end_idx]
    gate_data = np.tile(gate_data, (time_steps, 1))

    # Build up a full ratings matrix
    nd_ratings = None

    for rating in ratings:
        this_rating = rating * np.ones((1, constants.G_RNN_NUM_OUTPUTS))

        if nd_ratings is None:
            nd_ratings = this_rating
        else:
            nd_ratings = np.vstack((nd_ratings, this_rating))

    # Create network and trainer
    print('>>> Building Network...')
    net = PathPlanningNetwork()

    print('>>> Initializing Trainer...')
    trainer = PathPlanningTrainer(
        evolino_network=net,
        dataset=training_dataset,
        nBurstMutationEpochs=10,
        importance=nd_ratings
    )

    # Begin the training iterations
    fitness_list = []
    max_fitness = None
    max_fitness_epoch = None

    # Draw the generated path plot
    fig = plt.figure(1, facecolor='white')
    testing_axis = fig.add_subplot(111, projection='3d')

    fig.show()

    idx = 0
    current_convergence_streak = 0
    
    while True:
        print('>>> Training Network (Iteration: %3d)...' % (idx+1))
        trainer.train()

        # Determine fitness of this network
        current_fitness = trainer.evaluation.max_fitness
        fitness_list.append(current_fitness)

        print('>>> FITNESS: %f' % current_fitness)

        # Determine if this is the minimal error network
        if max_fitness is None or max_fitness < current_fitness:
            # This is the minimum, record it
            max_fitness = current_fitness
            max_fitness_epoch = idx

        # Draw the generated path after training
        print('>>> Testing Network...')

        generated_output = net.extrapolate(t_input, [output_initial_condition], len(t_input)-1)
        generated_output = np.vstack((output_initial_condition, generated_output))

        generated_input = np.hstack((t_input, gate_data))

        # Smash together the input and output
        generated_data = np.hstack((generated_input, generated_output))

        print('>>> Drawing Generated Path...')
        pathutils.display_path(testing_axis, generated_data, title='Generated Testing Path')
       
        plt.draw()

        if current_fitness > constants.G_RNN_CONVERGENCE_THRESHOLD:
            # We've encountered a fitness higher than threshold
            current_convergence_streak += 1
        else:
            # Streak ended. Reset the streak counter
            current_convergence_streak = 0

        if current_convergence_streak == constants.G_RNN_REQUIRED_CONVERGENCE_STREAK:
            print('>>> Convergence Achieved: %d Iterations' % idx)
            break
        elif idx == constants.G_RNN_MAX_ITERATIONS - 1:
            print('>>> Reached maximum iterations (%d)' % constants.G_RNN_MAX_ITERATIONS)
            break

        idx += 1

    # Draw the iteration fitness plot
    plt.figure(facecolor='white')
    plt.cla()
    plt.title('Fitness of RNN over %d Iterations' % (idx-1))
    plt.xlabel('Training Iterations')
    plt.ylabel('Fitness')
    plt.grid(True)

    plt.plot(range(len(fitness_list)), fitness_list, 'r-')

    plt.annotate('local max', xy=(max_fitness_epoch, fitness_list[max_fitness_epoch]),
            xytext=(max_fitness_epoch, fitness_list[max_fitness_epoch]+0.01),
            arrowprops=dict(facecolor='black', shrink=0.05))

    plt.show()

    # Return a full copy of the trained neural network
    return copy.deepcopy(net)
Exemplo n.º 3
0
def train_lt_network():
    """Train Long-Term Network

    Trains an Evolino LSTM neural network for long-term path planning for
    use in the surgical simulator.

    Returns:
        A copy of the fully-trained path planning neural network.
    """
    # Build up the list of files to use as training set
    filepath = "../../results/"

    training_filenames = ["sample1.dat", "sample2.dat", "sample3.dat", "sample4.dat"]

    testing_filename = "sample5.dat"

    # Get the training data and place it into a dataset
    training_dataset = None

    # Store all training set ratings
    ratings = np.array([])

    for training_filename in training_filenames:
        training_file = filepath + training_filename
        training_data = datastore.retrieve(training_file)

        # Normalize the time input of the data
        training_data = pathutils.normalize_time(training_data, t_col=constants.G_TIME_IDX)

        # Add this data sample to the training dataset
        training_dataset = datastore.list_to_dataset(
            training_data[:, constants.G_LT_INPUT_IDX : constants.G_LT_INPUT_IDX + constants.G_LT_NUM_INPUTS],
            training_data[:, constants.G_LT_OUTPUT_IDX : constants.G_LT_OUTPUT_IDX + constants.G_LT_NUM_OUTPUTS],
            dataset=training_dataset,
        )

        # Store the rating of the data
        this_rating = training_data[1:, constants.G_RATING_IDX]
        ratings = np.hstack((ratings, this_rating))

    # Get testing data
    testing_file = filepath + testing_filename
    testing_data = datastore.retrieve(testing_file)

    testing_data = pathutils.normalize_time(testing_data, t_col=constants.G_TIME_IDX)

    # Store the testing data in a datastore object
    testing_dataset = datastore.list_to_dataset(
        testing_data[:, constants.G_LT_INPUT_IDX : constants.G_LT_INPUT_IDX + constants.G_LT_NUM_INPUTS],
        testing_data[:, constants.G_LT_OUTPUT_IDX : constants.G_LT_OUTPUT_IDX + constants.G_LT_NUM_OUTPUTS],
        dataset=None,
    )

    # Build up a full ratings matrix
    nd_ratings = None

    for rating in ratings:
        this_rating = rating * np.ones((1, constants.G_LT_NUM_OUTPUTS))

        if nd_ratings is None:
            nd_ratings = this_rating
        else:
            nd_ratings = np.vstack((nd_ratings, this_rating))

    # Create network and trainer
    print(">>> Building Network...")
    net = network.LongTermPlanningNetwork()

    print(">>> Initializing Trainer...")
    trainer = network.LongTermPlanningTrainer(
        evolino_network=net, dataset=training_dataset, nBurstMutationEpochs=20, importance=nd_ratings
    )

    # Begin the training iterations
    fitness_list = []
    max_fitness = None
    max_fitness_epoch = None

    # Draw the generated path plot
    fig = plt.figure(1, facecolor="white")
    testing_axis = fig.add_subplot(111, projection="3d")

    fig.show()

    # Define paramters for convergence
    CONVERGENCE_THRESHOLD = -0.000005
    REQUIRED_CONVERGENCE_STREAK = 20

    idx = 0
    current_convergence_streak = 0

    while True:
        print(">>> Training Network (Iteration: %3d)..." % (idx + 1))
        trainer.train()

        # Determine fitness of this network
        current_fitness = trainer.evaluation.max_fitness
        fitness_list.append(current_fitness)

        print(">>> FITNESS: %f" % current_fitness)

        # Determine if this is the minimal error network
        if max_fitness is None or max_fitness < current_fitness:
            # This is the minimum, record it
            max_fitness = current_fitness
            max_fitness_epoch = idx

        # Draw the generated path after training
        print(">>> Testing Network...")
        washout_ratio = 1.0 / len(testing_data)
        _, generated_output, _ = net.calculateOutput(testing_dataset, washout_ratio=washout_ratio)

        generated_input = testing_data[: len(generated_output), : constants.G_TOTAL_NUM_INPUTS]

        # Smash together the input and output
        generated_data = np.hstack((generated_input, generated_output))

        print(">>> Drawing Generated Path...")
        pathutils.display_path(testing_axis, generated_data, testing_data, title="Generated Testing Path")

        plt.draw()

        if current_fitness > CONVERGENCE_THRESHOLD:
            # We've encountered a fitness higher than threshold
            current_convergence_streak += 1
        else:
            # Streak ended. Reset the streak counter
            current_convergence_streak = 0

        if current_convergence_streak == REQUIRED_CONVERGENCE_STREAK:
            print(">>> Convergence Achieved: %d Iterations" % idx)
            break

        idx += 1

    # Draw the iteration fitness plot
    plt.figure(facecolor="white")
    plt.cla()
    plt.title("Fitness of RNN over %d Iterations" % (idx - 1))
    plt.xlabel("Training Iterations")
    plt.ylabel("Fitness")
    plt.grid(True)

    plt.plot(range(len(fitness_list)), fitness_list, "r-")

    plt.annotate(
        "local max",
        xy=(max_fitness_epoch, fitness_list[max_fitness_epoch]),
        xytext=(max_fitness_epoch, fitness_list[max_fitness_epoch] + 0.01),
        arrowprops=dict(facecolor="black", shrink=0.05),
    )

    plt.show()

    # Return a full copy of the trained neural network
    return copy.deepcopy(net)