Ejemplo n.º 1
0
    theta, input_size, hidden_size_l1, lamb, sparsity_param, beta, train_data)
sae1_opt_theta, loss, d = (scipy.optimize.fmin_l_bfgs_b(fn,
                                                        sae1_theta,
                                                        maxfun=maxfun,
                                                        iprint=1))

if DISPLAY:
    W1, W2, b1, b2 = autoencoder.unflatten(sae1_opt_theta, input_size,
                                           hidden_size_l1)
    util.display_network(W1.T)

# === Step 3: Train the second sparse autoencoder ===
#
# Train the second sparse autoencoder on the first autoencoder features.
sae1_features = autoencoder.feedforward_autoencoder(sae1_opt_theta,
                                                    hidden_size_l1, input_size,
                                                    train_data)

# Randomly initialize the parameters
sae2_theta = autoencoder.initialize_parameters(hidden_size_l2, hidden_size_l1)

fn = lambda theta: autoencoder.sparse_autoencoder_loss(
    theta, hidden_size_l1, hidden_size_l2, lamb, sparsity_param, beta,
    sae1_features)
sae2_opt_theta, loss, d = (scipy.optimize.fmin_l_bfgs_b(fn,
                                                        sae2_theta,
                                                        maxfun=maxfun,
                                                        iprint=1))

if DISPLAY:
    W11, W21, b11, b21 = autoencoder.unflatten(sae1_opt_theta, input_size,
theta = autoencoder.initialize_parameters(hidden_size, input_size)

# The single-parameter function to minimize
fn = lambda theta: autoencoder.sparse_autoencoder_loss(
  theta, input_size, hidden_size,lamb, sparsity_param, beta, unlabeled_data)
# Find `opt_theta` by running the sparse autoencoder on unlabeled
# training images.
opt_theta, loss, d = (
  scipy.optimize.fmin_l_bfgs_b(fn, theta, maxfun=maxfun, iprint=1, m=20))

# Visualize weights
W1, W2, b1, b2 = autoencoder.unflatten(opt_theta, input_size, hidden_size)
util.display_network(W1.T)

# === Step 3: Extract Features from the Supervised Dataset ===
train_features = autoencoder.feedforward_autoencoder(
  opt_theta, hidden_size, input_size, train_data)
test_features = autoencoder.feedforward_autoencoder(
  opt_theta, hidden_size, input_size, test_data)

# === Step 4: Train the softmax classifier ===
lamb = 1e-4
num_classes = len(set(train_labels))
softmax_model = softmax.train(hidden_size, num_classes, lamb,
                              train_features, train_labels, maxfun=100)

# === Step 5: Testing ===
#
# Compute Predictions on the test set (testFeatures) using
# `softmax.predict`.
pred = softmax.predict(softmax_model, test_features)
acc = (test_labels == pred).mean()
#     beta, trainData), ...
#     sae1Theta, options);

fn = lambda theta: autoencoder.sparse_autoencoder_loss(
    theta, input_size, hidden_size_l1, lamb, sparsity_param, beta, train_data
)
sae1_opt_theta, loss, d = scipy.optimize.fmin_l_bfgs_b(fn, sae1_theta, maxfun=maxfun, iprint=1)

if DISPLAY:
    W1, W2, b1, b2 = autoencoder.unflatten(sae1_opt_theta, input_size, hidden_size_l1)
    util.display_network(W1.T)

# === Step 3: Train the second sparse autoencoder ===
#
# Train the second sparse autoencoder on the first autoencoder features.
sae1_features = autoencoder.feedforward_autoencoder(sae1_opt_theta, hidden_size_l1, input_size, train_data)

# Randomly initialize the parameters
sae2_theta = autoencoder.initialize_parameters(hidden_size_l2, hidden_size_l1)

fn = lambda theta: autoencoder.sparse_autoencoder_loss(
    theta, hidden_size_l1, hidden_size_l2, lamb, sparsity_param, beta, sae1_features
)
sae2_opt_theta, loss, d = scipy.optimize.fmin_l_bfgs_b(fn, sae2_theta, maxfun=maxfun, iprint=1)

if DISPLAY:
    W11, W21, b11, b21 = autoencoder.unflatten(sae1_opt_theta, input_size, hidden_size_l1)
    W12, W22, b12, b22 = autoencoder.unflatten(sae2_opt_theta, hidden_size_l1, hidden_size_l2)
    # TODO(zellyn): figure out how to display a 2-level network
    # display_network(log(W11' ./ (1-W11')) * W12');
Ejemplo n.º 4
0
fn = lambda theta: autoencoder.sparse_autoencoder_loss(
    theta, input_size, hidden_size, lamb, sparsity_param, beta, unlabeled_data)
# Find `opt_theta` by running the sparse autoencoder on unlabeled
# training images.
opt_theta, loss, d = (scipy.optimize.fmin_l_bfgs_b(fn,
                                                   theta,
                                                   maxfun=maxfun,
                                                   iprint=1,
                                                   m=20))

# Visualize weights
W1, W2, b1, b2 = autoencoder.unflatten(opt_theta, input_size, hidden_size)
util.display_network(W1.T)

# === Step 3: Extract Features from the Supervised Dataset ===
train_features = autoencoder.feedforward_autoencoder(opt_theta, hidden_size,
                                                     input_size, train_data)
test_features = autoencoder.feedforward_autoencoder(opt_theta, hidden_size,
                                                    input_size, test_data)

# === Step 4: Train the softmax classifier ===
lamb = 1e-4
num_classes = len(set(train_labels))
softmax_model = softmax.train(hidden_size,
                              num_classes,
                              lamb,
                              train_features,
                              train_labels,
                              maxfun=100)

# === Step 5: Testing ===
#