我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用theano.config.floatX()。
def padMatrix(seqs, labels, options): lengths = np.array([len(seq) for seq in seqs]) - 1 n_samples = len(seqs) maxlen = np.max(lengths) x = np.zeros((maxlen, n_samples, options['inputDimSize'])).astype(config.floatX) y = np.zeros((maxlen, n_samples, options['numClass'])).astype(config.floatX) mask = np.zeros((maxlen, n_samples)).astype(config.floatX) for idx, (seq, lseq) in enumerate(zip(seqs,labels)): for xvec, subseq in zip(x[:,idx,:], seq[:-1]): xvec[subseq] = 1. for yvec, subseq in zip(y[:,idx,:], lseq[1:]): yvec[subseq] = 1. mask[:lengths[idx], idx] = 1. lengths = np.array(lengths, dtype=config.floatX) return x, y, mask, lengths
def perform(self, node, inputs, output_storage): """ Calculate ROC AUC score. Parameters ---------- node : Apply instance Symbolic inputs and outputs. inputs : list Sequence of inputs. output_storage : list List of mutable 1-element lists. """ if roc_auc_score is None: raise RuntimeError("Could not import from sklearn.") y_true, y_score = inputs try: roc_auc = roc_auc_score(y_true, y_score) except ValueError: roc_auc = np.nan #rvalue = np.array((roc_auc, prec, reca, f1)) #[0][0] output_storage[0][0] = theano._asarray(roc_auc, dtype=config.floatX)
def padMatrixWithTime(seqs, times, options): lengths = np.array([len(seq) for seq in seqs]) - 1 n_samples = len(seqs) maxlen = np.max(lengths) inputDimSize = options['inputDimSize'] numClass = options['numClass'] x = np.zeros((maxlen, n_samples, inputDimSize)).astype(config.floatX) t = np.zeros((maxlen, n_samples)).astype(config.floatX) mask = np.zeros((maxlen, n_samples)).astype(config.floatX) for idx, (seq,time) in enumerate(zip(seqs,times)): for xvec, subseq in zip(x[:,idx,:], seq[:-1]): xvec[subseq] = 1. mask[:lengths[idx], idx] = 1. t[:lengths[idx], idx] = time[:-1] if options['useLogTime']: t = np.log(t + options['logEps']) return x, t, mask, lengths
def padMatrixWithTime(seqs, labels, times, options): lengths = np.array([len(seq) for seq in seqs]) - 1 n_samples = len(seqs) maxlen = np.max(lengths) inputDimSize = options['inputDimSize'] numClass = options['numClass'] x = np.zeros((maxlen, n_samples, inputDimSize)).astype(config.floatX) y = np.zeros((maxlen, n_samples, numClass)).astype(config.floatX) t = np.zeros((maxlen, n_samples)).astype(config.floatX) mask = np.zeros((maxlen, n_samples)).astype(config.floatX) for idx, (seq,time,label) in enumerate(zip(seqs,times,labels)): for xvec, subseq in zip(x[:,idx,:], seq[:-1]): xvec[subseq] = 1. for yvec, subseq in zip(y[:,idx,:], label[1:]): yvec[subseq] = 1. mask[:lengths[idx], idx] = 1. t[:lengths[idx], idx] = time[:-1] lengths = np.array(lengths, dtype=config.floatX) if options['useLogTime']: t = np.log(t + options['logEps']) return x, y, t, mask, lengths
def padMatrixWithoutTime(seqs, labels, options): lengths = np.array([len(seq) for seq in seqs]) - 1 n_samples = len(seqs) maxlen = np.max(lengths) inputDimSize = options['inputDimSize'] numClass = options['numClass'] x = np.zeros((maxlen, n_samples, inputDimSize)).astype(config.floatX) y = np.zeros((maxlen, n_samples, numClass)).astype(config.floatX) mask = np.zeros((maxlen, n_samples)).astype(config.floatX) for idx, (seq,label) in enumerate(zip(seqs,labels)): for xvec, subseq in zip(x[:,idx,:], seq[:-1]): xvec[subseq] = 1. for yvec, subseq in zip(y[:,idx,:], label[1:]): yvec[subseq] = 1. mask[:lengths[idx], idx] = 1. lengths = np.array(lengths, dtype=config.floatX) return x, y, mask, lengths
def local_mean_subtraction(input, kernel_size=5): input_shape = (input.shape[0], 1, input.shape[1], input.shape[2]) input = input.reshape(input_shape).astype(floatX) X = T.tensor4(dtype=floatX) filter_shape = (1, 1, kernel_size, kernel_size) filters = mean_filter(kernel_size).reshape(filter_shape) filters = shared(_asarray(filters, dtype=floatX), borrow=True) mean = conv2d(input=X, filters=filters, input_shape=input.shape, filter_shape=filter_shape, border_mode='half') new_X = X - mean f = function([X], new_X) return f(input)
def init_params(options): """ Global (not LSTM) parameter. For the embeding and the classifier. """ params = OrderedDict() # embedding randn = numpy.random.rand(options['n_words'], options['dim_proj']) params['Wemb'] = (0.01 * randn).astype(config.floatX) params = get_layer(options['encoder'])[0](options, params, prefix=options['encoder']) # classifier params['U'] = 0.01 * numpy.random.randn(options['dim_proj'], options['ydim']).astype(config.floatX) params['b'] = numpy.zeros((options['ydim'],)).astype(config.floatX) return params
def param_init_lstm(options, params, prefix='lstm'): """ Init the LSTM parameter: :see: init_params """ W = numpy.concatenate([ortho_weight(options['dim_proj']), ortho_weight(options['dim_proj']), ortho_weight(options['dim_proj']), ortho_weight(options['dim_proj'])], axis=1) params[_p(prefix, 'W')] = W U = numpy.concatenate([ortho_weight(options['dim_proj']), ortho_weight(options['dim_proj']), ortho_weight(options['dim_proj']), ortho_weight(options['dim_proj'])], axis=1) params[_p(prefix, 'U')] = U b = numpy.zeros((4 * options['dim_proj'],)) params[_p(prefix, 'b')] = b.astype(config.floatX) return params
def to_measure(self, q) : # Compute edges's vertices a = q[self.connectivity[:,0]] b = q[self.connectivity[:,1]] c = q[self.connectivity[:,2]] # A surface is represented as a sum of dirac, one for each triangle x = .33333333 * (a + b + c) # Mean # Cross product ab = (b-a).dimshuffle(0, 1, 'x') ac = (c-a).dimshuffle(0, 'x', 1) t = (ab * ac).reshape((self.connectivity.shape[0], 9)) cp = t.dot( np.array( [ [0., 0., 0., 0., 0., 1., 0., -1., 0.], [0., 0., -1., 0., 0., 0., 1., 0., 0.], [0., 1., 0., -1., 0., 0., 0., 0., 0.] ] ).T) mu = .5 * T.sqrt( (cp**2).sum(1) ) # Length mu = T.cast(mu, dtype=config.floatX) return (x, mu)
def to_varifold(self, q) : # Compute edges's vertices a = q[self.connectivity[:,0]] b = q[self.connectivity[:,1]] c = q[self.connectivity[:,2]] # A surface is represented as a sum of dirac, one for each triangle x = .33333333 * (a + b + c) # Mean # Cross product ab = (b-a).dimshuffle(0, 1, 'x') ac = (c-a).dimshuffle(0, 'x', 1) t = (ab * ac).reshape((self.connectivity.shape[0], 9)) cp = t.dot( np.array( [ [0., 0., 0., 0., 0., 1., 0., -1., 0.], [0., 0., -1., 0., 0., 0., 1., 0., 0.], [0., 1., 0., -1., 0., 0., 0., 0., 0.] ] ).T) mu = T.sqrt( (cp**2).sum(1) ) # Length u = ( cp / mu.dimshuffle(0,'x')) # Normal direction mu = T.cast(.5*mu, dtype=config.floatX) u = T.cast(u, dtype=config.floatX) return (x, mu, u)
def getRegTerm(self, params, We, initial_We, l_out, l_softmax, pickled_params): if params.traintype == "normal": l2 = 0.5*params.LC*sum(lasagne.regularization.l2(x) for x in self.network_params) if params.updatewords: return l2 + 0.5*params.LW*lasagne.regularization.l2(We-initial_We) else: return l2 elif params.traintype == "reg": tmp = lasagne.layers.get_all_params(l_out, trainable=True) idx = 1 l2 = 0. while idx < len(tmp): l2 += 0.5*params.LRC*(lasagne.regularization.l2(tmp[idx]-np.asarray(pickled_params[idx].get_value(), dtype = config.floatX))) idx += 1 tmp = lasagne.layers.get_all_params(l_softmax, trainable=True) l2 += 0.5*params.LC*sum(lasagne.regularization.l2(x) for x in tmp) return l2 + 0.5*params.LRW*lasagne.regularization.l2(We-initial_We) elif params.traintype == "rep": tmp = lasagne.layers.get_all_params(l_softmax, trainable=True) l2 = 0.5*params.LC*sum(lasagne.regularization.l2(x) for x in tmp) return l2 else: raise ValueError('Params.traintype not set correctly.')
def getDataSim2(batch, nout): g1, g1_pos = [], [] g2, g2_pos = [], [] for i in batch: g1.append(i[0].embeddings) g2.append(i[1].embeddings) g1_pos.append(i[0].pos_embeddings) g2_pos.append(i[1].pos_embeddings) g1x, g1x_pos, g1x_mask = prepare_data(g1, g1_pos) g2x, g2x_pos, g2x_mask = prepare_data(g2, g2_pos) scores = [] for i in batch: scores.append(float(i[2])) scores = np.asarray(scores, dtype=config.floatX) return (scores, g1x, g1x_pos, g1x_mask, g2x, g2x_pos, g2x_mask)
def getDataSentiment(batch): g1 = [] g1_pos = [] for i in batch: g1.append(i[0].embeddings) g1_pos.append(i[0].pos_embeddings) g1x, g1x_pos = prepare_data(g1,g1_pos) scores = [] for i in batch: temp = np.zeros(2) label = i[1].strip() if label == "0": temp[0]=1 if label == "1": temp[1]=1 scores.append(temp) scores = np.matrix(scores)+0.000001 scores = np.asarray(scores,dtype=config.floatX) return (scores,g1x,g1x_pos)
def init_params(options): params = OrderedDict() # lstm params if options['pretrain'] == '': params['Wemb'] = 1.0 * numpy.random.rand(options['n_words'], options['dim_emb']).astype(config.floatX) else: file = 'best_model/' + options['disease'] + '/best.' + options['pretrain'] + '.pkl' f = open(file, 'rb') opt, model = cPickle.load(f) params['Wemb'] = model['Wemb'] params = init_lstm_params(options, params) # top layer init_top_params = tasks[options['task']][4] params = init_top_params(options, params) return params
def prepare_adm(diag_set, pm_set): n_adm = len(diag_set) lengths = [ [len(diag) for diag in diag_set], [len(pm) for pm in pm_set] ] max_len = numpy.max(lengths) adm_list = numpy.zeros((2, n_adm, max_len)).astype('int64') adm_mask = numpy.zeros((2, n_adm, max_len)).astype(theano.config.floatX) for idx, diag in enumerate(diag_set): adm_list[0, idx, :lengths[0][idx]] = diag[:lengths[0][idx]] adm_mask[0, idx, :lengths[0][idx]] = 1 for idx, pm in enumerate(pm_set): if lengths[1][idx] == 0: pm = [0] lengths[1][idx] = 1 adm_list[1, idx, :lengths[1][idx]] = pm[:lengths[1][idx]] adm_mask[1, idx, :lengths[1][idx]] = 1 return adm_list, adm_mask
def _buildEmission(self, z, X, add_noise = False): """ Build subgraph to estimate conditional params """ if add_noise: inp_p = z + self.srng.normal(z.shape,0,0.0025,dtype=config.floatX) else: inp_p = z for p_l in range(self.params['p_layers']): inp_p = self._LinearNL(self.tWeights['p_'+str(p_l)+'_W'], self.tWeights['p_'+str(p_l)+'_b'], inp_p) if self.params['data_type']=='real': mu_p = self._LinearNL(self.tWeights['p_mu_W'],self.tWeights['p_mu_b'],inp_p, onlyLinear=True) logcov_p= self._LinearNL(self.tWeights['p_logcov_W'],self.tWeights['p_logcov_b'],inp_p, onlyLinear=True) negCLL_m= 0.5 * (np.log(2 * np.pi) + logcov_p + ((X - mu_p) / T.exp(0.5*logcov_p))**2) return (mu_p, logcov_p), negCLL_m.sum(1,keepdims=True) else: mean_p = T.nnet.sigmoid(self._LinearNL(self.tWeights['p_mean_W'],self.tWeights['p_mean_b'],inp_p,onlyLinear=True)) negCLL_m = T.nnet.binary_crossentropy(mean_p,X) return (mean_p,), negCLL_m.sum(1,keepdims=True)
def evaluateBound(self, dataset, batch_size, S=10): """ Evaluate bound S times on dataset """ N = dataset.shape[0] bound = 0 for bnum,st_idx in enumerate(range(0,N,batch_size)): end_idx = min(st_idx+batch_size, N) X = dataset[st_idx:end_idx].astype(config.floatX) for s in range(S): eps = np.random.randn(X.shape[0],self.params['dim_stochastic']).astype(config.floatX) if self.params['inference_model']=='single': batch_bound = self.evaluate(X=X, eps=eps) else: assert False,'Should not be here' bound += batch_bound bound /= float(N*S) return bound
def evaluateClassifier(self, dataset, batch_size): """ Evaluate neg log likelihood and accuracy """ N = dataset['X'].shape[0] crossentropy = 0 ncorrect = 0 for bnum, st_idx in enumerate(range(0,N,batch_size)): end_idx = min(st_idx+batch_size,N) X = self.sampleDataset(dataset['X'][st_idx:end_idx].astype(config.floatX)) Y = dataset['Y'][st_idx:end_idx].astype('int32') batch_crossentropy, batch_ncorrect = self.evaluate(X=X,Y=Y) crossentropy += batch_crossentropy ncorrect += batch_ncorrect crossentropy /= float(N) accuracy = ncorrect/float(N) return crossentropy, accuracy
def evaluateNLL(self, dataset, labels, batch_size = 200): """ Evaluate likelihood of dataset """ nll = 0 start_time = time.time() N = dataset.shape[0] for bnum,st_idx in enumerate(range(0,N,batch_size)): end_idx = min(st_idx+batch_size, N) X = dataset[st_idx:end_idx,:].astype(config.floatX) Y = labels[st_idx:end_idx][:,None].astype(config.floatX) batch_nll = self.evaluate(X=X, Y=Y) nll += batch_nll self._p(('\tBnum:%d, Batch Bound: %.4f')%(bnum,batch_nll/float(X.shape[0]))) nll /= float(X.shape[0]) end_time = time.time() self._p(('(Evaluation) NLL: %.4f [Took %.4f seconds]')%(nll,end_time-start_time)) return nll
def _getLSTMWeight(self, shape): """ http://yyue.blogspot.com/2015/01/a-brief-overview-of-deep-learning.html For LSTMs, use orthogonal initializations for the weight matrices and set the forget gate biases to be high """ if len(shape)==1: #bias dim = int(shape[0]/4) self._p('Sampling biases for LSTM from exponential distribution') return np.random.laplace(size=shape).astype(config.floatX) #return np.concatenate([self._getUniformWeight((dim,)),np.ones((dim,))*self.params['forget_bias'], # self._getUniformWeight((dim*2,))]).astype(config.floatX) elif len(shape)==2: #weight nin = shape[0] nout= shape[1] assert int(nout/4)==nin,'Not LSTM weight.' return np.concatenate([self._getOrthogonalWeight((nin,int(nout/4))), self._getOrthogonalWeight((nin,int(nout/4))), self._getOrthogonalWeight((nin,int(nout/4))), self._getOrthogonalWeight((nin,int(nout/4)))] ,axis=1).astype(config.floatX) else: assert False,'Should not get here'
def _getHe2015(self, shape): #http://cs231n.github.io/neural-networks-2/ if len(shape)==1: return np.random.normal(0,self.params['init_weight'],shape).astype(config.floatX) initializer = 'uniform' if self.params['nonlinearity']=='relu': K = np.sqrt(2./float((1+self.params['leaky_param']**2)*(shape[0]))) else: K = np.sqrt(1./float(shape[0])) if initializer=='uniform': return np.random.uniform(-K,K,shape).astype(config.floatX) elif initializer=='normal': return np.random.normal(0,K,shape).astype(config.floatX) else: assert False,'Invalid initializer in _getXavierWeight'
def build_model(tparams,options): #trng = RandomStreams(SEED) # Used for dropout. #use_noise = theano.shared(numpy_floatX(0.)) # x: n_steps * n_x x = tensor.matrix('x', dtype=config.floatX) n_steps = x.shape[0] h_decoder = decoder_layer(tparams, x) pred = tensor.nnet.sigmoid(tensor.dot(h_decoder,tparams['Vhid']) + tparams['bhid']) f_pred = theano.function([x],pred) cost = tensor.sum(tensor.nnet.binary_crossentropy(pred,x))/n_steps return x, f_pred, cost
def init_params(config): """ parameters for different layers including embedding, lstm, and classifier. """ params = OrderedDict() # embedding layer parameter randn = np.random.rand(config.n_words, config.dim_proj) params['Wemb'] = (0.01 * randn).astype(T_config.floatX) # lstm layer parameters params = param_init_lstm(config, params, prefix='lstm') # classifier parameters params['U'] = 0.01 * np.random.randn(config.dim_proj, config.ydim).astype(T_config.floatX) params['b'] = np.zeros((config.ydim,)).astype(T_config.floatX) return params
def param_init_lstm(config, params, prefix='lstm'): """ Init the LSTM parameter and attach it to the exitings params :see: init_params """ # each LSTM cell has 4 weight matrices for input and 4 weight matrices for # state W = np.concatenate([ortho_weight(config.dim_proj)]*4, axis=1) params[_p(prefix, 'W')] = W U = np.concatenate([ortho_weight(config.dim_proj)]*4, axis=1) params[_p(prefix, 'U')] = U b = np.zeros((4 * config.dim_proj,)) params[_p(prefix, 'b')] = b.astype(T_config.floatX) return params
def __init__(self, init_momentum, averaging_coeff=0.95, stabilizer=1e-2, use_first_order=False, bound_inc=False, momentum_clipping=None): init_momentum = float(init_momentum) assert init_momentum >= 0. assert init_momentum <= 1. averaging_coeff = float(averaging_coeff) assert averaging_coeff >= 0. assert averaging_coeff <= 1. stabilizer = float(stabilizer) assert stabilizer >= 0. self.__dict__.update(locals()) del self.self self.momentum = sharedX(self.init_momentum) self.momentum_clipping = momentum_clipping if momentum_clipping is not None: self.momentum_clipping = np.cast[config.floatX](momentum_clipping)
def __init__(self, init_momentum=0.9, averaging_coeff=0.99, stabilizer=1e-4, update_param_norm_ratio=0.003, gradient_clipping=None): init_momentum = float(init_momentum) assert init_momentum >= 0. assert init_momentum <= 1. averaging_coeff = float(averaging_coeff) assert averaging_coeff >= 0. assert averaging_coeff <= 1. stabilizer = float(stabilizer) assert stabilizer >= 0. self.__dict__.update(locals()) del self.self self.momentum = sharedX(self.init_momentum) self.update_param_norm_ratio = update_param_norm_ratio self.gradient_clipping = gradient_clipping if gradient_clipping is not None: self.gradient_clipping = np.cast[config.floatX](gradient_clipping)
def init_params(options,W): params = OrderedDict() # W is initialized by the pretrained word embedding params['Wemb'] = W.astype(config.floatX) # otherwise, W will be initialized randomly # n_words = options['n_words'] # n_x = options['n_x'] # params['Wemb'] = uniform_weight(n_words,n_x) length = len(options['filter_shapes']) for idx in range(length): params = param_init_encoder(options['filter_shapes'][idx],params,prefix=_p('cnn_encoder',idx)) n_h = options['feature_maps'] * length params['Wy'] = uniform_weight(n_h,options['n_y']) params['by'] = zero_bias(options['n_y']) return params
def init_params(options,W): n_h = options['n_h'] n_y = options['n_y'] params = OrderedDict() # W is initialized by the pretrained word embedding params['Wemb'] = W.astype(config.floatX) # otherwise, W will be initialized randomly # n_words = options['n_words'] # n_x = options['n_x'] # params['Wemb'] = uniform_weight(n_words,n_x) # bidirectional LSTM params = param_init_encoder(options,params,prefix="gru_encoder") params = param_init_encoder(options,params,prefix="gru_encoder_rev") params['Wy'] = uniform_weight(2*n_h,n_y) params['by'] = zero_bias(n_y) return params
def init_params(options,W): n_h = options['n_h'] n_y = options['n_y'] params = OrderedDict() # W is initialized by the pretrained word embedding params['Wemb'] = W.astype(config.floatX) # otherwise, W will be initialized randomly # n_words = options['n_words'] # n_x = options['n_x'] # params['Wemb'] = uniform_weight(n_words,n_x) # bidirectional LSTM params = param_init_encoder(options,params,prefix="lstm_encoder") params = param_init_encoder(options,params,prefix="lstm_encoder_rev") params['Wy'] = uniform_weight(2*n_h,n_y) params['by'] = zero_bias(n_y) return params
def load_wemb(params, vocab): wemb = pkl.load(open(prm.wordemb_path, 'rb')) dim_emb_orig = wemb.values()[0].shape[0] W = 0.01 * np.random.randn(prm.n_words, dim_emb_orig).astype(config.floatX) for word, pos in vocab.items(): if word in wemb: W[pos,:] = wemb[word] if prm.dim_emb < dim_emb_orig: pca =PCA(n_components=prm.dim_emb, copy=False, whiten=True) W = pca.fit_transform(W) params['W'] = W return params
def _causal_effect( hparams, mu1, mu1s_, mu2, mu2s_, tau_cmmn, obs1, obs2, Normal, floatX): u"""Distribution of observations. """ if hparams['causality'] == [1, 2]: # ---- Model 1: x1 -> x2 ---- x1s = obs1(mu=mu1 + mu1s_) b = Normal('b', mu=np.float32(0.), tau=np.float32(1 / tau_cmmn[1]), dtype=floatX) x2s = obs2(mu=mu2 + mu2s_ + b * (x1s - mu1 - mu1s_)) \ if hparams['subtract_mu_reg'] else \ obs2(mu=mu2 + mu2s_ + b * x1s) else: # ---- Model 2: x2 -> x1 ---- x2s = obs2(mu=mu2 + mu2s_) b = Normal('b', mu=np.float32(0.), tau=np.float32(1 / tau_cmmn[0]), dtype=floatX) x1s = obs1(mu=mu1 + mu1s_ + b * (x2s - mu2 - mu2s_)) \ if hparams['subtract_mu_reg'] else \ obs1(mu=mu1 + mu1s_ + b * x2s) return x1s, x2s, b
def __init__(self, feature_count=None, hidden_unit_count=None, category_count=None, archive=None): if archive is None and (feature_count is None or hidden_unit_count is None or category_count is None): raise ValueError( "If archive is not passed in, an " + Parameters.Globals.__name__ + " object needs all other constructor arguments to be integers.") if archive is None: self.embedding_weights = theano.shared( (0.01 * np.random.rand(feature_count, hidden_unit_count)).astype(config.floatX), # formerly 'Wemb' self.embedding_weights_literal) self.classifier_weights = theano.shared( 0.01 * np.random.randn(hidden_unit_count, category_count).astype(config.floatX), self.classifier_weights_literal) # formerly 'U' self.classifier_bias = theano.shared(np.zeros((category_count,)).astype(config.floatX), self.classifier_bias_literal) # formerly 'b' else: self.load_values_from_dict(archive)
def __init__(self, hidden_unit_count=None, archive=None): if archive is None and hidden_unit_count is None: raise ValueError( "If archive is not passed in, an " + Parameters.LSTM.__name__ + " object needs hidden_unit_count argument to be an integer.") if archive is None: gen__r_o_v = generate_random_orthogonal_vectors self.input_weights = theano.shared(np.concatenate([gen__r_o_v(hidden_unit_count), gen__r_o_v(hidden_unit_count), gen__r_o_v(hidden_unit_count), gen__r_o_v(hidden_unit_count)], axis=1), self.input_weights_literal) # formerly lstm_W self.hidden_weights = theano.shared(np.concatenate([gen__r_o_v(hidden_unit_count), gen__r_o_v(hidden_unit_count), gen__r_o_v(hidden_unit_count), gen__r_o_v(hidden_unit_count)], axis=1), self.hidden_weights_literal) # formerly lstm_U self.bias = theano.shared(np.zeros((4 * hidden_unit_count,)).astype(config.floatX), self.bias_literal) # formerly lstm_b else: self.load_values_from_dict(archive)
def read(self): # Check if there are changes to the read filehaschanged = os.stat(self.ymlfile).st_mtime != self.lastmodified # Update lastmodified timestamp if filehaschanged: self.lastmodified = os.stat(self.ymlfile).st_mtime else: return # Read from file with open(self.ymlfile, 'r') as f: update = yaml.load(f) # Update switches for switchname, switchvar in self.switches.items(): # Fetch if switchname in update.keys(): # Check if update needs to be eval-ed if isinstance(update[switchname], str) and update[switchname].startswith('np.'): switchvarval = eval(update[switchname]) else: switchvarval = getattr(np, config.floatX)(update[switchname]) # Set switch variable switchvar.set_value(switchvarval) return
def calculate_cost(test_model, dataset, options): batchSize = options['batchSize'] useTime = options['useTime'] costSum = 0.0 dataCount = 0 n_batches = int(np.ceil(float(len(dataset[0])) / float(batchSize))) for index in xrange(n_batches): batchX = dataset[0][index*batchSize:(index+1)*batchSize] if useTime: batchT = dataset[2][index*batchSize:(index+1)*batchSize] x, t, lengths = padMatrixWithTime(batchX, batchT, options) y = np.array(dataset[1][index*batchSize:(index+1)*batchSize]).astype(config.floatX) scores = test_model(x, y, t, lengths) else: x, lengths = padMatrixWithoutTime(batchX, options) y = np.array(dataset[1][index*batchSize:(index+1)*batchSize]).astype(config.floatX) scores = test_model(x, y, lengths) costSum += scores * len(batchX) dataCount += len(batchX) return costSum / dataCount
def test_select_distinct(self): """ Tests that MultinomialWOReplacementFromUniform always selects distinct elements """ p = tensor.fmatrix() u = tensor.fvector() n = tensor.iscalar() m = multinomial.MultinomialWOReplacementFromUniform('auto')(p, u, n) f = function([p, u, n], m, allow_input_downcast=True) n_elements = 1000 all_indices = range(n_elements) numpy.random.seed(12345) for i in [5, 10, 50, 100, 500, n_elements]: uni = numpy.random.rand(i).astype(config.floatX) pvals = numpy.random.randint(1, 100, (1, n_elements)).astype(config.floatX) pvals /= pvals.sum(1) res = f(pvals, uni, i) res = numpy.squeeze(res) assert len(res) == i, res assert numpy.all(numpy.in1d(numpy.unique(res), all_indices)), res
def test_select_distinct(self): """ Tests that multinomial_wo_replacement always selects distinct elements """ th_rng = RandomStreams(12345) p = tensor.fmatrix() n = tensor.iscalar() m = th_rng.multinomial_wo_replacement(pvals=p, n=n) f = function([p, n], m, allow_input_downcast=True) n_elements = 1000 all_indices = range(n_elements) numpy.random.seed(12345) for i in [5, 10, 50, 100, 500, n_elements]: pvals = numpy.random.randint(1, 100, (1, n_elements)).astype(config.floatX) pvals /= pvals.sum(1) res = f(pvals, i) res = numpy.squeeze(res) assert len(res) == i assert numpy.all(numpy.in1d(numpy.unique(res), all_indices)), res
def numpy_floatX(data): return numpy.asarray(data, dtype=floatX)
def get_variable(name, shape, initializer=None, dtype=floatX): if initializer != None: param = initializer(shape, dtype) else: param = random_normal_initializer()(shape, dtype) return theano.shared(value=param, name=name, borrow=True)
def numpy_floatX(data): return np.asarray(data, dtype=config.floatX)
def get_random_weight(dim1, dim2, left=-0.1, right=0.1): return np.random.uniform(left, right, (dim1, dim2)).astype(config.floatX)
def init_params(options): params = OrderedDict() np.random.seed(0) inputDimSize = options['inputDimSize'] numAncestors = options['numAncestors'] embDimSize = options['embDimSize'] hiddenDimSize = options['hiddenDimSize'] #hidden layer does not need an extra space attentionDimSize = options['attentionDimSize'] numClass = options['numClass'] params['W_emb'] = get_random_weight(inputDimSize+numAncestors, embDimSize) if len(options['embFile']) > 0: params['W_emb'] = load_embedding(options) options['embDimSize'] = params['W_emb'].shape[1] embDimSize = options['embDimSize'] params['W_attention'] = get_random_weight(embDimSize*2, attentionDimSize) params['b_attention'] = np.zeros(attentionDimSize).astype(config.floatX) params['v_attention'] = np.random.uniform(-0.1, 0.1, attentionDimSize).astype(config.floatX) params['W_gru'] = get_random_weight(embDimSize, 3*hiddenDimSize) params['U_gru'] = get_random_weight(hiddenDimSize, 3*hiddenDimSize) params['b_gru'] = np.zeros(3 * hiddenDimSize).astype(config.floatX) params['W_output'] = get_random_weight(hiddenDimSize, numClass) params['b_output'] = np.zeros(numClass).astype(config.floatX) return params
def build_model(tparams, leavesList, ancestorsList, options): dropoutRate = options['dropoutRate'] trng = RandomStreams(123) use_noise = theano.shared(numpy_floatX(0.)) x = T.tensor3('x', dtype=config.floatX) y = T.tensor3('y', dtype=config.floatX) mask = T.matrix('mask', dtype=config.floatX) lengths = T.vector('lengths', dtype=config.floatX) n_timesteps = x.shape[0] n_samples = x.shape[1] embList = [] for leaves, ancestors in zip(leavesList, ancestorsList): tempAttention = generate_attention(tparams, leaves, ancestors) tempEmb = (tparams['W_emb'][ancestors] * tempAttention[:,:,None]).sum(axis=1) embList.append(tempEmb) emb = T.concatenate(embList, axis=0) x_emb = T.tanh(T.dot(x, emb)) hidden = gru_layer(tparams, x_emb, options) hidden = dropout_layer(hidden, use_noise, trng, dropoutRate) y_hat = softmax_layer(tparams, hidden) * mask[:,:,None] logEps = 1e-8 cross_entropy = -(y * T.log(y_hat + logEps) + (1. - y) * T.log(1. - y_hat + logEps)) output_loglikelihood = cross_entropy.sum(axis=2).sum(axis=0) / lengths cost_noreg = T.mean(output_loglikelihood) if options['L2'] > 0.: cost = cost_noreg + options['L2'] * ((tparams['W_output']**2).sum() + (tparams['W_attention']**2).sum() + (tparams['v_attention']**2).sum()) return use_noise, x, y, mask, lengths, cost, cost_noreg, y_hat
def init_params(options): params = OrderedDict() inputSize = options['inputSize'] dimensionSize = options['dimensionSize'] rng = np.random.RandomState(1234) params['w'] = np.asarray(rng.uniform(low=-0.1, high=0.1, size=(inputSize, dimensionSize)), dtype=theano.config.floatX) rng = np.random.RandomState(12345) params['w_tilde'] = np.asarray(rng.uniform(low=-0.1, high=0.1, size=(inputSize, dimensionSize)), dtype=theano.config.floatX) params['b'] = np.zeros(inputSize).astype(theano.config.floatX) params['b_tilde'] = np.zeros(inputSize).astype(theano.config.floatX) return params
def build_model(tparams, options): weightVector = T.vector('weightVector', dtype=theano.config.floatX) iVector = T.vector('iVector', dtype='int32') jVector = T.vector('jVector', dtype='int32') cost = weightVector * (((tparams['w'][iVector] * tparams['w_tilde'][jVector]).sum(axis=1) + tparams['b'][iVector] + tparams['b_tilde'][jVector] - T.log(weightVector)) ** 2) return weightVector, iVector, jVector, cost.sum()
def load_data(infile): cooccurMap = pickle.load(open(infile, 'rb')) I = [] J = [] Weight = [] for key, value in cooccurMap.iteritems(): I.append(key[0]) J.append(key[1]) Weight.append(weightFunction(value)) shared_I = theano.shared(np.asarray(I, dtype='int32'), borrow=True) shared_J = theano.shared(np.asarray(J, dtype='int32'), borrow=True) shared_Weight = theano.shared(np.asarray(Weight, dtype=theano.config.floatX), borrow=True) return shared_I, shared_J, shared_Weight