Exemplo n.º 1
0
def train():
    model = np.zeros([10, 28, 28])
    totals = np.zeros(10) # accumulate total
    generator_func = read_num('training') # generator to read numbers
    for arr, num in generator_func:
        model[num] += arr
        totals[num] += 1
    produce_heatmap(model, True, True)
    
    return model, totals
Exemplo n.º 2
0
def process(model, totals, v = False):
    test_set = read_num('test')
    prob = np.vectorize(map_prob)
    for arr, num in test_set:
        prob_map = [np.multiply.reduce(np.multiply.reduce(prob(arr, model[i], totals[i]))) for i in range(10)]
        predict = np.argmax(prob_map)
        if v:
            if predict != num:
                pprint_num(arr)
        yield predict, num
Exemplo n.º 3
0
def main():
    """
    This is the starting point of your code!
    You can do what you want here, but the general workflow is explained here.

    This only serves as a base for your operations. You can modify this function as much as you want.
    In addition, you can use other functions to help aid your along the way.

    1. "Train" your model by adding images element wise.
    So if you have a 5, just take all the other existing 5s and add them together.
    Simulanously, make a another array to keep track of how many times each number appeared.

    See: train() for more details (line 80)

    Optional Tip: It might make it easier if you save the trained model in a file somewhere.
    Training is the easier part to implement, and it takes the longest time to do.

    2. Create your model.

    2. "Predict" other sets by taking the total probability that it can be each number (from 0-9)
    2a. To take the total probability, we use a slightly different formula:

    P(N) = ((Similar Pixels in training) + 1) / ((Num of appearances in Training set) + 11)

    2b. Find the Maximum Probability to find the result.
    This technique is called Maximum Likelyhood

    See: predict() for more detail (line )
    """


    ###################################################################################################
    # The following code is sample code to get you aquainted with read_num() and generator functions. #
    # It's your choice if you want to use Numpy or just use regular Python lists.                     #
    ###################################################################################################


    # This is the model used for processing.
    # 0 will be stored in the 0th index, 1 will be stored in the 1st index and so on
    model = np.zeros([10, 28, 28])
    # This will accumulate the total
    total = np.zeros(10)

    # Example code
    # Create a generator to begin reading numbers
    generator_func = read_num('training')

    # You can call generator functions in two ways:
    # Method 1. Use the next() function.
    arr_and_num = generator_func.next()

    # Read_num is a tuple. The 0th index will return a numpy array
    arr_like = arr_and_num[0]
    # The 1st will return the number the image is supposed to represent
    num = arr_and_num[1]

    # Using the pretty print function to visualize the number
    pprint_num(arr_like)
    # Confirm the image is our number
    print(num)

    # Method 2. Using a for loop
    for arr_and_num in generator_func:
        # Won't go through everything, but the logic is the same.
        # For the first one there is 60000 data sets. So you probably
        # Don't want to print them all...
        break