def __init__(self, x, y1_, y2_, learning_rate):
        """Creates a NonLinearModel.

        Inherits from Model.

        Parameters:
            x: the placeholder for the input tensor.
            y1_: the placeholder for the output 1 tensor.
            y2_: the placeholder for the output 2 tensor.

        Returns:
            A NonLinearModel object.
        """
        model.Model.__init__(self)

        # get sizes for the input and outputs
        num_cols_in = x.get_shape().as_list()[1]
        num_states_in = x.get_shape().as_list()[2]
        num_states_out1 = y1_.get_shape().as_list()[1]
        num_cols_out2 = y2_.get_shape().as_list()[1]
        num_states_out2 = y2_.get_shape().as_list()[2]

        # the first layer flattens the data so that it can be passed through a fully connected layer
        x_flat = utilities.reshape(x, [-1, num_cols_in*num_states_in])

        hidden1 = utilities.fc_layer(x_flat, num_cols_in*num_states_in, num_cols_in*num_states_in*4, layer_name='hidden_1')
        hidden2 = utilities.fc_layer(hidden1, num_cols_in*num_states_in*4, num_cols_in*num_states_in*2, layer_name='hidden_2')

        # the dropout layer reduces over fitting
        dropped, self._keep_prob = utilities.dropout(hidden2)

        # the network splits here:
        # the first softmax layer reduces the output to a percentage chance for each of the output states
        output1 = utilities.fc_layer(dropped, 2*num_cols_in*num_states_in, num_states_out1, 'softmax_1', act=tf.nn.softmax)

        # the second softmax layer reduces the output to a percentage chance for each SNPs output states
        with tf.name_scope('softmax_2'):
            fc_layer = utilities.fc_layer(dropped, 2*num_cols_in*num_states_in, num_states_out2*num_cols_out2, layer_name='identity', act=tf.identity)
            output2 = tf.nn.softmax(utilities.reshape(fc_layer, [-1, num_cols_out2, num_states_out2], name_suffix='3'))

        # each of the loss layers compares the probability distributions between the correspinding outputs to get an error metric for the network's outputs
        self._loss1 = utilities.calculate_cross_entropy(output1, y1_, name_suffix='1')
        self._loss2 = utilities.calculate_cross_entropy(output2, y2_, name_suffix='2')
        # these losses are compined into one for the training
        with tf.name_scope('combined_loss'):
            combined_loss = tf.add(self._loss1, self._loss2)

        # the loss is used with the back propagtion algorithm to use gradient descent based ADAM optimization to teach the network
        self._train_step = utilities.train(learning_rate, combined_loss, training_method=utilities.Optimizer.Adam, name_suffix='1')

        # the accuracies for each output are calculated by comparing them to the correct outputs
        self._accuracy1 = utilities.calculate_epi_accuracy(output1, y1_, name_suffix='1')
        self._accuracy2 = utilities.calculate_snp_accuracy(output2, y2_, name_suffix='2')

        # find the top predicted snps
        self._epi_snps, self._count = utilities.predict_snps(output2)

        # merge all the summaries
        self._merged = tf.merge_all_summaries()
        
    classifier = DeepVamp(dataset,random_seed=1234,
                          learning_rate=0.01,
                          decrease_constant=0.95,
                          hidden_size =[64],
                          filter_shapes=[(5,5)],
                          pool_size=[(4,4)],
                          pool_stride=[(4,4)],
                          update_rule = "None",
                          activation=[T.tanh],
                          batch_size=100
                          )

    # pdb.set_trace()
    #classifier.train(0).shape
    #pdb.set_trace()
    test_dataset = train.X[:100,...]
    #outt = classifier.use(test_dataset.transpose(0,3,1,2))

    classifier = utilities.train(classifier,max_epochs=500)
    import pdb            
    pdb.set_trace()
    outt = classifier.use(test_dataset.transpose(0,3,1,2))
    import cPickle
    f = file('obj.save', 'wb')
    cPickle.dump(classifier, f, protocol=cPickle.HIGHEST_PROTOCOL)
    f.close()




示例#3
0
optimizer = optim.Adam(model.classifier.parameters(),
                       lr=arguments.learning_rate)

