def get_embedding(sample):
    dft = np.abs(np.fft.rfft(sample))
    dft -= mean
    dft /= std
    model = autoencoder.Autoencoder(88201, 100)
    model.load_parameters(weight1, bias1, weight2, bias2, weight3, bias3)
    return model(dft).data
Example #2
0
def autoencoder_serial(index_name,
                       start,
                       end,
                       lookback=504):  # 1 year => 252 trading days
    c = composition.Composition(index_name)

    days = utilities.TradingDays(start, end).get_trading_days()
    basket = {}
    for d in days:
        logging.info('getting composition for %s' % d)
        basket[d] = c.get_composition(d)
    basket = pd.DataFrame.from_dict(basket, orient='index')
    r = returns.Returns(basket, start, end)
    returns_matrix_all = r.get_returns_matrix_all(
        frequency='daily')  # match frequency to that of trading days

    ARprediction_aic_auto = pd.DataFrame(
    )  # trading_day, component, lag_length, aic
    lreg_returns_predictions_AR_auto = pd.DataFrame(
    )  # predicting returns using the AR predicted components
    n_day = 0
    for trading_day in range(lookback, len(returns_matrix_all)):
        returns_matrix = returns_matrix_all[n_day:trading_day]
        n_day += 1
        # getting autoencoder components (encoded image), predicting the next trading day using AR.
        a = autoencoder.Autoencoder(returns_matrix=returns_matrix,
                                    n_factors=100)
        ARprediction_aic1, AR_predicted_components_auto = utilities.predict_components_AR(
            principal_components=a.encoded_images)
        ARprediction_aic1['trading_day'] = trading_day + 1
        ARprediction_aic_auto = ARprediction_aic_auto.append(ARprediction_aic1)
        # fitting a linear regression to predict returns from components, and using the AR predicted components (next trading day) to predict the returns. Return_Matrix must be multiplied by 100 (Autoencoder input was multiplied by 100 during training)
        lreg_returns_prediction_auto = utilities.predict_returns_lreg(
            principal_components=a.encoded_images,
            returns_matrix=returns_matrix * 100,
            predicted_components=AR_predicted_components_auto)
        lreg_returns_predictions_AR_auto = lreg_returns_predictions_AR_auto.append(
            pd.DataFrame(lreg_returns_prediction_auto,
                         columns=list(range(1, returns_matrix.shape[1] + 1))),
            ignore_index=True)
        print(lreg_returns_predictions_AR_auto.shape)
        print(lreg_returns_predictions_AR_auto)
    AR_auto_same_direction = np.where(
        lreg_returns_predictions_AR_auto.values * returns_matrix_all[lookback:]
        > 0, 1, 0)
    AR_auto_same_direction_df = pd.DataFrame(
        AR_auto_same_direction,
        columns=list(range(1, returns_matrix_all.shape[1] + 1)))
    AR_auto_same_direction_percent = sum(
        np.where(
            lreg_returns_predictions_AR_auto.values *
            returns_matrix_all[lookback:] > 0, 1, 0)) / len(
                returns_matrix_all[lookback:])
    AR_auto_same_direction_percent_df = pd.DataFrame(
        AR_auto_same_direction_percent)
    return ARprediction_aic_auto, lreg_returns_predictions_AR_auto, AR_auto_same_direction_df, AR_auto_same_direction_percent_df
