Exemple #1
0
def custom_demo(use_old_data=True):
    draw = Draw()
    if use_old_data:
        data = pickle.load("custom_data.p")

    if not use_old_data:
        img = np.concatenate(([1], np.ravel(draw.get_img())))
        label = raw_input("What did you draw? ")
        num_features = np.size(img) - 1  # Subtract 1 for the bias term
        features = [img]
        labels = [label]

        data = Data(features=features, labels=labels, theta=np.zeros((1, 785)),
                    num_features=num_features, num_labels=1, num_examples=1,
                    alpha=0.01, epsilon=0.1, label_set=[label])
        _gradient_ascent(data)

    try:
        while True:
            img = np.concatenate(([1], np.ravel(draw.get_img())))
            prediction = _predict(img, data)
            print 'I think you drew a', data.label_set[prediction]
            label = raw_input("What did you draw? ")

            data.features.append(img)
            data.labels.append(label)
            data.num_examples += 1
            if label not in data.label_set:
                data.label_set.append(label)
                data.num_labels += 1
                new_row = np.zeros((1, data.num_features+1))          #New label means we must add a row to theta
                data.theta = np.vstack((data.theta, new_row))
            _gradient_ascent(data)
    except KeyboardInterrupt:
        save = raw_input("Save data? ").lower()
        if save == 'y' or save == 'yes':
            pickle.dump(data, "custom_data.p")