Пример #1
0
    def addAllGridPointDeterminizingAbstractionBoolean(self, controller):

        complete_dataset = self.createDeterminizingCompleteDataset(controller)

        new_x = []

        self.x = complete_dataset[:, 0]  # x_all_gp
        self.size = len(self.x)

        bed = BinaryEncoderDecoder()

        n_x = len(bed.sntob(self.x_bounds[1]))  # Input bit length
        self.x_bounds = [self.getLowestX(), self.getHighestX()]
        self.x_bounds = [
            bed.ntoba(self.x_bounds[0], n_x),
            bed.ntoba(self.x_bounds[1], n_x)
        ]

        # convert the input and output pair to array of binary
        # and add the flag bit
        for i in range(self.size):
            new_x.append(bed.ntoba(self.x[i], n_x))

        self.x = new_x
        self.x_dim = n_x
        self.x_eta = []
        for i in range(self.x_dim):
            self.x_eta.append(0.5)

        self.y = (complete_dataset[:, 1:]).tolist()
        self.y_dim = controller.input_total_gp + 1
        self.y_eta = []
        for i in range(self.y_dim):
            self.y_eta.append(0.5)
Пример #2
0
    def __init__(self):
        self.type = None
        self.nn = MLP()
        self.training_method = None
        self.activation_function = None
        self.dropout_rate = 0.0

        self.training = True
        self.learning_rate = 0.1
        self.fitness_threshold = 0.75
        self.epoch_threshold = -1
        self.batch_size = 100
        self.shuffle_rate = 2500
        self.display_step = 1000

        self.epoch = 0

        self.layers = []

        self.data_set = None

        self.bed = BinaryEncoderDecoder()
        self.utils = Utilities()

        self.debug_mode = False

        # Plotting variables
        self.losses = []
        self.fitnesses = []
        self.iterations = []

        self.save_location = './nn/log/'
Пример #3
0
    def addAllGridPointDeterminizingSeparateBoolean(self, controller):

        complete_dataset = self.createDeterminizingCompleteDataset(controller)

        new_x = []

        self.x = complete_dataset[:, 0]  # x_all_gp
        self.size = len(self.x)

        bed = BinaryEncoderDecoder()

        new_x = self.createStateSeparateBooleanBinary(controller)
        n_x = len(new_x[0])
        # print(new_x)
        # print(n_x)

        self.x = new_x
        self.x_dim = n_x
        self.x_eta = []
        for i in range(self.x_dim):
            self.x_eta.append(0.5)

        self.y = (complete_dataset[:, 1:]).tolist()
        self.y_dim = controller.input_total_gp + 1
        self.y_eta = []
        for i in range(self.y_dim):
            self.y_eta.append(0.5)
Пример #4
0
    def addAllGridPointNonDeterministicSeparateBoolean(self, input_file,
                                                       controller):
        complete_dataset = self.createNonDeterministicCompleteDataset(
            input_file, controller)

        new_x = []
        new_y = []

        self.x = complete_dataset[:, 0]  # x_all_gp
        self.y = complete_dataset[:, 1]  # y_all_gp
        self.size = len(self.x)

        bed = BinaryEncoderDecoder()

        self.x_bounds = [self.getLowestX(), self.getHighestX()]
        new_x = self.createStateSeparateBooleanBinary(controller)
        n_x = len(new_x[0])
        # print(new_x)
        self.x_bounds = [new_x[0], new_x[-1]]

        self.y_bounds = [self.getLowestY(), self.getHighestY()]
        n_y = len(bed.sntob(self.y_bounds[1]))  # Output bit length
        self.y_bounds = [
            bed.ntoba(self.y_bounds[0], n_y),
            bed.ntoba(self.y_bounds[1], n_y)
        ]

        # convert the input and output pair to array of binary
        # and add the flag bit
        for i in range(self.size):
            new_y.append(bed.ntoba(self.y[i], n_y))

        self.x = new_x
        self.x_dim = n_x
        self.x_eta = []
        for i in range(self.x_dim):
            self.x_eta.append(0.5)

        self.y = new_y
        self.y_dim = n_y
        self.y_eta = []
        for i in range(self.y_dim):
            self.y_eta.append(0.5)
Пример #5
0
    def addAllGridPointDeterministicAbstractionClassification(
            self, controller):

        complete_dataset = self.createDeterministicCompleteDataset(controller)

        new_x = []
        new_y = []

        self.x = complete_dataset[:, 0]  # x_all_gp
        self.y = complete_dataset[:, 1]  # y_all_gp
        self.size = len(self.x)

        self.x_bounds = [self.getLowestX(), self.getHighestX()]
        new_x = (self.x[:, None]).tolist()
        self.x = new_x
        self.x_dim = 1
        self.x_eta = []
        for i in range(self.x_dim):
            self.x_eta.append(0.5)

        # binary for output
        bed = BinaryEncoderDecoder()
        n_y = len(bed.sntob(self.y_bounds[1]))  # Output bit length

        # change the bounds to binary
        self.y_bounds = [self.getLowestY(), self.getHighestY()]
        self.y_bounds = [
            bed.ntoba(self.y_bounds[0], n_y),
            bed.ntoba(self.y_bounds[1], n_y)
        ]

        # convert the input and output pair to array of binary
        for i in range(self.size):
            add_flag = bed.ntoba(self.y[i], n_y)
            add_flag.append(complete_dataset[i, 2])
            new_y.append(add_flag)

        self.y = new_y
        self.y_dim = n_y + 1
        self.y_eta = []
        for i in range(self.y_dim):
            self.y_eta.append(0.5)
