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
python类floatX()的实例源码
roc_auc.py 文件源码
项目:deep-mil-for-whole-mammogram-classification
作者: wentaozhu
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
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)
lstm_model_sim.py 文件源码
项目:Learning-sentence-representation-with-guidance-of-human-attention
作者: wangshaonan
项目源码
文件源码
阅读 16
收藏 0
点赞 0
评论 0
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.')
utils.py 文件源码
项目:Learning-sentence-representation-with-guidance-of-human-attention
作者: wangshaonan
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
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)
utils.py 文件源码
项目:Learning-sentence-representation-with-guidance-of-human-attention
作者: wangshaonan
项目源码
文件源码
阅读 17
收藏 0
点赞 0
评论 0
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_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 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 __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)