def __init__(self, latent_dim, hidden_dim, x_k, n_steps): # x, output, (n, x_k+2) self.latent_dim = latent_dim self.hidden_dim = hidden_dim self.x_k = x_k self.n_steps = n_steps # z = input, (batch_size, latent_dim) self.W_f = T.fmatrix("W_f") # z, (latent_dim, hidden_dim) self.U_f = T.fmatrix("U_f") # h, (hidden_dim, hidden_dim) V_f = T.fmatrix("V_f") # x, (x_k+2, hidden_dim) b_f = T.frow("b_f") # (hidden_dim,) W_i = T.fmatrix() U_i = T.fmatrix() V_i = T.fmatrix() b_i = T.frow() W_c = T.fmatrix() U_c = T.fmatrix() V_c = T.fmatrix() b_c = T.frow() W_o = T.fmatrix() U_o = T.fmatrix() V_o = T.fmatrix() b_o = T.frow() W_x = T.fmatrix() # x_t, (hidden_dim, x_k+1) b_x = T.frow() # (x_k+1,) """ x = last output (int32) h = hidden state z = input vector """ # sequences, non-sequences def func(_x, _h, _z, _W_f, _U_f, _V_f, _b_f, _W_i, _U_i, _V_i, _b_i, _W_c, _U_c, _V_c, _b_c, _W_o, _U_o, _V_o, _b_o, _W_x, _b_x): f = T.nnet.sigmoid(_V_f[_x, :] + T.dot(_z, _W_f) + T.dot(_h, _U_f) + _b_f) i = T.nnet.sigmoid(_V_i[_x, :] + T.dot(_z, _W_i) + T.dot(_h, _U_i) + _b_i) c = T.tanh(_V_c[_x, :] + T.dot(_z, _W_c) + T.dot(_h, _U_c) + _b_c) o = T.nnet.sigmoid(_V_o[_x, :] + T.dot(_z, _W_o) + T.dot(_h, _U_o) + _b_o) _h_t = (f * _h) + (i * c) _o_t = o * T.tanh(c) _x_t = T.argmax(T.dot(_o_t, _W_x) + _b_x, axis=-1) + 1 switch = T.eq(_x, 1) _x_t = switch + ((1 - switch) * _x_t) _x_t = T.cast(_x_t, 'int32') return _x_t, _h_t params = [ W_f, U_f, V_f, b_f, W_i, U_i, V_i, b_i, W_c, U_c, V_c, b_c, W_o, U_o, V_o, b_o, W_x, b_x ]
def test_gpu_opt_wor(): # We test the case where we put the op on the gpu when the output # is moved to the gpu. p = tensor.fmatrix() u = tensor.fvector() n = tensor.iscalar() for replace in [False, True]: m = multinomial.ChoiceFromUniform(odtype='auto', replace=replace)(p, u, n) assert m.dtype == 'int64', m.dtype f = function([p, u, n], m, allow_input_downcast=True, mode=mode_with_gpu) assert any([type(node.op) is GPUAChoiceFromUniform for node in f.maker.fgraph.toposort()]) n_samples = 3 pval = np.arange(10000 * 4, dtype='float32').reshape((10000, 4)) + 0.1 pval = pval / pval.sum(axis=1)[:, None] uval = np.ones(pval.shape[0] * n_samples) * 0.5 f(pval, uval, n_samples) # Test with a row, it was failing in the past. r = tensor.frow() m = multinomial.ChoiceFromUniform('auto', replace=replace)(r, u, n) assert m.dtype == 'int64', m.dtype f = function([r, u, n], m, allow_input_downcast=True, mode=mode_with_gpu) assert any([type(node.op) is GPUAChoiceFromUniform for node in f.maker.fgraph.toposort()]) pval = np.arange(1 * 4, dtype='float32').reshape((1, 4)) + 0.1 pval = pval / pval.sum(axis=1)[:, None] uval = np.ones_like(pval[:, 0]) * 0.5 f(pval, uval, 1)
def test_gpu_opt(): if not cuda.cuda_available: # Skip test if cuda_ndarray is not available. from nose.plugins.skip import SkipTest raise SkipTest('Optional package cuda not available') # We test the case where we put the op on the gpu when the output # is moved to the gpu. p = tensor.fmatrix() u = tensor.fvector() m = multinomial.MultinomialFromUniform('auto')(p, u) assert m.dtype == 'float32', m.dtype m_gpu = cuda.gpu_from_host(m) f = function([p, u], m_gpu, allow_input_downcast=True, mode=get_mode(True)) assert any([type(node.op) is multinomial.GpuMultinomialFromUniform for node in f.maker.fgraph.toposort()]) pval = numpy.arange(10000 * 4, dtype='float32').reshape((10000, 4))+0.1 pval = pval / pval.sum(axis=1)[:, None] uval = numpy.ones_like(pval[:, 0]) * 0.5 mval = f(pval, uval) # Test with a row, it was failing in the past. r = tensor.frow() m = multinomial.MultinomialFromUniform('auto')(r, u) assert m.dtype == 'float32', m.dtype m_gpu = cuda.gpu_from_host(m) f = function([r, u], m_gpu, allow_input_downcast=True, mode=get_mode(True)) assert any([type(node.op) is multinomial.GpuMultinomialFromUniform for node in f.maker.fgraph.toposort()]) pval = numpy.arange(1 * 4, dtype='float32').reshape((1, 4))+0.1 pval = pval / pval.sum(axis=1)[:, None] uval = numpy.ones_like(pval[:, 0]) * 0.5 mval2 = f(pval, uval)
def test_gpu_opt(): # Does have some overlap with test_multinomial_0 # We test the case where we put the op on the gpu when the output # is moved to the gpu. p = tensor.fmatrix() u = tensor.fvector() m = theano.sandbox.multinomial.MultinomialFromUniform('auto')(p, u) assert m.dtype == 'float32', m.dtype f = function([p, u], m, allow_input_downcast=True, mode=mode_with_gpu) assert any([type(node.op) is GPUAMultinomialFromUniform for node in f.maker.fgraph.toposort()]) pval = np.arange(10000 * 4, dtype='float32').reshape((10000, 4)) + 0.1 pval = pval / pval.sum(axis=1)[:, None] uval = np.ones_like(pval[:, 0]) * 0.5 f(pval, uval) # Test with a row, it was failing in the past. r = tensor.frow() m = theano.sandbox.multinomial.MultinomialFromUniform('auto')(r, u) assert m.dtype == 'float32', m.dtype f = function([r, u], m, allow_input_downcast=True, mode=mode_with_gpu) assert any([type(node.op) is GPUAMultinomialFromUniform for node in f.maker.fgraph.toposort()]) pval = np.arange(1 * 4, dtype='float32').reshape((1, 4)) + 0.1 pval = pval / pval.sum(axis=1)[:, None] uval = np.ones_like(pval[:, 0]) * 0.5 f(pval, uval)
def test_theano(self, data, types): ''' data: np.array with the last column as what type of data it is types: the number of types ''' self.test = np.zeros((self.units, types)) self.err = 0 s = T.frow('s') match = function( inputs=[s], outputs=self._match(s), # return err, bmu allow_input_downcast=True) # Rescale the test data the same way training data gets rescaled samples = data.shape[0] data[:, 0:self.features] = (data[:, 0:self.features] - self.means) / self.stds + 0.5 for i in range(samples): sample = data[i, 0:self.features][None, :] index = data[i, -1] err, unit = match(sample) self.test[unit, index] += 1 self.err += err self.err = self.err / float(self.units) print 'error:', self.err
def test_theano(self, data, types): ''' data: np.array with the last column as what type of data it is types: the number of types ''' self.test = np.zeros((self.units, types)) self.err = 0 s = T.frow('s') match = function( inputs = [s], outputs = self._match(s), # return err, bmu allow_input_downcast = True ) # Rescale the test data the same way training data gets rescaled samples = data.shape[0] data[:, 0:self.features] = (data[:, 0:self.features] - self.means)/self.stds + 0.5 for i in range(samples): sample = data[i, 0:self.features][None, :] index = data[i, -1] err, unit = match(sample) self.test[unit, index] += 1 self.err += err self.err = self.err/float(self.units) print 'error:', self.err
def test_broadcast_mismatch(self): data = self.rng.rand(2, 3).astype('float32') x = f32sc(data) print x.broadcastable y = tensor.frow('y') print y.broadcastable cond = theano.tensor.iscalar('cond') self.assertRaises(TypeError, ifelse, cond, x, y) self.assertRaises(TypeError, ifelse, cond, y, x)
def test_theano(self, data=None, weights=None, quiet=False): ''' data: np.array with the last column as what type of data it is types: the number of types ''' s = T.frow('s') match = function( inputs=[s], outputs=self._match(s), # return err, bmu allow_input_downcast=True) # Rescale the test data the same way training data gets rescaled if isinstance(data, np.ndarray) and isinstance(weights, np.ndarray): tstdata = data self.types = len(weights) else: f = h5py.File(self.datafile, 'r') tstdata = f[self.tst_route][()] tstdata = np.concatenate( (tstdata[:, 0:self.features], tstdata[:, -1:]), axis=1) weights = f[self.tst_route].attrs['weights'] tstdata[:, 0:self.features] = (tstdata[:, 0:self.features] - self.means) / self.stds + 0.5 self.types = len(weights) if not quiet: print 'testing on:', f[ self.tst_route].attrs['samples'], 'with weights', weights f.close() self.test = np.zeros((self.units, self.types)) self.err = 0 samples = tstdata.shape[0] for i in range(samples): sample = tstdata[i, 0:self.features][None, :] index = tstdata[i, -1] err, unit = match(sample) self.test[unit, index] += weights[index] self.err += err self.err = self.err / float(self.units) if not quiet: print 'error:', self.err return np.sum(self.test, axis=-1) # return the total number of hits in each cell
def test_theano(self, data = None, weights = None, quiet = False): ''' data: np.array with the last column as what type of data it is types: the number of types ''' s = T.frow('s') match = function( inputs = [s], outputs = self._match(s), # return err, bmu allow_input_downcast = True ) # Rescale the test data the same way training data gets rescaled if isinstance(data, np.ndarray) and isinstance(weights, np.ndarray): tstdata = data self.types = len(weights) else: f = h5py.File(self.datafile, 'r') tstdata = f[self.tst_route][()] tstdata = np.concatenate((tstdata[:, 0:self.features], tstdata[:, -1:]), axis = 1) weights = f[self.tst_route].attrs['weights'] tstdata[:, 0:self.features] = (tstdata[:, 0:self.features] - self.means)/self.stds + 0.5 self.types = len(weights) if not quiet: print 'testing on:', f[self.tst_route].attrs['samples'], 'with weights', weights f.close() self.test = np.zeros((self.units, self.types)) self.err = 0 samples = tstdata.shape[0] for i in range(samples): sample = tstdata[i, 0:self.features][None, :] index = tstdata[i, -1] err, unit = match(sample) self.test[unit, index] += weights[index] self.err += err self.err = self.err/float(self.units) if not quiet: print 'error:', self.err return np.sum(self.test, axis = -1) # return the total number of hits in each cell
def train_theano(self, quiet=False, razor=True): ''' A method that takes an np.array, scales it to have zero mean and unit standard deviation, and train the map on the data ''' # ----- # Define symbolic variables and compile the functions # ----- broadscalar = T.TensorType('float32', (True, True)) s = T.frow('s') w = broadscalar('w') win = T.frow('win') match = function( inputs=[s], outputs=self._match(s), # return err, bmu allow_input_downcast=True) update_map = function(inputs=[s, w, win], outputs=[], updates=self._update_map(s, w, win), allow_input_downcast=True) update_params = function(inputs=[], outputs=[], updates=self._update_params(), allow_input_downcast=True) # ---- # Load training data # ---- f = h5py.File(self.datafile, 'r') trndata = f[self.trn_route][()] weights = f[self.trn_route].attrs['weights'] f.close() trndata = np.concatenate( (trndata[:, 0:self.features], trndata[:, -1:]), axis=1) self.means = np.mean(trndata[:, 0:self.features], axis=0) self.stds = np.std(trndata[:, 0:self.features], axis=0) trndata[:, 0:self.features] = (trndata[:, 0:self.features] - self.means) / self.stds + 0.5 # ----- # Training starts here # ----- samples = trndata.shape[0] self.fires = np.zeros( (samples, 2)) # One for index of the neuron that fired and print '-------------------------------------------------------' print 'Training starts....' print 'Number of samples:', samples, 'with weights:', weights print 'Epochs: {0:2d}, Sigma: ({1:4f}, {2:4f}), Lrate: ({3:4f}, {4:4f}), Threshold: {5:3d}'.format( self.epochs, self.sigma_i, self.sigma_f, self.lrate_i, self.lrate_f, self.threshold) total_time = 0 for e in range(self.epochs): if not quiet: print '[ epoch: {0:5d}]'.format(e) print ' sigma:{0:5f} lrate:{1:5f}'.format( self.sigma.get_value(), self.lrate.get_value()) start = time.mktime(time.localtime()) ordering = np.random.permutation(samples) for i in ordering: sample = trndata[i, 0:self.features][None, :] weight = np.array([[trndata[i, -1]]]) error, unit = match(sample) if self.fires[i, 0] == unit: self.fires[i, 1] += 1 else: self.fires[i, 0] = unit self.fires[i, 1] = 0 if self.fires[i, 1] < self.threshold: winner = self.grid.get_value()[unit][None, :] update_map(sample, weight, winner) update_params() end = time.mktime(time.localtime()) total_time += (end - start) if not quiet: print ' stable samples:{0:5d}'.format( np.sum(self.fires[:, 1] >= self.threshold)) print ' time stamp:{0:5f}'.format(end - start) print 'average time per epoch: {0:5f}'.format(total_time / float(self.epochs)) print 'average time per sample: {0:5f}'.format(total_time / (self.epochs * samples)) print '-------------------------------------------------------'
def train_theano(self, quiet = False, razor = True): ''' A method that takes an np.array, scales it to have zero mean and unit standard deviation, and train the map on the data ''' # ----- # Define symbolic variables and compile the functions # ----- broadscalar = T.TensorType('float32', (True, True)) s = T.frow('s') w = broadscalar('w') win = T.frow('win') match = function( inputs = [s], outputs = self._match(s), # return err, bmu allow_input_downcast = True ) update_map = function( inputs = [s, w, win], outputs = [], updates = self._update_map(s, w, win), allow_input_downcast = True ) update_params = function( inputs = [], outputs = [], updates = self._update_params(), allow_input_downcast = True ) # ---- # Load training data # ---- f = h5py.File(self.datafile, 'r') trndata = f[self.trn_route][()] weights = f[self.trn_route].attrs['weights'] f.close() trndata = np.concatenate((trndata[:, 0:self.features], trndata[:, -1:]), axis = 1) self.means = np.mean(trndata[:, 0:self.features], axis = 0) self.stds = np.std(trndata[:, 0:self.features], axis = 0) trndata[:, 0:self.features] = (trndata[:, 0:self.features] - self.means)/self.stds + 0.5 # ----- # Training starts here # ----- samples = trndata.shape[0] self.fires = np.zeros((samples, 2)) # One for index of the neuron that fired and print '-------------------------------------------------------' print 'Training starts....' print 'Number of samples:', samples, 'with weights:', weights print 'Epochs: {0:2d}, Sigma: ({1:4f}, {2:4f}), Lrate: ({3:4f}, {4:4f}), Threshold: {5:3d}'.format(self.epochs, self.sigma_i, self.sigma_f, self.lrate_i, self.lrate_f, self.threshold) total_time = 0 for e in range(self.epochs): if not quiet: print '[ epoch: {0:5d}]'.format(e) print ' sigma:{0:5f} lrate:{1:5f}'.format(self.sigma.get_value(), self.lrate.get_value()) start = time.mktime(time.localtime()) ordering = np.random.permutation(samples) for i in ordering: sample = trndata[i, 0:self.features][None, :] weight = np.array([[trndata[i, -1]]]) error, unit = match(sample) if self.fires[i, 0] == unit: self.fires[i, 1] += 1 else: self.fires[i, 0] = unit self.fires[i, 1] = 0 if self.fires[i, 1] < self.threshold: winner = self.grid.get_value()[unit][None, :] update_map(sample, weight, winner) update_params() end = time.mktime(time.localtime()) total_time += (end - start) if not quiet: print ' stable samples:{0:5d}'.format(np.sum(self.fires[:, 1] >= self.threshold)) print ' time stamp:{0:5f}'.format(end - start) print 'average time per epoch: {0:5f}'.format(total_time/float(self.epochs)) print 'average time per sample: {0:5f}'.format(total_time/(self.epochs * samples)) print '-------------------------------------------------------'
def decoder(latent_dim, hidden_dim, x_k, n_steps): # x, output, (n, x_k+2) z = T.fmatrix("z") # (n, latent_dim) W_f = T.fmatrix("W_f") # z, (latent_dim, hidden_dim) U_f = T.fmatrix("U_f") # h, (hidden_dim, hidden_dim) V_f = T.fmatrix("V_f") # x, (x_k+2, hidden_dim) b_f = T.frow("b_f") # (hidden_dim,) W_i = T.fmatrix() U_i = T.fmatrix() V_i = T.fmatrix() b_i = T.frow() W_c = T.fmatrix() U_c = T.fmatrix() V_c = T.fmatrix() b_c = T.frow() W_o = T.fmatrix() U_o = T.fmatrix() V_o = T.fmatrix() b_o = T.frow() W_x = T.fmatrix() # x_t, (hidden_dim, x_k+1) b_x = T.frow() # (x_k+1,) """ x = last output (int32) h = hidden state z = input vector """ # sequences, non-sequences def func(_x, _h, _z, _W_f, _U_f, _V_f, _b_f, _W_i, _U_i, _V_i, _b_i, _W_c, _U_c, _V_c, _b_c, _W_o, _U_o, _V_o, _b_o, _W_x, _b_x): f = T.nnet.sigmoid(_V_f[_x, :] + T.dot(_z, _W_f) + T.dot(_h, _U_f) + _b_f) i = T.nnet.sigmoid(_V_i[_x, :] + T.dot(_z, _W_i) + T.dot(_h, _U_i) + _b_i) c = T.tanh(_V_c[_x, :] + T.dot(_z, _W_c) + T.dot(_h, _U_c) + _b_c) o = T.nnet.sigmoid(_V_o[_x, :] + T.dot(_z, _W_o) + T.dot(_h, _U_o) + _b_o) _h_t = (f * _h) + (i * c) _o_t = o * T.tanh(c) _x_t = T.argmax(T.dot(_o_t, _W_x) + _b_x, axis=-1) + 1 switch = T.eq(_x, 1) _x_t = switch + ((1 - switch) * _x_t) _x_t = T.cast(_x_t, 'int32') return _x_t, _h_t params = [ W_f, U_f, V_f, b_f, W_i, U_i, V_i, b_i, W_c, U_c, V_c, b_c, W_o, U_o, V_o, b_o, W_x, b_x ] def sample_params(): e = 1e-2 _W_f = np.random.uniform(-e, e, (latent_dim, hidden_dim)) _U_f = np.random.uniform(-e, e, (hidden_dim, hidden_dim)) _V_f = np.random.uniform(-e, e, (x_k + 2, hidden_dim)) _b_f = np.random.uniform(-e, e, (1, hidden_dim)) _W_i = np.random.uniform(-e, e, (latent_dim, hidden_dim)) _U_i = np.random.uniform(-e, e, (hidden_dim, hidden_dim)) _V_i = np.random.uniform(-e, e, (x_k + 2, hidden_dim)) _b_i = np.random.uniform(-e, e, (1, hidden_dim)) _W_c = np.random.uniform(-e, e, (latent_dim, hidden_dim)) _U_c = np.random.uniform(-e, e, (hidden_dim, hidden_dim)) _V_c = np.random.uniform(-e, e, (x_k + 2, hidden_dim)) _b_c = np.random.uniform(-e, e, (1, hidden_dim)) _W_o = np.random.uniform(-e, e, (latent_dim, hidden_dim)) _U_o = np.random.uniform(-e, e, (hidden_dim, hidden_dim)) _V_o = np.random.uniform(-e, e, (x_k + 2, hidden_dim)) _b_o = np.random.uniform(-e, e, (1, hidden_dim)) _W_x = np.random.uniform(-e, e, (hidden_dim, x_k + 1)) _b_x = np.random.uniform(-e, e, (1, x_k + 1)) params = [ _W_f, _U_f, _V_f, _b_f, _W_i, _U_i, _V_i, _b_i, _W_c, _U_c, _V_c, _b_c, _W_o, _U_o, _V_o, _b_o, _W_x, _b_x ] return [p.astype(np.float32) for p in params] n = z.shape[0] outputs_info = [{ 'initial': T.zeros((n, ), dtype='int32') }, { 'initial': T.zeros((n, hidden_dim), dtype='float32') }] x, h = theano.scan(func, outputs_info=outputs_info, non_sequences=[z] + params, n_steps=n_steps)[0] # print("X: {}".format(x)) x = T.transpose(x, (1, 0)) h = T.transpose(h, (1, 0, 2)) return z, params, [x - 1, h], sample_params
def train_theano(self, data): ''' A method that takes an np.array, scales it to have zero mean and unit standard deviation, and train the map on the data data: np.array with the last column being weights ''' # ----- # Define symbolic variables and compile the functions # ----- broadscalar = T.TensorType('float32', (True, True)) s = T.frow('s') w = broadscalar('w') win = T.frow('win') match = function( inputs=[s], outputs=self._match(s), # return err, bmu allow_input_downcast=True) update_map = function(inputs=[s, w, win], outputs=[], updates=self._update_map(s, w, win), allow_input_downcast=True) update_params = function(inputs=[], outputs=[], updates=self._update_params(), allow_input_downcast=True) # ----- # Training starts here # ----- # Preprocess the data scaler = preprocessing.StandardScaler().fit(data[:, 0:self.features]) data[:, 0:self.features] = scaler.transform( data[:, 0:self.features]) + 0.5 self.means = scaler.mean_ self.stds = scaler.std_ samples = data.shape[0] self.fires = np.zeros( (samples, 2)) # One for index of the neuron that fired and print 'Training starts....' print 'Number of samples:', samples for e in range(self.epochs): print 'epoch:', e print 'sigma:', self.sigma.get_value() print 'lrate:', self.lrate.get_value() start = time.mktime(time.localtime()) ordering = np.random.permutation(samples) for i in ordering: sample = data[i, 0:self.features][None, :] weight = np.array([[data[i, -1]]]) error, unit = match(sample) if self.fires[i, 0] == unit: self.fires[i, 1] += 1 else: self.fires[i, 0] = unit self.fires[i, 1] = 0 if self.fires[i, 1] < self.threshold: winner = self.grid.get_value()[unit][None, :] update_map(sample, weight, winner) update_params() print 'number of stable samples:', np.sum( self.fires[:, 1] >= self.threshold) end = time.mktime(time.localtime()) print 'time stamp:', end - start
def train_theano(self, data): ''' A method that takes an np.array, scales it to have zero mean and unit standard deviation, and train the map on the data data: np.array with the last column being weights ''' # ----- # Define symbolic variables and compile the functions # ----- broadscalar = T.TensorType('float32', (True, True)) s = T.frow('s') w = broadscalar('w') win = T.frow('win') match = function( inputs = [s], outputs = self._match(s), # return err, bmu allow_input_downcast = True ) update_map = function( inputs = [s, w, win], outputs = [], updates = self._update_map(s, w, win), allow_input_downcast = True ) update_params = function( inputs = [], outputs = [], updates = self._update_params(), allow_input_downcast = True ) # ----- # Training starts here # ----- # Preprocess the data scaler = preprocessing.StandardScaler().fit(data[:, 0:self.features]) data[:, 0:self.features] = scaler.transform(data[:, 0:self.features]) + 0.5 self.means = scaler.mean_ self.stds = scaler.std_ samples = data.shape[0] self.fires = np.zeros((samples, 2)) # One for index of the neuron that fired and print 'Training starts....' print 'Number of samples:', samples for e in range(self.epochs): print 'epoch:', e print 'sigma:', self.sigma.get_value() print 'lrate:', self.lrate.get_value() start = time.mktime(time.localtime()) ordering = np.random.permutation(samples) for i in ordering: sample = data[i, 0:self.features][None, :] weight = np.array([[data[i, -1]]]) error, unit = match(sample) if self.fires[i, 0] == unit: self.fires[i, 1] += 1 else: self.fires[i, 0] = unit self.fires[i, 1] = 0 if self.fires[i, 1] < self.threshold: winner = self.grid.get_value()[unit][None, :] update_map(sample, weight, winner) update_params() print 'number of stable samples:', np.sum(self.fires[:, 1] >= self.threshold) end = time.mktime(time.localtime()) print 'time stamp:', end - start