Пример #6
0
    def addAllGridPointDeterministicAbstractionBoolean(self, controller):

        complete_dataset = self.createDeterministicCompleteDataset(controller)

        new_x = []
        new_y = []

        self.x = complete_dataset[:, 0]  # x_all_gp
        self.y = complete_dataset[:, 1]  # y_all_gp
        self.size = len(self.x)

        bed = BinaryEncoderDecoder()

        n_x = len(bed.sntob(self.x_bounds[1]))  # Input bit length
        self.x_bounds = [self.getLowestX(), self.getHighestX()]
        self.x_bounds = [
            bed.ntoba(self.x_bounds[0], n_x),
            bed.ntoba(self.x_bounds[1], n_x)
        ]

        n_y = len(bed.sntob(self.y_bounds[1]))  # Output bit length
        self.y_bounds = [self.getLowestY(), self.getHighestY()]
        self.y_bounds = [
            bed.ntoba(self.y_bounds[0], n_y),
            bed.ntoba(self.y_bounds[1], n_y)
        ]

        # convert the input and output pair to array of binary
        # and add the flag bit
        for i in range(self.size):
            new_x.append(bed.ntoba(self.x[i], n_x))
            add_flag = bed.ntoba(self.y[i], n_y)
            add_flag.append(complete_dataset[i, 2])
            new_y.append(add_flag)

        self.x = new_x
        self.x_dim = n_x
        self.x_eta = []
        for i in range(self.x_dim):
            self.x_eta.append(0.5)

        self.y = new_y
        self.y_dim = n_y + 1
        self.y_eta = []
        for i in range(self.y_dim):
            self.y_eta.append(0.5)
Пример #7
0
    def formatToBinary(self):
        bed = BinaryEncoderDecoder()

        new_x = []
        new_y = []

        # the number of bit needed to store the input and output on binary format
        n_x = len(bed.sntob(self.x_bounds[1]))  # Input bit length
        n_y = len(bed.sntob(self.y_bounds[1]))  # Output bit length

        # convert the input and output pair to array of binary
        for i in range(self.size):
            new_x.append(bed.ntoba(self.x[i], n_x))
            new_y.append(bed.ntoba(self.y[i], n_y))

        # set x and y to converted x and y
        self.x = new_x
        self.y = new_y

        # set binary upper and lower bounds
        self.x_bounds = [
            bed.ntoba(self.x_bounds[0], n_x),
            bed.ntoba(self.x_bounds[1], n_x)
        ]
        self.y_bounds = [
            bed.ntoba(self.y_bounds[0], n_y),
            bed.ntoba(self.y_bounds[1], n_y)
        ]

        # set the dimension of x and y (elements per element of x and y)
        self.x_dim = n_x
        self.y_dim = n_y

        # set etas of x and y (0.5 for binary)
        self.x_eta = []
        self.y_eta = []
        for i in range(n_x):
            self.x_eta.append(0.5)
        for i in range(n_y):
            self.y_eta.append(0.5)

        self.size = len(self.x)
