Commit c442dbeb by liyinqiao

Reconstruction Range function

parent befa4b18
......@@ -850,6 +850,42 @@ void XTensor::Rand(int rNum, int 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]
>> lower - lower value of the range
......@@ -2114,36 +2150,6 @@ void XTensor::FreeData(XTensor * tensor, XMem * myMem, bool useBuf)
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
*/
......@@ -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);
}
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
>> a - the tensor we copy from
......
......@@ -307,6 +307,9 @@ public:
/* generate data items with a uniform distribution in [0, 1] */
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 */
void SetDataRand(DTYPE lower = 0.0F, DTYPE upper = 1.0F);
......@@ -443,10 +446,6 @@ public:
/* free the memory space of the matrix (in the global memory) */
static
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 */
......@@ -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 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) */
XTensor * NewTensor(const XTensor * a, bool isFilledData = true);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论