Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
87e10dc8
提交
87e10dc8
authored
10月 02, 2012
作者:
nouiz
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #988 from pascanur/c_softmax
A c version for softmax almost identical to the bias softmax one
上级
0f5f6ea1
c500ea83
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
106 行增加
和
0 行删除
+106
-0
nnet.py
theano/tensor/nnet/nnet.py
+106
-0
没有找到文件。
theano/tensor/nnet/nnet.py
浏览文件 @
87e10dc8
...
@@ -382,6 +382,112 @@ class Softmax(gof.Op):
...
@@ -382,6 +382,112 @@ class Softmax(gof.Op):
def
infer_shape
(
self
,
node
,
shape
):
def
infer_shape
(
self
,
node
,
shape
):
return
shape
return
shape
def
c_headers
(
self
):
return
[
'<iostream>'
,
'<cmath>'
]
@staticmethod
def
c_code_template
():
# this implementation was lifted from
# /u/bergstrj/cvs/bergstrj/src/feb07/nn.cxx
#TODO: put this into a templated function, in the support code
#TODO: declare the max of each row as an Op output
#TODO: set error messages for failures in this code
#TODO: use this to accept float32 and int32: node.inputs[0].type.dtype_specs()[1]
init_decl
=
"""
npy_intp* Nx =
%(x)
s->dimensions;
if (
%(x)
s->nd != 2)
{
PyErr_SetString(PyExc_ValueError, "a not 2d tensor");
%(fail)
s;
}
if ((
%(x)
s->descr->type_num != PyArray_DOUBLE) &&
(
%(x)
s->descr->type_num != PyArray_FLOAT))
{
PyErr_SetString(PyExc_TypeError, "a not float");
%(fail)
s;
}
if ((NULL ==
%(sm)
s)
|| (
%(sm)
s->dimensions[0] !=
%(x)
s->dimensions[0])
|| (
%(sm)
s->dimensions[1] !=
%(x)
s->dimensions[1]))
{
if (NULL !=
%(sm)
s) Py_XDECREF(
%(sm)
s);
%(sm)
s = (PyArrayObject*)PyArray_SimpleNew(2, PyArray_DIMS(
%(x)
s),
type_num_
%(x)
s);
if(!
%(sm)
s) {
PyErr_SetString(PyExc_MemoryError,
"failed to alloc sm output");
%(fail)
s
}
}
"""
begin_row_loop
=
"""
for (size_t i = 0; i < Nx[0]; ++i)
{
size_t j;
double sum = 0.0;
bool discount_max = false;
const dtype_
%(x)
s* __restrict__ x_i = (dtype_
%(x)
s*)(
%(x)
s->data +
%(x)
s->strides[0] * i);
dtype_
%(sm)
s* __restrict__ sm_i = (dtype_
%(sm)
s*)(
%(sm)
s->data +
%(sm)
s->strides[0] * i);
"""
inside_row_loop
=
"""
npy_intp Sx =
%(x)
s->strides[1]/sizeof(dtype_
%(x)
s);
npy_intp Ssm =
%(sm)
s->strides[1]/sizeof(dtype_
%(sm)
s);
size_t row_max_j=0;
dtype_
%(sm)
s row_max = x_i[0];
//std::cout << "0 " << row_max << "
\\
n";
// Get the maximum value of the row
for (j = 1; j < Nx[1]; ++j)
{
dtype_
%(sm)
s row_ij = x_i[j * Sx] ;
//std::cout << "1 " << row_ij << "
\\
n";
row_max_j = (row_ij > row_max) ? j : row_max_j;
row_max = (row_ij > row_max) ? row_ij : row_max;
}
for (j = 0; j < Nx[1]; ++j)
{
dtype_
%(sm)
s row_ij = x_i[j * Sx] ;
//std::cout << "2 " << j << " " << row_ij << " " << row_max << "
\\
n";
dtype_
%(sm)
s sm_ij = exp(row_ij - row_max);
//std::cout << "3 " << j << " " << sm_ij << "
\\
n";
sum += sm_ij;
sm_i[j * Ssm] = sm_ij;
}
//cblas_dscal(x.N, 1.0 / sum, &mat_at(s,i,0), s.n);
double sum_inv = 1.0 / sum;
for (j = 0; j < Nx[1]; ++j)
{
sm_i[j * Ssm] *= sum_inv;
}
"""
end_row_loop
=
"""
}
"""
return
(
init_decl
,
begin_row_loop
,
inside_row_loop
,
end_row_loop
)
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
x
,
=
inp
sm
,
=
out
code_template
=
''
.
join
(
self
.
c_code_template
())
return
code_template
%
dict
(
locals
(),
**
sub
)
@staticmethod
def
c_code_cache_version
():
return
(
1
,)
softmax
=
Softmax
()
softmax
=
Softmax
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论