# start training network
print("")
print("+ Training network: " + gpu_msg)
print("+ Epochs = {}, ".format(arguments.n_epochs),
      "arch = {}, ".format(arguments.architechture),
      "hidden units = {}, ".format(arguments.hidden_units),
      "dropout = {:.3E}, ".format(arguments.dropout),
      "learning rate = {:.3E}, ".format(arguments.learning_rate))

model, optimizer = utl.train(model,
                             train_loader,
                             valid_loader,
                             criterion,
                             optimizer,
                             arguments.n_epochs,
                             device,
                             print_steps=arguments.print_steps)

# test network
test_loss, test_accuracy = utl.validation(model, test_loader, criterion,
                                          device)
print("")
print(
    "+ Training COMPLETED: final test accuracy = {:.3f}".format(test_accuracy))

# save model
# create checkpoint
checkpoint = {
    'model_name': arguments.architechture,
示例#4
0
    parser.add_argument('--trainData',
                        type=str,
                        help="Path to train data directory for training",
                        default="../train_data/")
    parser.add_argument('--trainModel',
                        type=str,
                        help="Name of model which training will produce",
                        default='model.pkl')
    parser.add_argument('-tD',
                        '--testData',
                        type=str,
                        help="Path to test image for testing",
                        default='../raw/')
    parser.add_argument('-tM',
                        '--testModel',
                        type=str,
                        help="Path to pickle model for testing",
                        default='model.pkl')
    parser.add_argument('-t',
                        '--threshold',
                        type=float,
                        default=1.2,
                        help='Face recognition threshold for facenet')
    parser.add_argument('-v', '--verbose', action='store_true')
    args = parser.parse_args()
    if (args.type == "train"):
        utilities.train(args)
    else:
        model = setup(args)
        print(run(model, args))
    def __init__(self, x, y1_, y2_, learning_rate):
        """Creates a ScalingModel.

        Inherits from Model.

        Parameters:
            x: the placeholder for the input tensor.
            y1_: the placeholder for the output 1 tensor.
            y2_: the placeholder for the output 2 tensor.

        Returns:
            A ScalingModel object.
        """
        model.Model.__init__(self)

        # get sizes for the input and outputs
        num_cols_in = x.get_shape().as_list()[1]
        num_states_out1 = y1_.get_shape().as_list()[1]
        num_cols_out2 = y2_.get_shape().as_list()[1]
        num_states_out2 = y2_.get_shape().as_list()[2]

        self._keep_prob = tf.placeholder(tf.float32)

        # first layer reshapes the input to make it 4d as required by the convolution layers
        x_4d = utilities.reshape(x, [-1, num_cols_in, 3, 1], name_suffix='1')

        # the first convolution layer preserves the shape and increases the number of channels to 8
        conv1 = utilities.conv_layer(x_4d, [3, 3, 1, 8],
                                     padding='SAME',
                                     name_suffix='1')

        # the first pooling layer simply halves the data size along the SNP dimmension
        pool1 = utilities.pool_layer(conv1,
                                     shape=[1, 2, 1, 1],
                                     strides=[1, 2, 1, 1],
                                     name_suffix='1')

        # the second convolution layer preserves the shape and increases the number of channels to 16
        conv2 = utilities.conv_layer(pool1, [3, 3, 8, 16],
                                     padding='SAME',
                                     name_suffix='2')

        # the second pooling layer halves the data size along the SNP dimmension
        pool2 = utilities.pool_layer(conv2,
                                     shape=[1, 2, 1, 1],
                                     strides=[1, 2, 1, 1],
                                     name_suffix='2')

        # the third convolution layer reduces reduces the number of states dimmension to size 1 and increases the number of channels to 32
        conv3 = utilities.conv_layer(pool2, [1, 3, 16, 32],
                                     padding='VALID',
                                     name_suffix='3')

        # the third pooling layer halves the data size along the SNP dimmension
        pool3 = utilities.pool_layer(conv3,
                                     shape=[1, 2, 1, 1],
                                     strides=[1, 2, 1, 1],
                                     name_suffix='3')

        # the next layer flattens the data so that it can be passed through a fully connected layer
        final_shape = pool3.get_shape()
        flatten_size = int(final_shape[1] * final_shape[2] * final_shape[3])
        flatten = utilities.reshape(pool3, [-1, flatten_size], name_suffix='2')

        # the network splits here:
        # the first softmax layer reduces the output to a percentage chance for each of the output states
        hidden1 = utilities.fc_layer(flatten,
                                     flatten_size,
                                     int(flatten_size / 100),
                                     layer_name='hidden_1')
        dropped1, _ = utilities.dropout(hidden1,
                                        name_suffix='1',
                                        keep_prob=self._keep_prob)
        hiddenx = utilities.fc_layer(dropped1,
                                     int(flatten_size / 100),
                                     int(flatten_size / 200),
                                     layer_name='hidden_x')
        droppedx, _ = utilities.dropout(hiddenx,
                                        name_suffix='x',
                                        keep_prob=self._keep_prob)
        output1 = utilities.fc_layer(droppedx,
                                     int(flatten_size / 200),
                                     num_states_out1,
                                     layer_name='softmax_1',
                                     act=tf.nn.softmax)

        # the first fully connected layer halves the data size
        hidden2_1 = utilities.fc_layer(flatten,
                                       flatten_size,
                                       100,
                                       layer_name='hidden_2_1',
                                       act=tf.identity)
        hidden2_2 = utilities.fc_layer(hidden2_1,
                                       100,
                                       int(flatten_size / 2),
                                       layer_name='hidden_2_2')

        # the dropout layer reduces over fitting
        dropped2, _ = utilities.dropout(hidden2_2,
                                        name_suffix='2',
                                        keep_prob=self._keep_prob)

        # the second fully connected layer halves the data size again
        hidden3_1 = utilities.fc_layer(dropped2,
                                       int(flatten_size / 2),
                                       100,
                                       layer_name='hidden_3_1',
                                       act=tf.identity)
        hidden3_2 = utilities.fc_layer(hidden3_1,
                                       100,
                                       int(flatten_size / 4),
                                       layer_name='hidden_3_2')

        dropped3, _ = utilities.dropout(hidden3_2,
                                        name_suffix='3',
                                        keep_prob=self._keep_prob)

        # the second softmax layer reduces the output to a percentage chance for each SNPs output states
        with tf.name_scope('softmax_2'):
            fc_layer_1 = utilities.fc_layer(dropped3,
                                            int(flatten_size / 4),
                                            100,
                                            layer_name='identity_1',
                                            act=tf.identity)
            fc_layer_2 = utilities.fc_layer(fc_layer_1,
                                            100,
                                            num_states_out2 * num_cols_out2,
                                            layer_name='identity_2',
                                            act=tf.identity)
            output2 = tf.nn.softmax(
                utilities.reshape(fc_layer_2,
                                  [-1, num_cols_out2, num_states_out2],
                                  name_suffix='3'))

        # each of the loss layers compares the probability distributions between the correspinding outputs to get an error metric for the network's outputs
        self._loss1 = utilities.calculate_cross_entropy(output1,
                                                        y1_,
                                                        name_suffix='1')
        self._loss2 = utilities.calculate_cross_entropy(output2,
                                                        y2_,
                                                        name_suffix='2')
        # these losses are compined into one for the training
        with tf.name_scope('combined_loss'):
            combined_loss = tf.add(self._loss1, self._loss2)

        # the loss is used with the back propagtion algorithm to use gradient descent based ADAM optimization to teach the network
        self._train_step = utilities.train(
            learning_rate,
            combined_loss,
            training_method=utilities.Optimizer.Adam,
            name_suffix='1')

        # the accuracies for each output are calculated by comparing them to the correct outputs
        self._accuracy1 = utilities.calculate_epi_accuracy(output1,
                                                           y1_,
                                                           name_suffix='1')
        self._accuracy2 = utilities.calculate_snp_accuracy(output2,
                                                           y2_,
                                                           name_suffix='2')

        # find the top predicted snps
        self._epi_snps, self._count = utilities.predict_snps(output2)

        # merge all the summaries
        self._merged = tf.merge_all_summaries()
示例#6
0
def main():
    # Training settings
    parser = argparse.ArgumentParser(description='Models of change detection')
    parser.add_argument('--image-set',
                        type=str,
                        default='A',
                        metavar='I',
                        help='image set to train on: A, B, C, D (default: A)')
    parser.add_argument(
        '--model',
        type=str,
        default='STPNet',
        metavar='M',
        help='model to train: STPNet, RNN, or STPRNN (default: STPNet)')
    parser.add_argument('--noise-std',
                        type=float,
                        default=0.0,
                        metavar='N',
                        help='standard deviation of noise (default: 0.0)')
    parser.add_argument('--syn-tau',
                        type=float,
                        default=6.0,
                        metavar='N',
                        help='STPNet recovery time constant (default: 6.0)')
    parser.add_argument('--hidden-dim',
                        type=int,
                        default=16,
                        metavar='N',
                        help='hidden dimension of model (default: 16)')
    parser.add_argument('--l2-penalty',
                        type=float,
                        default=0.0,
                        metavar='L2',
                        help='L2 penalty on hidden activations (default: 0.0)')
    parser.add_argument('--pos-weight',
                        type=float,
                        default=1.0,
                        metavar='W',
                        help='weight on positive examples (default: 1.0)')
    parser.add_argument('--seq-length',
                        type=int,
                        default=50000,
                        metavar='N',
                        help='length of each trial (default: 50000)')
    parser.add_argument('--delay-dur',
                        type=int,
                        default=500,
                        metavar='N',
                        help='delay duration (default: 500 ms)')
    parser.add_argument('--batch-size',
                        type=int,
                        default=128,
                        metavar='N',
                        help='number of train trial batches (default: 128)')
    parser.add_argument('--epochs',
                        type=int,
                        default=5000,
                        metavar='N',
                        help='epoch train criterion (default: 5000)')
    parser.add_argument('--dprime',
                        type=float,
                        default=2.0,
                        metavar='N',
                        help='dprime train criterion (default: 2.0)')
    parser.add_argument(
        '--patience',
        type=int,
        default=1,
        metavar='N',
        help='number of epochs to wait above baseline (default: 1)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=100,
        metavar='N',
        help='how many epochs to wait before logging training status')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    args = parser.parse_args()
    use_cuda = not args.no_cuda and torch.cuda.is_available()

    device = torch.device("cuda" if use_cuda else "cpu")

    # Set random seed
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)

    # Create train stimulus generator
    train_generator = StimGenerator(image_set=args.image_set,
                                    seed=args.seed,
                                    batch_size=args.batch_size,
                                    seq_length=args.seq_length,
                                    delay_dur=args.delay_dur)

    # Get input dimension of feature vector
    input_dim = len(train_generator.feature_dict[0])

    # Create model
    if args.model == 'STPNet':
        model = STPNet(input_dim=input_dim,
                       hidden_dim=args.hidden_dim,
                       syn_tau=args.syn_tau,
                       noise_std=args.noise_std).to(device)
    elif args.model == 'STPRNN':
        model = STPRNN(input_dim=input_dim,
                       hidden_dim=args.hidden_dim,
                       syn_tau=args.syn_tau,
                       noise_std=args.noise_std).to(device)
    elif args.model == 'RNN':
        model = OptimizedRNN(input_dim=input_dim,
                             hidden_dim=args.hidden_dim,
                             noise_std=args.noise_std).to(device)
    else:
        raise ValueError("Model not found")

    # Define loss function
    criterion = torch.nn.BCEWithLogitsLoss(reduction='none',
                                           pos_weight=torch.tensor(
                                               [args.pos_weight]).to(device))
    optimizer = torch.optim.Adam(model.parameters())

    # Initialize tracking variables
    loss_list = []
    dprime = 0
    dprime_list = []
    wait = 0

    for epoch in range(1, args.epochs + 1):
        # Train model
        loss, dprime = train(args, device, train_generator, model, criterion,
                             optimizer)

        loss_list.append(loss)
        dprime_list.append(dprime)

        if epoch % args.log_interval == 0:
            # Print current progress
            print("Epoch: {}  loss: {:.4f}  dprime: {:.2f}".format(
                epoch, loss, dprime))

        if dprime < args.dprime:
            # Reset wait count
            wait = 0
        else:
            # Increase wait count
            wait += 1
            # Stop training after wait exceeds patience
            if wait >= args.patience:
                break

    # Save trained model
    save_dir = './PARAM/' + args.model
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    save_path = save_dir + '/model_train_seed_' + str(args.seed) + '.pt'
    torch.save(
        {
            'epoch': epoch,
            'loss': loss_list,
            'dprime': dprime_list,
            'state_dict': model.state_dict()
        }, save_path)
