def connect(self, inputs):
energy = tensor.dot(inputs, self.W) + self.b
energy = energy.reshape([energy.shape[0] * energy.shape[1], energy.shape[2]])
log_scores = tensor.log(tensor.nnet.softmax(energy))
predictions = tensor.argmax(log_scores, axis=-1)
return (log_scores, predictions)
python类argmax()的实例源码
def jaccard(y_pred, y_true, n_classes, one_hot=False):
assert (y_pred.ndim == 2) or (y_pred.ndim == 1)
# y_pred to indices
if y_pred.ndim == 2:
y_pred = T.argmax(y_pred, axis=1)
if one_hot:
y_true = T.argmax(y_true, axis=1)
# Compute confusion matrix
cm = T.zeros((n_classes, n_classes))
for i in range(n_classes):
for j in range(n_classes):
cm = T.set_subtensor(
cm[i, j], T.sum(T.eq(y_pred, i) * T.eq(y_true, j)))
# Compute Jaccard Index
TP_perclass = T.cast(cm.diagonal(), _FLOATX)
FP_perclass = cm.sum(1) - TP_perclass
FN_perclass = cm.sum(0) - TP_perclass
num = TP_perclass
denom = TP_perclass + FP_perclass + FN_perclass
return T.stack([num, denom], axis=0)
def accuracy(y_pred, y_true, void_labels, one_hot=False):
assert (y_pred.ndim == 2) or (y_pred.ndim == 1)
# y_pred to indices
if y_pred.ndim == 2:
y_pred = T.argmax(y_pred, axis=1)
if one_hot:
y_true = T.argmax(y_true, axis=1)
# Compute accuracy
acc = T.eq(y_pred, y_true).astype(_FLOATX)
# Create mask
mask = T.ones_like(y_true, dtype=_FLOATX)
for el in void_labels:
indices = T.eq(y_true, el).nonzero()
if any(indices):
mask = T.set_subtensor(mask[indices], 0.)
# Apply mask
acc *= mask
acc = T.sum(acc) / T.sum(mask)
return acc
def update_conf_mat(self, y, p_y_given_x):
"""
Update the confusion matrix with the given true labels and estimated
labels.
"""
if self.n_out == 1:
y_decision = (p_y_given_x > self.threshold)
else:
y_decision = np.argmax(p_y_given_x, axis=1)
for i in xrange(y.shape[0]):
self.conf_mat[y[i]][y_decision[i]] += 1
def _get_hidden_layer_connectivity(self, layerIdx):
layer_size = self._hidden_sizes[layerIdx]
if layerIdx == 0:
p_vals = self._get_p(T.min(self.layers_connectivity[layerIdx]))
else:
p_vals = self._get_p(T.min(self.layers_connectivity_updates[layerIdx-1]))
# #Implementations of np.choose in theano GPU
# return T.nonzero(self._mrng.multinomial(pvals=[self._p_vals] * layer_size, dtype=theano.config.floatX))[1].astype(dtype=theano.config.floatX)
# return T.argmax(self._mrng.multinomial(pvals=[self._p_vals] * layer_size, dtype=theano.config.floatX), axis=1)
return T.sum(T.cumsum(self._mrng.multinomial(pvals=T.tile(p_vals[::-1][None, :], (layer_size, 1)), dtype=theano.config.floatX), axis=1), axis=1)
roc_auc.py 文件源码
项目:deep-mil-for-whole-mammogram-classification
作者: wentaozhu
项目源码
文件源码
阅读 28
收藏 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
print(y_true.shape)
y_true = np.argmax(y_true, axis=1)
y_score = np.argmax(y_score, axis=1)
#print(type(y_true), y_true.shape, type(y_score), y_score.shape)
try:
TP = np.sum(y_true[y_score==1]==1)*1. #/ sum(y_true)
FP = np.sum(y_true[y_score==1]==0)*1. #/ (y_true.shape[0]-sum(y_true))
prec = TP / (TP+FP+1e-6)
except ValueError:
prec = np.nan
#rvalue = np.array((roc_auc, prec, reca, f1))
#[0][0]
output_storage[0][0] = theano._asarray(prec, dtype=config.floatX)
roc_auc.py 文件源码
项目:deep-mil-for-whole-mammogram-classification
作者: wentaozhu
项目源码
文件源码
阅读 24
收藏 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
y_true = np.argmax(y_true, axis=1)
y_score = np.argmax(y_score, axis=1)
try:
TP = np.sum(y_true[y_score==1]==1)*1. #/ sum(y_true)
FN = np.sum(y_true[y_score==0]==1)*1. #/ sum(y_true)
reca = TP / (TP+FN+1e-6)
except ValueError:
reca = np.nan
#rvalue = np.array((roc_auc, prec, reca, f1))
#[0][0]
output_storage[0][0] = theano._asarray(reca, dtype=config.floatX)
roc_auc.py 文件源码
项目:deep-mil-for-whole-mammogram-classification
作者: wentaozhu
项目源码
文件源码
阅读 40
收藏 0
点赞 0
评论 0
def on_epoch_end(self, epoch, logs={}):
if epoch % self.interval == 0:
y_pred = self.model.predict(self.X_val, verbose=0)
#print(y_pred.shape)
if self.mymil:
y_true = self.y_val.max(axis=1)
y_score = y_pred.max(axis=1)#>0.5
else:
y_true = self.y_val[:,1] #np.argmax(self.y_val, axis=1)
y_score = y_pred[:,1] #np.argmax(y_pred, axis=1)
sortindex = np.argsort(y_score)
y_score = y_score[sortindex]
y_true = y_true[sortindex]
bestacc, bestthresh = np.mean(y_true == np.ones_like(y_true)), y_score[0]-0.001
for thresh in y_score:
acc = np.mean(y_true == (y_score>thresh))
if acc > bestacc:
bestacc, bestthresh = acc, thresh
y_score = y_score>bestthresh
#y_score = y_score >0.5
acc = np.mean(y_true == y_score)
assert(acc == bestacc)
print("interval evaluation - epoch: {:d} - acc: {:.2f}".format(epoch, acc))
if acc > self.acc:
self.acc = acc
for f in os.listdir('./'):
if f.startswith(self.filepath+'acc'):
os.remove(f)
self.model.save(self.filepath+'acc'+str(acc)+'ep'+str(epoch)+'.hdf5')
roc_auc.py 文件源码
项目:deep-mil-for-whole-mammogram-classification
作者: wentaozhu
项目源码
文件源码
阅读 33
收藏 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
y_true = np.argmax(y_true, axis=1)
y_score = np.argmax(y_score, axis=1)
try:
TP = np.sum(y_true[y_score==1]==1)*1. #/ sum(y_true)
FP = np.sum(y_true[y_score==1]==0)*1. #/ (y_true.shape[0]-sum(y_true))
#TN = np.sum(truey[predy==0]==0)*1. / (truey.shape[0]-sum(truey))
FN = np.sum(y_true[y_score==0]==1)*1. #/ sum(y_true)
#prec = TP / (TP+FP+1e-6)
#reca = TP / (TP+FN+1e-6)
#f1 = 2*prec*reca / (prec+reca+1e-6)
f1 = 2*TP / (2*TP +FP +FN)
except ValueError:
f1 = np.nan
#rvalue = np.array((roc_auc, prec, reca, f1))
#[0][0]
output_storage[0][0] = theano._asarray(f1, dtype=config.floatX)
def get_output_for(self, input, **kwargs):
def max_fn(f, mask, prev_score, prev_back, W_sim):
next_score = prev_score.dimshuffle(0, 1, 'x') + f.dimshuffle(0, 'x', 1) + W_sim.dimshuffle('x', 0, 1)
next_back = T.argmax(next_score, axis = 1)
next_score = T.max(next_score, axis = 1)
mask = mask.dimshuffle(0, 'x')
next_score = next_score * mask + prev_score * (1.0 - mask)
next_back = next_back * mask + prev_back * (1.0 - mask)
next_back = T.cast(next_back, 'int32')
return [next_score, next_back]
def produce_fn(back, mask, prev_py):
# back: inst * class, prev_py: inst, mask: inst
next_py = back[T.arange(prev_py.shape[0]), prev_py]
next_py = mask * next_py + (1.0 - mask) * prev_py
next_py = T.cast(next_py, 'int32')
return next_py
f = T.dot(input, self.W)
init_score, init_back = f[:, 0, :], T.zeros_like(f[:, 0, :], dtype = 'int32')
if CRF_INIT:
init_score = init_score + self.W_init[0].dimshuffle('x', 0)
([scores, backs], _) = theano.scan(fn = max_fn, \
sequences = [f.dimshuffle(1, 0, 2)[1: ], self.mask_input.dimshuffle(1, 0)[1: ]], \
outputs_info = [init_score, init_back], non_sequences = [self.W_sim], strict = True)
init_py = T.argmax(scores[-1], axis = 1)
init_py = T.cast(init_py, 'int32')
# init_py: inst, backs: time * inst * class
pys, _ = theano.scan(fn = produce_fn, \
sequences = [backs, self.mask_input.dimshuffle(1, 0)[1:]], outputs_info = [init_py], go_backwards = True)
# pys: (rev_time - 1) * inst
pys = pys.dimshuffle(1, 0)[:, :: -1]
# pys : inst * (time - 1)
return T.concatenate([pys, init_py.dimshuffle(0, 'x')], axis = 1)
def predict(self, test_set_x):
predict_model = theano.function(inputs=[self.x], outputs=self.pred)
predict_proba = predict_model(test_set_x)
predict = numpy.argmax(predict_proba, axis=1)
return predict
def pred_sharedparams(self, mus, sigmas, corxy, pis, prediction_method='mixture'):
"""
Given a mixture of Gaussians infer a mu that maximizes the mixture.
There are two modes:
If prediction_method==mixture then predict one of the mus that maximizes
\mathcal{P}(\boldsymbol{x}) = \sum_{k=1}^{K} \pi_k \mathcal{N}(\boldsymbol{x} \vert \boldsymbol{\mu_k}, \Sigma_k)
If prediction_method==pi return the mu that has the largest pi.
"""
if prediction_method == 'mixture':
X = mus[:, np.newaxis, :]
diff = X - mus
diffprod = np.prod(diff, axis=-1)
sigmainvs = 1.0 / sigmas
sigmainvprods = sigmainvs[:, 0] * sigmainvs[:, 1]
sigmas2 = sigmas ** 2
corxy2 = corxy ** 2
diff2 = diff ** 2
diffsigma = diff2 / sigmas2
diffsigmanorm = np.sum(diffsigma, axis=-1)
z = diffsigmanorm - 2 * corxy * diffprod * sigmainvprods
oneminuscorxy2inv = 1.0 / (1.0 - corxy2)
term = -0.5 * z * oneminuscorxy2inv
expterm = np.exp(term)
probs = (0.5 / np.pi) * sigmainvprods * np.sqrt(oneminuscorxy2inv) * expterm
piprobs = pis[:, np.newaxis, :] * probs
piprobsum = np.sum(piprobs, axis=-1)
preds = np.argmax(piprobsum, axis=1)
selected_mus = mus[preds, :]
return selected_mus
elif prediction_method == 'pi':
logging.info('only pis are used for prediction')
preds = np.argmax(pis, axis=1)
selected_mus = mus[preds, :]
#selected_sigmas = sigmas[np.arange(sigmas.shape[0]), :, preds]
#selected_corxy = corxy[np.arange(corxy.shape[0]),preds]
#selected_pis = pis[np.arange(pis.shape[0]),preds]
return selected_mus
def get_symb_mus(self, mus, sigmas, corxy, pis, prediction_method="pi"):
"""
Can be used to train an autoencoder that given location
trains a mixture density layer and then outputs the same
location
symbolycally predict the mu that maximizes the mixture model
either based on mixture probability of the component
with highest pi, see pred_sharedparams
"""
if prediction_method == "mixture":
"""
sigmainvs = 1.0 / sigmas
sigmainvprods = sigmainvs[:,:, 0] * sigmainvs[:,:, 1]
sigmas2 = sigmas ** 2
corxy2 = corxy **2
diff2 = diff ** 2
diffsigma = diff2 / sigmas2
diffsigmanorm = np.sum(diffsigma, axis=-1)
z = diffsigmanorm - 2 * corxy * diffprod * sigmainvprods
oneminuscorxy2inv = 1.0 / (1.0 - corxy2)
expterm = np.exp(-0.5 * z * oneminuscorxy2inv)
expterm = 1.0
probs = (0.5 / np.pi) * sigmainvprods * T.sqrt(oneminuscorxy2inv) * expterm
probs = pis * probs
"""
logging.fatal("not implemented!")
sys.exit()
elif prediction_method == "pi":
preds = T.argmax(pis, axis=1)
selected_mus = mus[T.arange(mus.shape[0]), preds, :]
return selected_mus
def pred_sharedparams_sym(self, mus, sigmas, corxy, pis, prediction_method='mixture'):
'''
select mus that maximize \sum_{pi_i * prob_i(mu)} if prediction_method is mixture
else
select the component with highest pi if prediction_method is pi.
'''
if prediction_method == 'mixture':
X = mus[:, np.newaxis, :]
diff = X - mus
diffprod = T.prod(diff, axis=-1)
sigmainvs = 1.0 / sigmas
sigmainvprods = sigmainvs[:, 0] * sigmainvs[:, 1]
sigmas2 = sigmas ** 2
corxy2 = corxy **2
diff2 = diff ** 2
diffsigma = diff2 / sigmas2
diffsigmanorm = T.sum(diffsigma, axis=-1)
z = diffsigmanorm - 2 * corxy * diffprod * sigmainvprods
oneminuscorxy2inv = 1.0 / (1.0 - corxy2)
term = -0.5 * z * oneminuscorxy2inv
expterm = T.exp(term)
probs = (0.5 / np.pi) * sigmainvprods * T.sqrt(oneminuscorxy2inv) * expterm
piprobs = pis[:, np.newaxis, :] * probs
piprobsum = T.sum(piprobs, axis=-1)
preds = T.argmax(piprobsum, axis=1)
selected_mus = mus[preds, :]
return selected_mus
elif prediction_method == 'pi':
logging.info('only pis are used for prediction')
preds = T.argmax(pis, axis=1)
selected_mus = mus[preds, :]
return selected_mus
else:
raise('%s is not a valid prediction method' %prediction_method)
def __init__(self, input, n_in, n_out):
""" Initialize the parameters of the logistic regression
:type input: theano.tensor.TensorType
:param input: symbolic variable that describes the input of the
architecture (one minibatch)
:type n_in: int
:param n_in: number of input units, the dimension of the space in
which the datapoints lie
:type n_out: int
:param n_out: number of output units, the dimension of the space in
which the labels lie
"""
# initialize with 0 the weights W as a matrix of shape (n_in, n_out)
self.W = theano.shared(value=numpy.zeros((n_in, n_out),
dtype=theano.config.floatX),
name='W', borrow=True)
# initialize the baises b as a vector of n_out 0s
self.b = theano.shared(value=numpy.zeros((n_out,),
dtype=theano.config.floatX),
name='b', borrow=True)
# compute vector of class-membership probabilities in symbolic form
self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)
# compute prediction as class whose probability is maximal in
# symbolic form
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
# parameters of the model
self.params = [self.W, self.b]
def _error_func(self, y):
return 100 * T.mean(T.neq(T.argmax(y, axis=1), self.k))
def predict(self, x):
return self.compute(x).argmax(axis=1)
def argmax(x, axis=-1):
return T.argmax(x, axis=axis, keepdims=False)
def chooseBestAction(self, state):
""" Get the best action for a belief state
Arguments
---------
state : one belief state
Returns
-------
The best action : int
"""
q_vals = self.qValues(state)
return np.argmax(q_vals),np.max(q_vals)
def categorical_accuracy(probs, y, mask, length_var):
probs_shp = probs.shape
# (n_samples, n_timesteps_f)
predicted = T.argmax(probs, axis=-1)
# (n_samples * n_timesteps_f, n_labels)
probs = probs.reshape([probs_shp[0]*probs_shp[1], probs_shp[2]])
# (n_samples * n_timesteps_f)
y_flat = y.flatten()
acc = lasagne.objectives.categorical_accuracy(probs, y_flat)
# (n_samples, n_timesteps_f)
acc = acc.reshape((probs_shp[0], probs_shp[1]))
acc = acc * mask
acc = T.sum(acc, axis=1) / length_var
return acc, predicted