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)
python类_asarray()的实例源码
roc_auc.py 文件源码
项目:deep-mil-for-whole-mammogram-classification
作者: wentaozhu
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
roc_auc.py 文件源码
项目:deep-mil-for-whole-mammogram-classification
作者: wentaozhu
项目源码
文件源码
阅读 29
收藏 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
项目源码
文件源码
阅读 25
收藏 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 sharedX_value(value, name=None, borrow=None, dtype=None):
"""Share a single value after transforming it to floatX type.
value: a value
name: variable name (str)
borrow: boolean
dtype: the type of the value when shared. default: theano.config.floatX
"""
if dtype is None:
dtype = theano.config.floatX
return theano.shared(
theano._asarray(value, dtype=dtype), name=name, borrow=borrow)
def sharedX_value(value, name=None, borrow=None, dtype=None):
"""Share a single value after transforming it to floatX type.
value: a value
name: variable name (str)
borrow: boolean
dtype: the type of the value when shared. default: theano.config.floatX
"""
if dtype is None:
dtype = theano.config.floatX
return theano.shared(
theano._asarray(value, dtype=dtype), name=name, borrow=borrow)
def sharedX_value(value, name=None, borrow=None, dtype=None):
"""Share a single value after transforming it to floatX type.
value: a value
name: variable name (str)
borrow: boolean
dtype: the type of the value when shared. default: theano.config.floatX
"""
if dtype is None:
dtype = theano.config.floatX
return theano.shared(
theano._asarray(value, dtype=dtype), name=name, borrow=borrow)
basic_layer.py 文件源码
项目:learning-class-invariant-features
作者: sbelharbi
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def sharedX_value(value, name=None, borrow=None, dtype=None):
"""Share a single value after transforming it to floatX type.
value: a value
name: variable name (str)
borrow: boolean
dtype: the type of the value when shared. default: theano.config.floatX
"""
if dtype is None:
dtype = theano.config.floatX
return theano.shared(
theano._asarray(value, dtype=dtype), name=name, borrow=borrow)
def test_may_share_memory_scipy():
a = scipy.sparse.csc_matrix(scipy.sparse.eye(5, 3))
b = scipy.sparse.csc_matrix(scipy.sparse.eye(4, 3))
def as_ar(a):
return theano._asarray(a, dtype='int32')
for a_, b_, rep in [(a, a, True), (b, b, True), (a, b, False),
(a, a.data, True), (a, a.indptr, True),
(a, a.indices, True), (a, as_ar(a.shape), False),
(a.data, a, True), (a.indptr, a, True),
(a.indices, a, True), (as_ar(a.shape), a, False),
(b, b.data, True), (b, b.indptr, True),
(b, b.indices, True), (b, as_ar(b.shape), False),
(b.data, b, True), (b.indptr, b, True),
(b.indices, b, True), (as_ar(b.shape), b, False),
(b.data, a, False), (b.indptr, a, False),
(b.indices, a, False), (as_ar(b.shape), a, False)]:
assert may_share_memory(a_, b_) == rep
assert may_share_memory(b_, a_) == rep
# test that it raise error when needed.
for a_, b_, rep in [(a, (0,), False), (a, 1, False), (a, None, False)]:
assert may_share_memory(a_, b_, False) == rep
assert may_share_memory(b_, a_, False) == rep
try:
may_share_memory(a_, b_)
raise Exception("An error was expected")
except TypeError:
pass
try:
may_share_memory(b_, a_)
raise Exception("An error was expected")
except TypeError:
pass
def test_may_share_memory():
a = scipy.sparse.csc_matrix(scipy.sparse.eye(5, 3))
b = scipy.sparse.csc_matrix(scipy.sparse.eye(4, 3))
as_ar = lambda a: theano._asarray(a, dtype='int32')
for a_, b_, rep in [(a, a, True),
(b, b, True),
(a, b, False),
(a, a.data, True),
(a, a.indptr, True),
(a, a.indices, True),
(a, as_ar(a.shape), False),
(a.data, a, True),
(a.indptr, a, True),
(a.indices, a, True),
(as_ar(a.shape), a, False),
(b, b.data, True),
(b, b.indptr, True),
(b, b.indices, True),
(b, as_ar(b.shape), False),
(b.data, b, True),
(b.indptr, b, True),
(b.indices, b, True),
(as_ar(b.shape), b, False),
(b.data, a, False),
(b.indptr, a, False),
(b.indices, a, False),
(as_ar(b.shape), a, False),
(a.transpose(), a, True),
(b.transpose(), b, True),
(a.transpose(), b, False),
(b.transpose(), a, False),
]:
assert SparseType.may_share_memory(a_, b_) == rep
def perform(self, node, inputs, out):
(csm,) = inputs
out[0][0] = csm.data
if str(csm.data.dtype) == 'int32':
out[0][0] = theano._asarray(out[0][0], dtype='int32')
# backport
out[1][0] = theano._asarray(csm.indices, dtype='int32')
out[2][0] = theano._asarray(csm.indptr, dtype='int32')
out[3][0] = theano._asarray(csm.shape, dtype='int32')
def perform(self, node, inputs, outputs):
(x, ind1, ind2) = inputs
(out,) = outputs
assert _is_sparse(x)
out[0] = theano._asarray(x[ind1, ind2], x.dtype)
def perform(self, node, inputs, outputs):
(x, y) = inputs
(out,) = outputs
assert _is_dense(y)
# The asarray is needed as in some case, this return a
# numpy.matrixlib.defmatrix.matrix object and not an ndarray.
out[0] = theano._asarray(x + y, dtype=node.outputs[0].type.dtype)
def perform(self, node, inputs, out):
x, y = inputs
out = out[0]
x_is_sparse = _is_sparse(x)
y_is_sparse = _is_sparse(y)
if not x_is_sparse and not y_is_sparse:
raise TypeError(x)
rval = x * y
if x_is_sparse and y_is_sparse:
rval = rval.toarray()
out[0] = theano._asarray(rval, dtype=node.outputs[0].dtype)
def perform(self, node, inputs, outputs):
(a_val, a_ind, a_ptr, a_nrows, b) = inputs
(out,) = outputs
a = scipy.sparse.csc_matrix((a_val, a_ind, a_ptr),
(a_nrows, b.shape[0]),
copy=False)
# out[0] = a.dot(b)
out[0] = theano._asarray(a * b, dtype=node.outputs[0].type.dtype)
assert _is_dense(out[0]) # scipy 0.7 automatically converts to dense
def test_add_canonizer_problem0():
n_segments = 10
label = lscalar('label')
segment_labels = label + theano._asarray([0] * n_segments, dtype='int64')
r = segment_labels * 5
f = function([label], r)
def test_gemv_dimensions(self):
A = T.matrix('A')
x, y = T.vectors('x', 'y')
alpha = theano.shared(theano._asarray(1.0, dtype=config.floatX),
name='alpha')
beta = theano.shared(theano._asarray(1.0, dtype=config.floatX),
name='beta')
z = beta * y + alpha * T.dot(A, x)
f = theano.function([A, x, y], z)
# Matrix value
A_val = numpy.ones((5, 3), dtype=config.floatX)
# Different vector length
ones_3 = numpy.ones(3, dtype=config.floatX)
ones_4 = numpy.ones(4, dtype=config.floatX)
ones_5 = numpy.ones(5, dtype=config.floatX)
ones_6 = numpy.ones(6, dtype=config.floatX)
f(A_val, ones_3, ones_5)
f(A_val[::-1, ::-1], ones_3, ones_5)
self.assertRaises(ValueError, f, A_val, ones_4, ones_5)
self.assertRaises(ValueError, f, A_val, ones_3, ones_6)
self.assertRaises(ValueError, f, A_val, ones_4, ones_6)
# The following gemv tests were added in March 2011 by Ian Goodfellow
# and are based on the gemv tests from scipy
# http://projects.scipy.org/scipy/browser/trunk/scipy/linalg/tests/test_fblas.py?rev=6803
# NOTE: At the time these tests were written, theano did not have a
# conjugate function. If such a thing is ever added, the tests involving
# conjugate should be ported over as well.
def _numpy_true_div(x, y):
"""Performs true division, and cast the result in the type we expect.
We define that function so we can use it in TrueDivTester.expected,
because simply calling numpy.true_divide could cause a dtype mismatch.
"""
out = numpy.true_divide(x, y)
# Use floatX as the result of int / int
if x.dtype in tensor.discrete_dtypes and y.dtype in tensor.discrete_dtypes:
out = theano._asarray(out, dtype=config.floatX)
return out
def test_or(self):
for dtype in self.dtype:
x, y = vector(dtype=dtype), vector(dtype=dtype)
fn = inplace_func([x, y], x | y)
l = theano._asarray([0, 0, 1, 1], dtype=dtype)
r = theano._asarray([0, 1, 0, 1], dtype=dtype)
v = fn(l, r)
self.assertTrue(numpy.all(v == (operator.or_(l, r))), (l, r, v))
def test_xor(self):
for dtype in self.dtype:
x, y = vector(dtype=dtype), vector(dtype=dtype)
fn = inplace_func([x, y], x ^ y)
ix = x
ix = inplace.xor_inplace(ix, y)
gn = inplace_func([x, y], ix)
l = theano._asarray([0, 0, 1, 1], dtype=dtype)
r = theano._asarray([0, 1, 0, 1], dtype=dtype)
v = fn(l, r)
self.assertTrue(numpy.all(v == (operator.xor(l, r))), (l, r, v))
v = gn(l, r)
# test the in-place stuff
self.assertTrue(numpy.all(l == numpy.asarray([0, 1, 1, 0])), l)
def test_and(self):
for dtype in self.dtype:
x, y = vector(dtype=dtype), vector(dtype=dtype)
fn = inplace_func([x, y], x & y)
l = theano._asarray([0, 0, 1, 1], dtype=dtype)
r = theano._asarray([0, 1, 0, 1], dtype=dtype)
v = fn(l, r)
self.assertTrue(numpy.all(v == (operator.and_(l, r))), (l, r, v))