Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F60267541
f1.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Apr 28, 18:57
Size
1 KB
Mime Type
text/x-python
Expires
Tue, Apr 30, 18:57 (2 d)
Engine
blob
Format
Raw Data
Handle
17275727
Attached To
R8206 networkTraining
f1.py
View Options
import
torch
def
reverse
(
t
):
idx
=
[
i
for
i
in
range
(
t
.
size
(
0
)
-
1
,
-
1
,
-
1
)]
idx
=
torch
.
LongTensor
(
idx
)
it
=
t
.
index_select
(
0
,
idx
)
return
it
def
PRFromHistograms
(
hPos
,
hNeg
):
print
(
"hPos"
,
hPos
,
"hNeg"
,
hNeg
)
positives
=
hPos
.
sum
()
negatives
=
hNeg
.
sum
()
print
(
"positives, negatives"
,
positives
,
negatives
)
truepositives
=
reverse
(
hPos
.
clone
())
.
long
()
.
cumsum
(
dim
=
0
)
falsepositives
=
reverse
(
hNeg
.
clone
())
.
long
()
.
cumsum
(
dim
=
0
)
predpositives
=
torch
.
add
(
truepositives
,
falsepositives
)
#protect against zero division
predpositives
[
predpositives
==
0
]
=
1
precision
=
torch
.
Tensor
.
div_
(
truepositives
.
float
(),
predpositives
.
float
())
recall
=
torch
.
Tensor
.
div
(
truepositives
.
float
(),
positives
.
float
())
precision
[
precision
<=
0
]
=
1e-12
recall
[
recall
<=
0
]
=
1e-12
print
(
"precision,recall"
,
precision
,
recall
)
return
precision
,
recall
def
PRFromOutGt
(
outps
,
targs
,
nbins
=
10000
):
hPos
=
torch
.
zeros
(
nbins
)
hNeg
=
torch
.
zeros
(
nbins
)
for
o
,
t
in
zip
(
outps
,
targs
):
pos
=
o
[
lbl
==
1
]
neg
=
t
[
lbl
==
0
]
hPos
+=
torch
.
from_numpy
(
pos
.
astype
(
np
.
float32
))
.
histc
(
nbins
,
0
,
1
)
hNeg
+=
torch
.
from_numpy
(
neg
.
astype
(
np
.
float32
))
.
histc
(
nbins
,
0
,
1
)
precision
,
recall
=
f1
.
PRFromHistograms
(
hPos
,
hNeg
)
f1s
=
f1
.
F1FromPR
(
precision
,
recall
)
f
=
f1s
.
max
()
return
f
def
F1FromPR
(
p
,
r
):
suminv
=
torch
.
pow
(
p
,
-
1
)
+
torch
.
pow
(
r
,
-
1
)
f1s
=
torch
.
pow
(
suminv
,
-
1
)
.
mul
(
2
)
print
(
"f1s"
,
f1s
)
return
f1s
Event Timeline
Log In to Comment