예제 #1
0
def run_pam(args):
    set_global_seeds(args['seed'])
    dataset = DataLoader(args['dataset'])
    X_train, X_test, X_val, y_train, y_test, y_val = dataset.prepare_train_test_val(
        args)
    model = Perceptron(feature_dim=X_train.shape[-1], margin=args['margin'])
    model.fit(X_train, y_train)
    return model.score(X_test, y_test)
예제 #2
0
def find_best_margin(args):
    """ return `best_margin / 0.1` """
    set_global_seeds(args['seed'])
    dataset = DataLoader(args['dataset'])
    X_train, X_test, X_val, y_train, y_test, y_val = dataset.prepare_train_test_val(
        args)

    results = []
    for margin in MARGINS:
        model = Perceptron(feature_dim=X_train.shape[-1], margin=margin)
        model.fit(X_train, y_train)
        results.append(model.score(X_val, y_val))
    return results
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

from models.perceptron import Perceptron

# Use custom styling from file
matplotlib.rc_file('../plotstyle')

# Data for AND gate
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype='float32')
y = np.array([[0], [0], [0], [1]], dtype='float32')

# Define and train model
model = Perceptron(data=X, labels=y, num_input=2)
model.fit(alpha=0.1, epochs=5000)

# Print results
print('x1\tx2\tlabel\tprediction')
for i in range(X.shape[0]):
    print('{x1}\t{x2}\t{label}\t{prediction}'.format(x1=X[i, 0],
                                                     x2=X[i, 1],
                                                     label=y[i, 0],
                                                     prediction=model.predict(
                                                         X[i, :])[0][0]))

# Plot results
weights = model.w
bias = model.b

x_fit, y_fit = np.linspace(-1, 2, 100), []