Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
杨迪
NiuTrans.Tensor
Commits
c442dbeb
Commit
c442dbeb
authored
Sep 16, 2019
by
liyinqiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reconstruction Range function
parent
befa4b18
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
52 行增加
和
34 行删除
+52
-34
source/tensor/XTensor.cpp
+46
-30
source/tensor/XTensor.h
+6
-4
没有找到文件。
source/tensor/XTensor.cpp
查看文件 @
c442dbeb
...
@@ -850,6 +850,42 @@ void XTensor::Rand(int rNum, int cNum)
...
@@ -850,6 +850,42 @@ void XTensor::Rand(int rNum, int cNum)
_SetDataRand
(
this
,
rNum
,
cNum
);
_SetDataRand
(
this
,
rNum
,
cNum
);
}
}
/* generate data items with a range by start, end and the step
>> start - the begin of the array
>> end - the end of the array (not included self)
>> step - the step of two items
*/
void
XTensor
::
Range
(
DTYPE
lower
,
DTYPE
upper
,
DTYPE
step
)
{
CheckNTErrors
((
order
==
1
),
"Tensor must be 1 dimension!"
);
/* compute the true length according to the (start, end, step) */
DTYPE
size
=
fabs
(
upper
-
lower
);
int
num
=
ceil
(
size
/
fabs
(
step
));
CheckNTErrors
((
unitNum
==
num
),
"Unit number of the tensor is not matched."
);
/* init a integer array to store the sequence */
void
*
data
=
NULL
;
if
(
dataType
==
X_INT
)
{
data
=
new
int
[
num
];
for
(
int
i
=
0
;
i
<
num
;
i
++
)
*
((
int
*
)
data
+
i
)
=
lower
+
i
*
step
;
}
else
if
(
dataType
==
X_FLOAT
)
{
data
=
new
float
[
num
];
for
(
int
i
=
0
;
i
<
num
;
i
++
)
*
((
float
*
)
data
+
i
)
=
lower
+
i
*
step
;
}
else
{
ShowNTErrors
(
"TODO!"
);
}
/* set the data from the array */
SetData
(
data
,
num
);
delete
[]
data
;
}
/*
/*
set the tensor items by a uniform distribution in range [lower, upper]
set the tensor items by a uniform distribution in range [lower, upper]
>> lower - lower value of the range
>> lower - lower value of the range
...
@@ -2114,36 +2150,6 @@ void XTensor::FreeData(XTensor * tensor, XMem * myMem, bool useBuf)
...
@@ -2114,36 +2150,6 @@ void XTensor::FreeData(XTensor * tensor, XMem * myMem, bool useBuf)
tensor
->
isInGlobalMem
=
false
;
tensor
->
isInGlobalMem
=
false
;
}
}
/* set the tensor with an data array
>> tensor - XTensor. it must be on CPU
>> start - the begin of the array
>> end - the end of the array (not included self)
>> step - the step of two items
*/
void
XTensor
::
Range
(
XTensor
*
tensor
,
int
start
,
int
end
,
int
step
)
{
if
(
tensor
==
NULL
)
return
;
/* get the length of tensor */
int
length
=
tensor
->
GetDim
(
0
);
/* compute the true length according to the (start, end, step) */
int
a
=
abs
(
end
-
start
);
int
freq
=
ceil
(
1.0
*
a
/
abs
(
step
));
/* init a integer array to store the sequence */
int
*
index
=
new
int
[
freq
];
for
(
int
i
=
0
;
i
<
freq
;
i
++
)
index
[
i
]
=
start
+
i
*
step
;
CheckNTErrors
((
length
==
freq
),
"the length of the tensor is not matched"
);
/* set the data from the array */
tensor
->
SetData
(
index
,
freq
);
delete
[]
index
;
}
/*************************************************
/*************************************************
* we define the "new and delete" functions below
* we define the "new and delete" functions below
*/
*/
...
@@ -2892,6 +2898,16 @@ XTensor * NewTensor5DV2(const int d0, const int d1, const int d2, const int d3,
...
@@ -2892,6 +2898,16 @@ XTensor * NewTensor5DV2(const int d0, const int d1, const int d2, const int d3,
return
NewTensorV2
(
5
,
dims
,
myDataType
,
myDevID
,
isEnableGrad
);
return
NewTensorV2
(
5
,
dims
,
myDataType
,
myDevID
,
isEnableGrad
);
}
}
XTensor
*
NewTensorRange
(
int
lower
,
int
upper
,
int
step
,
const
TENSOR_DATA_TYPE
myDataType
,
const
int
myDevID
,
const
bool
isEnableGrad
)
{
int
size
=
abs
(
upper
-
lower
);
int
unitNum
=
ceil
(
1.0
*
size
/
abs
(
step
));
XTensor
*
tensor
=
NewTensor1DV2
(
unitNum
,
myDataType
,
myDevID
,
isEnableGrad
);
tensor
->
Range
(
lower
,
upper
,
step
);
return
tensor
;
}
/*
/*
generate a copy of XTensor
generate a copy of XTensor
>> a - the tensor we copy from
>> a - the tensor we copy from
...
...
source/tensor/XTensor.h
查看文件 @
c442dbeb
...
@@ -307,6 +307,9 @@ public:
...
@@ -307,6 +307,9 @@ public:
/* generate data items with a uniform distribution in [0, 1] */
/* generate data items with a uniform distribution in [0, 1] */
void
Rand
(
int
rNum
,
int
cNum
);
void
Rand
(
int
rNum
,
int
cNum
);
/* generate data items with a range by start, end and the step */
void
Range
(
DTYPE
lower
,
DTYPE
upper
,
DTYPE
step
);
/* set tensor items by a uniform distribution */
/* set tensor items by a uniform distribution */
void
SetDataRand
(
DTYPE
lower
=
0
.
0
F
,
DTYPE
upper
=
1
.
0
F
);
void
SetDataRand
(
DTYPE
lower
=
0
.
0
F
,
DTYPE
upper
=
1
.
0
F
);
...
@@ -443,10 +446,6 @@ public:
...
@@ -443,10 +446,6 @@ public:
/* free the memory space of the matrix (in the global memory) */
/* free the memory space of the matrix (in the global memory) */
static
static
void
FreeData
(
XTensor
*
matrix
,
XMem
*
myMem
=
NULL
,
bool
useBuf
=
false
);
void
FreeData
(
XTensor
*
matrix
,
XMem
*
myMem
=
NULL
,
bool
useBuf
=
false
);
/* generate the range tensor by start, end and the step */
static
void
Range
(
XTensor
*
tensor
,
int
start
,
int
end
,
int
step
);
};
};
/* we make a unique id for every tensor */
/* we make a unique id for every tensor */
...
@@ -591,6 +590,9 @@ XTensor * NewTensor5DV2(const int d0, const int d1, const int d2, const int d3,
...
@@ -591,6 +590,9 @@ XTensor * NewTensor5DV2(const int d0, const int d1, const int d2, const int d3,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
TENSOR_DATA_TYPE
myDataType
=
X_FLOAT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
/* generate a dense vector by range */
XTensor
*
NewTensorRange
(
int
lower
,
int
upper
,
int
step
,
const
TENSOR_DATA_TYPE
myDataType
=
X_INT
,
const
int
myDevID
=
-
1
,
const
bool
isEnableGrad
=
true
);
/* generate a copy of XTensor (with a reference to a given tensor) */
/* generate a copy of XTensor (with a reference to a given tensor) */
XTensor
*
NewTensor
(
const
XTensor
*
a
,
bool
isFilledData
=
true
);
XTensor
*
NewTensor
(
const
XTensor
*
a
,
bool
isFilledData
=
true
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论