示例#7
0
import utilities as util
from matplotlib import pyplot as plt
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import sys

# Get Data
mnist, _ = input_data.read_data_sets('MNIST_data/',
                                     one_hot=True).train.next_batch(10000)

# Create a VAE and Train
network_architecture = {"hdn_dim": 500, 'latent_dim': 2, 'input_dim': 784}
vae_2d = util.train(mnist,
                    network_architecture,
                    learning_rate=0.001,
                    batch_size=100,
                    training_epochs=int(sys.argv[1]),
                    display_step=1,
                    kind='mnist')

# Generate and show a digits in the latent space
nx = ny = 20
x_values = np.linspace(-3, 3, nx)
y_values = np.linspace(-3, 3, ny)

canvas = np.empty((28 * ny, 28 * nx))
for i, yi in enumerate(x_values):
    for j, xi in enumerate(y_values):
        z_mu = np.array([[xi, yi]])
        x_mean = vae_2d.generate(z_mu)
        canvas[(nx - i - 1) * 28:(nx - i) * 28,
    def __init__(self, x, y1_, y2_, learning_rate):
        """Creates a ScalingModel.

        Inherits from Model.

        Parameters:
            x: the placeholder for the input tensor.
            y1_: the placeholder for the output 1 tensor.
            y2_: the placeholder for the output 2 tensor.

        Returns:
            A ScalingModel object.
        """
        model.Model.__init__(self)

        # get sizes for the input and outputs
        num_cols_in = x.get_shape().as_list()[1]
        num_states_out1 = y1_.get_shape().as_list()[1]
        num_cols_out2 = y2_.get_shape().as_list()[1]
        num_states_out2 = y2_.get_shape().as_list()[2]

        self._keep_prob = tf.placeholder(tf.float32)

        # first layer reshapes the input to make it 4d as required by the convolution layers
        x_4d = utilities.reshape(x, [-1, num_cols_in, 3, 1], name_suffix='1')

        # the first convolution layer preserves the shape and increases the number of channels to 8
        conv1 = utilities.conv_layer(x_4d, [3, 3, 1, 8], padding='SAME', name_suffix='1')

        # the first pooling layer simply halves the data size along the SNP dimmension
        pool1 = utilities.pool_layer(conv1, shape=[1, 2, 1, 1], strides=[1, 2, 1, 1], name_suffix='1')

        # the second convolution layer preserves the shape and increases the number of channels to 16
        conv2 = utilities.conv_layer(pool1, [3, 3, 8, 16], padding='SAME', name_suffix='2')

        # the second pooling layer halves the data size along the SNP dimmension
        pool2 = utilities.pool_layer(conv2, shape=[1, 2, 1, 1], strides=[1, 2, 1, 1], name_suffix='2')

        # the third convolution layer reduces reduces the number of states dimmension to size 1 and increases the number of channels to 32
        conv3 = utilities.conv_layer(pool2, [1, 3, 16, 32], padding='VALID', name_suffix='3')

        # the third pooling layer halves the data size along the SNP dimmension
        pool3 = utilities.pool_layer(conv3, shape=[1, 2, 1, 1], strides=[1, 2, 1, 1], name_suffix='3')

        # the next layer flattens the data so that it can be passed through a fully connected layer
        final_shape = pool3.get_shape()
        flatten_size = int(final_shape[1]*final_shape[2]*final_shape[3])
        flatten = utilities.reshape(pool3, [-1, flatten_size], name_suffix='2')

        # the network splits here:
        # the first softmax layer reduces the output to a percentage chance for each of the output states
        hidden1 = utilities.fc_layer(flatten, flatten_size, int(flatten_size/100), layer_name='hidden_1')
        dropped1, _ = utilities.dropout(hidden1, name_suffix='1', keep_prob=self._keep_prob)
        hiddenx = utilities.fc_layer(dropped1, int(flatten_size/100), int(flatten_size/200), layer_name='hidden_x')
        droppedx, _ = utilities.dropout(hiddenx, name_suffix='x', keep_prob=self._keep_prob)
        output1 = utilities.fc_layer(droppedx, int(flatten_size/200), num_states_out1, layer_name='softmax_1', act=tf.nn.softmax)

        # the first fully connected layer halves the data size
        hidden2_1 = utilities.fc_layer(flatten, flatten_size, 100, layer_name='hidden_2_1', act=tf.identity)
        hidden2_2 = utilities.fc_layer(hidden2_1, 100, int(flatten_size/2), layer_name='hidden_2_2')

        # the dropout layer reduces over fitting
        dropped2, _ = utilities.dropout(hidden2_2, name_suffix='2', keep_prob=self._keep_prob)

        # the second fully connected layer halves the data size again
        hidden3_1 = utilities.fc_layer(dropped2, int(flatten_size/2), 100, layer_name='hidden_3_1', act=tf.identity)
        hidden3_2 = utilities.fc_layer(hidden3_1, 100, int(flatten_size/4), layer_name='hidden_3_2')

        dropped3, _ = utilities.dropout(hidden3_2, name_suffix='3', keep_prob=self._keep_prob)

        # the second softmax layer reduces the output to a percentage chance for each SNPs output states
        with tf.name_scope('softmax_2'):
            fc_layer_1 = utilities.fc_layer(dropped3, int(flatten_size/4), 100, layer_name='identity_1', act=tf.identity)
            fc_layer_2 = utilities.fc_layer(fc_layer_1, 100, num_states_out2*num_cols_out2, layer_name='identity_2', act=tf.identity)
            output2 = tf.nn.softmax(utilities.reshape(fc_layer_2, [-1, num_cols_out2, num_states_out2], name_suffix='3'))

        # each of the loss layers compares the probability distributions between the correspinding outputs to get an error metric for the network's outputs
        self._loss1 = utilities.calculate_cross_entropy(output1, y1_, name_suffix='1')
        self._loss2 = utilities.calculate_cross_entropy(output2, y2_, name_suffix='2')
        # these losses are compined into one for the training
        with tf.name_scope('combined_loss'):
            combined_loss = tf.add(self._loss1, self._loss2)

        # the loss is used with the back propagtion algorithm to use gradient descent based ADAM optimization to teach the network
        self._train_step = utilities.train(learning_rate, combined_loss, training_method=utilities.Optimizer.Adam, name_suffix='1')

        # the accuracies for each output are calculated by comparing them to the correct outputs
        self._accuracy1 = utilities.calculate_epi_accuracy(output1, y1_, name_suffix='1')
        self._accuracy2 = utilities.calculate_snp_accuracy(output2, y2_, name_suffix='2')

        # find the top predicted snps
        self._epi_snps, self._count = utilities.predict_snps(output2)

        # merge all the summaries
        self._merged = tf.merge_all_summaries()
示例#9
0
# examine the state space
state = env_info.vector_observations[0]
print('States look like:', state)
state_size = len(state)
print('States have length:', state_size)


if solver_to_use[0]:
    # # #  --------- Vanilla DQN agent --------- # # #
    print('\nVanilla DQN')

    # initialize agent
    agent = DQNAgent(state_size, action_size, seed=0)

    # train with linear epsilon decrease
    scores = train(agent, env, n_episodes=2000, eps_start=1, eps_end=0.1, eps_decay=0.995, decline='lin')

    if not os.path.exists('checkpoints'):
        os.mkdir('checkpoints')

    # save network weights
    torch.save(agent.network_local.state_dict(), 'checkpoints/checkpoint_vanilla.pth')

    if not os.path.exists('plots'):
        os.mkdir('plots')

    # plot the scores
    plot_scores(scores, filename='plots/plot_vanilla.png')

    # close environment
    env.close()