Exemple #1
0
def ForNet(hidden_layers,fc_layers):
    '''
    Additional path for forward supervision.
    :param hidden_layers: input layer (4D tensor).
    :param fc_layers: number of fc layers.
    :return: alternative prediction.
    '''
    args = netp.get_default_parser(None)
    nF = args.nF

    layer = hidden_layers[nF]

    output_size = 2
    size_map = layer.get_shape().as_list()[-1]

    if fc_layers == 1:
        # Fully connected final layer
        W_fcout = weight_variable([size_map, output_size])
        b_fcout = bias_variable([output_size])

        y_2 = tf.nn.sigmoid(tf.matmul(layer,W_fcout) + b_fcout)

    else:
        print('ERROR: wrong (not implemented) number of fc layers in ForNet')

    return y_2
Exemple #2
0
def write_maxMI_from_fig(str_names, mode, save_name):
    """Plot the data in the given names with the given mode"""
    args = netp.get_default_parser(None)
    [
        font_size, axis_font, bar_font, colorbar_axis, sizes, yticks, xticks,
        title_strs, f, axes
    ] = load_figures(mode, str_names)
    #Go over all the files
    file_name = "Max_MI_%s.txt" % (version)  #("VERSION_NOT_IMPLEMENTED")
    file = open(file_name, "a")
    #    file.write("trstepF lambdaF nF trstepR lambdaR nR maxI(T;Y) correspondingI(X;T) [for each layer]")
    file.write(
        "\n%d %s %d %d %s %d " %
        (trstepF, args.lambdaF, args.nF, trstepR, args.lambdaR, args.nR))
    for i in range(len(str_names)):
        for j in range(len(str_names[i])):
            name_s = str_names[i][j]
            #Load data for the given file
            data_array = utils.get_data(name_s)
            data = np.squeeze(np.array(data_array['information']))
            I_XT_array = np.array(extract_array(data, 'local_IXT'))
            I_TY_array = np.array(extract_array(data, 'local_ITY'))
            maxI_TY = np.amax(I_TY_array, 0)
            correspI_XT = np.argmax(I_TY_array, 0)
            for ii in range(0, I_TY_array.shape[1]):
                file = open(file_name, "a")
                file.write("%.5f %.5f " %
                           (maxI_TY[ii], I_XT_array[correspI_XT[ii], ii]))
            print("Maximum I(T;Y) saved in %s" % file_name)
    file = open(file_name, "a")
    file.close
Exemple #3
0
    def __init__(self, rand_int=0, num_of_samples=None, args=None):
        if args == None:
            args = netp.get_default_parser(num_of_samples)
        self.cov_net = args.cov_net
        self.calc_information = args.calc_information
        self.run_in_parallel = args.run_in_parallel
        self.num_ephocs = args.num_ephocs
        self.learning_rate = args.learning_rate
        self.batch_size = args.batch_size
        self.activation_function = args.activation_function
        self.interval_accuracy_display = args.interval_accuracy_display
        self.save_grads = args.save_grads
        self.num_of_repeats = args.num_of_repeats
        self.calc_information_last = args.calc_information_last
        self.num_of_bins = args.num_of_bins
        self.interval_information_display = args.interval_information_display
        self.save_ws = args.save_ws
        self.name = args.data_dir + args.data_name
        # The arch of the networks
        self.layers_sizes = netp.select_network_arch(args.net_type)
        # The percents of the train data samples
        self.train_samples = np.linspace(1, 100, 199)[[[x * 2 - 2 for x in index] for index in args.inds]]
        # The indexs that we want to calculate the information for them in logspace interval
        self.epochs_indexes = np.unique(
            np.logspace(np.log2(args.start_samples), np.log2(args.num_ephocs), args.num_of_samples, dtype=int,
                        base=2)) - 1
        max_size = np.max([len(layers_size) for layers_size in self.layers_sizes])
        # load data
        self.data_sets = load_data(self.name, args.random_labels)
        # create arrays for saving the data
        self.ws, self.grads, self.information, self.models, self.names, self.networks, self.weights = [
            [[[[None] for k in range(len(self.train_samples))] for j in range(len(self.layers_sizes))]
             for i in range(self.num_of_repeats)] for _ in range(7)]

        self.loss_train, self.loss_test, self.test_error, self.train_error, self.l1_norms, self.l2_norms = \
            [np.zeros((self.num_of_repeats, len(self.layers_sizes), len(self.train_samples), len(self.epochs_indexes)))
             for _ in range(6)]

        params = {'sampleLen': len(self.train_samples),
                  'nDistSmpls': args.nDistSmpls,
                  'layerSizes': ",".join(str(i) for i in self.layers_sizes[0]), 'nEpoch': args.num_ephocs,
                  'batch': args.batch_size,
                  'nRepeats': args.num_of_repeats, 'nEpochInds': len(self.epochs_indexes),
                  'LastEpochsInds': self.epochs_indexes[-1], 'DataName': args.data_name,
                  'lr': args.learning_rate}

        self.name_to_save = args.name + "_" + "_".join([str(i) + '=' + str(params[i]) for i in params])
        params['train_samples'], params['CPUs'], params[
            'directory'], params['epochsInds'] = self.train_samples, NUM_CORES, self.name_to_save, self.epochs_indexes
        self.params = params
        self.rand_int = rand_int
        # If we trained already the network
        self.traind_network = False