Example #3
0
def mnist(digit=None,
          torusHack=False,
          autoencoded=False,
          which='train',
          everyNth=1):
    np.random.seed(1)  # TODO Not the right place to do this.
    datasetFile = "mnist.pkl.gz"
    f = gzip.open(datasetFile, 'rb')
    datasets = cPickle.load(f)
    train_set, valid_set, test_set = datasets
    f.close()
    if which == 'train':
        input, output = train_set
    elif which == 'validation':
        input, output = valid_set
    elif which == 'test':
        input, output = test_set
    else:
        assert which in ('train', 'validation', 'test')

    input = input.reshape((-1, 28, 28))
    if digit is not None:
        input = input[output == digit]
    if torusHack:
        # This is a SINGLE sample, translated and multiplied.
        sample = input[0]
        inputRows = []
        for dx in range(28):
            for dy in range(28):
                s = sample.copy()
                s = np.hstack((s[:, dy:], s[:, :dy]))
                s = np.vstack((s[dx:, :], s[:dx, :]))
                inputRows.append(s)
        input = np.array(inputRows)
        input = np.vstack([[input] * 10])
    input = np.random.permutation(input)
    input = input[::everyNth]
    input = input.astype(np.float32)

    if autoencoded:
        autoencoderFile = "../lasagne-demo/conv_ae.pkl"
        mu = 0.13045
        sigma = 0.30729
        ae = autoencoder.Autoencoder(cPickle.load(open(autoencoderFile, 'r')),
                                     mu=mu,
                                     sigma=sigma)
        ae.split()
        encodedInput = ae.encode(input.reshape((-1, 1, 28, 28)))
        assert encodedInput.shape[1] == 40
        # encodedInput = encodedInput.reshape((-1, 8, 5))
        # print encodedInput.shape
        # return encodedInput, (8, 5)
        decodedInput = ae.decode(encodedInput)
        return decodedInput.reshape((-1, 28, 28)), (28, 28)
    else:
        return input, (28, 28)
Example #4
0
 def autoencoder_one_day(self, trading_day):
     returns_matrix = self.returns_matrix_all[trading_day -
                                              self.lookback:trading_day]
     # getting autoencoder components (encoded image), predicting the next trading day using AR
     a = autoencoder.Autoencoder(returns_matrix=returns_matrix,
                                 n_factors=100)
     ARprediction_aic, AR_predicted_components_auto = utilities.predict_components_AR(
         principal_components=a.encoded_images)
     ARprediction_aic['trading_day'] = trading_day + 1
     # fitting a linear regression to predict returns from components, and using the AR predicted components (next trading day) to predict the returns.
     # Return_Matrix must be multiplied by 100 (Autoencoder input was multiplied by 100 during training)
     lreg_returns_prediction_auto_AR = utilities.predict_returns_lreg(
         principal_components=a.encoded_images,
         returns_matrix=returns_matrix * 100,
         predicted_components=AR_predicted_components_auto)
     lreg_returns_prediction_AR_auto = pd.DataFrame(
         lreg_returns_prediction_auto_AR,
         columns=list(range(1, returns_matrix.shape[1] + 1)))
     lreg_returns_prediction_AR_auto['trading_day'] = trading_day + 1
     # predicting returns from AR components using decoder
     deco_returns_prediction_auto_AR = a.decoder.predict(
         AR_predicted_components_auto.to_numpy())
     deco_returns_prediction_AR_auto = pd.DataFrame(
         deco_returns_prediction_auto_AR,
         columns=list(range(1, returns_matrix.shape[1] + 1)))
     deco_returns_prediction_AR_auto['trading_day'] = trading_day + 1
     # predicting the next trading day using ARMA
     ARMA_predicted_components_auto = utilities.predict_components_ARMA(
         principal_components=a.encoded_images)
     # fitting a linear regression to predict returns from components, and using the ARMA predicted components (next trading day) to predict the returns.
     # Return_Matrix must be multiplied by 100 (Autoencoder input was multiplied by 100 during training)
     lreg_returns_prediction_auto_ARMA = utilities.predict_returns_lreg(
         principal_components=a.encoded_images,
         returns_matrix=returns_matrix * 100,
         predicted_components=ARMA_predicted_components_auto)
     lreg_returns_prediction_ARMA_auto = pd.DataFrame(
         lreg_returns_prediction_auto_ARMA,
         columns=list(range(1, returns_matrix.shape[1] + 1)))
     lreg_returns_prediction_ARMA_auto['trading_day'] = trading_day + 1
     # predicting returns from ARMA components using decoder
     deco_returns_prediction_auto_ARMA = a.decoder.predict(
         ARMA_predicted_components_auto.to_numpy())
     deco_returns_prediction_ARMA_auto = pd.DataFrame(
         deco_returns_prediction_auto_ARMA,
         columns=list(range(1, returns_matrix.shape[1] + 1)))
     deco_returns_prediction_ARMA_auto['trading_day'] = trading_day + 1
     print(lreg_returns_prediction_ARMA_auto)
     return ARprediction_aic, lreg_returns_prediction_AR_auto, lreg_returns_prediction_ARMA_auto, deco_returns_prediction_AR_auto, deco_returns_prediction_ARMA_auto
 def pretrain_layer(self, layer_idx, max_iter, dataset):
     """ Pretrains network layer using autoencoder.
     """
     shape = (self.shape[layer_idx],
              self.shape[layer_idx + 1],
              self.shape[layer_idx])
     lae = ae.Autoencoder(0.01, shape, self.session)
     for i in range(max_iter):
         x, y = dataset.train.next_batch(50)
         if layer_idx > 0:
             # Get representation of the input on the layer_idx'th layer
             x = self.feed_forward(layer_idx).eval({self.x: x},
                                                   session=self.session)
         lae.train_batch((x,))
     self.weights[layer_idx] = tf.Variable(lae.weights[0])
     self.biases[layer_idx] = tf.Variable(lae.biases[0])