class NeuralNetworkManager:
    def __init__(self):
        self.type = None
        self.nn = MLP()
        self.training_method = None
        self.hidden_activation_function = None
        self.output_activation_function = None
        self.dropout_rate = 0.0
        
        self.training = True
        self.learning_rate = 0.1
        self.fitness_threshold = 0.75
        self.epoch_threshold = -1
        self.batch_size = 100
        self.shuffle_rate = 2500
        self.display_step = 1000
        
        self.epoch = 0
        
        self.layers = []
        
        self.data_set = None
        
        self.bed = BinaryEncoderDecoder()
        self.utils = Utilities()
        self.controller = StaticController()
        
        self.debug_mode = False
        
        # Plotting variables
        self.losses = []
        self.fitnesses = []
        self.iterations = []
        
        self.save_location = './nn/log/'

        self.encode = None

        self.non_det = False
        
    # Getters and setters
    def getType(self): return self.type
    def getTrainingMethod(self): return self.training_method
    def getActivationFunctionHidden(self): return self.hidden_activation_function
    def getActivationFunctionOutput(self): return self.output_activation_function
    def getLearningRate(self): return self.learning_rate
    def getFitnessThreshold(self): return self.fitness_threshold
    def getBatchSize(self): return self.batch_size
    def getDisplayStep(self): return self.display_step
    def getEpoch(self): return self.epoch
    def getEpochThreshold(self): return self.epoch_threshold
    def getDropoutRate(self): return self.dropout_rate
    def getShuffleRate(self): return self.shuffle_rate
    def getSaveLocation(self): return self.save_location
    
    def setType(self, value): self.type = value
    def setTrainingMethod(self, optimizer): self.training_method = optimizer
    def setActivationFunctionHidden(self, activation_function): self.hidden_activation_function = activation_function
    def setActivationFunctionOutput(self, activation_function): self.output_activation_function = activation_function
    def setLearningRate(self, value): self.learning_rate = value
    def setFitnessThreshold(self, value): self.fitness_threshold = value
    def setBatchSize(self, value): self.batch_size = value
    def setDisplayStep(self, value): self.display_step = value
    def setEpochThreshold(self, value): self.epoch_threshold = value
    def setDropoutRate(self, value): self.dropout_rate = value
    def setShuffleRate(self, value): self.shuffle_rate = value
    def setSaveLocation(self, value): self.save_location = value
    def setDebugMode(self, value): self.debug_mode = value
    def setDataSet(self, data_set): self.data_set = data_set
    
    def setEncodeTypes(self, value): self.encode = value
    
    # Hidden layer generation functions
    # Linearly increase/decrease neurons per hidden layer based on the input and ouput neurons
    def linearHiddenLayers(self, num_hidden_layers):
        self.layers = []
        
        x_dim = self.data_set.getXDim()
        y_dim = self.data_set.getYDim()
        
        a = (y_dim - x_dim)/(num_hidden_layers + 1)
        
        self.layers.append(x_dim)
        for i in range(1, num_hidden_layers + 1):
            self.layers.append(round(x_dim + a*i))
        self.layers.append(y_dim)
        
        return self.layers
    
    # Rectangular hidden layer
    def rectangularHiddenLayers(self, width, height):
        self.layers = []
        
        self.layers.append(self.data_set.getXDim())
        for i in range(width):
            self.layers.append(height)
        self.layers.append(self.data_set.getYDim())
  
      
    #Customize layer sturcture
    def customHiddenLayers(self, layer):
        self.layers = []
        
        x_dim = self.data_set.getXDim()
        y_dim = self.data_set.getYDim()
        
        self.layers.append(x_dim)
        for i in range(1, len(layer)+1):
            self.layers.append(layer[i-1])
        self.layers.append(y_dim)
        
        return self.layers


     # Initialize neural network
    def initializeNeuralNetwork(self):
        if(self.debug_mode):
            print("\nNeural network initialization:")

        if(self.type == NNTypes.MLP):
            self.nn = MLP()
            self.nn.setDebugMode(self.debug_mode)
            if(self.debug_mode):
                print("Neural network type: MLP")
            
        # Initialize network and loss function
        self.nn.setNeurons(self.layers)
        self.nn.setDropoutRate(self.dropout_rate)
        self.nn.setActivationFunctionHidden(self.hidden_activation_function)
        self.nn.setActivationFunctionOutput(self.output_activation_function)
        self.nn.setTaskTypes(self.encode)
        self.nn.initializeNetwork()
        
        # Print neural network status
        if(self.debug_mode):
            print("Generated network neuron topology: " + str(self.layers) + " with dropout rate: " + str(self.nn.getDropoutRate()))
        
        
    # Initialize training function
    def initializeTraining(self, learning_rate, fitness_threshold, batch_size, display_step, epoch_threshold = -1, shuffle_rate = 10000):      
        self.learning_rate = learning_rate
        self.fitness_threshold = fitness_threshold
        self.batch_size = batch_size
        self.display_step = display_step
        self.epoch_threshold = epoch_threshold
        self.shuffle_rate = shuffle_rate
        
        self.nn.initializeLossFunction()
        self.nn.initializeTrainFunction(self.training_method, self.learning_rate)
        
        
    # Initialize fitness function
    def initializeFitnessFunction(self):
        with tf.name_scope("fitness"):
            eta = self.data_set.getYEta()
            size = self.data_set.getSize()
            
            lower_bound = tf.subtract(self.nn.y, eta)
            upper_bound = tf.add(self.nn.y, eta)
            
            is_fit = tf.logical_and(tf.greater_equal(self.nn.predictor, lower_bound), tf.less(self.nn.predictor, upper_bound))
            non_zero = tf.to_float(tf.count_nonzero(tf.reduce_min(tf.cast(is_fit, tf.int8), 1)))
            self.fitness = non_zero/size

            tf.summary.scalar("fitness", self.fitness)
        
        
    # General initialization function to call all functions
    def initialize(self, learning_rate, fitness_threshold, batch_size, display_step, epoch_threshold = -1, shuffle_rate = 10000):
        self.initializeNeuralNetwork()
        self.initializeFitnessFunction()
        self.initializeTraining(learning_rate, fitness_threshold, batch_size, display_step, epoch_threshold, shuffle_rate)

        self.train_writer = tf.summary.FileWriter(self.save_location, self.nn.session.graph)
        
        
    # Check a state against the dataset and nn by using its id in the dataset
    def checkByIndex(self, index, out):
        x = self.data_set.x[index]
        estimation = self.nn.estimate([x])[0]
        y = self.data_set.getY(index)
        
        y_eta = self.data_set.getYEta()
        equal = True
        for i in range(self.data_set.getYDim()):
            if(not((y[i] - y_eta[i]) <= estimation[i] and (y[i] + y_eta[i]) > estimation[i])):
                equal = False
        
        if(out):
            print("u: " + str(y) + " u_: " + str(np.round(estimation,2)) + " within etas: " + str(equal))
            
        return equal
    
    
    # Check fitness of the neural network for a specific dataset and return wrong states
    # as of right now it assumes a binary encoding of the dataset
    def checkFitness(self, data_set):
        self.data_set = data_set
        
        size = self.data_set.getSize()
        fit = size
        
        wrong = []
        
        x, y = self.data_set.x, self.data_set.y
        y_eta = self.data_set.getYEta()
        y_dim = self.data_set.getYDim()
        
        estimation = self.nn.estimate(self.data_set.x)
        
        for i in range(size):
            equal = True
            for j in range(y_dim):
                if(not((y[i][j] - y_eta[j]) <= estimation[i][j] and (y[i][j] + y_eta[j]) > estimation[i][j]) and equal):
                    wrong.append(self.bed.baton(x[i]))
                    fit -= 1
                    equal = False
        fitness = fit/size*100
        return fitness, wrong

    # Check fitness of the neural network for a specific dataset and return wrong states
    # as of right now it assumes a binary encoding of the dataset
    def checkFitnessAllGridPoint(self, data_set):
        print("\nCalculating fitness and storing wrong states")
        self.data_set = data_set
        
        size = self.data_set.getSize()
        fit = size
        
        wrong = []
        
        x, y = self.data_set.x, self.data_set.y
        y_eta = self.data_set.getYEta()
        y_dim = self.data_set.getYDim()
        
        estimation = self.nn.estimate(self.data_set.x)
    
        # for binary
        labels = np.array(y)
        # predictions = np.array(np.round(estimation))
        # invalid_flag = (labels[:,-1] == 0)
        
        if(not self.non_det):
            # check the control input prediction and invalid flag based on controller type (D or ND)
            if(self.controller.con_det):
                predictions = np.array(np.round(estimation))
                # array of boolean with output flag equal to invalid control
                invalid_flag = (labels[:,-1] == 0)
                # check the control input prediction
                same_inputs = (predictions[:,:-1] == labels[:,:-1]).all(axis = 1)
                # check if the prediction and true output is equal
                same_flag = (predictions[:,-1] == labels[:,-1])
            else:
                predictions = np.array(estimation)
                invalid_flag = (labels[:,-1] == 1)
                predictions_soft = np.zeros_like(predictions)
                predictions_soft[np.arange(len(predictions)), predictions.argmax(1)] = 1
                same_inputs = (labels[np.arange(len(labels)), predictions_soft.argmax(1)])
                # check if the prediction flag and true output flag is equal 
                car_u = self.controller.input_total_gp
                flag_one = np.logical_and(np.argmax(predictions_soft, 1) == (car_u), labels[:,-1] == 1)
                flag_zero = np.logical_and(np.argmax(predictions_soft, 1) != (car_u), labels[:,-1] == 0)
                same_flag = np.logical_or(flag_one, flag_zero)
                
            # same_flag = (predictions[:,-1] == labels[:,-1])
            early_check = np.logical_and(invalid_flag, same_flag)

            # if it is same flag and same input, we have the performance here
            same_all = np.logical_and(same_flag, same_inputs)
            # we need to or to compensate incorrect prediction but does not matter because it is not winning domain
            logic_all = np.logical_or(early_check, same_all)
            # average the value to get fitness in the range of [0,1]
            fitness = (np.mean(logic_all))

            # valid inputs but wrong prediction           
            wrong_idx = np.where(np.logical_and((logic_all == 0), np.logical_not(invalid_flag)))
        else:
            predictions = np.array(np.round(estimation))
            fitness = (np.mean(predictions == labels))    
            wrong_idx = np.where((np.logical_not(predictions == labels)).any(axis=1))

        # get the index of wrong states
        states = np.array(self.data_set.x)
         
        if(self.data_set.encode == EncodeTypes.Classification):
            wrong = np.squeeze(states[wrong_idx])
        elif(self.data_set.encode == EncodeTypes.Boolean):
            if(self.data_set.order == Ordering.Original):
                wrong = states[wrong_idx]
                wrong_temp = list(map(self.bed.baton, wrong))
                
                wrong_x = list(map(self.controller.stox, wrong_temp))
                
                wrong = []
                for i in range(len(wrong_x)):
                    wrong.append([wrong_temp[i], wrong_x[i]])

            else:
                ss_dim = int(self.controller.state_space_dim)
                wrong_states = states[wrong_idx]
                if(wrong_states.size == 0):
                    return fitness, wrong
                
                # len_one_dim = int(len(wrong_states[0])/ss_dim)
                bit_dim = self.controller.bit_dim

                temp = []
                for i in range(ss_dim):
                    # bin_i = wrong_states[:,i*len_one_dim:(i+1)*len_one_dim]
                    bin_i = wrong_states[:,i*bit_dim[i]:(i+1)*bit_dim[i]]
                    array_i_add_dim = np.array(list(map(self.bed.baton, bin_i)))[:,None]
                    temp.append(array_i_add_dim)                
                
                wrong_temp = temp[0]
                for i in range(ss_dim-1):
                    wrong_temp = np.concatenate((wrong_temp, temp[i+1]), axis = 1) 

                wrong_x = list(map(self.controller.sstox, wrong_temp))
                wrong_s = list(map(self.controller.sstos, wrong_temp))

                wrong = []
                for i in range(len(wrong_s)):
                    wrong.append([wrong_s[i], wrong_x[i]])

        return fitness, wrong

    # Fitness modification for including as wel the non-winning domain to the NN
    # the last bit as the valid flag change the calculation quite a lot
    def allGridPointFitness(self, data_set):
        self.data_set = data_set
        
        size = self.data_set.getSize()
        fit = size
        
        wrong = []
        
        x, y = self.data_set.x, self.data_set.y
        y_eta = self.data_set.getYEta()
        y_dim = self.data_set.getYDim()
        
        estimation = self.nn.estimate(self.data_set.x)
        
        # calculate the fitness by using np array to optimize computation
        labels = np.array(y)
        # predictions = np.array(np.round(estimation))

        if(not self.non_det):
            # check the control input prediction and invalid flag based on controller type (D or ND)
            if(self.controller.con_det):
                # print('deterministic')
                predictions = np.array(np.round(estimation))
                # array of boolean with output flag equal to invalid control
                invalid_flag = (labels[:,-1] == 0)
                # check the control input prediction
                same_inputs = (predictions[:,:-1] == labels[:,:-1]).all(axis = 1)
                # check if the prediction flag and true output flag is equal 
                same_flag = (predictions[:,-1] == labels[:,-1])
            else:
                # print('determinizing')
                predictions = np.array(estimation)
                invalid_flag = (labels[:,-1] == 1)
                predictions_soft = np.zeros_like(predictions)
                predictions_soft[np.arange(len(predictions)), predictions.argmax(1)] = 1
                same_inputs = (labels[np.arange(len(labels)), predictions_soft.argmax(1)])
                # check if the prediction flag and true output flag is equal 
                car_u = self.controller.input_total_gp
                # print(car_u)
                # check the correctness of the output prediction
                # both for invalid and valid label 
                flag_one = np.logical_and(np.argmax(predictions_soft, 1) == (car_u), labels[:,-1] == 1)
                flag_zero = np.logical_and(np.argmax(predictions_soft, 1) != (car_u), labels[:,-1] == 0)
                
                same_flag = np.logical_or(flag_one, flag_zero)
                # print(np.mean(same_flag))
            
            # if it is invalid and it predicted right we have a flag that we do not have to check the rest of the bit
            early_check = np.logical_and(invalid_flag, same_flag)
            
            # if it is the same flag and same input, we have the performance here
            same_all = np.logical_and(same_flag, same_inputs)
            # we need to or to compensate incorrect prediction but does not matter because it is not winning domain
            logic_all = np.logical_or(early_check, same_all)
            # average the value to get fitness in the range of [0,1]
            fitness = (np.mean(logic_all))
        else:
            # print('non-deterministic')
            predictions = np.array(np.round(estimation))
            fitness = (np.mean(predictions == labels))

        # print(labels[:10])
        # print(predictions[:10])
        
        return fitness

    # Fitness modification for including as wel the non-winning domain to the NN
    # the last bit as the valid flag change the calculation quite a lot
    def allGridPointFitnessDeterminizing(self, data_set):
        self.data_set = data_set
        
        size = self.data_set.getSize()
        fit = size
        
        wrong = []
        
        x, y = self.data_set.x, self.data_set.y
        y_eta = self.data_set.getYEta()
        y_dim = self.data_set.getYDim()
        
        estimation = self.nn.estimate(self.data_set.x)
        
        # calculate the fitness by using np array to optimize computation
        labels = np.array(y)
        predictions = np.array(np.round(estimation))

        # array of boolean with output flag equal to invalid control
        invalid_flag = (labels[:,-1] == 1)
        # check if the prediction and true output is equal
        same_flag = (predictions[:,-1] == labels[:,-1])
        # if it is invalid and it predicted right we have a flag that we do not have to check the rest of the bit
        early_check = np.logical_and(invalid_flag, same_flag)
        
        # TO DO branch condition regression and classification
        # check the control input prediction
        # same_inputs = (predictions[:,:-1] == labels[:,:-1]).all(axis = 1)

        predictions_soft = np.zeros_like(predictions)
        predictions_soft[np.arange(len(predictions)), predictions.argmax(1)] = 1
        same_inputs = (labels[np.arange(len(labels)), predictions_soft.argmax(1)])
        
        # if it is same flag and same input, we have the performance here
        same_all = np.logical_and(same_flag, same_inputs)
        # we need to or to compensate incorrect prediction but does not matter because it is not winning domain
        logic_all = np.logical_or(early_check, same_all)
        # average the value to get fitness in the range of [0,1]
        fitness = (np.mean(logic_all))
        
        return fitness

    # Randomly check neural network against a dataset
    def randomCheck(self, data_set):
        self.data_set = data_set
        
        self.initializeFitnessFunction()        

        print("\nValidating:")
        for i in range(10):
            r = round(random.random()*(self.data_set.getSize()-1))
            self.checkByIndex(r, True)

        
    # Train network
    def train(self):
        self.clear()
        
        print("\nTraining (Ctrl+C to interrupt):")
        signal.signal(signal.SIGINT, self.interrupt)

        i, batch_index, loss, fit = 0,0,0,0.0
        old_epoch = 0
        stagnan = False

        self.merged_summary = tf.summary.merge_all()
        
        start_time = time.time()
        while self.training:
            batch = self.data_set.getBatch(self.batch_size, batch_index)
            loss, summary = self.nn.trainStep(batch, self.merged_summary)
                
            # if(i % self.shuffle_rate == 0 and i != 0): self.data_set.shuffle()
            # if(fit > 0.999):
                # fit = self.allGridPointFitness(self.data_set)
                # self.batch_size = 4096

            # if(i % self.display_step == 0 and i != 0):
            if((self.epoch % self.display_step == 0) and (old_epoch != self.epoch)):
                old_epoch = self.epoch

                # fit = self.nn.runInSession(self.fitness, self.data_set.x, self.data_set.y, 1.0)
                fit = self.allGridPointFitness(self.data_set)
                
                # self.addToLog(loss, fit, i)
                self.addToLog(loss, fit, self.epoch)
                print("i = " + str(i) + "\tepoch = " + str(self.epoch) + "\tloss = " + str(float("{0:.4f}".format(loss))) + "\tfit = " + str(float("{0:.4f}".format(fit))))
                self.train_writer.add_summary(summary, i)
                
            if(self.epoch >= self.epoch_threshold and self.epoch_threshold > 0):
                print("i = " + str(i) + "\tepoch = " + str(self.epoch) + "\tloss = " + str(float("{0:.4f}".format(loss))) + "\tfit = " + str(float("{0:.4f}".format(fit))))
                print("Finished training, epoch threshold reached")
                break
            
            if(fit >= self.fitness_threshold):
                print("Finished training")
                break

            if (len(self.fitnesses) > 40) and stagnan == False:
                if((self.fitnesses[-1] - 0.001) <= self.fitnesses[-40]):
                    print("Finished training, fitness did not improve after "+str(40*self.display_step)+" epoch")
                    stagnan = True
            
            if(math.isnan(loss)):
                print("i = " + str(i) + "\tepoch = " + str(self.epoch) + "\tloss = " + str(float("{0:.3f}".format(loss))) + "\tfit = " + str(float("{0:.3f}".format(fit))))
                print("Finished training, solution did not converge")
                break
            
            batch_index += self.batch_size
            if(batch_index >= self.data_set.getSize()): 
                batch_index = batch_index % self.data_set.getSize()
                self.data_set.shuffle()
                self.epoch += 1
            
            i += 1
 
        end_time = time.time()
        print("Time taken: " + self.utils.formatTime(end_time - start_time))
        
        
    # Interrupt handler to interrupt the training while in progress
    def interrupt(self, signal, frame):
        self.training = False
          
        
    # Plotting loss and fitness functions
    def plot(self):      
        plt.figure(1)
        plt.plot(self.iterations, self.losses, 'bo')
        plt.xlabel("Iterations")
        plt.ylabel("Loss")
        plt.grid()
        x1,x2,y1,y2 = plt.axis()
        plt.axis((x1,x2,0,y2+0.1))
        
        plt.figure(2)
        plt.plot(self.iterations, self.fitnesses, 'r-')
        plt.xlabel("Iterations")
        plt.ylabel("Fitness")
        plt.grid()
        x1,x2,y1,y2 = plt.axis()
        plt.axis((x1,x2,0,1))
        plt.show()
        

    # Add to log
    def addToLog(self, loss, fit, iteration):
        self.losses.append(loss)
        self.fitnesses.append(fit)
        self.iterations.append(iteration)
        
    # Get projected data size
    def getDataSize(self):
        size = self.nn.calculateDataSize()     
        print("Minimal neural network size of: " + self.utils.formatBytes(size))
        return size

    # Clear variables
    def clear(self):
        self.epoch = 0
        self.training = True

        self.fitnesses = []
        self.iterations = []
        self.losses = []
        
    # Save network
    def save(self, filename):
        print("\nSaving neural network")
        self.nn.save(filename)
    

    # Close session
    def close(self):
        self.nn.close()
        self.train_writer.close()
        
    # create loosing points to be plotted on matlab
    def createLoosingPoints(self, wrong_states):
        print("\nStoring loosing states for matlab simulation")
        # print("\nLoop", self.controller.state_total_gp)
        # for i in range(self.controller.state_total_gp):
            #if i not in self.controller.states or i in wrong:
            # if i not in self.controller.states:
                # loosing_states.append(i)
        total = set(range(self.controller.state_total_gp))
        winning = set(self.controller.states)
        loosing_states = total - winning 
        # print(len(total), len(winning), len(loosing_states))
        # print("\nConvert to x")
        loosing_states_x = list(map(self.controller.stox, loosing_states))
        return loosing_states_x