Exemple #4
0
    def __init__(self, rand_int=0, num_of_samples=None, args=None):
        if args == None:
            args = netp.get_default_parser(num_of_samples)
        self.conv_net = args.conv_net # boolian indicating if this should be a convolutional architecture. defunct
        self.calc_information = args.calc_information # boolean indicating whether to find the info or not
        self.calcMethod = args.calcMethod # name for the method of calculating the information
        self.run_in_parallel = args.run_in_parallel # boolean for its name
        self.num_epochs = args.num_epochs # how many epochs to run the model
        self.learning_rate = args.learning_rate
        self.batch_size = args.batch_size
        self.activation_function = args.activation_function # 0 is tanh, 1 is ReLU
        self.interval_accuracy_display = args.interval_accuracy_display
        self.save_grads = args.save_grads # bool, used for plotting the gradients
        self.num_of_repeats = args.num_of_repeats
        self.calc_information_last = args.calc_information_last # whether to calculate the information after running the network
        self.num_of_bins = args.num_of_bins
        self.interval_information_display = args.interval_information_display
        self.save_ws = args.save_ws
        self.name = args.data_dir + args.data_name
        # The arch of the networks
        self.layers_sizes = netp.select_network_arch(args.net_type)
        # The percents of the train data samples
        self.train_samples = np.linspace(1, 100, 199)[[[x * 2 - 2 for x in index] for index in args.inds]]
        # The indexs that we want to calculate the information for them in logspace interval
        self.epochs_indexes = np.unique(
                np.logspace(np.log2(args.start_samples), np.log2(args.num_epochs), args.num_of_samples, dtype=int,
                            base=2)) - 1
        max_size = np.max([len(layers_size) for layers_size in self.layers_sizes])
        # load data
        self.data_sets = load_data(self.name, args.random_labels)
        # create arrays for saving the data
        # ws is the activation in the (repeat, layer, training example)
        self.ws, self.grads, self.information, self.models, self.names, self.networks, self.weights = [
                [[[[None] for k in range(len(self.train_samples))] for j in range(len(self.layers_sizes))]
                 for i in range(self.num_of_repeats)] for _ in range(7)]
        # ws has "shape" train_samples by layer_sizes by num_of_repeats
        self.loss_train, self.loss_test, self.test_error, self.train_error, self.l1_norms, self.l2_norms = [np.zeros((self.num_of_repeats, len(self.layers_sizes), len(self.train_samples), len(self.epochs_indexes))) for _ in range(6)]

        params = {'sampleLen': len(self.train_samples),
                  'nDistSmpls': args.nDistSmpls,
                  'layerSizes': ",".join(str(i) for i in self.layers_sizes[0]), 'nEpoch': args.num_epochs, 'batch': args.batch_size,
                  'nRepeats': args.num_of_repeats, 'nEpochInds': len(self.epochs_indexes),
                  'LastEpochsInds': self.epochs_indexes[-1], 'DataName': args.data_name,
                  'lr': args.learning_rate}

        self.name_to_save = args.name + "_" + "_".join([str(i) + '=' + str(params[i]) for i in params])
        params['train_samples'], params['CPUs'], params[
                'directory'], params['epochsInds'] = self.train_samples, NUM_CORES, self.name_to_save, self.epochs_indexes
        self.params = params
        self.rand_int = rand_int
        # If we trained already the network
        self.traind_network = False