Example #6
0
def main():
	os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
	DATASET_DIRECTORY = '../data_part1'
	
	X, y, X_hidden = dataset_manip.load_dataset(DATASET_DIRECTORY)
	
	X = resize_image_set(X, (64, 64, 1))
	X_hidden = resize_image_set(X_hidden, (64, 64, 1))
	
	print('X.shape = ' + str(X.shape))
	print('X_hidden.shape = ' + str(X_hidden.shape))
	
	X_train, X_validation, y_train, y_validation = dataset_manip.split_dataset(X, y, rate = 0.9)
	
	model = autoencoder.Autoencoder(input_shape = X.shape[1 : ], model_path = './model_files/autoencoder_model', batch_size = 8, first_run = True)
	print(model.measure_loss(X_validation))
	model.train(X_train, X_validation, num_epochs = 100)
	print(model.measure_loss(X_validation))
    def __load_autoencoder(self):
        """ Loads the autoencoder model. """
        net = autoencoder.Autoencoder(self._input_shape,
                                      eye_shape=self._eye_shape)
        self.__autoencoder = net.build()

        # Load the saved weights.
        logger.info("Loading autoencoder weights from %s." %
                    (self.__autoenc_file))
        self.__autoencoder.load_weights(self.__autoenc_file)

        # Load the cluster data.
        logger.info("Loading clusters from %s." % (self.__cluster_file))
        cluster_file = file(self.__cluster_file, "rb")
        self.__clusters = pickle.load(cluster_file)

        # Freeze the model.
        utils.freeze_all(self.__autoencoder)
Example #8
0
def main():
    autoencoder1 = autoencoder.Autoencoder(
        layer_dims=[784, 128, 36, 128, 784],
        epochs=1,
        activity_regularizer=None,  #regularizers.l1(0),
        use_pretrained=False,
        model_name="128_36")  ### TODO: CHANGE THE NAME!!!!!!!!!

    m = load_model("128_36_autoencoder.h5")
    # m.compile(optimizer='adadelta', loss='binary_crossentropy')
    # print(m.layers[0].get_weights())
    for layer in m.layers:
        weights = layer.get_weights()

    for i in range(len(weights)):
        print(i)
        print(weights[i].shape)

    print(type(weights[0]))