Пример #9
0
class NeuralNetworkManager:
    def __init__(self):
        self.type = None
        self.nn = MLP()
        self.training_method = None
        self.activation_function = None
        self.dropout_rate = 0.0

        self.training = True
        self.learning_rate = 0.1
        self.fitness_threshold = 0.75
        self.epoch_threshold = -1
        self.batch_size = 100
        self.shuffle_rate = 2500
        self.display_step = 1000

        self.epoch = 0

        self.layers = []

        self.data_set = None

        self.bed = BinaryEncoderDecoder()
        self.utils = Utilities()

        self.debug_mode = False

        # Plotting variables
        self.losses = []
        self.fitnesses = []
        self.iterations = []

        self.save_location = './nn/log/'

    # Getters and setters
    def getType(self):
        return self.type

    def getTrainingMethod(self):
        return self.training_method

    def getActivationFunction(self):
        return self.activation_function

    def getLearningRate(self):
        return self.learning_rate

    def getFitnessThreshold(self):
        return self.fitness_threshold

    def getBatchSize(self):
        return self.batch_size

    def getDisplayStep(self):
        return self.display_step

    def getEpoch(self):
        return self.epoch

    def getEpochThreshold(self):
        return self.epoch_threshold

    def getDropoutRate(self):
        return self.dropout_rate

    def getShuffleRate(self):
        return self.shuffle_rate

    def getSaveLocation(self):
        return self.save_location

    def setType(self, type):
        self.type = type

    def setTrainingMethod(self, optimizer):
        self.training_method = optimizer

    def setActivationFunction(self, activation_function):
        self.activation_function = activation_function

    def setLearningRate(self, value):
        self.learning_rate = value

    def setFitnessThreshold(self, value):
        self.fitness_threshold = value

    def setBatchSize(self, value):
        self.batch_size = value

    def setDisplayStep(self, value):
        self.display_step = value

    def setEpochThreshold(self, value):
        self.epoch_threshold = value

    def setDropoutRate(self, value):
        self.dropout_rate = value

    def setShuffleRate(self, value):
        self.shuffle_rate = value

    def setSaveLocation(self, value):
        self.save_location = value

    def setDebugMode(self, value):
        self.debug_mode = value

    def setDataSet(self, data_set):
        self.data_set = data_set

    # Hidden layer generation functions
    # Linearly increase/decrease neurons per hidden layer based on the input and ouput neurons
    def linearHiddenLayers(self, num_hidden_layers):
        self.layers = []

        x_dim = self.data_set.getXDim()
        y_dim = self.data_set.getYDim()

        a = (y_dim - x_dim) / (num_hidden_layers + 1)

        self.layers.append(x_dim)
        for i in range(1, num_hidden_layers + 1):
            self.layers.append(round(x_dim + a * i))
        self.layers.append(y_dim)

        return self.layers

    # Rectangular hidden layer
    def rectangularHiddenLayers(self, width, height):
        self.layers = []

        self.layers.append(self.data_set.getXDim())
        for i in range(width):
            self.layers.append(height)
        self.layers.append(self.data_set.getYDim())

    #Customize layer sturcture
    def customHiddenLayers(self, layer):
        self.layers = []

        x_dim = self.data_set.getXDim()
        y_dim = self.data_set.getYDim()

        self.layers.append(x_dim)
        for i in range(1, len(layer) + 1):
            self.layers.append(layer[i - 1])
        self.layers.append(y_dim)

        return self.layers

    # Initialize neural network
    def initializeNeuralNetwork(self):
        if (self.debug_mode):
            print("\nNeural network initialization:")

        if (self.type == NNTypes.MLP):
            self.nn = MLP()
            self.nn.setDebugMode(self.debug_mode)
            if (self.debug_mode):
                print("Neural network type: MLP")

        # Initialize network and loss function
        self.nn.setNeurons(self.layers)
        self.nn.setDropoutRate(self.dropout_rate)
        self.nn.setActivationFunction(self.activation_function)
        self.nn.initializeNetwork()

        # Print neural network status
        if (self.debug_mode):
            print("Generated network neuron topology: " + str(self.layers) +
                  " with dropout rate: " + str(self.nn.getDropoutRate()))

    # Initialize training function
    def initializeTraining(self,
                           learning_rate,
                           fitness_threshold,
                           batch_size,
                           display_step,
                           epoch_threshold=-1,
                           shuffle_rate=10000):
        self.learning_rate = learning_rate
        self.fitness_threshold = fitness_threshold
        self.batch_size = batch_size
        self.display_step = display_step
        self.epoch_threshold = epoch_threshold
        self.shuffle_rate = shuffle_rate

        self.nn.initializeLossFunction()
        self.nn.initializeTrainFunction(self.training_method,
                                        self.learning_rate)

    # Initialize fitness function
    def initializeFitnessFunction(self):
        with tf.name_scope("fitness"):
            eta = self.data_set.getYEta()
            size = self.data_set.getSize()

            lower_bound = tf.subtract(self.nn.y, eta)
            upper_bound = tf.add(self.nn.y, eta)

            is_fit = tf.logical_and(
                tf.greater_equal(self.nn.predictor, lower_bound),
                tf.less(self.nn.predictor, upper_bound))
            non_zero = tf.to_float(
                tf.count_nonzero(tf.reduce_min(tf.cast(is_fit, tf.int8), 1)))
            self.fitness = non_zero / size

            tf.summary.scalar("fitness", self.fitness)

    # General initialization function to call all functions
    def initialize(self,
                   learning_rate,
                   fitness_threshold,
                   batch_size,
                   display_step,
                   epoch_threshold=-1,
                   shuffle_rate=10000):
        self.initializeNeuralNetwork()
        self.initializeFitnessFunction()
        self.initializeTraining(learning_rate, fitness_threshold, batch_size,
                                display_step, epoch_threshold, shuffle_rate)

        self.train_writer = tf.summary.FileWriter(self.save_location,
                                                  self.nn.session.graph)

    # Check a state against the dataset and nn by using its id in the dataset
    def checkByIndex(self, index, out):
        x = self.data_set.x[index]
        estimation = self.nn.estimate([x])[0]
        y = self.data_set.getY(index)

        y_eta = self.data_set.getYEta()
        equal = True
        for i in range(self.data_set.getYDim()):
            if (not ((y[i] - y_eta[i]) <= estimation[i] and
                     (y[i] + y_eta[i]) > estimation[i])):
                equal = False

        if (out):
            print("u: " + str(y) + " u_: " + str(numpy.round(estimation, 2)) +
                  " within etas: " + str(equal))

        return equal

    # Check fitness of the neural network for a specific dataset and return wrong states
    # as of right now it assumes a binary encoding of the dataset
    def checkFitness(self, data_set):
        self.data_set = data_set

        size = self.data_set.getSize()
        fit = size

        wrong = []

        x, y = self.data_set.x, self.data_set.y
        y_eta = self.data_set.getYEta()
        y_dim = self.data_set.getYDim()

        estimation = self.nn.estimate(self.data_set.x)

        for i in range(size):
            equal = True
            for j in range(y_dim):
                if (not ((y[i][j] - y_eta[j]) <= estimation[i][j] and
                         (y[i][j] + y_eta[j]) > estimation[i][j]) and equal):
                    wrong.append(self.bed.baton(x[i]))
                    fit -= 1
                    equal = False

        fitness = fit / size * 100
        print("\nDataset fitness: " + str(float("{0:.3f}".format(fitness))) +
              "%")

        return fitness, wrong

    # Randomly check neural network against a dataset
    def randomCheck(self, data_set):
        self.data_set = data_set

        self.initializeFitnessFunction()

        print("\nValidating:")
        for i in range(10):
            r = round(random.random() * (self.data_set.getSize() - 1))
            self.checkByIndex(r, True)

    # Train network
    def train(self):
        self.clear()

        print("\nTraining (Ctrl+C to interrupt):")
        signal.signal(signal.SIGINT, self.interrupt)

        i, batch_index, loss, fit = 0, 0, 0, 0.0

        self.merged_summary = tf.summary.merge_all()

        start_time = time.time()
        while self.training:
            batch = self.data_set.getBatch(self.batch_size, batch_index)
            loss, summary = self.nn.trainStep(batch, self.merged_summary)

            if (i % self.shuffle_rate == 0 and i != 0): self.data_set.shuffle()

            if (i % self.display_step == 0 and i != 0):
                fit = self.nn.runInSession(self.fitness, self.data_set.x,
                                           self.data_set.y, 1.0)

                self.addToLog(loss, fit, i)
                print("i = " + str(i) + "\tepoch = " + str(self.epoch) +
                      "\tloss = " + str(float("{0:.3f}".format(loss))) +
                      "\tfit = " + str(float("{0:.3f}".format(fit))))
                self.train_writer.add_summary(summary, i)

            if (self.epoch >= self.epoch_threshold
                    and self.epoch_threshold > 0):
                print("i = " + str(i) + "\tepoch = " + str(self.epoch) +
                      "\tloss = " + str(float("{0:.3f}".format(loss))) +
                      "\tfit = " + str(float("{0:.3f}".format(fit))))
                print("Finished training, epoch threshold reached")
                break

            if (fit >= self.fitness_threshold):
                print("Finished training")
                break

            if (math.isnan(loss)):
                print("i = " + str(i) + "\tepoch = " + str(self.epoch) +
                      "\tloss = " + str(float("{0:.3f}".format(loss))) +
                      "\tfit = " + str(float("{0:.3f}".format(fit))))
                print("Finished training, solution did not converge")
                break

            batch_index += self.batch_size
            if (batch_index >= self.data_set.getSize()):
                batch_index = batch_index % self.data_set.getSize()
                self.epoch += 1

            i += 1

        end_time = time.time()
        print("Time taken: " + self.utils.formatTime(end_time - start_time))

    # Interrupt handler to interrupt the training while in progress
    def interrupt(self, signal, frame):
        self.training = False

    # Plotting loss and fitness functions
    def plot(self):
        plt.figure(1)
        plt.plot(self.iterations, self.losses, 'bo')
        plt.xlabel("Iterations")
        plt.ylabel("Loss")
        plt.grid()
        x1, x2, y1, y2 = plt.axis()
        plt.axis((x1, x2, 0, y2 + 0.1))

        plt.figure(2)
        plt.plot(self.iterations, self.fitnesses, 'r-')
        plt.xlabel("Iterations")
        plt.ylabel("Fitness")
        plt.grid()
        x1, x2, y1, y2 = plt.axis()
        plt.axis((x1, x2, 0, 1))
        plt.show()

    # Add to log
    def addToLog(self, loss, fit, iteration):
        self.losses.append(loss)
        self.fitnesses.append(fit)
        self.iterations.append(iteration)

    # Get projected data size
    def getDataSize(self):
        size = self.nn.calculateDataSize()
        print("Minimal neural network size of: " +
              self.utils.formatBytes(size))
        return size

    # Clear variables
    def clear(self):
        self.epoch = 0
        self.training = True

        self.fitnesses = []
        self.iterations = []
        self.losses = []

    # Save network
    def save(self, filename):
        print("\nSaving neural network")
        self.nn.save(filename)

    # Close session
    def close(self):
        self.nn.close()
        self.train_writer.close()
