|
|
|
@ -20,6 +20,7 @@ from ..framework import convert_np_dtype_to_dtype_
|
|
|
|
|
from ..framework import Variable
|
|
|
|
|
from ..initializer import Constant, force_init_on_cpu
|
|
|
|
|
from ..core import VarDesc
|
|
|
|
|
from ..imperative import base as imperative_base
|
|
|
|
|
from .layer_function_generator import templatedoc
|
|
|
|
|
import numpy
|
|
|
|
|
|
|
|
|
@ -104,15 +105,15 @@ def create_global_var(shape,
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
shape(list[int]): shape of the variable
|
|
|
|
|
value(float): the value of the variable. The new created
|
|
|
|
|
value(float): the value of the variable. The new created
|
|
|
|
|
variable will be filled with it.
|
|
|
|
|
dtype(string): data type of the variable
|
|
|
|
|
persistable(bool): if this variable is persistable.
|
|
|
|
|
persistable(bool): if this variable is persistable.
|
|
|
|
|
Default: False
|
|
|
|
|
force_cpu(bool): force this variable to be on CPU.
|
|
|
|
|
force_cpu(bool): force this variable to be on CPU.
|
|
|
|
|
Default: False
|
|
|
|
|
name(str|None): The name of the variable. If set to None the variable
|
|
|
|
|
name will be generated automatically.
|
|
|
|
|
name(str|None): The name of the variable. If set to None the variable
|
|
|
|
|
name will be generated automatically.
|
|
|
|
|
Default: None
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
@ -121,21 +122,33 @@ def create_global_var(shape,
|
|
|
|
|
Examples:
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
var = fluid.create_global_var(shape=[2,3], value=1.0, dtype='float32',
|
|
|
|
|
var = fluid.create_global_var(shape=[2,3], value=1.0, dtype='float32',
|
|
|
|
|
persistable=True, force_cpu=True, name='new_var')
|
|
|
|
|
"""
|
|
|
|
|
helper = LayerHelper("global_var", **locals())
|
|
|
|
|
var = helper.create_global_variable(
|
|
|
|
|
dtype=dtype, shape=shape, persistable=persistable, name=name)
|
|
|
|
|
helper.set_variable_initializer(
|
|
|
|
|
var, initializer=Constant(
|
|
|
|
|
value=float(value), force_cpu=force_cpu))
|
|
|
|
|
dtype=dtype,
|
|
|
|
|
shape=shape,
|
|
|
|
|
persistable=persistable,
|
|
|
|
|
name=name,
|
|
|
|
|
stop_gradient=True)
|
|
|
|
|
print("set_variable_initializer, ", var.name)
|
|
|
|
|
if imperative_base.enabled():
|
|
|
|
|
var = helper.set_variable_initializer(
|
|
|
|
|
var, initializer=Constant(
|
|
|
|
|
value=float(value), force_cpu=force_cpu))
|
|
|
|
|
print("get var", var)
|
|
|
|
|
else:
|
|
|
|
|
helper.set_variable_initializer(
|
|
|
|
|
var, initializer=Constant(
|
|
|
|
|
value=float(value), force_cpu=force_cpu))
|
|
|
|
|
|
|
|
|
|
return var
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cast(x, dtype):
|
|
|
|
|
"""
|
|
|
|
|
This layer takes in the Variable :attr:`x` with :attr:`x.dtype` and casts
|
|
|
|
|
This layer takes in the Variable :attr:`x` with :attr:`x.dtype` and casts
|
|
|
|
|
it to the output with :attr:`dtype`.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
@ -199,9 +212,9 @@ def tensor_array_to_tensor(input, axis=1, name=None):
|
|
|
|
|
and returns that as the output.
|
|
|
|
|
|
|
|
|
|
A simple example as below:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Given:
|
|
|
|
|
|
|
|
|
|
input.data = {[[0.6, 0.1, 0.3],
|
|
|
|
@ -210,9 +223,9 @@ def tensor_array_to_tensor(input, axis=1, name=None):
|
|
|
|
|
[1.8]],
|
|
|
|
|
[[2.3, 2.1],
|
|
|
|
|
[2.5, 2.4]]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
axis = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Then:
|
|
|
|
|
|
|
|
|
|
output.data = [[0.6, 0.1, 0.3, 1.3, 2.3, 2.1],
|
|
|
|
@ -493,12 +506,12 @@ def argmax(x, axis=0):
|
|
|
|
|
|
|
|
|
|
def argsort(input, axis=-1, name=None):
|
|
|
|
|
"""
|
|
|
|
|
Performs sorting on the input Variable along the given axis, and outputs
|
|
|
|
|
sorted data Varibale and its corresponding index Variable with the same
|
|
|
|
|
Performs sorting on the input Variable along the given axis, and outputs
|
|
|
|
|
sorted data Varibale and its corresponding index Variable with the same
|
|
|
|
|
shape as :attr:`input`.
|
|
|
|
|
|
|
|
|
|
.. code-block:: text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For example, the given axis is -1 and the input Variable
|
|
|
|
|
|
|
|
|
|
input = [[0.15849551, 0.45865775, 0.8563702 ],
|
|
|
|
@ -511,15 +524,15 @@ def argsort(input, axis=-1, name=None):
|
|
|
|
|
|
|
|
|
|
and the sorted indices along the given axis turn outs to be
|
|
|
|
|
|
|
|
|
|
indices = [[0, 1, 2],
|
|
|
|
|
indices = [[0, 1, 2],
|
|
|
|
|
[0, 2, 1]]
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
input(Variable): The input Variable for sorting.
|
|
|
|
|
axis(int): The axis along which to sort the input Variable. When
|
|
|
|
|
:attr:`axis` < 0, the actual axis will be :attr:`axis` +
|
|
|
|
|
axis(int): The axis along which to sort the input Variable. When
|
|
|
|
|
:attr:`axis` < 0, the actual axis will be :attr:`axis` +
|
|
|
|
|
rank(:attr:`input`). Default -1, the last dimension.
|
|
|
|
|
name(str|None): (optional) A name for this layer. If set None, the
|
|
|
|
|
name(str|None): (optional) A name for this layer. If set None, the
|
|
|
|
|
layer will be named automatically.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|