def main():
    input_csv_file_name = sys.argv[1]
    output_csv_file_name = sys.argv[2]

    # input values are in the form of [age, weight, height]
    input_values = Reader.csv(input_csv_file_name)

    cases = [
        (0.001, 100),
        (0.005, 100),
        (0.01, 100),
        (0.05, 100),
        (0.1, 100),
        (0.5, 100),
        (1, 100),
        (5, 100),
        (10, 100),
        #custom
        (0.01, 1000),
    ]

    Reporter.write_output(file_name=output_csv_file_name,
                          content="",
                          should_overwrite_file=True)

    for learning_rate, iterations in cases:
        # reset betas each time
        betas = [
            # intercept 0
            0,
            # age
            0,
            # weight
            0
        ]

        training_inputs = [[x[0], x[1]] for x in input_values]
        expected_classifiers = [x[2] for x in input_values]

        betas = GradientDescent.run(training_inputs=training_inputs,
                                    expected_classifiers=expected_classifiers,
                                    learning_rate=learning_rate,
                                    betas=betas,
                                    iterations=iterations)

        # write lines to output file
        Reporter.write_output(
            file_name=output_csv_file_name,
            content=",".join(
                map(str,
                    [learning_rate, iterations, betas[0], betas[1], betas[2]]))
            + "\n",
        )

        Visualizer.draw_chart(input_values=training_inputs,
                              weights=betas,
                              file_name="figures/figure_" +
                              "_".join(map(str, [learning_rate, iterations])))
예제 #2
0
def main():
    input_csv_file_name = sys.argv[1]
    output_csv_file_name = sys.argv[2]

    # input values are in the form of [feature_1, feature_2, label]
    input_values = Reader.csv(input_csv_file_name)

    # Track previous weights and allow to compare against latest weight to check convergence
    previous_weights = [0, 0, 0]
    weights = None

    Reporter.write_output(file_name=output_csv_file_name,
                          content="",
                          should_overwrite_file=True)

    training_inputs = [[x[0], x[1]] for x in input_values]
    results = [x[2] for x in input_values]

    iterations = 0

    while (previous_weights != weights):
        # Past the initial condition, we want to track the previous_weight
        if (weights != None):
            # update previous weight so we can remember for comparison
            previous_weights = weights
            # import ipdb; ipdb.set_trace()

        # weights will be list in the form of [b or w_0, w_1, w_2]
        weights = PerceptronLearning.run(training_inputs=training_inputs,
                                         results=results,
                                         initial_weights=previous_weights,
                                         iterations=1)

        # write lines to output file
        Reporter.write_output(
            file_name=output_csv_file_name,
            content=','.join(map(str, [weights[1], weights[2], weights[0]])) +
            "\n",
        )

        # create png images of the figures
        Visualizer.draw_chart(input_values=input_values,
                              weights=weights,
                              file_name="figures/figure_" + str(iterations))

        iterations += 1