PaddlePaddle
- abs
- acos
- add
- add_n
- addmm
- all
- allclose
- any
- arange
- argmax
- argmin
- argsort
- asin
- assign
- atan
- bernoulli
- bmm
- broadcast_to
- cast
- ceil
- cholesky
- chunk
- clip
- concat
- conj
- cos
- cosh
- CPUPlace
- cross
- CUDAPinnedPlace
- CUDAPlace
- cumsum
- DataParallel
- diag
- disable_static
- dist
- divide
- dot
- empty
- empty_like
- enable_static
- equal
- equal_all
- erf
- exp
- expand
- expand_as
- eye
- flatten
- flip
- floor
- floor_divide
- flops
- full
- full_like
- gather
- gather_nd
- get_cuda_rng_state
- get_cudnn_version
- get_default_dtype
- get_device
- grad
- greater_equal
- greater_than
- histogram
- imag
- in_dynamic_mode
- increment
- index_sample
- index_select
- inverse
- is_compiled_with_cuda
- is_compiled_with_xpu
- is_empty
- is_tensor
- isfinite
- isinf
- isnan
- kron
- less_equal
- less_than
- linspace
- load
- log
- log10
- log1p
- log2
- logical_and
- logical_not
- logical_or
- logical_xor
- logsumexp
- masked_select
- matmul
- max
- maximum
- mean
- median
- meshgrid
- min
- minimum
- mm
- mod
- Model
- multinomial
- multiplex
- multiply
- mv
- no_grad
- nonzero
- norm
- normal
- not_equal
- numel
- ones
- ones_like
- ParamAttr
- pow
- prod
- rand
- randint
- randn
- randperm
- rank
- real
- reciprocal
- reshape
- reshape_
- roll
- round
- rsqrt
- save
- scale
- scatter
- scatter_
- scatter_nd
- scatter_nd_add
- seed
- set_cuda_rng_state
- set_default_dtype
- set_device
- shape
- shard_index
- sign
- sin
- sinh
- slice
- sort
- split
- sqrt
- square
- squeeze
- squeeze_
- stack
- stanh
- std
- strided_slice
- subtract
- sum
- summary
- t
- tan
- tanh
- tanh_
- Tensor
- tile
- to_tensor
- topk
- trace
- transpose
- tril
- triu
- unbind
- uniform
- unique
- unsqueeze
- unsqueeze_
- unstack
- var
- where
- XPUPlace
- zeros
- zeros_like
- create_lod_tensor
- create_random_int_lodtensor
- cuda_pinned_places
- data
- DataFeedDesc
- DataFeeder
- device_guard
- DistributeTranspiler
- DistributeTranspilerConfig
- get_flags
-
- adaptive_pool2d
- adaptive_pool3d
- add_position_encoding
- affine_channel
- affine_grid
- anchor_generator
- argmax
- argmin
- argsort
- array_length
- array_read
- array_write
- assign
- autoincreased_step_counter
- BasicDecoder
- beam_search
- beam_search_decode
- bipartite_match
- box_clip
- box_coder
- box_decoder_and_assign
- bpr_loss
- brelu
- Categorical
- center_loss
- clip
- clip_by_norm
- collect_fpn_proposals
- concat
- cond
- continuous_value_model
- cosine_decay
- create_array
- create_py_reader_by_data
- create_tensor
- crop
- crop_tensor
- cross_entropy
- ctc_greedy_decoder
- cumsum
- data
- DecodeHelper
- Decoder
- deformable_conv
- deformable_roi_pooling
- density_prior_box
- detection_output
- diag
- distribute_fpn_proposals
- double_buffer
- dropout
- dynamic_gru
- dynamic_lstm
- dynamic_lstmp
- DynamicRNN
- edit_distance
- elementwise_add
- elementwise_div
- elementwise_floordiv
- elementwise_max
- elementwise_min
- elementwise_mod
- elementwise_pow
- elementwise_sub
- elu
- embedding
- equal
- expand
- expand_as
- exponential_decay
- eye
- fc
- fill_constant
- filter_by_instag
- flatten
- fsp_matrix
- gather
- gather_nd
- gaussian_random
- gelu
- generate_mask_labels
- generate_proposal_labels
- generate_proposals
- get_tensor_from_selected_rows
- greater_equal
- greater_than
- GreedyEmbeddingHelper
- grid_sampler
- gru_unit
- GRUCell
- hard_shrink
- hard_sigmoid
- hard_swish
- has_inf
- has_nan
- hash
- hsigmoid
- huber_loss
- IfElse
- im2sequence
- image_resize
- image_resize_short
- increment
- inplace_abn
- inverse_time_decay
- iou_similarity
- isfinite
- kldiv_loss
- l2_normalize
- label_smooth
- leaky_relu
- less_equal
- less_than
- linear_chain_crf
- linear_lr_warmup
- locality_aware_nms
- lod_append
- lod_reset
- logsigmoid
- lrn
- lstm
- lstm_unit
- LSTMCell
- margin_rank_loss
- matmul
- matrix_nms
- maxout
- mean
- merge_selected_rows
- mse_loss
- mul
- multiclass_nms
- MultivariateNormalDiag
- natural_exp_decay
- noam_decay
- Normal
- not_equal
- one_hot
- ones
- ones_like
- pad
- pad2d
- pad_constant_like
- piecewise_decay
- pixel_shuffle
- polygon_box_transform
- polynomial_decay
- pool2d
- pool3d
- pow
- prior_box
- prroi_pool
- psroi_pool
- py_reader
- random_crop
- range
- rank_loss
- read_file
- reduce_all
- reduce_any
- reduce_max
- reduce_mean
- reduce_min
- reduce_prod
- reduce_sum
- relu
- relu6
- reorder_lod_tensor_by_rank
- reshape
- resize_bilinear
- resize_nearest
- resize_trilinear
- retinanet_detection_output
- retinanet_target_assign
- reverse
- rnn
- RNNCell
- roi_align
- roi_perspective_transform
- roi_pool
- rpn_target_assign
- sampled_softmax_with_cross_entropy
- SampleEmbeddingHelper
- sampling_id
- scatter
- selu
- sequence_concat
- sequence_conv
- sequence_enumerate
- sequence_expand
- sequence_expand_as
- sequence_first_step
- sequence_last_step
- sequence_mask
- sequence_pad
- sequence_pool
- sequence_reshape
- sequence_reverse
- sequence_scatter
- sequence_slice
- sequence_softmax
- sequence_unpad
- shuffle_channel
- sigmoid_cross_entropy_with_logits
- sigmoid_focal_loss
- sign
- similarity_focus
- size
- smooth_l1
- soft_relu
- softmax
- softplus
- softshrink
- softsign
- space_to_depth
- split
- squeeze
- ssd_loss
- stack
- StaticRNN
- strided_slice
- sum
- sums
- swish
- Switch
- tanh
- tanh_shrink
- target_assign
- teacher_student_sigmoid_loss
- tensor_array_to_tensor
- thresholded_relu
- topk
- TrainingHelper
- unbind
- Uniform
- uniform_random
- unique
- unique_with_counts
- unsqueeze
- warpctc
- where
- While
- while_loop
- yolo_box
- yolov3_loss
- zeros
- zeros_like
- load_op_library
- LoDTensor
- LoDTensorArray
- memory_optimize
- one_hot
- release_memory
- require_version
- set_flags
- Tensor
- Overview
- AdaptiveAvgPool1D
- AdaptiveAvgPool2D
- AdaptiveAvgPool3D
- AdaptiveMaxPool1D
- AdaptiveMaxPool2D
- AdaptiveMaxPool3D
- AlphaDropout
- AvgPool1D
- AvgPool2D
- AvgPool3D
- BatchNorm
- BatchNorm1D
- BatchNorm2D
- BatchNorm3D
- BCELoss
- BCEWithLogitsLoss
- BeamSearchDecoder
- Bilinear
- BiRNN
- ClipGradByGlobalNorm
- ClipGradByNorm
- ClipGradByValue
- Conv1D
- Conv1DTranspose
- Conv2D
- Conv2DTranspose
- Conv3D
- Conv3DTranspose
- CosineSimilarity
- CrossEntropyLoss
- CTCLoss
- Dropout
- Dropout2D
- Dropout3D
- dynamic_decode
- ELU
- Embedding
- Flatten
-
- adaptive_avg_pool1d
- adaptive_avg_pool2d
- adaptive_avg_pool3d
- adaptive_max_pool1d
- adaptive_max_pool2d
- adaptive_max_pool3d
- affine_grid
- alpha_dropout
- avg_pool1d
- avg_pool2d
- avg_pool3d
- batch_norm
- bilinear
- binary_cross_entropy
- binary_cross_entropy_with_logits
- conv1d
- conv1d_transpose
- conv2d
- conv2d_transpose
- conv3d
- conv3d_transpose
- cosine_similarity
- cross_entropy
- ctc_loss
- diag_embed
- dice_loss
- dropout
- dropout2d
- dropout3d
- elu
- elu_
- embedding
- gather_tree
- gelu
- grid_sample
- hardshrink
- hardsigmoid
- hardswish
- hardtanh
- hsigmoid_loss
- instance_norm
- interpolate
- kl_div
- l1_loss
- label_smooth
- layer_norm
- leaky_relu
- linear
- local_response_norm
- log_loss
- log_sigmoid
- log_softmax
- margin_ranking_loss
- max_pool1d
- max_pool2d
- max_pool3d
- maxout
- mse_loss
- nll_loss
- normalize
- npair_loss
- one_hot
- pad
- pixel_shuffle
- prelu
- relu
- relu6
- relu_
- selu
- sigmoid
- sigmoid_focal_loss
- smooth_l1_loss
- softmax
- softmax_
- softmax_with_cross_entropy
- softplus
- softshrink
- softsign
- square_error_cost
- swish
- tanhshrink
- temporal_shift
- thresholded_relu
- unfold
- upsample
- GELU
- GroupNorm
- GRU
- GRUCell
- Hardshrink
- Hardsigmoid
- Hardswish
- Hardtanh
- HSigmoidLoss
- InstanceNorm1D
- InstanceNorm2D
- InstanceNorm3D
- KLDivLoss
- L1Loss
- Layer
- LayerList
- LayerNorm
- LeakyReLU
- Linear
- LocalResponseNorm
- LogSigmoid
- LogSoftmax
- LSTM
- LSTMCell
- MarginRankingLoss
- Maxout
- MaxPool1D
- MaxPool2D
- MaxPool3D
- MSELoss
- MultiHeadAttention
- NLLLoss
- Pad1D
- Pad2D
- Pad3D
- PairwiseDistance
- ParameterList
- PixelShuffle
- PReLU
- ReLU
- ReLU6
- RNN
- RNNCellBase
- SELU
- Sequential
- Sigmoid
- SimpleRNN
- SimpleRNNCell
- SmoothL1Loss
- Softmax
- Softplus
- Softshrink
- Softsign
- SpectralNorm
- Swish
- SyncBatchNorm
- Tanh
- Tanhshrink
- ThresholdedReLU
- Transformer
- TransformerDecoder
- TransformerDecoderLayer
- TransformerEncoder
- TransformerEncoderLayer
- Upsample
- UpsamplingBilinear2D
- UpsamplingNearest2D
- append_backward
- BuildStrategy
- CompiledProgram
- cpu_places
- create_global_var
- create_parameter
- cuda_places
- data
- default_main_program
- default_startup_program
- deserialize_persistables
- deserialize_program
- device_guard
- ExecutionStrategy
- Executor
- global_scope
- gradients
- InputSpec
- load
- load_from_file
- load_inference_model
- load_program_state
- name_scope
- ParallelExecutor
- Program
- program_guard
- py_func
- save
- save_inference_model
- save_to_file
- scope_guard
- serialize_persistables
- serialize_program
- set_program_state
- Variable
- WeightNormParamAttr
-
- adjust_brightness
- adjust_contrast
- adjust_hue
- adjust_saturation
- BaseTransform
- BrightnessTransform
- center_crop
- CenterCrop
- ColorJitter
- Compose
- ContrastTransform
- crop
- Grayscale
- hflip
- HueTransform
- Normalize
- normalize
- Pad
- pad
- RandomCrop
- RandomHorizontalFli
- RandomResizedCrop
- RandomRotation
- RandomVerticalFlip
- Resize
- resize
- rotate
- SaturationTransform
- to_grayscale
- to_tensor
- ToTensor
- Transpose
- vflip
paddle.static / Variable
Variable¶
- 注意:
-
1. 请不要直接调用 Variable 的构造函数,因为这会造成严重的错误发生!
2. 在静态图形模式下:请使用 Block.create_var 创建一个静态的 Variable ,该静态的 Variable 在使用 Executor 执行前是没有实际数据的。
3. 在 Dygraph 模式下:请使用 cn_api_fluid_dygraph_to_variable 创建一个拥有实际数据的 Variable
在Fluid中,OP的每个输入和输出都是 Variable 。多数情况下, Variable 用于保存不同种类的数据或训练标签。
Variable 总是属于某一个 Block 。所有 Variable 都有其自己的 name
,不同 Block 中的两个 Variable 可以具有相同的名称。如果使用的 不是 Dygraph 模式,那么同一个 Block 中的两个或更多 Variable 拥有相同 name
将意味着他们会共享相同的内容。通常我们使用这种方式来实现 参数共享
Variable 有很多种。它们每种都有自己的属性和用法。请参考 framework.proto 以获得详细信息。 Variable 的大多数成员变量可以设置为 None
。它的意思是它不可用或稍后指定。
如果您希望创建一个 Variable 那么可以参考如下示例:
示例代码:
- 在静态图形模式下:
-
import paddle.fluid as fluid cur_program = fluid.Program() cur_block = cur_program.current_block() new_variable = cur_block.create_var(name="X", shape=[-1, 23, 48], dtype='float32')
- 在 Dygraph 模式下:
-
import paddle.fluid as fluid import numpy as np with fluid.dygraph.guard(): new_variable = fluid.dygraph.to_variable(np.arange(10))
-
detach
( ) ¶
注意:
产生一个新的,和当前计算图分离的,但是拥有当前 Variable 其内容的临时变量
返回:一个新的,和当前计算图分离的,但是拥有当前 Variable 其内容的临时 Variable
返回类型:(Variable | 和输入的 Dtype
一致)
- 示例代码
-
import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable from paddle.fluid.dygraph import Linear import numpy as np data = np.random.uniform(-1, 1, [30, 10, 32]).astype('float32') with fluid.dygraph.guard(): linear = Linear(32, 64) data = to_variable(data) x = linear(data) y = x.detach()
-
numpy
( ) ¶
注意:
1. 该API只在 Dygraph 模式下生效
返回一个 ndarray
来表示当前 Variable 的值
返回:numpy
的数组,表示当前 Variable 的实际值
返回类型:ndarray,dtype
和输入的 dtype
一致
- 示例代码
-
import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable from paddle.fluid.dygraph import Linear import numpy as np data = np.random.uniform(-1, 1, [30, 10, 32]).astype('float32') with fluid.dygraph.guard(): linear = Linear(32, 64) data = to_variable(data) x = linear(data) print(x.numpy())
-
set_value
( ) ¶
注意:
1. 该API只在 Dygraph 模式下生效
为此 Variable 设置一个新的值。
参数:
返回:无
抛出异常: ValueError
- 当要赋于的新值的 shape
和此 Variable 原有的 shape
不同时,抛出 ValueError
。
- 示例代码
-
import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable from paddle.fluid.dygraph import Linear import numpy as np data = np.ones([3, 1024], dtype='float32') with fluid.dygraph.guard(): linear = fluid.dygraph.Linear(1024, 4) t = to_variable(data) linear(t) # 使用默认参数值调用前向 custom_weight = np.random.randn(1024, 4).astype("float32") linear.weight.set_value(custom_weight) # 将参数修改为自定义的值 out = linear(t) # 使用新的参数值调用前向
-
backward
( ) ¶
注意:
从该节点开始执行反向
参数:
retain_graph (bool,可选) – 该参数用于确定反向梯度更新完成后反向梯度计算图是否需要保留(retain_graph为True则保留反向梯度计算图)。若用户打算在执行完该方法(
backward
)后,继续向之前已构建的计算图中添加更多的Op,则需要设置retain_graph
值为True(这样才会保留之前计算得到的梯度)。可以看出,将retain_graph
设置为False可降低内存的占用。默认值为False。
返回:无
- 示例代码
-
import numpy as np import paddle paddle.disable_static() x = np.ones([2, 2], np.float32) inputs = [] for _ in range(10): tmp = paddle.to_tensor(x) # 如果这里我们不为输入tmp设置stop_gradient=False,那么后面loss也将因为这个链路都不需要梯度 # 而不产生梯度 tmp.stop_gradient=False inputs.append(tmp) ret = paddle.sums(inputs) loss = paddle.reduce_sum(ret) loss.backward()
-
gradient
( ) ¶
注意:
获取该 Variable 的梯度值
返回:如果 Variable 的类型是LoDTensor(参见 cn_user_guide_lod_tensor ),返回该 Variable 类型为 ndarray
的梯度值;如果 Variable 的类型是SelectedRows,返回该 Variable 类型为 ndarray
的梯度值和类型为 ndarray
的词id组成的tuple。
返回类型:ndarray
或者 tuple of ndarray
, 返回类型 tuple of ndarray
仅在 Embedding 层稀疏更新时产生。
- 示例代码
-
import paddle.fluid as fluid import numpy as np # example1: 返回ndarray x = np.ones([2, 2], np.float32) with fluid.dygraph.guard(): inputs2 = [] for _ in range(10): tmp = fluid.dygraph.base.to_variable(x) tmp.stop_gradient=False inputs2.append(tmp) ret2 = fluid.layers.sums(inputs2) loss2 = fluid.layers.reduce_sum(ret2) loss2.backward() print(loss2.gradient()) # example2: 返回tuple of ndarray with fluid.dygraph.guard(): embedding = fluid.dygraph.Embedding( size=[20, 32], param_attr='emb.w', is_sparse=True) x_data = np.arange(12).reshape(4, 3).astype('int64') x_data = x_data.reshape((-1, 3, 1)) x = fluid.dygraph.base.to_variable(x_data) out = embedding(x) out.backward() print(embedding.weight.gradient())
-
clear_gradient
( ) ¶
注意:
设置该 Variable 的梯度为零
返回:无
- 示例代码
-
import paddle.fluid as fluid import numpy as np x = np.ones([2, 2], np.float32) with fluid.dygraph.guard(): inputs2 = [] for _ in range(10): tmp = fluid.dygraph.base.to_variable(x) tmp.stop_gradient=False inputs2.append(tmp) ret2 = fluid.layers.sums(inputs2) loss2 = fluid.layers.reduce_sum(ret2) loss2.backward() print(loss2.gradient()) loss2.clear_gradient() print("After clear {}".format(loss2.gradient()))
-
to_string
( ) ¶
注意:
1. 该API只在非 Dygraph 模式下生效
获取该 Variable 的静态描述字符串
- 参数:(仅在非 Dygraph 模式下生效)
-
throw_on_error (bool) - 是否在没有设置必需字段时抛出异常。
with_details (bool) - 值为true时,打印更多关于 Variable 的信息,如
error_clip
,stop_gradient
等
返回:用于静态描述该 Variable 的字符串
返回类型: str
抛出异常: ValueError
- 当 throw_on_error == true
,当没有设置任何必需的字段时,抛出 ValueError
。
- 示例代码
-
import paddle.fluid as fluid cur_program = fluid.Program() cur_block = cur_program.current_block() new_variable = cur_block.create_var(name="X", shape=[-1, 23, 48], dtype='float32') print(new_variable.to_string(True)) print("\n=============with detail===============\n") print(new_variable.to_string(True, True))
-
clone
( self ) ¶
返回一个新的 Variable
, 其复制原 Variable
并且新的 Variable
也被保留在计算图中,即复制的新 Variable
也参与反向计算。调用 out = tensor.clone()
与 out = assign(tensor)
效果一样。
返回:复制的新 Variable
返回类型: Variable
- 示例代码
-
import paddle paddle.enable_static() # create a static Variable x = paddle.static.data(name='x', shape=[3, 2, 1]) # create a cloned Variable y = x.clone()
-
astype
( self, dtype ) ¶
将该 Variable 中的数据转换成目标 Dtype
返回:一个全新的转换了 Dtype
的 Variable
返回类型: Variable
示例代码
- 在静态图模式下:
-
import paddle.fluid as fluid startup_prog = fluid.Program() main_prog = fluid.Program() with fluid.program_guard(startup_prog, main_prog): original_variable = fluid.data(name = "new_variable", shape=[2,2], dtype='float32') new_variable = original_variable.astype('int64') print("new var's dtype is: {}".format(new_variable.dtype))
- 在 Dygraph 模式下:
-
import paddle.fluid as fluid import numpy as np x = np.ones([2, 2], np.float32) with fluid.dygraph.guard(): original_variable = fluid.dygraph.to_variable(x) print("original var's dtype is: {}, numpy dtype is {}".format(original_variable.dtype, original_variable.numpy().dtype)) new_variable = original_variable.astype('int64') print("new var's dtype is: {}, numpy dtype is {}".format(new_variable.dtype, new_variable.numpy().dtype))
属性¶
-
stop_gradient
¶
注意:该属性在 Dygraph 模式下除参数以外默认值为 True
,而参数的该属性默认值为 False
。在静态图下所有的 Variable 该属性默认值都为 False
是否从此 Variable 开始,之前的相关部分都停止梯度计算
- 示例代码
-
import paddle.fluid as fluid import numpy as np with fluid.dygraph.guard(): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") linear = fluid.Linear(13, 5, dtype="float32") linear2 = fluid.Linear(3, 3, dtype="float32") a = fluid.dygraph.to_variable(value0) b = fluid.dygraph.to_variable(value1) c = fluid.dygraph.to_variable(value2) out1 = linear(a) out2 = linear2(b) out1.stop_gradient = True out = fluid.layers.concat(input=[out1, out2, c], axis=1) out.backward() # 可以发现这里linear的参数梯度变成了None assert linear.weight.gradient() is None assert out1.gradient() is None
-
persistable
¶
注意:该属性我们即将废弃,此介绍仅为了帮助用户理解概念, 1.6版本后用户可以不再关心该属性
-
name
¶
注意:在非 Dygraph 模式下,那么同一个 Block 中的两个或更多 Variable 拥有相同 name
将意味着他们会共享相同的内容。通常我们使用这种方式来实现参数共享
此 Variable 的名字(str)
-
shape
¶
注意:该属性是只读属性
此 Variable 的维度
-
dtype
¶
注意:该属性是只读属性
此 Variable 的实际数据类型
-
lod_level
¶
注意:
1. 该属性是只读属性
2. Dygraph 模式下,不支持该属性,该值为零
此 Variable 的 LoD
信息,关于 LoD
可以参考 api_fluid_LoDTensor 相关内容
-
type
¶
注意:该属性是只读属性
此 Variable 的内存模型,例如是:api_fluid_LoDTensor, 或者SelectedRows
此页内容是否对您有帮助
感谢反馈!