Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
flybirding10011
DI-treetensor
提交
82220e54
D
DI-treetensor
项目概览
flybirding10011
/
DI-treetensor
与 Fork 源项目一致
Fork自
OpenDILab开源决策智能平台 / DI-treetensor
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DI-treetensor
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
82220e54
编写于
9月 30, 2021
作者:
HansBug
😆
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dev(hansbug): add operation show
上级
71a65314
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
101 addition
and
0 deletion
+101
-0
docs/source/best_practice/operation/index.rst
docs/source/best_practice/operation/index.rst
+19
-0
docs/source/best_practice/operation/operation.demo.py
docs/source/best_practice/operation/operation.demo.py
+81
-0
docs/source/index.rst
docs/source/index.rst
+1
-0
未找到文件。
docs/source/best_practice/operation/index.rst
0 → 100644
浏览文件 @
82220e54
Customized Operations For Different Fields
============================================
Here is another example of the custom operations implemented \
with both native torch API and treetensor API.
.. literalinclude:: operation.demo.py
:language: python
:linenos:
The output should be like below, and all the assertions can \
be passed.
.. literalinclude:: operation.demo.py.txt
:language: text
:linenos:
The implement with treetensor API is much simpler and clearer.
docs/source/best_practice/operation/operation.demo.py
0 → 100644
浏览文件 @
82220e54
import
copy
import
torch
import
treetensor.torch
as
ttorch
T
,
B
=
3
,
4
def
with_nativetensor
(
batch_
):
mean_b_list
=
[]
even_index_a_list
=
[]
for
i
in
range
(
len
(
batch_
)):
for
k
,
v
in
batch_
[
i
].
items
():
if
k
==
'a'
:
v
=
v
.
float
()
even_index_a_list
.
append
(
v
[::
2
])
elif
k
==
'b'
:
v
=
v
.
float
()
transformed_v
=
torch
.
pow
(
v
,
2
)
+
1.0
mean_b_list
.
append
(
transformed_v
.
mean
())
elif
k
==
'c'
:
for
k1
,
v1
in
v
.
items
():
if
k1
==
'd'
:
v1
=
v1
.
float
()
else
:
print
(
'ignore keys: {}'
.
format
(
k1
))
else
:
print
(
'ignore keys: {}'
.
format
(
k
))
for
i
in
range
(
len
(
batch_
)):
for
k
in
batch_
[
i
].
keys
():
if
k
==
'd'
:
batch_
[
i
][
k
][
'noise'
]
=
torch
.
randn
(
size
=
(
3
,
4
,
5
))
mean_b
=
sum
(
mean_b_list
)
/
len
(
mean_b_list
)
even_index_a
=
torch
.
stack
(
even_index_a_list
,
dim
=
0
)
return
batch_
,
mean_b
,
even_index_a
def
with_treetensor
(
batch_
):
batch_
=
[
ttorch
.
tensor
(
b
)
for
b
in
batch_
]
batch_
=
ttorch
.
stack
(
batch_
)
batch_
=
batch_
.
float
()
batch_
.
b
=
ttorch
.
pow
(
batch_
.
b
,
2
)
+
1.0
batch_
.
c
.
noise
=
ttorch
.
randn
(
size
=
(
B
,
3
,
4
,
5
))
mean_b
=
batch_
.
b
.
mean
()
even_index_a
=
batch_
.
a
[:,
::
2
]
batch_
=
ttorch
.
split
(
batch_
,
split_size_or_sections
=
1
,
dim
=
0
)
return
batch_
,
mean_b
,
even_index_a
def
get_data
():
return
{
'a'
:
torch
.
rand
(
size
=
(
T
,
8
)),
'b'
:
torch
.
rand
(
size
=
(
6
,)),
'c'
:
{
'd'
:
torch
.
randint
(
0
,
10
,
size
=
(
1
,))
}
}
if
__name__
==
"__main__"
:
batch
=
[
get_data
()
for
_
in
range
(
B
)]
batch0
,
mean0
,
even_index_a0
=
with_nativetensor
(
copy
.
deepcopy
(
batch
))
batch1
,
mean1
,
even_index_a1
=
with_treetensor
(
copy
.
deepcopy
(
batch
))
print
(
batch0
)
print
(
'
\n\n
'
)
print
(
batch1
)
assert
torch
.
abs
(
mean0
-
mean1
)
<
1e-6
print
(
'mean0 & mean1:'
,
mean0
,
mean1
)
print
(
'
\n
'
)
assert
torch
.
abs
((
even_index_a0
-
even_index_a1
).
max
())
<
1e-6
print
(
'even_index_a0:'
,
even_index_a0
)
print
(
'even_index_a1:'
,
even_index_a1
)
assert
len
(
batch0
)
==
B
assert
len
(
batch1
)
==
B
assert
isinstance
(
batch1
[
0
],
ttorch
.
Tensor
)
print
(
batch1
[
0
].
shape
)
docs/source/index.rst
浏览文件 @
82220e54
...
...
@@ -20,6 +20,7 @@ module.
:caption: Best Practice
best_practice/stack/index
best_practice/operation/index
.. toctree::
:maxdepth: 2
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录