def test_transformer(): from fuel.transformers import ScaleAndShift from fuel.datasets import IndexableDataset from fuel.streams import DataStream from fuel.schemes import ShuffledScheme seed = 1234 rng = numpy.random.RandomState(seed) features = rng.randint(256, size=(8, 2, 2)) targets = rng.randint(4, size=(8, 1)) dataset = IndexableDataset(indexables=OrderedDict([('features', features), ('targets', targets)]), axis_labels=OrderedDict([('features', ('batch', 'height', 'width')), ('targets', ('batch', 'index'))])) scheme = ShuffledScheme(examples=dataset.num_examples, batch_size=2) data_stream = DataStream(dataset=dataset, iteration_scheme=scheme) scale = 1.0 / features.std() shift = - scale * features.mean() standardized_stream = ScaleAndShift(data_stream=data_stream, scale=scale, shift=shift, which_sources=('features',)) for batch in standardized_stream.get_epoch_iterator(): print(batch)
def test_scale_and_shift(): stream = DataStream( IterableDataset({ 'features': [1, 2, 3], 'targets': [0, 1, 0] })) wrapper = ScaleAndShift(stream, 2, -1, which_sources=('targets', )) assert list(wrapper.get_epoch_iterator()) == [(1, -1), (2, 1), (3, -1)]
class TestScaleAndShift(object): def setUp(self): dataset = IterableDataset( OrderedDict([('features', [1, 2, 3]), ('targets', [0, 1, 0])]), axis_labels={'features': ('batch'), 'targets': ('batch')}) self.stream = DataStream(dataset) self.wrapper = ScaleAndShift( self.stream, 2, -1, which_sources=('targets',)) def test_scale_and_shift(self): assert_equal(list(self.wrapper.get_epoch_iterator()), [(1, -1), (2, 1), (3, -1)]) def test_axis_labels_are_passed_through(self): assert_equal(self.wrapper.axis_labels, self.stream.axis_labels)
class TestScaleAndShift(object): def setUp(self): dataset = IterableDataset( OrderedDict([('features', [1, 2, 3]), ('targets', [0, 1, 0])]), axis_labels={'features': ('batch'), 'targets': ('batch')}) self.stream = DataStream(dataset) self.wrapper = ScaleAndShift( self.stream, 2, -1, which_sources=('targets',)) def test_scale_and_shift(self): assert_equal(list(self.wrapper.get_epoch_iterator()), [(1, -1), (2, 1), (3, -1)]) def test_axis_labels_are_passed_through(self): assert_equal(self.wrapper.axis_labels, self.stream.axis_labels)
split_dict = { 'train': { 'images': (0, train_feature.shape[0]), 'targets': (0, train_target.shape[0]) } } f.attrs['split'] = H5PYDataset.create_split_array(split_dict) f.flush() f.close() train_set = H5PYDataset('../../data/dataset.hdf5', which_sets=('train',)) #data_stream = DataStream(dataset=train_set, iteration_scheme=scheme) #state = train_set.open() scheme = ShuffledScheme(examples=train_set.num_examples, batch_size=4) data_stream = DataStream(dataset=train_set, iteration_scheme=scheme) for data in data_stream.get_epoch_iterator(): print(data[0], data[1]) standardized_stream = ScaleAndShift(data_stream=data_stream, scale=255,shift=0, which_sources=('features',)) for data in standardized_stream.get_epoch_iterator(): print(data[0], data[1]) #train_set.close(state)
rate = 16000 for i, sample in enumerate(raw_audio[:n_samples]): pyplot.plot(sample) pyplot.savefig(save_dir +"original_%i.png" % i) pyplot.close() wavfile.write(save_dir + "original_{}.wav".format(i), rate, sample) data_stream = ScaleAndShift(data_stream, scale = 1/data_std, shift = -data_mean/data_std) data_stream = Mapping(data_stream, _downsample_and_upsample, add_sources=('upsampled',)) epoch_iterator = data_stream.get_epoch_iterator() raw_audio_std, upsampled_audio = next(epoch_iterator) for i in xrange(n_iter-1): x_tr,y_tr = next(epoch_iterator) raw_audio_std = numpy.hstack([raw_audio_std, x_tr]) upsampled_audio = numpy.hstack([upsampled_audio, y_tr]) for i,(original_, upsampled_) in enumerate( zip(raw_audio_std, upsampled_audio)[:n_samples]): f, (ax1, ax2) = pyplot.subplots(2, sharex=True, sharey=True) ax1.plot(original_) ax2.plot(upsampled_) f.subplots_adjust(hspace=0)
main_loop = load(os.path.join(exp_path, exp_file)) rate = 16000 for i, sample in enumerate(raw_audio[:n_samples]): pyplot.plot(sample) pyplot.savefig(save_dir + "original_%i.png" % i) pyplot.close() wavfile.write(save_dir + "original_{}.wav".format(i), rate, sample) data_stream = ScaleAndShift(data_stream, scale=1 / data_std, shift=-data_mean / data_std) data_stream = Mapping(data_stream, _downsample_and_upsample, add_sources=("upsampled",)) epoch_iterator = data_stream.get_epoch_iterator() raw_audio_std, upsampled_audio = next(epoch_iterator) for i in xrange(n_iter - 1): x_tr, y_tr = next(epoch_iterator) raw_audio_std = numpy.hstack([raw_audio_std, x_tr]) upsampled_audio = numpy.hstack([upsampled_audio, y_tr]) for i, (original_, upsampled_) in enumerate(zip(raw_audio_std, upsampled_audio)[:n_samples]): f, (ax1, ax2) = pyplot.subplots(2, sharex=True, sharey=True) ax1.plot(original_) ax2.plot(upsampled_) f.subplots_adjust(hspace=0) f.savefig(save_dir + "comparison_upsample_%i.png" % i)
def test_scale_and_shift(): stream = DataStream( IterableDataset( OrderedDict([('features', [1, 2, 3]), ('targets', [0, 1, 0])]))) wrapper = ScaleAndShift(stream, 2, -1, which_sources=('targets',)) assert list(wrapper.get_epoch_iterator()) == [(1, -1), (2, 1), (3, -1)]
generator.weights_init = IsotropicGaussian(0.01) generator.biases_init = Constant(0.) generator.initialize() #ipdb.set_trace() cost_matrix = generator.cost_matrix(x, x_mask) cost = cost_matrix.sum()/x_mask.sum() cost.name = "sequence_log_likelihood" ############## # Test with first batch ############## x_tr, x_mask_tr = next(data_stream.get_epoch_iterator()) f1 = function([x, x_mask], cost) #print f1(x_tr, x_mask_tr) #ipdb.set_trace() ################ # Optimization Algorithm ################ cg = ComputationGraph(cost) model = Model(cost) algorithm = GradientDescent( cost=cost, parameters=cg.parameters, step_rule=CompositeRule([StepClipping(10.0), Adam(lr)]),