Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
64726dfb
提交
64726dfb
authored
4月 11, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
3e3e3093
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
25 行增加
和
25 行删除
+25
-25
nnet.py
theano/tensor/nnet/nnet.py
+25
-25
没有找到文件。
theano/tensor/nnet/nnet.py
浏览文件 @
64726dfb
...
@@ -15,7 +15,7 @@ revisited later when all the intermediate part are on the GPU.
...
@@ -15,7 +15,7 @@ revisited later when all the intermediate part are on the GPU.
from
__future__
import
absolute_import
,
print_function
,
division
from
__future__
import
absolute_import
,
print_function
,
division
import
logging
import
logging
import
warnings
import
warnings
import
numpy
import
numpy
as
np
from
six.moves
import
xrange
from
six.moves
import
xrange
import
theano
import
theano
...
@@ -85,7 +85,7 @@ class SoftmaxWithBias(gof.Op):
...
@@ -85,7 +85,7 @@ class SoftmaxWithBias(gof.Op):
if
x
.
size
==
0
:
if
x
.
size
==
0
:
# Numpy doesn't like the max of a zero-sized object.
# Numpy doesn't like the max of a zero-sized object.
output_storage
[
0
][
0
]
=
n
umpy
.
zeros
(
x
.
shape
,
dtype
=
x
.
dtype
)
output_storage
[
0
][
0
]
=
n
p
.
zeros
(
x
.
shape
,
dtype
=
x
.
dtype
)
return
return
x_dtype
=
x
.
dtype
x_dtype
=
x
.
dtype
...
@@ -94,7 +94,7 @@ class SoftmaxWithBias(gof.Op):
...
@@ -94,7 +94,7 @@ class SoftmaxWithBias(gof.Op):
x
=
x
.
astype
(
'float32'
)
x
=
x
.
astype
(
'float32'
)
x_plus_b
=
x
+
b
[
None
,
:]
x_plus_b
=
x
+
b
[
None
,
:]
e_x
=
n
umpy
.
exp
(
x_plus_b
-
x_plus_b
.
max
(
axis
=
1
)[:,
None
])
e_x
=
n
p
.
exp
(
x_plus_b
-
x_plus_b
.
max
(
axis
=
1
)[:,
None
])
e_x
*=
1.0
/
e_x
.
sum
(
axis
=
1
)[:,
None
]
e_x
*=
1.0
/
e_x
.
sum
(
axis
=
1
)[:,
None
]
# default for copy is True and we don't need a copy if the
# default for copy is True and we don't need a copy if the
# data type matches.
# data type matches.
...
@@ -314,7 +314,7 @@ class SoftmaxGrad(gof.Op):
...
@@ -314,7 +314,7 @@ class SoftmaxGrad(gof.Op):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
dy
,
sm
=
input_storage
dy
,
sm
=
input_storage
dx
=
n
umpy
.
zeros_like
(
sm
)
dx
=
n
p
.
zeros_like
(
sm
)
# dx[i,j] = - (\sum_k dy[i,k] sm[i,k]) sm[i,j] + dy[i,j] sm[i,j]
# dx[i,j] = - (\sum_k dy[i,k] sm[i,k]) sm[i,j] + dy[i,j] sm[i,j]
for
i
in
xrange
(
sm
.
shape
[
0
]):
for
i
in
xrange
(
sm
.
shape
[
0
]):
dy_times_sm_i
=
dy
[
i
]
*
sm
[
i
]
dy_times_sm_i
=
dy
[
i
]
*
sm
[
i
]
...
@@ -435,7 +435,7 @@ class Softmax(gof.Op):
...
@@ -435,7 +435,7 @@ class Softmax(gof.Op):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
x
,
=
input_storage
x
,
=
input_storage
e_x
=
n
umpy
.
exp
(
x
-
x
.
max
(
axis
=
1
)[:,
None
])
e_x
=
n
p
.
exp
(
x
-
x
.
max
(
axis
=
1
)[:,
None
])
sm
=
e_x
/
e_x
.
sum
(
axis
=
1
)[:,
None
]
sm
=
e_x
/
e_x
.
sum
(
axis
=
1
)[:,
None
]
output_storage
[
0
][
0
]
=
sm
output_storage
[
0
][
0
]
=
sm
...
@@ -620,7 +620,7 @@ class LogSoftmax(gof.Op):
...
@@ -620,7 +620,7 @@ class LogSoftmax(gof.Op):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
x
,
=
input_storage
x
,
=
input_storage
xdev
=
x
-
x
.
max
(
axis
=
1
)[:,
None
]
xdev
=
x
-
x
.
max
(
axis
=
1
)[:,
None
]
lsm
=
xdev
-
n
umpy
.
log
(
numpy
.
sum
(
numpy
.
exp
(
xdev
),
axis
=
1
,
lsm
=
xdev
-
n
p
.
log
(
np
.
sum
(
np
.
exp
(
xdev
),
axis
=
1
,
keepdims
=
True
))
keepdims
=
True
))
output_storage
[
0
][
0
]
=
lsm
output_storage
[
0
][
0
]
=
lsm
...
@@ -1003,27 +1003,27 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
...
@@ -1003,27 +1003,27 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
raise
ValueError
(
'y_idx must have same number of rows as x'
)
raise
ValueError
(
'y_idx must have same number of rows as x'
)
if
any
(
y_idx
<
0
):
if
any
(
y_idx
<
0
):
raise
ValueError
(
"y_i value out of bounds"
)
raise
ValueError
(
"y_i value out of bounds"
)
sm
=
n
umpy
.
zeros_like
(
x
)
# softmax
sm
=
n
p
.
zeros_like
(
x
)
# softmax
nll
=
n
umpy
.
zeros
(
x
.
shape
[
0
],
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
# nll(y | softmax(x))
nll
=
n
p
.
zeros
(
x
.
shape
[
0
],
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
# nll(y | softmax(x))
am
=
n
umpy
.
zeros_like
(
y_idx
)
am
=
n
p
.
zeros_like
(
y_idx
)
for
i
in
xrange
(
sm
.
shape
[
0
]):
for
i
in
xrange
(
sm
.
shape
[
0
]):
# add the bias vector to the i'th row of x
# add the bias vector to the i'th row of x
row
=
x
[
i
]
+
b
row
=
x
[
i
]
+
b
# get the maximum value of i'th row for numerically safe
# get the maximum value of i'th row for numerically safe
# softmax / nll
# softmax / nll
am
[
i
]
=
n
umpy
.
argmax
(
row
)
am
[
i
]
=
n
p
.
argmax
(
row
)
m
=
row
[
am
[
i
]]
m
=
row
[
am
[
i
]]
# compute the unnormalized softmax, and normalization constant
# compute the unnormalized softmax, and normalization constant
sm
[
i
]
=
n
umpy
.
exp
(
row
-
m
)
sm
[
i
]
=
n
p
.
exp
(
row
-
m
)
sum_j
=
n
umpy
.
sum
(
sm
[
i
])
# sum_j(exp(x[j] - m))
sum_j
=
n
p
.
sum
(
sm
[
i
])
# sum_j(exp(x[j] - m))
# normalized our softmax
# normalized our softmax
sm
[
i
]
*=
1.0
/
sum_j
sm
[
i
]
*=
1.0
/
sum_j
# store the nll
# store the nll
nll
[
i
]
=
-
row
[
y_idx
[
i
]]
+
m
+
n
umpy
.
log
(
sum_j
)
nll
[
i
]
=
-
row
[
y_idx
[
i
]]
+
m
+
n
p
.
log
(
sum_j
)
output_storage
[
0
][
0
]
=
nll
output_storage
[
0
][
0
]
=
nll
output_storage
[
1
][
0
]
=
sm
output_storage
[
1
][
0
]
=
sm
...
@@ -1200,7 +1200,7 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op):
...
@@ -1200,7 +1200,7 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op):
dy
,
sm
,
y_idx
=
input_storage
dy
,
sm
,
y_idx
=
input_storage
if
any
(
y_idx
<
0
):
if
any
(
y_idx
<
0
):
raise
ValueError
(
"y_i value out of bounds"
)
raise
ValueError
(
"y_i value out of bounds"
)
dx
=
n
umpy
.
zeros_like
(
sm
)
dx
=
n
p
.
zeros_like
(
sm
)
if
dy
.
ndim
==
0
:
if
dy
.
ndim
==
0
:
dy
=
dy
[
None
]
dy
=
dy
[
None
]
incr
=
int
(
dy
.
shape
[
0
]
>
1
)
incr
=
int
(
dy
.
shape
[
0
]
>
1
)
...
@@ -1391,7 +1391,7 @@ class CrossentropyCategorical1HotGrad(gof.Op):
...
@@ -1391,7 +1391,7 @@ class CrossentropyCategorical1HotGrad(gof.Op):
def
perform
(
self
,
node
,
inp
,
out
):
def
perform
(
self
,
node
,
inp
,
out
):
g_y
,
coding_dist
,
true_one_of_n
=
inp
g_y
,
coding_dist
,
true_one_of_n
=
inp
g_coding_strg
,
=
out
g_coding_strg
,
=
out
g_coding
=
n
umpy
.
zeros_like
(
coding_dist
)
g_coding
=
n
p
.
zeros_like
(
coding_dist
)
for
i
in
xrange
(
len
(
g_y
)):
for
i
in
xrange
(
len
(
g_y
)):
g_coding
[
i
,
true_one_of_n
[
i
]]
=
(
-
g_y
[
i
]
/
g_coding
[
i
,
true_one_of_n
[
i
]]
=
(
-
g_y
[
i
]
/
coding_dist
[
i
,
true_one_of_n
[
i
]])
coding_dist
[
i
,
true_one_of_n
[
i
]])
...
@@ -1450,9 +1450,9 @@ class CrossentropyCategorical1Hot(gof.Op):
...
@@ -1450,9 +1450,9 @@ class CrossentropyCategorical1Hot(gof.Op):
def
perform
(
self
,
node
,
inp
,
out
):
def
perform
(
self
,
node
,
inp
,
out
):
coding
,
one_of_n
=
inp
coding
,
one_of_n
=
inp
y_out
,
=
out
y_out
,
=
out
y
=
n
umpy
.
zeros_like
(
coding
[:,
0
])
y
=
n
p
.
zeros_like
(
coding
[:,
0
])
for
i
in
xrange
(
len
(
y
)):
for
i
in
xrange
(
len
(
y
)):
y
[
i
]
=
-
n
umpy
.
log
(
coding
[
i
,
one_of_n
[
i
]])
y
[
i
]
=
-
n
p
.
log
(
coding
[
i
,
one_of_n
[
i
]])
y_out
[
0
]
=
y
y_out
[
0
]
=
y
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
...
@@ -1659,9 +1659,9 @@ def _is_const(z, val, approx=False):
...
@@ -1659,9 +1659,9 @@ def _is_const(z, val, approx=False):
except
tensor
.
NotScalarConstantError
:
except
tensor
.
NotScalarConstantError
:
return
False
return
False
if
approx
:
if
approx
:
return
n
umpy
.
allclose
(
maybe
,
val
)
return
n
p
.
allclose
(
maybe
,
val
)
else
:
else
:
return
n
umpy
.
all
(
maybe
==
val
)
return
n
p
.
all
(
maybe
==
val
)
@opt.register_specialize
(
'fast_compile_gpu'
)
@opt.register_specialize
(
'fast_compile_gpu'
)
...
@@ -1792,7 +1792,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1792,7 +1792,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
# set out_grad according to the numerator, it may be divided later
# set out_grad according to the numerator, it may be divided later
# num should be a vector or a scalar
# num should be a vector or a scalar
if
num
.
ndim
==
1
or
n
umpy
.
all
(
num
.
broadcastable
):
if
num
.
ndim
==
1
or
n
p
.
all
(
num
.
broadcastable
):
out_grad
*=
-
num
out_grad
*=
-
num
else
:
else
:
return
return
...
@@ -1818,7 +1818,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1818,7 +1818,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
rest
=
tensor
.
mul
(
*
[
other_inputs
])
rest
=
tensor
.
mul
(
*
[
other_inputs
])
# Check that rest is a vector or a scalar
# Check that rest is a vector or a scalar
if
rest
.
ndim
==
1
or
n
umpy
.
all
(
rest
.
broadcastable
):
if
rest
.
ndim
==
1
or
n
p
.
all
(
rest
.
broadcastable
):
adv_subtensor
=
input
adv_subtensor
=
input
out_grad
/=
rest
out_grad
/=
rest
break
break
...
@@ -2099,14 +2099,14 @@ class Prepend_scalar_constant_to_each_row(gof.Op):
...
@@ -2099,14 +2099,14 @@ class Prepend_scalar_constant_to_each_row(gof.Op):
output
,
=
out
output
,
=
out
new_shape
=
(
mat
.
shape
[
0
],
mat
.
shape
[
1
]
+
1
)
new_shape
=
(
mat
.
shape
[
0
],
mat
.
shape
[
1
]
+
1
)
if
output
[
0
]
is
None
:
if
output
[
0
]
is
None
:
output
[
0
]
=
n
umpy
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
output
[
0
]
=
n
p
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
out
=
output
[
0
]
out
=
output
[
0
]
else
:
else
:
if
output
[
0
]
.
shape
!=
new_shape
:
if
output
[
0
]
.
shape
!=
new_shape
:
try
:
try
:
output
[
0
]
.
resize
(
new_shape
)
output
[
0
]
.
resize
(
new_shape
)
except
Exception
:
except
Exception
:
output
[
0
]
=
n
umpy
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
output
[
0
]
=
n
p
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
out
=
output
[
0
]
out
=
output
[
0
]
out
[:,
0
]
.
fill
(
self
.
val
.
data
)
out
[:,
0
]
.
fill
(
self
.
val
.
data
)
...
@@ -2147,14 +2147,14 @@ class Prepend_scalar_to_each_row(gof.Op):
...
@@ -2147,14 +2147,14 @@ class Prepend_scalar_to_each_row(gof.Op):
output
,
=
out
output
,
=
out
new_shape
=
(
mat
.
shape
[
0
],
mat
.
shape
[
1
]
+
1
)
new_shape
=
(
mat
.
shape
[
0
],
mat
.
shape
[
1
]
+
1
)
if
output
[
0
]
is
None
:
if
output
[
0
]
is
None
:
output
[
0
]
=
n
umpy
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
output
[
0
]
=
n
p
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
out
=
output
[
0
]
out
=
output
[
0
]
else
:
else
:
if
output
[
0
]
.
shape
!=
new_shape
:
if
output
[
0
]
.
shape
!=
new_shape
:
try
:
try
:
output
[
0
]
.
resize
(
new_shape
)
output
[
0
]
.
resize
(
new_shape
)
except
Exception
:
except
Exception
:
output
[
0
]
=
n
umpy
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
output
[
0
]
=
n
p
.
empty
(
new_shape
,
dtype
=
mat
.
dtype
)
out
=
output
[
0
]
out
=
output
[
0
]
out
[:,
0
]
.
fill
(
val
)
out
[:,
0
]
.
fill
(
val
)
out
[:,
1
:]
=
mat
out
[:,
1
:]
=
mat
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论