Exemplo n.º 1
0
def main(loss_name, prior=0.7, n_s=500, n_u=500, end_to_end=False):
    if loss_name == 'squared':
        SU = SU_SL
    elif loss_name == 'double-hinge':
        SU = SU_DH

    # load dataset
    n_test = 100
    x_s, x_u, x_test, y_test = load_dataset(n_s, n_u, n_test, prior)
    x_train, y_train = convert_su_data_sklearn_compatible(x_s, x_u)

    if end_to_end:
        # use KM2 (Ramaswamy et al., 2016)
        est_prior = class_prior_estimation(x_s, x_u)
    else:
        # use the pre-fixed class-prior
        est_prior = prior

    # cross-validation
    lam_list = [1e-01, 1e-04, 1e-07]
    score_cv_list = []
    for lam in lam_list:
        clf = SU(prior=est_prior, lam=lam)
        score_cv = cross_val_score(clf, x_train, y_train, cv=5).mean()
        score_cv_list.append(score_cv)

    # training with the best hyperparameter
    lam_best = lam_list[np.argmax(score_cv_list)]
    clf = SU(prior=est_prior, lam=lam_best)
    clf.fit(x_train, y_train)

    # test prediction
    y_pred = clf.predict(x_test)
    accuracy = accuracy_score(y_test, y_pred)
    print(accuracy)
Exemplo n.º 2
0
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from misc import load_dataset
from model import model
from predict import predict

# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset(
)

# Example of a picture
index = 8
plt.imshow(train_set_x_orig[index])
plt.show()
print("y = " + str(train_set_y[:, index]) + ", it's a '" +
      classes[np.squeeze(train_set_y[:, index])].decode("utf-8") +
      "' picture.")

m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]

print("Number of training examples: m_train = " + str(m_train))
print("Number of testing examples: m_test = " + str(m_test))
print("Height/Width of each image: num_px = " + str(num_px))
print("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")
print("train_set_x shape: " + str(train_set_x_orig.shape))
Exemplo n.º 3
0
import numpy as np
import matplotlib.pyplot as plt
import sklearn
import sklearn.datasets
from misc import predict, load_dataset, plot_decision_boundary, predict_dec
from model import model

# %matplotlib inline
plt.rcParams['figure.figsize'] = (7.0, 4.0)  # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

# load image dataset: blue/red dots in circles
train_X, train_Y, test_X, test_Y = load_dataset()

parameters = model(train_X, train_Y, initialization="zeros")
print("On the train set:")
predictions_train = predict(train_X, train_Y, parameters)
print("On the test set:")
predictions_test = predict(test_X, test_Y, parameters)
plt.title("Model with Zeros initialization")
axes = plt.gca()
axes.set_xlim([-1.5, 1.5])
axes.set_ylim([-1.5, 1.5])
plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X,
                       train_Y)

parameters = model(train_X, train_Y, initialization="random")
print("On the train set:")
predictions_train = predict(train_X, train_Y, parameters)
print("On the test set:")
Exemplo n.º 4
0
import matplotlib.pyplot as plt

from model import model
from misc import plot_decision_boundary, load_dataset, predict, predict_dec

# %matplotlib inline
plt.rcParams['figure.figsize'] = (7.0, 4.0)  # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

train_X, train_Y = load_dataset()

# train 3-layer model
layers_dims = [train_X.shape[0], 5, 2, 1]
parameters = model(train_X, train_Y, layers_dims, optimizer="gd")

# Predict
predictions = predict(train_X, train_Y, parameters)

# Plot decision boundary
plt.title("Model with Gradient Descent optimization")
axes = plt.gca()
axes.set_xlim([-1.5, 2.5])
axes.set_ylim([-1, 1.5])
plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X,
                       train_Y)

# train 3-layer model
layers_dims = [train_X.shape[0], 5, 2, 1]
parameters = model(train_X,
                   train_Y,