Example #9
0
    def __init__(self,
                 d_input,
                 d_hidden_autoencoders,
                 d_out,
                 corruptions,
                 batch_size,
                 pre_lr=0.001,
                 ft_lr=0.1):
        super(SDA, self).__init__()
        self.d_input = d_input
        self.d_hidden_autoencoders = list(d_hidden_autoencoders)
        self.d_out = d_out
        self.corruptions = corruptions
        self.batch_size = batch_size
        self.pre_lr = pre_lr
        self.ft_lr = ft_lr

        # Create one sequential module containing all autoencoders and logistic layer
        self.sequential = torch.nn.Sequential()

        # Create the Autoencoders
        self.autoencoders_ref = []
        for i, (d, c) in enumerate(zip(d_hidden_autoencoders, corruptions)):
            if i == 0:
                curr_input = d_input
            else:
                curr_input = d_hidden_autoencoders[i - 1]
            dna = ae.Autoencoder(curr_input, d, batch_size, corruption=c)
            self.autoencoders_ref.append("autoencoder_" + str(i))
            self.sequential.add_module(self.autoencoders_ref[-1], dna)

        # Create the Logistic Layer
        self.sequential.add_module(
            "top_linear1",
            torch.nn.Linear(d_hidden_autoencoders[-1], d_out, bias=True))
        self.sequential.top_linear1 = torch.nn.Linear(
            d_hidden_autoencoders[-1], d_out, bias=True)
        self.sequential.top_linear1.weight.data = torch.zeros(
            self.sequential.top_linear1.weight.data.size())
        self.sequential.top_linear1.bias.data = torch.zeros(d_out)
        self.sequential.add_module("softmax", torch.nn.LogSoftmax())
Example #10
0
def autoencoder_example():
    mnist_train = np.fromfile('mnist_training.csv', sep=" ")
    mnist_train = np.array(mnist_train.reshape(256, 1000)).transpose()

    mnist_targets = np.fromfile('mnist_training_targets.csv', sep=" ")
    mnist_targets = np.array(mnist_targets.reshape(10, 1000)).transpose()

    X = mnist_train
    y = mnist_targets
    X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets(
        np.array(X), np.array(y), "digits_rbm_mnist_autoencode")
    X_val = np.vstack([X_val, X_test])
    y_val = np.vstack([y_val, y_test])

    hid_layer = 300

    autoenc = ac.Autoencoder(X.shape[1],
                             hid_layer,
                             denoise=True,
                             denoise_percent=0.5)
    costs, val_costs = autoenc.optimize(X,
                                        iters=3000,
                                        learning_rate=0.05,
                                        val_set=X_val)

    h_x, a = autoenc.forward_prop(X_val)
    print h_x[0:3, 0:10]
    print X_val[0:3, 0:10]

    print "training error:", costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()
Example #11
0
    # Training Data
    cfg = utils.get_cfg(sys.argv[1])
    file_list = utils.load_data(cfg['trainpath'])
    batch = minibatch(file_list, cfg['batchsize'], cfg['width'], cfg['height'],
                      True)

    # Training Parameters
    learning_rate = cfg['learningrate']
    num_steps = cfg['maxepoch']
    batch_size = cfg['batchsize']

    # Network Parameters
    num_hidden_1 = cfg['hidden1num']  # 1st layer num features
    num_hidden_2 = cfg['hidden2num']  # 2nd layer num features (the latent dim)
    num_input = cfg['width'] * cfg['height'] * 1  # data input
    network = ae.Autoencoder(num_input, num_hidden_1, num_hidden_2)

    # tf Graph input (only pictures)
    x = tf.placeholder("float", [None, num_input])
    encoded, decoded = network.autoencode(x)

    # Define loss and optimizer, minimize the squared error
    loss = network.loss(x, decoded)
    optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(loss)

    # Initialize the variables (i.e. assign their default value)
    init = tf.global_variables_initializer()

    # Start Training
    # Start a new TF session
    with tf.Session() as sess:
Example #12
0
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import autoencoder

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
n_samples = mnist.train.num_examples

sess = tf.Session()

np.random.seed(0)
tf.set_random_seed(0)

