def test_one_step_tensor_delay_buffer(): """Test a buffer which delays the data one step""" shape = (3, 2, 1) creator = AllocatingCreator(device='cpu') buffer = create_delay_buffer(creator, True, shape) no_delay = create_delay_buffer(creator, False, shape) buffers = [buffer, no_delay] nans = creator.full(shape, FLOAT_NAN) a = creator.ones(shape) b = creator.ones(shape) * 2 c = creator.ones(shape) * 3 d = creator.ones(shape) * 4 _push(buffers, a) assert same(buffer.read(), nans) assert same(no_delay.read(), a) _push(buffers, b) assert same(buffer.read(), a) assert same(no_delay.read(), b) _push(buffers, c) assert same(buffer.read(), b) assert same(no_delay.read(), c) _push(buffers, d) assert same(buffer.read(), c) assert same(no_delay.read(), d)
def test_sample_learning_batch_combinations(self, method, flock_indices, elements_written): flock_size = 3 creator = AllocatingCreator('cpu') f_size = len(flock_indices) if flock_indices is not None else 3 buffer = SPFlockBuffer(creator, buffer_size=20, n_cluster_centers=3, flock_size=flock_size, input_size=5) if flock_indices is not None: buffer.set_flock_indices( creator.tensor(flock_indices, dtype=torch.int64)) buffer.total_data_written[:] = elements_written buffer.clusters.stored_data[:, :elements_written] = creator.tensor( [0, 1, 0]) buffer.inputs.stored_data[:, :elements_written, :] = creator.tensor( [1.3, 0.2, 0.6, 0.4, 0.1]) # use some dummy value here to check that it is rewriting all the lines in res dummy_value = -2.1 sampled_data = creator.full((f_size, elements_written, 5), fill_value=dummy_value) buffer.sample_learning_batch(elements_written, sampled_data, method) assert (sampled_data == dummy_value).any().item() == 0
def _create_expected_images_tensor(creator: AllocatingCreator): device = creator.device dtype = get_float(device) expected_images_tensor = creator.full([3, 2, 3, 3], dtype=dtype, device=device, fill_value=1.0) expected_images_tensor[0, 1, 2, :] = creator.tensor([1.0, 0.0, 0.0]) expected_images_tensor[1, 1, 2, :] = creator.tensor([0.0, 1.0, 0.0]) expected_images_tensor[2, 1, 2, :] = creator.tensor([0.0, 0.0, 1.0]) return creator.cat([expected_images_tensor, expected_images_tensor])