def __init__(self, creator: TensorCreator, lower_bound: int, upper_bound: int, random: np.random, generate_new_every_n: int = 1, generate_random_intervals=False): super().__init__(creator.device) self._generate_new_every_n = generate_new_every_n self._next_generation = generate_new_every_n self._step = -1 self._lower_bound = lower_bound self._upper_bound = upper_bound self._random = random self._random_generation_intervals = generate_random_intervals self._scalar_output = creator.full([1], fill_value=FLOAT_NAN, dtype=self._float_dtype, device=self._device) self._one_hot_output = creator.full([self._upper_bound], fill_value=FLOAT_NAN, dtype=self._float_dtype, device=self._device) self._current_value = creator.zeros((1, ), dtype=creator.long)
def __init__(self, creator: TensorCreator, output_height: int, output_width: int, params: WeightedAvgNodeParams, num_weights: int = 0): super().__init__(creator.device) self._params = params self._creator = creator if self._params.weights_on_input: self.num_weights = num_weights weights = np.ones((output_height, output_width, self.num_weights)) else: assert len( self._params.weights) != 0, 'Parametric weights not set!' assert self._params.num_inputs == len(self._params.weights) self.num_weights = len(self._params.weights) weights = np.ones((output_height, output_width, self.num_weights)) for i in range(self.num_weights): weights[i] = np.dot(weights[i], params.weights[i]) self.node_weights = creator.tensor(weights, dtype=creator.float, device=creator.device) self.last_output = creator.zeros([output_width, output_height], dtype=creator.float, device=creator.device)
def __init__(self, creator: TensorCreator, device, params: CartographicParams): self._params = params.clone() super().__init__(device) self.map_rgb = creator.zeros( self._params.shape, device=device) self.map_raw = creator.zeros( self._params.shape[:-1], device=device) self.cmap = cm.get_cmap( self._params.colormap) self.cnorm = colors.Normalize( vmin=self._params.cmin, vmax=self._params.cmax) self.initdone = False self.tartget_x = 0 self.tartget_y = 0 # Smoothing Kernel if self._params.smoothing: self.kernel = (torch.ones( (self._params.kernel_shape[0], self._params.kernel_shape[1]))) / (self._params.kernel_shape[0] * self._params.kernel_shape[1]) self.kernel = self.kernel.to(device='cuda') self.kernel = self.kernel.unsqueeze(0).unsqueeze(0)
def __init__(self, params: ExpertParams, creator: TensorCreator): super().__init__(creator.device) float_dtype = get_float(self._device) self.params = params flock_size = params.flock_size self.n_cluster_centers = params.n_cluster_centers self.seq_lookbehind = params.temporal.seq_lookbehind # self.context_size = self.n_cluster_centers * 2 self.n_providers = self.params.temporal.n_providers # Context is: <SP_output> # <Rewards> # <Punishments> # # <Pred_clusters for next step> # <NaNs> # <NaNs> # With optional NaN Padding depending on the context size in the params self.output_context = creator.full( (flock_size, 2, NUMBER_OF_CONTEXT_TYPES, self.n_cluster_centers), fill_value=float("nan"), device=self._device, dtype=float_dtype) self.index_tensor = creator.arange( start=0, end=flock_size, device=self._device).view(-1, 1).expand(flock_size, self.n_cluster_centers) self.create_flocks(params, creator)
def __init__(self, creator: TensorCreator, network: nn.Module, optimizer: Optimizer, storage: ObservationStorage, params: NNetParams): """Unit constructor. Args: creator: creator of this node network: pytorch neural network module optimizer: pytorch optimizer object storage: baselines observation storage object params: baselines parameter object """ super().__init__(creator.device) self.params = params self.network = network self.optimizer = optimizer self.storage = storage self.device = creator.device self.output = creator.zeros( SpaceEngineersConnectorConfig().task_to_agent_buffer_size, dtype=self._float_dtype, device=self._device) self.label = creator.zeros( SpaceEngineersConnectorConfig().task_to_agent_buffer_size, dtype=self._float_dtype, device=self._device) self.task_control = creator.zeros(4, device=self._device) self.cur_train_step = 0 self.last_train_loss = 0
def __init__(self, creator: TensorCreator, input_dims, device, channel_first=False): super().__init__(device) self.creator = creator self._input_dims = input_dims self._channel_first = channel_first self.r_output_tensor = creator.zeros(input_dims, device=device, dtype=self._float_dtype) self.g_output_tensor = creator.zeros(input_dims, device=device, dtype=self._float_dtype) self.b_output_tensor = creator.zeros(input_dims, device=device, dtype=self._float_dtype) self.concat_output_tensor = creator.zeros([input_dims[0]*3] + list(input_dims[1:]), device=device, dtype=self._float_dtype)
def __init__(self, creator: TensorCreator, seq: SequenceGenerator): super().__init__(creator.device) self._seq = seq self._creator = creator self.output = creator.zeros(1, dtype=self._float_dtype, device=self._device) self.sequence_number = creator.zeros(1, dtype=self._float_dtype, device=self._device)
def __init__(self, creator: TensorCreator, params: DatasetSimplePointGravityParams, random: np.random): super().__init__(creator.device) self._random = random self.point_pos = params.point_pos self.attractor_distance = params.attractor_distance self.output_data = creator.zeros(*params.canvas_shape, device=self._device) self._state = States.BLANK self.move_strategy = params.move_strategy self._frame_backbuffer = creator.zeros_like(self.output_data)
def __init__(self, creator: TensorCreator, tensor_shape: Tuple): self._odd_tensor = creator.full(tensor_shape, FLOAT_NAN, device=creator.device, dtype=creator.float) self._even_tensor = creator.full(tensor_shape, FLOAT_NAN, device=creator.device, dtype=creator.float) self._num_writes = 0
def __init__(self, creator: TensorCreator, noise_amplitude=0.2): super().__init__(creator.device) self._noise_amplitude = noise_amplitude self.bitmap = creator.zeros([1, 2], dtype=self._float_dtype, device=self._device) self._current_step = 0 self._data = creator.tensor([[0., 0], [1., 0], [0., 1], [1, 1]], dtype=self._float_dtype, device=self._device) self._no_data = self._data.shape[1]
def __init__(self, creator: TensorCreator, actions_descriptor: AgentActionsDescriptor): super().__init__(creator.device) self.action_output = creator.zeros(len( actions_descriptor.action_names()), dtype=self._float_dtype, device=self._device)
def __init__(self, output_shape, creator: TensorCreator, squeeze_channel: bool): super().__init__(creator.device) self.output = creator.zeros(output_shape, dtype=self._float_dtype, device=self._device) self._squeeze_channel = squeeze_channel
def __init__(self, creator: TensorCreator, device, mapping: torch.Tensor, output_shape, dimension: int): super().__init__(device) self.dimension = dimension self._mapping = mapping self.output = creator.zeros(output_shape, dtype=self._float_dtype, device=device)
def __init__(self, func, output_shapes, creator: TensorCreator): super().__init__(creator.device) self.func = func self.outputs = [] for shape in output_shapes: self.outputs.append( creator.zeros(shape, dtype=self._float_dtype, device=self._device))
def _init_node(self, creator: TensorCreator): device = self._device if self._seq_params is not None: # each _init_node, also the new SequenceGenerator should be created (reset the current position etc) # self._seq = SequenceGenerator.from_params(self._seq_params, random=self._random) transition_probs = self._seq_params.transition_probs if self._seq_params.transition_probs is not None \ else SequenceGenerator.default_transition_probs(self._seq_params.seqs) self._seq = SequenceGenerator(self._seq_params.seqs, transition_probs, random=self._random) self._update_class_filter_by_sequences() else: self._seq = None self._sanitize_class_filter() class_filter = self._params.class_filter dataset_result = self._dataset.get_filtered( class_filter ) if class_filter is not None else self._dataset.get_all() self._data = dataset_result.data.type(FLOAT_TYPE_CPU) / 255.0 self._labels = dataset_result.labels self._data_seq = self._get_data_sequence_iterator() # init the output tensors self.label_tensor = creator.zeros(1, device=device, dtype=torch.int64) self.output_sequence_id = creator.zeros(1, device=device, dtype=torch.int64) self.output_data = creator.zeros((28, 28), device=device, dtype=self._float_dtype) if self._params.one_hot_labels: self.output_label = creator.zeros(10, device=device, dtype=self._float_dtype) else: self.output_label = creator.zeros(1, device=device, dtype=torch.int64)
def __init__(self, creator: TensorCreator, shape: List[int], distribution: Distribution = Distribution.Uniform, amplitude: float = 1., has_input: bool = True): super().__init__(creator.device) self.distribution = distribution self.amplitude = amplitude if has_input: self._image_buffer = creator.zeros(*shape, dtype=self._float_dtype, device=self._device) self._random_numbers = creator.zeros(*shape, dtype=self._float_dtype, device=self._device) self.output = creator.zeros(*shape, dtype=self._float_dtype, device=self._device)
def __init__(self, creator: TensorCreator, input_height: int, input_width: int, output_height: int, output_width: int, num_channels: int, params: FocusNodeParams): super().__init__(creator.device) self._width = output_width self._height = output_height self._num_channels = num_channels self._params = params self._creator = creator self.last_mask = creator.zeros( [input_height, input_width, num_channels], dtype=creator.float, device=creator.device) self.last_output = creator.zeros( [output_height, output_width, num_channels], dtype=creator.float, device=creator.device)
def __init__(self, creator: TensorCreator, output_height: int, output_width: int, num_channels: int, params: VisitedAreaParams): super().__init__(creator.device) self._creator = creator self._params = params self.last_visited_area = creator.zeros( [output_height, output_width, num_channels], dtype=creator.float, device=creator.device)
def __init__(self, creator: TensorCreator, height: int, width: int, num_channels: int, params: MotionDetectionParams): super().__init__(creator.device) self._height = height self._width = width self._num_channels = num_channels self._params = params self._has_previous_image = False bw_shape = [self._height, self._width] self.previous_image = creator.zeros(bw_shape, dtype=creator.float, device=creator.device) self.temp_image = creator.zeros(bw_shape, dtype=creator.float, device=creator.device) self.motion_map = creator.zeros(bw_shape, dtype=creator.float, device=creator.device)
def __init__(self, creator: TensorCreator, params: DatasetAlphabetParams, random: Optional[RandomState] = None): super().__init__(creator.device) self._validate_params(params) random = random or np.random.RandomState() # Generate all symbols generator = AlphabetGenerator(params.padding_right) all_symbols = generator.create_symbols(params.symbols) self.all_symbols = creator.zeros_like(all_symbols) self.all_symbols.copy_(all_symbols.to(creator.device)) # Create output tensors shape = list(self.all_symbols.shape) self.output_data = creator.zeros(shape[1:], device=creator.device) self.output_label = creator.zeros(1, dtype=torch.int64, device=creator.device) self.output_sequence_id = creator.zeros(1, dtype=torch.int64, device=creator.device) self.output_sequence_id_one_hot = creator.zeros( (1, len(params.sequence_probs.seqs)), dtype=self._float_dtype, device=creator.device) if params.mode == DatasetAlphabetMode.SEQUENCE_PROBS: seqs = [ self.convert_string_to_positions(params.symbols, seq) for seq in params.sequence_probs.seqs ] transition_probs = params.sequence_probs.transition_probs or SequenceGenerator.default_transition_probs( seqs) self.seq = SequenceGenerator(seqs, transition_probs, random=random) self._current = next(self.seq) self._n_symbols = shape[0]
def __init__(self, creator: TensorCreator, params: DatasetSENavigationParams, random: np.random.RandomState): super().__init__(creator.device) self._params = params.clone() self._random = random dataset = DatasetSeTask1(self._params.dataset_size) size, images, labels = dataset.get_all() self._img_size = size self._n_samples = len(images) self._positions_permuted = None # load the images and positions self._images = images.type(self._float_dtype).to(self._device) self._positions = labels.type(self._float_dtype).to(self._device) / 100.0 # compute landmarks for each position divisor = SpaceDivisor(self._params.horizontal_segments, self._params.vertical_segments, self._device) self._landmarks, self._landmarks_one_hot = divisor.get_landmarks(self._positions) # prepare the tensors for the first step if self._params.channel_first: self._images = self._images.permute(0, 3, 1, 2) img_size = [DatasetSeBase.N_CHANNELS, *self._params.dataset_dims] else: img_size = [*self._params.dataset_dims, DatasetSeBase.N_CHANNELS] self.last_image = creator.full(img_size, fill_value=FLOAT_NAN, dtype=self._float_dtype, device=self._device) self.last_position = creator.full(self._positions[0].shape, fill_value=FLOAT_NAN, dtype=self._float_dtype, device=self._device) self.last_landmark = creator.full(self._landmarks[0].shape, fill_value=FLOAT_NAN, dtype=self._float_dtype, device=self._device) self.last_landmark_one_hot = creator.full(self._landmarks_one_hot[0].shape, fill_value=FLOAT_NAN, dtype=self._float_dtype, device=self._device) self.task_id_const = creator.full([1], 1.0, dtype=self._float_dtype, device=self._device) self.testing_phase_indicator_const = creator.full([1], 0.0, dtype=self._float_dtype, device=self._device) self.unused_output = creator.full([1], FLOAT_NEG_INF, dtype=self._float_dtype, device=self._device) self._pos = -1 self._pos_permuted = -1
def __init__(self, creator: TensorCreator, dim, input_shape, split_sizes, dtype): super().__init__(creator.device) self._input_shape = input_shape self._split_sizes = split_sizes self._dim = dim self.output_tensors = [] self._indices = [] if -1 in split_sizes: raise InvalidParameterException( "Automatic split size not supported") input_dim_size = input_shape[dim] outputs_size = sum(split_sizes) if outputs_size > input_dim_size: message = f"The combined output size ({outputs_size} is larger than dimension {dim}: {input_dim_size})" raise InvalidParameterException(message) split_start = 0 for split in split_sizes: output_dims = list(input_shape) output_dims[dim] = split output_tensor = creator.zeros(output_dims, dtype=dtype, device=self._device) self.output_tensors.append(output_tensor) # Create the index which will be used for the data splitting. index = creator.arange(split_start, split_start + split, dtype=torch.long, device=self._device) self._indices.append(index) split_start += split
def __init__(self, creator: TensorCreator, params: DisentangledWorldNodeParams): super().__init__(creator.device) self._params = copy.copy(params) self._render_world = params.render_world_class( (params.sx, params.sy), **params.render_world_params) width = self._render_world.world_size[0] height = self._render_world.world_size[1] self.bitmap = creator.zeros((width, height, 3), dtype=self._float_dtype, device=self._device)
def __init__(self, creator: TensorCreator, params: DatasetSeObjectsParams, random): super().__init__(creator.device) self._params = params self._random = random self._save_memory = params.save_gpu_memory dataset = DatasetSeTask0(self._params.dataset_size) size, train, test = dataset.get_all() self._train_instance_ids = train[2].to('cpu') self._test_instance_ids = test[2].to('cpu') self._img_size = size # load the images and labels if self._save_memory: device = 'cpu' else: device = self._device self._train_images = train[0].to(device) self._train_labels = train[1].to(self._device) self._test_images = test[0].to(device) self._test_labels = test[1].to(self._device) # prepare the tensors for the first step img_size = [*self._params.dataset_dims, DatasetSeBase.N_CHANNELS] num_classes = self._train_labels.shape[1] self.last_image = self._create_tensor(img_size, FLOAT_NAN, creator) self.last_label = self._create_tensor([num_classes], FLOAT_NAN, creator) self.last_truth = self._create_tensor([num_classes], FLOAT_NAN, creator) self.hidden_label = self._create_tensor([num_classes], FLOAT_NAN, creator) self.last_instance_id = self._create_tensor([1], FLOAT_NAN, creator) self.task_id_const = self._create_tensor([1], 0.0, creator) self.unused_output = self._create_tensor([1], FLOAT_NEG_INF, creator) self.testing_phase_indicator = self._create_tensor([1], FLOAT_NAN, creator) self.training_pos = creator.full([1], fill_value=-1, dtype=torch.long, device=self._device) self._reset_indexes_and_filtering()
def __init__(self, creator: TensorCreator, data_size: Tuple[int, ...], input_dims: Tuple[int, int, int], parent_rf_dims: Size2D, parent_rf_stride_dims: Stride = None, flatten_output_grid_dimensions=False): super().__init__(creator.device) self._parent_rf_dims = parent_rf_dims self._parent_rf_stride_dims = parent_rf_stride_dims self.creator = creator self._rf_mapping = self.create_rf_mapping(input_dims, parent_rf_dims, parent_rf_stride_dims, flatten_output_grid_dimensions, self._device) self.output_tensor = creator.zeros(data_size, device=self._device, dtype=self._float_dtype)
def __init__(self, creator: TensorCreator, input_tensor_shapes, dtype, dim=0, flatten=False): """Usage of flatten: flatten input vectors first.""" super().__init__(creator.device) self._flatten = flatten self._dim = dim self._input_tensor_shapes = input_tensor_shapes if not flatten: output_dims = list(input_tensor_shapes[0]) output_dims[dim] = 0 for input_shape in input_tensor_shapes: self._check_shape(input_shape, output_dims, dim) output_dims[dim] += input_shape[dim] else: self._flatten_input_shapes = [reduce(lambda a, b: a * b, x) for x in input_tensor_shapes] output_dims = (sum(self._flatten_input_shapes),) self.output = creator.zeros(*output_dims, dtype=dtype, device=self._device)
def __init__(self, input_shape, random: np.random.RandomState, creator: TensorCreator, first_non_expanded_dim, n_outputs, n_samples): super().__init__(creator.device) self.n_outputs = n_outputs input_shape = tuple(input_shape) total_size = dim_prod(input_shape[first_non_expanded_dim:]) filters_sparse = [] self.counts = torch.zeros(input_shape[first_non_expanded_dim:], dtype=self._float_dtype, device=self._device) for _ in range(n_outputs): filter_dense = random.choice(total_size, n_samples, replace=False) filter_sparse = np.zeros(total_size) filter_sparse[filter_dense] = 1 filter_sparse = torch.tensor(filter_sparse, dtype=self._float_dtype, device=self._device) filter_sparse = filter_sparse.view( input_shape[first_non_expanded_dim:]) self.counts += filter_sparse filter_sparse = filter_sparse.expand(input_shape) filters_sparse.append(filter_sparse) self.filters = filters_sparse if (self.counts == 0).any().item() == 1: logger.warning("Some subset of receptive field is not included.") self.counts = self.counts.expand(input_shape) self.output_tensors = \ [creator.zeros(input_shape, dtype=self._float_dtype, device=self._device) for _ in range(n_outputs)]
def __init__(self, creator: TensorCreator, input_shape, start_dim: int, end_dim: int): super().__init__(creator.device) if start_dim < 0: start_dim = len(input_shape) + start_dim if end_dim < 0: end_dim = len(input_shape) + end_dim if not (0 <= start_dim < len(input_shape)): raise ValueError(f"start_dim {start_dim} out of input_shape {input_shape}.") if not (0 <= end_dim < len(input_shape)): raise ValueError(f"end_dim {end_dim} out of input_shape {input_shape}.") self.input_shape = input_shape self.start_dim = start_dim self.end_dim = end_dim dim_product = int(torch.prod(torch.tensor(input_shape[start_dim:end_dim + 1])).item()) self.new_shape = input_shape[:start_dim] + (dim_product,) if end_dim + 1 < len(input_shape): self.new_shape += input_shape[end_dim + 1:] self.output = creator.zeros(tuple(self.new_shape), dtype=self._float_dtype, device=self._device)
def __init__(self, creator: TensorCreator, params: GridWorldParams): super().__init__(creator.device) self.validate_params(params) self.params = params # render a world map tile_size = self.params.tile_size width = self.params.world_width * self.params.tile_size height = self.params.world_height * self.params.tile_size self.bit_map = creator.zeros([height, width], dtype=torch.uint8) if isinstance(creator, MeasuringCreator): self.last_image = creator.zeros([height, width], dtype=self._float_dtype, device=self._device) self.circle = creator.zeros([tile_size, tile_size], dtype=self._float_dtype, device=self._device) self.circle_wall = creator.zeros([tile_size, tile_size, 3], dtype=self._float_dtype, device=self._device) self.triangle_wall = creator.zeros([tile_size, tile_size, 3], dtype=self._float_dtype, device=self._device) else: self.circle = interpolate(creator.tensor(self.circle_template).view(1, 1, 9, 9), size=[tile_size, tile_size]).view(tile_size, tile_size).type(torch.uint8) self.triangle_wall = interpolate(creator.tensor(self.triangle_wall_template).view(1, 1, 9, 9), size=[tile_size, tile_size]).view(tile_size, tile_size).type(torch.uint8) self.circle_wall = interpolate(creator.tensor(self.circle_wall_template).view(1, 1, 9, 9), size=[tile_size, tile_size]).view(tile_size, tile_size).type(torch.uint8) self.bit_map_last_action = creator.zeros([height + 1, width], dtype=self._float_dtype, device=self._device) self.bit_map_padding = width - 4 self.render_map(self.bit_map) self.pos = creator.tensor(self.params.agent_pos[::-1], dtype=self._float_dtype, device=self._device) self.last_position = creator.zeros(2, dtype=self._float_dtype, device=self._device) self.last_position_one_hot_matrix = creator.zeros((params.world_height, params.world_width), dtype=self._float_dtype, device=self._device) self.last_action = creator.zeros(4, dtype=self._float_dtype, device=self._device) self.reward = creator.zeros(1, dtype=self._float_dtype, device=self._device) width = self.params.egocentric_width * self.params.tile_size height = self.params.egocentric_height * self.params.tile_size self.egocentric_image = creator.zeros((width, height), dtype=self._float_dtype, device=self._device) self.ego_last_action = creator.zeros((width+1, height), dtype=self._float_dtype, device=self._device) self.ego_padding = height - 4 if not isinstance(creator, MeasuringCreator): # Initial rendering of the outputs, all further renderings will be just updates self.last_image = creator.tensor(self.bit_map, dtype=self._float_dtype, device=self._device) self.last_position.copy_(self.pos) self._render_outputs()
def _create_tensor(self, sizes, creator: TensorCreator): return creator.full(sizes, fill_value=FLOAT_NAN, dtype=self._float_dtype, device=self._device)