batch_size = 100
num_epochs = 50
display_step = 2
network_architecture = [784, 500, 500, 2]

ae = autoencoder.Autoencoder(sess, network_architecture, batch_size=batch_size)
ae.fit(mnist.train.images, num_epochs)

x_sample, y_sample = mnist.test.next_batch(5000)
z_mu = ae.transform(x_sample)
plt.figure(figsize=(8, 6))
plt.scatter(z_mu[:, 0], z_mu[:, 1], c=np.argmax(y_sample, 1))
plt.colorbar()
plt.savefig('test_ae.png')
def autoencoder_example():
    mnist_train = np.fromfile('mnist_training.csv', sep=" ")
    mnist_train = np.array(mnist_train.reshape(256, 1000)).transpose()

    mnist_targets = np.fromfile('mnist_training_targets.csv', sep=" ")
    mnist_targets = np.array(mnist_targets.reshape(10, 1000)).transpose()

    X = mnist_train
    y = mnist_targets
    X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets(
        np.array(X), np.array(y), "digits_rbm_mnist_autoencode")
    X_val = np.vstack([X_val, X_test])
    y_val = np.vstack([y_val, y_test])

    hid_layer = 300

    autoenc = ac.Autoencoder(X.shape[1],
                             hid_layer,
                             denoise=True,
                             denoise_percent=0.5)
    costs, val_costs = autoenc.optimize(X,
                                        iters=1500,
                                        learning_rate=0.1,
                                        val_set=X_val)

    print "::: first encoding done :::"
    print "training error:", costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()

    thetas = neur.create_initial_thetas([64, hid_layer, 10], 0.12)
    thetas[0] = autoenc.encode_weights

    thetas, costs, val_costs = neur.gradient_decent(X,
                                                    y,
                                                    learning_rate=0.01,
                                                    hidden_layer_sz=hid_layer,
                                                    iter=5000,
                                                    thetas=thetas,
                                                    X_val=X_val,
                                                    y_val=y_val,
                                                    do_dropout=True,
                                                    dropout_percentage=0.9,
                                                    do_early_stopping=True)

    h_x, a = neur.forward_prop(X_val, thetas)
    print "percentage correct predictions: ", ut.percent_equal(
        ut.map_to_max_binary_result(h_x), y_val)
    print "training error:", costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()
Example #14
0
import pickle
import numpy as np
import autoencoder

training_data, validation_data, test_data = mnist_loader.load_data_wrapper()

# only get x inputs
training_data, test_data = np.array(list(training_data))[:, 0], np.array(
    list(test_data))[:, 0]
training_data, test_data = np.array(
    list(map(lambda x: np.squeeze(x), training_data))), np.array(
        list(map(lambda x: np.squeeze(x), test_data)))
print("processed data")

# [ 784, 400, L50, 400, 784]
autoencoder = autoencoder.Autoencoder([784, 400], [400, 784], 50)
print("finished building autoencoder")

