开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 471

MatrixHan/Paddle

forked from PaddlePaddle/Paddle
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
develop
分支 (302)
标签 (66)
develop
fix-numpy-issue
release/2.3
release/1.8
revert-43882-revert-41944-smaller_inference_api_test
revert-41944-smaller_inference_api_test
release/2.3-fc-ernie-fix
revert-43155-fix_ut_tempfile
ZHUI-patch-1
fix_tensor_type
dingjiaweiww-patch-1
revert-41065-revert-40993-mv_ele_floordiv_pow
revert-41068-revert-40790-phi_new
prv-onednn-2.5
fix_rnn_docs
add_some_yaml_config
move_slice_to_pten
enable_eager_model_test
move_yolo_box_to_phi
move_sgd_to_phi
v2.3.2
v2.3.1
v2.3.0
v2.3.0-rc0
v2.2.2
v2.2.1
v2.2.0
v2.2.0-bak0
v2.2.0-rc0
v2.1.3
v2.1.2
v2.1.1
v2.1.0
v2.1.0-rc0
v2.0.2
v2.0.1
v2.0.0
v2.0.0-rc1
v2.0.0-rc0
v1.8.5
develop
分支 (302)
标签 (66)
develop
fix-numpy-issue
release/2.3
release/1.8
revert-43882-revert-41944-smaller_inference_api_test
revert-41944-smaller_inference_api_test
release/2.3-fc-ernie-fix
revert-43155-fix_ut_tempfile
ZHUI-patch-1
fix_tensor_type
dingjiaweiww-patch-1
revert-41065-revert-40993-mv_ele_floordiv_pow
revert-41068-revert-40790-phi_new
prv-onednn-2.5
fix_rnn_docs
add_some_yaml_config
move_slice_to_pten
enable_eager_model_test
move_yolo_box_to_phi
move_sgd_to_phi
v2.3.2
v2.3.1
v2.3.0
v2.3.0-rc0
v2.2.2
v2.2.1
v2.2.0
v2.2.0-bak0
v2.2.0-rc0
v2.1.3
v2.1.2
v2.1.1
v2.1.0
v2.1.0-rc0
v2.0.2
v2.0.1
v2.0.0
v2.0.0-rc1
v2.0.0-rc0
v1.8.5
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
develop
分支 (302)
标签 (66)
develop
fix-numpy-issue
release/2.3
release/1.8
revert-43882-revert-41944-smaller_inference_api_test
revert-41944-smaller_inference_api_test
release/2.3-fc-ernie-fix
revert-43155-fix_ut_tempfile
ZHUI-patch-1
fix_tensor_type
dingjiaweiww-patch-1
revert-41065-revert-40993-mv_ele_floordiv_pow
revert-41068-revert-40790-phi_new
prv-onednn-2.5
fix_rnn_docs
add_some_yaml_config
move_slice_to_pten
enable_eager_model_test
move_yolo_box_to_phi
move_sgd_to_phi
v2.3.2
v2.3.1
v2.3.0
v2.3.0-rc0
v2.2.2
v2.2.1
v2.2.0
v2.2.0-bak0
v2.2.0-rc0
v2.1.3
v2.1.2
v2.1.1
v2.1.0
v2.1.0-rc0
v2.0.2
v2.0.1
v2.0.0
v2.0.0-rc1
v2.0.0-rc0
v1.8.5
array.py 9.62 KB
一键复制 编辑 原始数据 按行查看 历史
Sing_chan 提交于 2022年06月05日 10:58 +08:00 . 【code format check upgrade】 step2:yapf (#42944)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Define functions about array.
import paddle
from ..static import Variable
from ..framework import LayerHelper, core, _non_static_mode
from ..fluid.data_feeder import check_type
from ..fluid.data_feeder import check_variable_and_dtype
__all__ = []
def array_length(array):
"""
This OP is used to get the length of the input array.
Args:
array (list|Tensor): The input array that will be used to compute the length. In dynamic mode, ``array`` is a Python list. But in static mode, array is a Tensor whose VarType is LOD_TENSOR_ARRAY.
Returns:
Tensor: 1-D Tensor with shape [1], which is the length of array.
Examples:
.. code-block:: python
import paddle
arr = paddle.tensor.create_array(dtype='float32')
x = paddle.full(shape=[3, 3], fill_value=5, dtype="float32")
i = paddle.zeros(shape=[1], dtype="int32")
arr = paddle.tensor.array_write(x, i, array=arr)
arr_len = paddle.tensor.array_length(arr)
print(arr_len) # 1
"""
if _non_static_mode():
assert isinstance(
array,
list), "The 'array' in array_write must be a list in dygraph mode"
return len(array)
if not isinstance(
array,
Variable) or array.type != core.VarDesc.VarType.LOD_TENSOR_ARRAY:
raise TypeError(
"array should be tensor array vairable in array_length Op")
helper = LayerHelper('array_length', **locals())
tmp = helper.create_variable_for_type_inference(dtype='int64')
tmp.stop_gradient = True
helper.append_op(type='lod_array_length',
inputs={'X': [array]},
outputs={'Out': [tmp]})
return tmp
def array_read(array, i):
"""
This OP is used to read data at the specified position from the input array.
Case:
.. code-block:: text
Input:
The shape of first three tensors are [1], and that of the last one is [1,2]:
array = ([0.6], [0.1], [0.3], [0.4, 0.2])
And:
i = [3]
Output:
output = [0.4, 0.2]
Args:
array (list|Tensor): The input array. In dynamic mode, ``array`` is a Python list. But in static mode, array is a Tensor whose ``VarType`` is ``LOD_TENSOR_ARRAY``.
i (Tensor): 1-D Tensor, whose shape is [1] and dtype is int64. It represents the
specified read position of ``array``.
Returns:
Tensor: A Tensor that is read at the specified position of ``array``.
Examples:
.. code-block:: python
import paddle
arr = paddle.tensor.create_array(dtype="float32")
x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32")
i = paddle.zeros(shape=[1], dtype="int32")
arr = paddle.tensor.array_write(x, i, array=arr)
item = paddle.tensor.array_read(arr, i)
print(item) # [[5., 5., 5.]]
"""
if _non_static_mode():
assert isinstance(
array,
list), "The 'array' in array_read must be list in dygraph mode"
assert isinstance(
i, Variable
), "The index 'i' in array_read must be Variable in dygraph mode"
assert i.shape == [
1
], "The shape of index 'i' should be [1] in dygraph mode"
i = i.numpy().item(0)
return array[i]
check_variable_and_dtype(i, 'i', ['int64'], 'array_read')
helper = LayerHelper('array_read', **locals())
if not isinstance(
array,
Variable) or array.type != core.VarDesc.VarType.LOD_TENSOR_ARRAY:
raise TypeError("array should be tensor array vairable")
out = helper.create_variable_for_type_inference(dtype=array.dtype)
helper.append_op(type='read_from_array',
inputs={
'X': [array],
'I': [i]
},
outputs={'Out': [out]})
return out
def array_write(x, i, array=None):
"""
This OP writes the input ``x`` into the i-th position of the ``array`` returns the modified array.
If ``array`` is none, a new array will be created and returned.
Args:
x (Tensor): The input data to be written into array. It's multi-dimensional
Tensor or LoDTensor. Data type: float32, float64, int32, int64 and bool.
i (Tensor): 1-D Tensor with shape [1], which represents the position into which
``x`` is written.
array (list|Tensor, optional): The array into which ``x`` is written. The default value is None,
when a new array will be created and returned as a result. In dynamic mode, ``array`` is a Python list.
But in static mode, array is a Tensor whose ``VarType`` is ``LOD_TENSOR_ARRAY``.
Returns:
list|Tensor: The input ``array`` after ``x`` is written into.
Examples:
.. code-block:: python
import paddle
arr = paddle.tensor.create_array(dtype="float32")
x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32")
i = paddle.zeros(shape=[1], dtype="int32")
arr = paddle.tensor.array_write(x, i, array=arr)
item = paddle.tensor.array_read(arr, i)
print(item) # [[5., 5., 5.]]
"""
if _non_static_mode():
assert isinstance(
x, Variable
), "The input data 'x' in array_write must be Variable in dygraph mode"
assert isinstance(
i, Variable
), "The index 'i' in array_write must be Variable in dygraph mode"
assert i.shape == [
1
], "The shape of index 'i' should be [1] in dygraph mode"
i = i.numpy().item(0)
if array is None:
array = create_array(x.dtype)
assert isinstance(
array,
list), "The 'array' in array_write must be a list in dygraph mode"
assert i <= len(
array
), "The index 'i' should not be greater than the length of 'array' in dygraph mode"
if i < len(array):
array[i] = x
else:
array.append(x)
return array
check_variable_and_dtype(i, 'i', ['int64'], 'array_write')
check_type(x, 'x', (Variable), 'array_write')
helper = LayerHelper('array_write', **locals())
if array is not None:
if not isinstance(
array, Variable
) or array.type != core.VarDesc.VarType.LOD_TENSOR_ARRAY:
raise TypeError(
"array should be tensor array vairable in array_write Op")
if array is None:
array = helper.create_variable(
name="{0}.out".format(helper.name),
type=core.VarDesc.VarType.LOD_TENSOR_ARRAY,
dtype=x.dtype)
helper.append_op(type='write_to_array',
inputs={
'X': [x],
'I': [i]
},
outputs={'Out': [array]})
return array
def create_array(dtype, initialized_list=None):
"""
This OP creates an array. It is used as the input of :ref:`api_paddle_tensor_array_array_read` and
:ref:`api_paddle_tensor_array_array_write`.
Args:
dtype (str): The data type of the elements in the array. Support data type: float32, float64, int32, int64 and bool.
initialized_list(list): Used to initialize as default value for created array.
All values in initialized list should be a Tensor.
Returns:
list|Tensor: An empty array. In dynamic mode, ``array`` is a Python list. But in static mode, array is a Tensor
whose ``VarType`` is ``LOD_TENSOR_ARRAY``.
Examples:
.. code-block:: python
import paddle
arr = paddle.tensor.create_array(dtype="float32")
x = paddle.full(shape=[1, 3], fill_value=5, dtype="float32")
i = paddle.zeros(shape=[1], dtype="int32")
arr = paddle.tensor.array_write(x, i, array=arr)
item = paddle.tensor.array_read(arr, i)
print(item) # [[5., 5., 5.]]
"""
array = []
if initialized_list is not None:
if not isinstance(initialized_list, (list, tuple)):
raise TypeError(
"Require type(initialized_list) should be list/tuple, but received {}"
.format(type(initialized_list)))
array = list(initialized_list)
# NOTE: Only support plain list like [x, y,...], not support nested list in static mode.
for val in array:
if not isinstance(val, Variable):
raise TypeError(
"All values in `initialized_list` should be Variable, but recevied {}."
.format(type(val)))
if _non_static_mode():
return array
helper = LayerHelper("array", **locals())
tensor_array = helper.create_variable(
name="{0}.out".format(helper.name),
type=core.VarDesc.VarType.LOD_TENSOR_ARRAY,
dtype=dtype)
for val in array:
array_write(x=val, i=array_length(tensor_array), array=tensor_array)
return tensor_array
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

PaddlePaddle (PArallel Distributed Deep LEarning 并行分布式深度学习)是百度研发的深度学习平台,具有易用,高效,灵活和可伸缩等特点,为百度内部多项产品提供深度学习算法支持
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/git.matrix.net/Paddle.git
git@gitee.com:git.matrix.net/Paddle.git
git.matrix.net
Paddle
Paddle
develop
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /