def __createLetter(self, index): self.array[index] = 1 value = TensorFactory.createTensorValues(self.array, cuda=self.cuda) self.array[index] = 0 return value
def generate_LSTM_input(self, observations): num_observations = len(observations) value = TensorFactory.createTensorZeros(tupleShape=(num_observations, self.limit_directions, self.max_layers, self.mutations), cuda=self.cuda) for observation in range(num_observations): directions = observations[observation].path num_directions = len(directions) tensors_directions = [] for i in range(num_directions): tensors_directions.append(self.__direction_to_tensor(directions[i])) for i in range(num_directions, self.limit_directions): tensors_directions.append(self.__direction_to_tensor(directions[num_directions-1])) for index in range(value.shape[1]): value[observation][index] = tensors_directions[index] del tensors_directions return value
def generate_LSTM_predict(self, observation): directions_number = len(observation.path) value = TensorFactory.createTensorZeros(tupleShape=(1, directions_number, self.max_layers, self.mutations), cuda=self.cuda) for i in range(directions_number): value[0][i] = self.__direction_to_tensor(observation.path[i]) return value
def __get_unit_input(self, unit_index, data): shape = data.shape if len(shape) > 3: value = TensorFactory.createTensorZeros(tupleShape=(shape[0], shape[2], shape[3]), cuda=self.cuda_flag, requiresGrad=False) else: value = TensorFactory.createTensorZeros(tupleShape=(shape[0], 1, shape[2]), cuda=self.cuda_flag, requiresGrad=False) # Se genera la entrada para la unidad de memoria respectiva. i = 0 for word in data: letter = word[unit_index] value[i] = letter.clone() i += 1 return value
def __init__(self, dna, cuda, momentum, weight_decay, enable_activaiton, enable_track_stats=True, dropout_value=0, enable_last_activation=True): self.cuda_flag = cuda self.dna = dna self.nodes = [] self.enable_track_stats = enable_track_stats self.momentum = momentum self.weight_decay = weight_decay self.enable_activation = enable_activaiton self.label = tensorFactory.createTensor(body=[1], cuda=self.cuda_flag, requiresGrad=False) self.foward_value = None self.total_value = 0 self.loss_history = [] self.dropout_value = dropout_value self.enable_last_activation = enable_last_activation
def convertWordsToTensor(self, wordsArray): words_amount = len(wordsArray) max_letters = self.__getLongestWordValue(wordsArray) letter_size = len(self.array) tensorValue = TensorFactory.createTensorZeros(tupleShape=(words_amount, max_letters, letter_size), cuda=self.cuda) i = 0 for word in wordsArray: j = 0 for letter in word: tensorValue[i][j] = self.getTensor(letter) j += 1 i += 1 return tensorValue
def __direction_to_tensor(self, direction): index_layer = direction[0] mutation = direction[1] index_mutation = self.mutation_to_index.get(mutation) if index_layer >= self.max_layers: raise Exception("index out of range: {:d} (max layers: {:d})".format(index_layer, self.max_layers)) if index_mutation is None: raise Exception("mutation doesnt exist {}".format(mutation)) value = TensorFactory.createTensorZeros(tupleShape=(self.max_layers, self.mutations), cuda=self.cuda) if index_mutation >= 0: value[index_layer, index_mutation] = 1 return value