autoencoder.train(training_data, test_data)
print("finished training!")
'''=
# Creates filename and prepares network properties(performance and parameters) for saving
# name follows naming scheme: inputsize_layer1size_layer2size_...outputsize 
file_name = str(net.sizes).replace("[","").replace("]","").replace(" ","").replace(",","_")+'.pkl'
new_net_properties = [net.performance, net.weights, net.biases, ]

Saves network properties (learned parameters and performance). 
If the shape had been previously trained, it will override previous saved file if 
new performance is better. If it is a new shape, it will save to a new file

Example #15
0
    i=0
    for filename in wordlists.fileids():
        brut_data.append(wordlists.words(filename))
        label[i] = random.randint(0,1)#### !!!!!!!!!!
        i+=1
        corpus = utils.TFIDFvectorize(utils.word_to_index(brut_data))
    return corpus, label    

#Main
if __name__ == "__main__":
    corpus, label = load_data()
    n = corpus.shape[1]
    batch_size = corpus.shape[0]
    kw = utils.extract_keywords(corpus,label,2)
    print kw
    idx, corpus = utils.next_batch(batch_size, corpus, np.array([1]))
    pml, pcl = utils.extract_pair(label, 50, idx)
    autoencoder = au.Autoencoder(n, batch_size, 200, 300, corpus, \
                                 kw, pml, pcl)
    autoencoder.init_placeholder()
    autoencoder.init_weights()
    autoencoder.init_biases()
    autoencoder.init_layers()
    autoencoder.init_mask()
    autoencoder.init_losses()
    autoencoder.train(1500, 0.01, [float(sys.argv[2]), float(sys.argv[3]), \
                                   float(sys.argv[4])])
    autoencoder.plot_loss(sys.argv[5], \
                          "Variation des losses : a0= %s a1= %s a2= %s "\
                          % (sys.argv[2],sys.argv[3],sys.argv[4]))
Example #16
0
num_epochs = 500
print_freq = 400
save_freq = 8000
learning_rate = 0.005

filename = "mnist.pt"

MNIST_data = tv.datasets.MNIST("data/MNIST/", train = True, download = True,
                                transform = T.ToTensor())

train_sampler = D.sampler.SubsetRandomSampler(range(0, int(0.9995 * len(MNIST_data))))
test_sampler = D.sampler.SubsetRandomSampler(range(int(0.9995*len(MNIST_data)), len(MNIST_data)-1))
trainloader = D.DataLoader(MNIST_data, sampler=train_sampler)
testloader = D.DataLoader(MNIST_data, sampler=test_sampler)

autoenc = autoencoder.Autoencoder(in_out_size, encoded_size)

def load_saved():
    if isfile("saved_models/" + filename):
        autoenc.load_state_dict(torch.load("saved_models/" + filename))
        print("loading saved model")

if (sys.argv[1] == "train"):
    if "--load" in sys.argv:
        load_saved()
    train.train(autoenc, trainloader, filename, print_freq, save_freq, num_epochs, learning_rate)
elif (sys.argv[1] == "test"):
    load_saved()
    test.test(autoenc, testloader, filename)
else:
    train.train(autoenc, trainloader, filename, print_freq, save_freq, num_epochs, learning_rate)
Example #17
0
l5 = layers.FCLayer(shape5, afuns.LinearActivationFunction(), use_bias=True)
l6 = layers.FCLayer(shape6, afuns.SigmoidActivationFunction(), use_bias=True)
l7 = layers.FCLayer(shape7, afuns.ReluActivationFunction(), use_bias=True)
l8 = layers.FCLayer(shape8, afuns.ReluActivationFunction(), use_bias=True)

mnist = scipy.io.loadmat('./data/mnist-original.mat')
data = mnist['data'].T / 255
labels = mnist['label'].T
data = data[:20000, :]
labels = labels[:20000, :]
train, test, label_train, label_test = train_test_split(data,
                                                        labels,
                                                        test_size=0.33)

layers = [l1, l2, l3, l4, l5, l6, l7, l8]
autoenc = autoencoder.Autoencoder(layers)

initial_weights = autoenc.net.get_weights()

# SGD momentum
autoenc.net.set_weights(initial_weights)
hist_sgd_m = autoenc.run_sgd(train.T, num_epoch=50, display=True)

# SGD
autoenc.net.set_weights(initial_weights)
hist_sgd_m = autoenc.run_sgd(train.T, momentum=0, num_epoch=50, display=True)

# RMSprop
autoenc.net.set_weights(initial_weights)
hist_rmsprop = autoenc.run_rmsprop(train.T, num_epoch=50, display=True)
Example #18
0
'''
Import
'''

import numpy as np

import scipy.misc
import img_dataset as ig
import tensorflow as tf
import autoencoder as md

print('beginning training')
minst = ig.ImgDataset("train-images-idx3-ubyte.gz")

ds = md.Autoencoder(28 * 28)

ds.train()

print('training complete')
Example #19
0
        for j in range(2000):
            rand = ra.randint(0, int(len(data) * .5) - 5)
            ann.partial_fit(data[rand:rand + 5])
    return ann


def train(ann, data):
    for i in range(int(len(data) * .05), len(data), 1):
        ann.partial_fit([data[i]])
    return ann


def test(ann, data):
    errors = np.zeros((len(data), ))
    for i in range(len(data)):
        errors[i] = ann.calc_total_cost([data[i]])
    return errors


if __name__ == '__main__':
    data = get_data('monday_reduced.csv')
    ann = ae.Autoencoder(len(data[0]), len(data[0] * .75))
    ann = train_batches(ann, data)
    print("Done batches")
    ann = train(ann, data)
    print("Done training")
    del data
    data2 = np.genfromtxt('tuesday_reduced.csv')
    errors = test(ann, data2)
    np.savetxt('errors_single.csv', errors)
Example #20
0
        errors[i] = outer_layer.calc_total_cost([error_layer])
    return errors

def stupid():
    print(len(ensemble_errors))
    print(ensemble_errors)

if __name__ == '__main__':
    
    error_layer = np.zeros((len(mapping),))
    train_data = get_data('C:/CICIDS/monday_reduced.csv')
    shape = np.shape(train_data)
   
    
    for a in mapping:
        ensemble_layer.append(ae.Autoencoder(len(a), int(len(a)*.75)))
    outer_layer = ae.Autoencoder(len(ensemble_layer), int(len(ensemble_layer)*.75))
    
    ensemble_layer, outer_layer = train_ensemble_batches(train_data, mapping, ensemble_layer, outer_layer)
    ensemble_layer, outer_layer = train(train_data, shape, mapping, ensemble_layer, outer_layer)
    del train_data
    print("Done Training")
    data2 = get_data('C:/CICIDS/tuesday_reduced.csv')
    errors = test(data2, ensemble_layer, outer_layer, mapping)
    np.savetxt('errors_tuesday_ensemble.csv', errors)


    '''
    
     #Stuff might need to look at later
    #train_data = np.delete(train_data, [61, 60, 59, 58, 57, 56, 49, 33, 32, 31], axis = 1)
    def __init__(self,
                 in_path_photo=None,
                 in_path_oil=None,
                 autoencoder_path=None,
                 num_epochs=100,
                 batch_size=50,
                 learning_rate=0.0002,
                 recon_loss_weight=10,
                 penalty_coef=10,
                 verbose=True):
        """
        This class implements the siamese architecture.
        :param in_path_photo: (string) the file path indicating the location of the training data for photographs.
        :param in_path_oil: the file path indicating the location of the training data for oil.
        :param autoencoder_path: (string) the path where the save files of the autoencoders are stored.
        :param num_epochs: (int) the number of epochs.
        :param batch_size: (int) the batch size.
        :param learning_rate: (int) the learning rate for the Adam optimizer.
        :param recon_loss_weight: (float) the parameter that scales the cycle consistency / reconstruction loss (beta)
        :param penalty_coef: (float) the penalty coefficient for the Wasserstein GAN (lambda)
        :param verbose: (boolean) if true, the training process is printed to console
        """
        self.device = torch.device(
            'cuda' if torch.cuda.is_available() else 'cpu')
        self.use_cuda = torch.cuda.is_available()
        self.in_path_photo = in_path_photo
        self.in_path_oil = in_path_oil
        self.num_epochs = num_epochs
        self.batch_size = batch_size
        self.learning_rate = learning_rate
        self.recon_loss_weight = recon_loss_weight
        self.penalty_coef = penalty_coef
        self.verbose = verbose

        self.start_epoch = 1
        self.d_photo_losses = []
        self.d_oil_losses = []
        self.g_photo_losses = []
        self.g_oil_losses = []
        self.photo_rec_losses = []
        self.oil_rec_losses = []

        self.auto_photo = autoencoder.Autoencoder()
        self.auto_photo.load(autoencoder_path + 'autoencoder_photo_20.pth')
        self.auto_photo.eval()
        self.auto_oil = autoencoder.Autoencoder()
        self.auto_oil.load(autoencoder_path + 'autoencoder_oil_20.pth')
        self.auto_oil.eval()

        self.map_v_to_u = model.Map().cuda() if self.use_cuda else model.Map()
        self.map_u_to_v = model.Map().cuda() if self.use_cuda else model.Map()

        self.v_to_u_optimizer = torch.optim.Adam(self.map_v_to_u.parameters(),
                                                 lr=learning_rate,
                                                 betas=(0.5, 0.9))
        self.u_to_v_optimizer = torch.optim.Adam(self.map_u_to_v.parameters(),
                                                 lr=learning_rate,
                                                 betas=(0.5, 0.9))

        self.discriminator_photo = model.Discriminator().cuda(
        ) if self.use_cuda else model.Discriminator()
        self.discriminator_oil = model.Discriminator().cuda(
        ) if self.use_cuda else model.Discriminator()

        self.d_photo_optimizer = torch.optim.Adam(
            self.discriminator_photo.parameters(),
            lr=learning_rate,
            betas=(0.5, 0.9))
        self.d_oil_optimizer = torch.optim.Adam(
            self.discriminator_oil.parameters(),
            lr=learning_rate,
            betas=(0.5, 0.9))