Exemple #5
0
def RecNet(hidden_layers,fc_layers,data_size):
    '''
    The main function that defines the RecNet.
    :param hidden_layers: input layer (4D tensor).
    :param data_size: size of the original data.
    :param fc_layers: number of fc layers.
    :return: reconstructed image.
    '''
    args = netp.get_default_parser(None)
    nF = args.nF

    layer = hidden_layers[nF]

    size_map = layer.get_shape().as_list()[-1]

    if fc_layers == 1:
        # Fully connected final layer
        W_fcout = weight_variable([size_map, data_size])
        b_fcout = bias_variable([data_size])

        Recx = tf.nn.sigmoid(tf.matmul(layer, W_fcout) + b_fcout)

    elif fc_layers == 3:
        # First layer: 1 fc layers of 512x3 units
        fcB_size = (data_size+size_map)//2
        W_fcB = weight_variable([int(size_map), fcB_size])
        b_fcB = bias_variable([fcB_size])

        h_fcB = tf.nn.relu(tf.matmul(layer, W_fcB) + b_fcB)

        # Second layer: fc layer of 1024x3 units
        fcX_size = (data_size+size_map)*3//4
        W_fcX = weight_variable([fcB_size,fcX_size])
        b_fcX = bias_variable([fcX_size])

        h_fcX = tf.nn.relu(tf.matmul(h_fcB, W_fcX) + b_fcX)

        # Fully connected final layer
        W_fcout = weight_variable([fcX_size, data_size])
        b_fcout = bias_variable([data_size])

        Recx = tf.nn.sigmoid(tf.matmul(h_fcX, W_fcout) + b_fcout)

    else:
        print('ERROR: wrong number of fc layers in RecNet')

    return Recx
Exemple #6
0
def save_plot_data(str_names, mode, save_name):
    """Save the data in the given names with the given mode"""
    args = netp.get_default_parser(None)
    [
        font_size, axis_font, bar_font, colorbar_axis, sizes, yticks, xticks,
        title_strs, f, axes
    ] = load_figures(mode, str_names)
    #Go over all the files
    for i in range(len(str_names)):
        for j in range(len(str_names[i])):
            name_s = str_names[i][j]
            #Load data for the given file
            data_array = utils.get_data(name_s)
            data = np.squeeze(np.array(data_array['information']))
            I_XT_array = np.array(extract_array(data, 'local_IXT'))
            I_TY_array = np.array(extract_array(data, 'local_ITY'))
            maxI_TY = np.amax(I_TY_array, 0)
            correspI_XT = np.argmax(I_TY_array, 0)
            text_x = 'I_XT_%s.out' % version
            text_y = 'I_YT_%s.out' % version
            np.savetxt(text_x, I_XT_array, delimiter=' ')
            np.savetxt(text_y, I_TY_array, delimiter=' ')
Exemple #7
0
import functools
import tensorflow as tf
import numpy as np
from idnns.networks.utils import _convert_string_dtype
from idnns.networks.models import multi_layer_perceptron
from idnns.networks.models import deepnn
from idnns.networks.models import ForNet
from idnns.networks.models import RecNet
from idnns.networks.ops import *
from idnns.networks import network_paramters as netp
args = netp.get_default_parser(None)
trstepF = args.trstepF
trstepR = args.trstepR
#trstepF = 1000
#trstepR = 1000
lambdaF = args.lambdaF
lambdaR = args.lambdaR


def lazy_property(function):
    attribute = '_cache_' + function.__name__

    @property
    @functools.wraps(function)
    def decorator(self):
        # print hasattr(self, attribute)
        if not hasattr(self, attribute):
            setattr(self, attribute, function(self))
        return getattr(self, attribute)

    return decorator