Пример #10
0
    def addAllGridPointDeterministicSeparateBoolean(self, controller):

        complete_dataset = self.createDeterministicCompleteDataset(controller)

        new_x = []
        new_y = []

        self.x = complete_dataset[:, 0]  # x_all_gp
        self.y = complete_dataset[:, 1]  # y_all_gp;
        self.size = len(self.x)

        bed = BinaryEncoderDecoder()

        self.x = np.array(list(map(controller.stoss, self.x)))
        state_space_dim = int(controller.state_space_dim)
        X = []
        n_X = []
        for j in range(state_space_dim):
            X.append(self.x[:, j])
            n_X.append(len(bin(int(np.max(X[j])))) - 2)
            print(np.max(X[j]))

        print(n_X)

        controller.bit_dim = n_X

        swapped = X[0][:, None]
        for k in range(len(X) - 1):
            swapped = np.concatenate((X[k + 1][:, None], swapped), axis=1)

        x_int8 = swapped.view(np.uint8)
        x_ordered = np.flip(x_int8, 1)
        total_bit = 32
        x_unpacked = np.unpackbits(x_ordered).reshape(
            -1, total_bit * state_space_dim)

        idx_keep = []
        for k in range(state_space_dim):
            idx_keep.append(
                np.arange((k + 1) * total_bit - n_X[k], (k + 1) * total_bit))

        reduced = idx_keep[0]
        for k in range(len(X) - 1):
            reduced = np.concatenate((reduced, idx_keep[k + 1]))

        print(x_unpacked[:, reduced].shape)
        new_x = (x_unpacked[:, reduced]).tolist()
        n_x = len(new_x[0])
        print(n_x)

        n_y = len(bed.sntob(self.y_bounds[1]))  # Output bit length
        # bounds for y are still the same, but bounds for x are different, just let it be for now
        self.y_bounds = [self.getLowestY(), self.getHighestY()]
        self.y_bounds = [
            bed.ntoba(self.y_bounds[0], n_y),
            bed.ntoba(self.y_bounds[1], n_y)
        ]

        # convert the input and output pair to array of binary
        # and add the flag bit
        for i in range(self.size):
            add_flag = bed.ntoba(self.y[i], n_y)
            add_flag.append(complete_dataset[i, 2])
            new_y.append(add_flag)

        self.x = new_x
        self.x_dim = n_x
        self.x_eta = []
        for i in range(self.x_dim):
            self.x_eta.append(0.5)

        self.y = new_y
        self.y_dim = n_y + 1
        self.y_eta = []
        for i in range(self.y_dim):
            self.y_eta.append(0.5)