Example #22
0
import tensorflow as tf

batch_size = 32
epochs = 10
learning_rate = 1e-2
momentum = 9e-1
code_dim = 2
original_dim = bops.shape[1]
hidden_dim = min(original_dim * 10, 100)
weight_lambda = 1e-5

training_dataset = tf.data.Dataset.from_tensor_slices(bops).batch(batch_size)

autoencoder = ae.Autoencoder(original_dim=original_dim,
                             code_dim=code_dim,
                             hidden_dim=hidden_dim,
                             weight_lambda=weight_lambda)
#opt = tf.optimizers.SGD(learning_rate=learning_rate, momentum=momentum)
opt = tf.optimizers.Adam(learning_rate=learning_rate)

with open("loss_%s" % input_filename, "w") as loss_file:
    real_step = 0
    for epoch in range(epochs):
        for step, batch_features in enumerate(training_dataset):
            ae.train(ae.loss, autoencoder, opt, batch_features)
            loss_value = ae.loss(autoencoder, batch_features)
            real_step += 1
            print("%d %f" % (real_step, loss_value), file=loss_file)

encoded_bops = autoencoder.encoder(tf.constant(bops))
test_image_size = test_image.get_shape().as_list()[1:3]
patch_size = 64
stride = 16

ksizes = [1, patch_size, patch_size, 1] 
strides = [1, stride, stride, 1]
rates = [1, 1, 1, 1]

train_image_patches = tf.image.extract_patches(train_image, ksizes, strides, rates, 'VALID')
train_image_patches = tf.reshape(train_image_patches, [-1, patch_size, patch_size, 3])

test_image_patches = tf.image.extract_patches(test_image, ksizes, strides, rates, 'VALID')
test_patches_count = test_image_patches.get_shape().as_list()[1:3]
test_image_patches = tf.reshape(test_image_patches, [-1, patch_size, patch_size, 3])

network = autoencoder.Autoencoder(patch_size)
try:
    network.load("model")
except IOError:
    network.train(test_image_patches, "model")

reconstructed = network(test_image_patches)

debug = True
if debug:
    
    fig=plt.figure(figsize=(15, 4))
    offset = int(368 / stride) * test_patches_count[1] + int(480 / stride)
    cols = 10
    for i in range(1, cols):
        fig.add_subplot(2, cols, i)
Example #24
0
'''
Import
'''


import numpy as np

import scipy.misc
import img_dataset as ig
import tensorflow as tf
import autoencoder as md


ds = md.Autoencoder(28*28, sample=True)

ds.sample()