Commit 2598f4dc by Ting PAN

Align the tensor abstraction

Summary:
This commit aligns the properties and methods of tensor class.
1 parent 413dbad0
Showing with 545 additions and 827 deletions
......@@ -368,7 +368,7 @@ class Input(Layer):
namespace='Tensor'),
shape=self.blob_shapes[i],
dtype='float32',
).placeholder() for i in range(len(self.blob_shapes))]
).constant() for i in range(len(self.blob_shapes))]
class Normalize(Layer):
......
......@@ -110,7 +110,7 @@ class Reshape(object):
reshape1 = dali.ops.Reshape(shape=(2, 3))
y = reshape1(inputs['x'])
# Reshape to the shape given by another tensor
# Reshape to the shape of other tensor
reshape2 = dali.ops.Reshape()
z = reshape2(inputs['x'], inputs['x_shape'])
```
......
......@@ -7,13 +7,13 @@ dragon
-------
`class EagerTensor <dragon/EagerTensor.html>`_
: Tensor abstraction under the eager execution.
: Tensor abstraction for eager executing.
`class GradientTape <dragon/GradientTape.html>`_
: Record the operations for auto differentiation.
`class Tensor <dragon/Tensor.html>`_
: Tensor abstraction under the graph execution.
: Tensor abstraction for graph executing.
`class TensorSpec <dragon/TensorSpec.html>`_
: Spec to describe properties of a tensor.
......@@ -25,10 +25,10 @@ dragon
---------
`arange(...) <dragon/arange.html>`_
: Return a tensor with evenly spaced values within a interval.
: Return a tensor of evenly spaced values within a interval.
`assign(...) <dragon/assign.html>`_
: Assign the value to ref.
: Assign the value to input.
`broadcast_to(...) <dragon/broadcast_to.html>`_
: Broadcast input according to a given shape.
......@@ -46,10 +46,10 @@ dragon
: Concatenate the inputs along the given axis.
`constant(...) <dragon/constant.html>`_
: Return a tensor taking the value content.
: Return a tensor initialized from the value.
`copy(...) <dragon/copy.html>`_
: Copy the value to ref.
: Copy the input.
`create_function(...) <dragon/create_function.html>`_
: Create a callable graph from the specified outputs.
......@@ -70,10 +70,10 @@ dragon
: Return a tensor constructed as the identity matrix.
`eye_like(...) <dragon/eye_like.html>`_
:Return a tensor shaping like another constructed as the identity matrix.
:Return a tensor of identity matrix with shape as the other.
`fill(...) <dragon/fill.html>`_
: Return a tensor filled with the specific value.
: Return a tensor filled with the scalar value.
`flatten(...) <dragon/flatten.html>`_
: Flatten the input along the given axes.
......@@ -97,22 +97,22 @@ dragon
: Load a shared library.
`masked_assign(...) <dragon/masked_assign.html>`_
: Assign the value to ref where mask is **1**.
: Assign the value to input where mask is 1.
`masked_select(...) <dragon/masked_select.html>`_
: Select the elements where the given mask is **1**.
: Select the elements of input where mask is 1.
`name_scope(...) <dragon/name_scope.html>`_
: Context-manager to nest the name as prefix for operations.
`nonzero(...) <dragon/nonzero.html>`_
: Return the indices of non-zero elements.
: Return the index of non-zero elements.
`ones(...) <dragon/ones.html>`_
: Return a tensor filled with ones.
`ones_like(...) <dragon/ones_like.html>`_
: Return a tensor shaping like another filled with ones.
: Return a tensor of ones with shape as the other.
`one_hot(...) <dragon/one_hot.html>`_
: Return the one-hot representation for input.
......@@ -163,7 +163,7 @@ dragon
: Return a tensor filled with zeros.
`zeros_like(...) <dragon/zeros_like.html>`_
: Return a tensor shaping like another filled with zeros.
: Return a tensor of zeros with shape as the other.
.. toctree::
:hidden:
......
......@@ -92,10 +92,6 @@ __add__
#######
.. automethod:: dragon.EagerTensor.__add__
__div__
#######
.. automethod:: dragon.EagerTensor.__div__
__float__
#########
.. automethod:: dragon.EagerTensor.__float__
......@@ -116,10 +112,6 @@ __iadd__
########
.. automethod:: dragon.EagerTensor.__iadd__
__idiv__
########
.. automethod:: dragon.EagerTensor.__idiv__
__imul__
########
.. automethod:: dragon.EagerTensor.__imul__
......@@ -132,6 +124,10 @@ __isub__
########
.. automethod:: dragon.EagerTensor.__isub__
__itruediv__
############
.. automethod:: dragon.EagerTensor.__itruediv__
__le__
######
.. automethod:: dragon.EagerTensor.__le__
......@@ -152,10 +148,6 @@ __radd__
########
.. automethod:: dragon.EagerTensor.__radd__
__rdiv__
########
.. automethod:: dragon.EagerTensor.__rdiv__
__rmul__
########
.. automethod:: dragon.EagerTensor.__rmul__
......@@ -164,6 +156,10 @@ __rsub__
########
.. automethod:: dragon.EagerTensor.__rsub__
__rtruediv__
############
.. automethod:: dragon.EagerTensor.__rtruediv__
__setitem__
###########
.. automethod:: dragon.EagerTensor.__setitem__
......@@ -172,18 +168,13 @@ __sub__
#######
.. automethod:: dragon.EagerTensor.__sub__
__truediv__
###########
.. automethod:: dragon.EagerTensor.__truediv__
.. _dragon.assign(...): assign.html
.. _dragon.cast(...): cast.html
.. _dragon.copy(...): copy.html
.. _dragon.math.add(...): math/add.html
.. _dragon.math.div(...): math/div.html
.. _dragon.math.greater(...): math/greater.html
.. _dragon.math.greater_equal(...): math/greater_equal.html
.. _dragon.math.less(...): math/less.html
.. _dragon.math.less_equal(...): math/less_equal.html
.. _dragon.math.mul(...): math/mul.html
.. _dragon.math.negative(...): math/negative.html
.. _dragon.math.sub(...): math/sub.html
.. _dragon.masked_assign(...): masked_assign.html
.. _dragon.masked_select(...): masked_select.html
.. _dragon.reshape(...): reshape.html
......
......@@ -65,10 +65,6 @@ normal
######
.. automethod:: dragon.Tensor.normal
placeholder
###########
.. automethod:: dragon.Tensor.placeholder
reshape
#######
.. automethod:: dragon.Tensor.reshape
......@@ -85,10 +81,6 @@ uniform
#######
.. automethod:: dragon.Tensor.uniform
variable
########
.. automethod:: dragon.Tensor.variable
Overrides
---------
......@@ -96,10 +88,6 @@ __add__
#######
.. automethod:: dragon.Tensor.__add__
__div__
#######
.. automethod:: dragon.Tensor.__div__
__float__
#########
.. automethod:: dragon.Tensor.__float__
......@@ -140,10 +128,6 @@ __radd__
########
.. automethod:: dragon.Tensor.__radd__
__rdiv__
########
.. automethod:: dragon.Tensor.__rdiv__
__rmul__
########
.. automethod:: dragon.Tensor.__rmul__
......@@ -160,24 +144,21 @@ __sub__
#######
.. automethod:: dragon.Tensor.__sub__
__rtruediv__
############
.. automethod:: dragon.Tensor.__rtruediv__
__truediv__
############
.. automethod:: dragon.Tensor.__truediv__
.. _dragon.assign(...): assign.html
.. _dragon.cast(...): cast.html
.. _dragon.copy(...): copy.html
.. _dragon.math.add(...): math/add.html
.. _dragon.math.div(...): math/div.html
.. _dragon.math.greater(...): math/greater.html
.. _dragon.math.greater_equal(...): math/greater_equal.html
.. _dragon.math.less(...): math/less.html
.. _dragon.math.less_equal(...): math/less_equal.html
.. _dragon.math.mul(...): math/mul.html
.. _dragon.math.negative(...): math/negative.html
.. _dragon.math.sub(...): math/sub.html
.. _dragon.masked_assign(...): masked_assign.html
.. _dragon.masked_select(...): masked_select.html
.. _dragon.reshape(...): reshape.html
.. _dragon.slice(...): slice.html
.. _dragon.workspace.feed_tensor(...): workspace/feed_tensor.html
.. _dragon.workspace.fetch_tensor(...): workspace/fetch_tensor.html
.. raw:: html
......
......@@ -13,13 +13,13 @@ dragon.random
: Return a tensor initialized from the glorot uniform distribution.
`multinomial(...) <random/multinomial.html>`_
: Return a tensor with indices sampled from multinomial distribution.
: Return a tensor with indices sampled from the multinomial distribution.
`normal(...) <random/normal.html>`_
: Return a tensor initialized from the normal distribution.
`normal_like(...) <random/normal_like.html>`_
: Return a tensor shaping like another initialized from the normal distribution.
: Return a tensor initialized from the normal distribution with shape as the other.
`set_seed(...) <random/set_seed.html>`_
: Set the global random seed.
......@@ -31,7 +31,7 @@ dragon.random
: Return a tensor initialized from the uniform distribution.
`uniform_like(...) <random/uniform_like.html>`_
: Return a tensor shaping like another initialized from the uniform distribution.
: Return a tensor initialized from the uniform distribution with shape as the other.
.. toctree::
:hidden:
......
......@@ -71,13 +71,10 @@ TensorFlow
* `tensorflow <tensorflow.html>`_
* `tensorflow.bitwise <tensorflow/bitwise.html>`_
* `tensorflow.dtypes <tensorflow/dtypes.html>`_
* `tensorflow.initializers <tensorflow/initializers.html>`_
* `tensorflow.keras <tensorflow/keras.html>`_
* `tensorflow.linalg <tensorflow/linalg.html>`_
* `tensorflow.losses <tensorflow/losses.html>`_
* `tensorflow.math <tensorflow/math.html>`_
* `tensorflow.nn <tensorflow/nn.html>`_
* `tensorflow.optimizers <tensorflow/optimizers.html>`_
* `tensorflow.random <tensorflow/random.html>`_
TensorLayer
......@@ -233,27 +230,18 @@ Modules
`Module vm.tensorflow.dtypes <tensorflow/dtypes.html>`_
: Virtual API for ``tensorflow.dtypes`` namespace.
`Module vm.tensorflow.initializers <tensorflow/initializers.html>`_
: Virtual API for ``tensorflow.initializers`` namespace.
`Module vm.tensorflow.keras <tensorflow/keras.html>`_
: Virtual API for ``tensorflow.keras`` namespace.
`Module vm.tensorflow.linalg <tensorflow/linalg.html>`_
: Virtual API for ``tensorflow.linalg`` namespace.
`Module vm.tensorflow.losses <tensorflow/losses.html>`_
: Virtual API for ``tensorflow.losses`` namespace.
`Module vm.tensorflow.math <tensorflow/math.html>`_
: Virtual API for ``tensorflow.math`` namespace.
`Module vm.tensorflow.nn <tensorflow/nn.html>`_
: Virtual API for ``tensorflow.nn`` namespace.
`Module vm.tensorflow.optimizers <tensorflow/optimizers.html>`_
: Virtual API for ``tensorflow.optimizers`` namespace.
`Module vm.tensorflow.random <tensorflow/random.html>`_
: Virtual API for ``tensorflow.random`` namespace.
......@@ -325,13 +313,10 @@ Modules
tensorflow
tensorflow/bitwise
tensorflow/dtypes
tensorflow/initializers
tensorflow/keras
tensorflow/linalg
tensorflow/losses
tensorflow/math
tensorflow/nn
tensorflow/optimizers
tensorflow/random
tensorlayer/initializers
tensorlayer/layers
......
......@@ -28,7 +28,7 @@ vm.tensorflow
: Concatenate the values along the given axis.
`constant(...) <tensorflow/constant.html>`_
: Return a tensor taking the value content.
: Return a tensor initialized from the value.
`device(...) <tensorflow/device.html>`_
: Context-manager to nest the the device spec.
......@@ -40,7 +40,7 @@ vm.tensorflow
: Return a tensor constructed as the identity matrix.
`fill(...) <tensorflow/fill.html>`_
: Return a tensor filled with the specific value.
: Return a tensor filled with the scalar value.
`gather(...) <tensorflow/gather.html>`_
: Select the elements according to the indices along the given axis.
......@@ -61,16 +61,16 @@ vm.tensorflow
: Return a tensor filled with ones.
`ones_like(...) <tensorflow/ones_like.html>`_
: Return a tensor shaping like another filled with ones.
: Return a tensor of ones with shape as the other.
`one_hot(...) <tensorflow/one_hot.html>`_
: Return the one-hot representation from indices.
: Return the one-hot representation for input.
`pad(...) <tensorflow/pad.html>`_
: Pad the input according to the given sizes.
`range(...) <tensorflow/range.html>`_
: Return a tensor with evenly spaced values within a interval.
: Return a tensor of evenly spaced values within a interval.
`reshape(...) <tensorflow/reshape.html>`_
: Change the dimensions of input.
......@@ -94,7 +94,7 @@ vm.tensorflow
: Return a tensor filled with zeros.
`zeros_like(...) <tensorflow/zeros_like.html>`_
: Return a tensor shaping like another filled with zeros.
: Return a tensor of zeros with shape as the other.
.. toctree::
:hidden:
......
vm.tensorflow.initializers
==========================
.. only:: html
Classes
-------
`class Constant <keras/initializers/Constant.html>`_
: Fill tensors with a scalar.
`class GlorotNormal <keras/initializers/GlorotNormal.html>`_
: Fill tensors according to a glorot normal distribution.
`class GlorotUniform <keras/initializers/GlorotUniform.html>`_
: Fill tensors according to a glorot uniform distribution.
`class Initializer <keras/initializers/GlorotUniform.html>`_
: The basic Initializer.
`class Ones <keras/initializers/Ones.html>`_
: Fill tensors with ones.
`class RandomNormal <keras/initializers/RandomNormal.html>`_
: Fill tensors according to a random normal distribution.
`class RandomUniform <keras/initializers/RandomUniform.html>`_
: Fill tensors according to a random uniform distribution.
`class TruncatedNormal <keras/initializers/TruncatedNormal.html>`_
: Fill tensors according to a truncated normal distribution.
`class VarianceScaling <keras/initializers/VarianceScaling.html>`_
: Fill tensors with the random values adapting to shape.
`class Zeros <keras/initializers/Zeros.html>`_
: Fill tensors with zeros.
.. raw:: html
<style>
h1:before {
content: "Module: dragon.";
color: #103d3e;
}
</style>
......@@ -7,31 +7,31 @@ initializers
-------
`class Constant <initializers/Constant.html>`_
: Fill tensors with a scalar.
: Fill tensor with a scalar value.
`class GlorotNormal <initializers/GlorotNormal.html>`_
: Fill tensors according to a glorot normal distribution.
: Fill tensor from a glorot normal distribution.
`class GlorotUniform <initializers/GlorotUniform.html>`_
: Fill tensors according to a glorot uniform distribution.
: Fill tensor from a glorot uniform distribution.
`class Initializer <initializers/GlorotUniform.html>`_
: The basic Initializer.
`class Ones <initializers/Ones.html>`_
: Fill tensors with ones.
: Fill tensor with ones.
`class RandomNormal <initializers/RandomNormal.html>`_
: Fill tensors according to a random normal distribution.
: Fill tensor from a normal distribution.
`class RandomUniform <initializers/RandomUniform.html>`_
: Fill tensors according to a random uniform distribution.
: Fill tensor from an uniform distribution.
`class TruncatedNormal <initializers/TruncatedNormal.html>`_
: Fill tensors according to a truncated normal distribution.
: Fill tensor from a truncated normal distribution.
`class VarianceScaling <initializers/VarianceScaling.html>`_
: Fill tensors with the random values adapting to shape.
: Fill tensor from a scaled random distribution.
`class Zeros <initializers/Zeros.html>`_
: Fill tensors with zeros.
......
......@@ -7,18 +7,18 @@ optimizers
-------
`class Adam <optimizers/Adam.html>`_
: The optimizer which implements Adam algorithm.
: The optimizer to apply Adam algorithm.
`[Kingma & Ba, 2014] <https://arxiv.org/abs/1412.6980>`_.
`class Optimizer <optimizers/Optimizer.html>`_
: The base class for optimizers.
`class RMSprop <optimizers/RMSprop.html>`_
: The optimizer which implements RMSprop algorithm.
: The optimizer to apply RMSprop algorithm.
`[Hinton et.al, 2013] <http://www.cs.utoronto.ca/~bonner/courses/2016s/csc321/lectures/lec6.pdf>`_.
`class SGD <optimizers/SGD.html>`_
: The optimizer which implements SGD algorithm.
: The optimizer to apply SGD algorithm.
.. toctree::
:hidden:
......
vm.tensorflow.losses
====================
.. only:: html
Classes
-------
`class BinaryCrossentropy <keras/losses/BinaryCrossentropy.html>`_
: A criterion to compute the binary cross entropy with contiguous targets.
`class CategoricalCrossentropy <keras/losses/CategoricalCrossentropy.html>`_
: A criterion to compute the categorical cross entropy with contiguous targets.
`class Loss <keras/losses/Loss.html>`_
: The base class for loss criterion.
`class MeanAbsoluteError <keras/losses/MeanAbsoluteError.html>`_
: A criterion to compute the reduced element-wise absolute value difference.
`class MeanSquaredError <keras/losses/MeanSquaredError.html>`_
: A criterion to compute the reduced element-wise squared error.
`class SparseCategoricalCrossentropy <keras/losses/SparseCategoricalCrossentropy.html>`_
: A criterion to compute the categorical cross entropy with sparse labels.
Functions
---------
`binary_crossentropy(...) <keras/losses/binary_crossentropy.html>`_
: Compute the binary cross entropy with contiguous targets.
`categorical_crossentropy(...) <keras/losses/categorical_crossentropy.html>`_
: Compute the categorical cross entropy with contiguous targets.
`mean_absolute_error(...) <keras/losses/mean_absolute_error.html>`_
: Compute the reduced element-wise absolute value difference.
`mean_squared_error(...) <keras/losses/mean_squared_error.html>`_
: Compute the reduced element-wise squared error.
`sparse_categorical_crossentropy(...) <keras/losses/sparse_categorical_crossentropy.html>`_
: Compute the categorical cross entropy with sparse labels.
.. raw:: html
<style>
h1:before {
content: "Module: dragon.";
color: #103d3e;
}
</style>
vm.tensorflow.optimizers
========================
.. only:: html
Classes
-------
`class Adam <keras/optimizers/Adam.html>`_
: The optimizer which implements Adam algorithm.
`[Kingma & Ba, 2014] <https://arxiv.org/abs/1412.6980>`_.
`class Optimizer <keras/optimizers/Optimizer.html>`_
: The base class for optimizers.
`class RMSprop <keras/optimizers/RMSprop.html>`_
: The optimizer which implements RMSprop algorithm.
`[Hinton et.al, 2013] <http://www.cs.utoronto.ca/~bonner/courses/2016s/csc321/lectures/lec6.pdf>`_.
`class SGD <keras/optimizers/SGD.html>`_
: The optimizer which implements SGD algorithm.
.. raw:: html
<style>
h1:before {
content: "Module: dragon.";
color: #103d3e;
}
</style>
......@@ -7,31 +7,31 @@ vm.tensorlayer.initializers
-------
`class Constant <initializers/Constant.html>`_
: Fill tensors with a scalar.
: Fill tensor with a scalar value.
`class GlorotNormal <initializers/GlorotNormal.html>`_
: Fill tensors according to a glorot normal distribution.
: Fill tensor from a glorot normal distribution.
`class GlorotUniform <initializers/GlorotUniform.html>`_
: Fill tensors according to a glorot uniform distribution.
: Fill tensor from a glorot uniform distribution.
`class Initializer <initializers/GlorotUniform.html>`_
: The basic Initializer.
`class Ones <initializers/Ones.html>`_
: Fill tensors with ones.
: Fill tensor with ones.
`class RandomNormal <initializers/RandomNormal.html>`_
: Fill tensors according to a random normal distribution.
: Fill tensor from a normal distribution.
`class RandomUniform <initializers/RandomUniform.html>`_
: Fill tensors according to a random uniform distribution.
: Fill tensor from an uniform distribution.
`class TruncatedNormal <initializers/TruncatedNormal.html>`_
: Fill tensors according to a truncated normal distribution.
: Fill tensor from a truncated normal distribution.
`class Zeros <initializers/Zeros.html>`_
: Fill tensors with zeros.
: Fill tensor with zeros.
.. toctree::
:hidden:
......
......@@ -34,7 +34,7 @@ vm.torch
: Compute the element-wise addition.
`arange(...) <torch/arange.html>`_
: Return a tensor with evenly spaced values within a interval.
: Return a tensor of evenly spaced values within a interval.
`argmax(...) <torch/argmax.html>`_
: Return the indices of maximum elements along the given axis.
......@@ -163,14 +163,11 @@ vm.torch
`nonzero(...) <torch/nonzero.html>`_
: Return the indices of non-zero elements.
`normal(...) <torch/normal.html>`_
: Return a tensor with a normal distribution.
`ones(...) <torch/ones.html>`_
: Return a tensor with value 1 filled.
: Return a tensor filled with ones.
`ones_like(...) <torch/ones_like.html>`_
: Return a tensor with value 1 filled, shape as input.
: Return a tensor of ones with shape as the other.
`one_hot(...) <torch/one_hot.html>`_
: Return the one-hot representation for input.
......@@ -182,10 +179,10 @@ vm.torch
: Compute the power of input.
`rand(...) <torch/rand.html>`_
: Return a float tensor with a uniform distribution of U(0, 1).
: Return a tensor from the uniform distribution of U(0, 1).
`randn(...) <torch/randn.html>`_
: Return a float tensor with a normal distribution of N(0, 1).
: Return a tensor from the normal distribution of N(0, 1).
`reciprocal(...) <torch/reciprocal.html>`_
: Compute the reciprocal of input.
......@@ -235,9 +232,6 @@ vm.torch
`topk_acc(...) <torch/topk_acc.html>`_
: Compute the top-k accuracy according to the label.
`uniform(...) <torch/uniform.html>`_
: Return a tensor with a normal distribution.
`unsqueeze(...) <torch/unsqueeze.html>`_
: Expand the dimensions of input with size 1.
......@@ -245,10 +239,10 @@ vm.torch
: Select the elements from two branches under the condition.
`zeros(...) <torch/zeros.html>`_
: Return a tensor with value 0 filled.
: Return a tensor filled with zeros.
`zeros_like(...) <torch/zeros_like.html>`_
: Return a tensor with value 0s filled, shape as input.
: Return a tensor of zeros with shape as the other.
.. toctree::
:hidden:
......@@ -301,7 +295,6 @@ vm.torch
torch/ne
torch/no_grad
torch/nonzero
torch/normal
torch/ones
torch/ones_like
torch/one_hot
......@@ -328,7 +321,6 @@ vm.torch
torch/tensor
torch/topk
torch/topk_acc
torch/uniform
torch/unsqueeze
torch/where
torch/zeros_like
......
......@@ -26,6 +26,10 @@ id
###
.. autoattribute:: dragon.vm.torch.Tensor.id
is_leaf
#######
.. autoattribute:: dragon.vm.torch.Tensor.is_leaf
requires_grad
#############
.. autoattribute:: dragon.vm.torch.Tensor.requires_grad
......@@ -341,6 +345,10 @@ reshape\_
#########
.. automethod:: dragon.vm.torch.Tensor.reshape_
retain_grad
###########
.. automethod:: dragon.vm.torch.Tensor.retain_grad
round
#####
.. automethod:: dragon.vm.torch.Tensor.round
......
normal
======
.. autofunction:: dragon.vm.torch.normal
.. raw:: html
<style>
h1:before {
content: "torch.";
color: #103d3e;
}
</style>
......@@ -7,17 +7,17 @@ vm.torch.optim
-------
`class Adam <optim/Adam.html>`_
: The optimizer which implements Adam algorithm.
: The optimizer to apply Adam algorithm.
`class Optimizer <optim/Optimizer.html>`_
: The base class of optimizers.
`class RMSprop <optim/RMSprop.html>`_
: The optimizer which implements RMSprop algorithm.
: The optimizer to apply RMSprop algorithm.
`[Hinton et.al, 2013] <http://www.cs.utoronto.ca/~bonner/courses/2016s/csc321/lectures/lec6.pdf>`_.
`class SGD <optim/SGD.html>`_
: The optimizer which implements SGD algorithm.
: The optimizer to apply SGD algorithm.
.. toctree::
:hidden:
......
uniform
=======
.. autofunction:: dragon.vm.torch.uniform
.. raw:: html
<style>
h1:before {
content: "torch.";
color: #103d3e;
}
</style>
......@@ -255,7 +255,7 @@ class FunctionGuard(object):
)
shape = input_signature[i].shape
dtype = input_signature[i].dtype
inputs.append(Tensor(name, shape, dtype).variable())
inputs.append(Tensor(name, shape, dtype).constant())
with context.name_scope('${%d}' % id(self)), eager_context.graph_mode():
returns = nest.flatten(self._python_function(*inputs))
outputs, dummies = [], []
......
......@@ -259,7 +259,7 @@ class Function(object):
"""
self.outputs = [Tensor(name) for name in graph_def.output]
self.inputs = [Tensor(name).variable() for name in graph_def.input]
self.inputs = [Tensor(name).constant() for name in graph_def.input]
# Fill with all known graph elements.
add_device_option(graph_def)
......@@ -294,7 +294,7 @@ def create_function(inputs=None, outputs=None, givens=None, updater=None):
Tensors that catch any operators can be used to create a graph:
```python
x = dragon.Tensor('x', dtype='float32').variable()
x = dragon.Tensor('x', dtype='float32').constant()
y = x * 2
f = dragon.create_function(outputs=y)
```
......@@ -316,12 +316,12 @@ def create_function(inputs=None, outputs=None, givens=None, updater=None):
Specify ``givens`` to substitute tensors before creating:
```python
x = dragon.Tensor('x', dtype='float32').variable()
x = dragon.Tensor('x', dtype='float32').constant()
y = x * 2
foo = dragon.create_function(outputs=y)
# "bar" takes "x2" as input, and also writes to "y"
x2 = dragon.Tensor('x2', dtype='float32').variable()
x2 = dragon.Tensor('x2', dtype='float32').constant()
bar = dragon.create_function(outputs=y, givens={x: x2})
```
......
......@@ -261,7 +261,7 @@ class Workspace(backend.Workspace):
return self.HasTensor(_stringify_object(tensor))
def merge_from(self, other):
"""Merge resources from another workspace.
"""Merge resources from the other.
The ``other`` will not be reset until ``self`` is reset.
Carefulness should be taken to associate with the workspaces.
......
......@@ -202,7 +202,7 @@ def elu(inputs, alpha=1., **kwargs):
inputs : dragon.Tensor
The input tensor.
alpha : float, optional, default=1.
The value of :math:`\alpha`.
The value to :math:`\alpha`.
Returns
-------
......@@ -247,7 +247,7 @@ def leaky_relu(inputs, alpha=0.2, **kwargs):
inputs : dragon.Tensor
The input tensor.
alpha : number, optional, default=0.2
The value of :math:`\alpha`.
The value to :math:`\alpha`.
Returns
-------
......@@ -466,9 +466,9 @@ def selu(inputs, alpha=1.67326, gamma=1.0507, **kwargs):
inputs : dragon.Tensor
The input tensor.
alpha : float, optional, default=1.67326
The value of :math:`\alpha`.
The value to :math:`\alpha`.
gamma : float, optional, default=1.0507
The value of :math:`\gamma`.
The value to :math:`\gamma`.
Returns
-------
......
......@@ -25,7 +25,7 @@ from dragon.core.util import nest
def arange(start, stop=None, step=1, dtype='int64', **kwargs):
r"""Return a tensor with evenly spaced values within a interval.
r"""Return a tensor of evenly spaced values within a interval.
Specify ``start`` and ``stop`` to determine an interval:
......@@ -537,7 +537,7 @@ def flatten(inputs, axis=0, num_axes=-1, keep_axes=None, **kwargs):
Examples:
```python
x = dragon.Tensor(shape=[1, 2, 3, 4]).variable()
x = dragon.Tensor(shape=[1, 2, 3, 4]).constant()
print(dragon.flatten(x, axis=1, num_axes=-1).shape) # (1, 24)
print(dragon.flatten(x, axis=1, num_axes=2).shape) # (1, 6, 4)
print(dragon.flatten(x, keep_axes=1)) # (24,)
......@@ -634,7 +634,7 @@ def index_select(inputs, indices, axis=0, **kwargs):
@OpSchema.num_inputs(2)
def masked_select(inputs, **kwargs):
"""Select the elements where the given mask is **1**.
"""Select the elements of input where mask is 1.
Parameters
----------
......@@ -908,14 +908,14 @@ def multinomial(inputs, num_samples=1, eps=0., normalize=False, **kwargs):
@OpSchema.num_inputs(1)
def nonzero(inputs, **kwargs):
r"""Return the indices of non-zero elements.
r"""Return the index of non-zero elements.
.. math:: y = \{i, \text{ if } x[i] \text{ is True }\}
.. math:: \text{out} = \{i, \text{ if } \text{input}[i] \neq 0
Parameters
----------
inputs : dragon.Tensor
The tensor :math:`x`.
The input tensor.
Returns
-------
......@@ -938,8 +938,8 @@ def one_hot(inputs, depth, on_value=1, off_value=0, **kwargs):
.. math::
\text{out}[i][j] =
\begin{cases}
\text{Val}_{off}, & \text{ if } x[i] \neq j \\
\text{Val}_{on}, & \text{ otherwise }
\text{off\_value}, & \text{ if } \text{input}[i] \neq j \\
\text{on\_value}, & \text{ otherwise }
\end{cases}
The max value of indices, i.e., the ``depth`` should be specified:
......
......@@ -26,23 +26,23 @@ from dragon.core.util import nest
@ArgHelper.repeated_desc('starts')
@ArgHelper.repeated_desc('sizes')
def assign(inputs, starts=None, sizes=None, **kwargs):
r"""Assign the value to ref.
r"""Assign the value to input.
.. math:: \text{Ref}[start:start + size, ...] = \text{Value}
.. math:: \text{input}[\text{start}:\text{start} + \text{size}, ...] = \text{value}
Parameters
----------
inputs : Sequence[dragon.Tensor]
The **ref** and **value**.
The input and value tensor.
starts : Sequence[Union[int, dragon.Tensor]], optional
The start pos of each dimension.
The start location for each dimension.
sizes : Sequence[Union[int, dragon.Tensor]], optional
The size of each dimension.
The number of elements assigned from start.
Returns
-------
dragon.Tensor
The **ref**.
The input tensor.
"""
args = parse_args(locals())
......@@ -104,24 +104,24 @@ def copy(inputs, **kwargs):
@OpSchema.num_inputs(3)
def masked_assign(inputs, **kwargs):
r"""Assign the value to ref where mask is **1**.
r"""Assign the value to input where mask is 1.
.. math::
\text{Ref}[i] =
\begin{cases}
\text{Value}[i], & \text{ if } \text{Mask}[i] = 1 \\
\text{Ref}[i], & \text{ otherwise }
\text{input}[i] =
\begin{cases}
\text{value}[i], & \text{ if } \text{mask}[i] = 1 \\
\text{input}[i], & \text{ otherwise }
\end{cases}
Parameters
----------
inputs : Sequence[dragon.Tensor]
The **ref**, **value** and **mask** tensor.
The input, value and mask tensor.
Returns
-------
dragon.Tensor
The **ref** tensor..
The input tensor.
"""
args = parse_args(locals())
......
......@@ -27,7 +27,7 @@ from dragon.core.ops.utils import parse_args
def constant(value, dtype=None, shape=None, name=None):
r"""Return a tensor taking the value content.
r"""Return a tensor initialized from the value.
Examples:
......@@ -40,7 +40,7 @@ def constant(value, dtype=None, shape=None, name=None):
Parameters
----------
value : array_like
The constant value.
The value to initialize from.
dtype : str, optional
The optional data type.
shape : Sequence[int], optional
......@@ -68,7 +68,7 @@ def constant(value, dtype=None, shape=None, name=None):
def eye(n, m=None, k=0, dtype='float32', **kwargs):
r"""Return a tensor constructed as the identity matrix.
"""Return a tensor constructed as the identity matrix.
The rows and cols of matrix are determined by ``n`` and ``m``:
......@@ -131,7 +131,7 @@ def eye(n, m=None, k=0, dtype='float32', **kwargs):
@OpSchema.num_inputs(1)
def eye_like(other, k=0, dtype='float32', **kwargs):
r"""Return a tensor shaping like another constructed as the identity matrix.
"""Return a tensor of identity matrix with shape as the other.
The rows and cols of matrix are hinted by the input tensor:
......@@ -183,14 +183,14 @@ def eye_like(other, k=0, dtype='float32', **kwargs):
@ArgHelper.repeated_desc(name='shape', name_v2='dims')
def fill(shape, value=0, dtype=None, **kwargs):
r"""Return a tensor filled with the specific value.
r"""Return a tensor filled with the scalar value.
.. math:: y \leftarrow \text{value}
.. math:: \text{out} \leftarrow \text{value}
Parameters
----------
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
The tensor shape.
value : number, optional, default=0
The value to fill.
dtype : str, optional, default='float32'
......@@ -224,21 +224,19 @@ def fill(shape, value=0, dtype=None, **kwargs):
@ArgHelper.repeated_desc(name='shape', name_v2='dims')
def glorot_normal(shape, scale=2, mode='FAN_IN', dtype='float32', **kwargs):
def glorot_normal(shape, scale=2.0, mode='fan_in', dtype='float32', **kwargs):
r"""Return a tensor initialized from the glorot normal distribution.
The **GlorotNormal** distribution is defined as:
.. math:: X \sim N(0, \sqrt{\frac{scale}{\text{FAN}}})
.. math:: \text{out} \sim \mathcal{N}(0, \sqrt{\frac{scale}{\text{fan}}})
Parameters
----------
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
scale : number, optional, default=2
The scale factor of distribution.
mode : {'FAN_IN', 'FAN_OUT', 'FAN_AVG'}, optional
The mode to compute the normalizer.
The tensor shape.
mode : {'fan_in', 'fan_out', 'fan_avg'}, optional
The mode to compute fans.
scale : float, optional, default=2.0
The scale factor to distribution.
dtype : str, optional, default='float32'
The optional data type.
......@@ -266,25 +264,21 @@ def glorot_normal(shape, scale=2, mode='FAN_IN', dtype='float32', **kwargs):
@ArgHelper.repeated_desc(name='shape', name_v2='dims')
def glorot_uniform(shape, scale=3, mode='FAN_IN', dtype='float32', **kwargs):
def glorot_uniform(shape, mode='fan_in', scale=3.0, dtype='float32', **kwargs):
r"""Return a tensor initialized from the glorot uniform distribution.
The **GlorotUniform** distribution is defined as:
.. math::
X \sim U(
-\sqrt{\frac{scale}{\text{FAN}}},
\sqrt{\frac{scale}{\text{FAN}}}
)
\text{out} \sim \mathcal{U}(-\sqrt{\frac{scale}{\text{fan}}},
\sqrt{\frac{scale}{\text{fan}}})
Parameters
----------
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
scale : number, optional, default=3
The scale factor of distribution.
mode : {'FAN_IN', 'FAN_OUT', 'FAN_AVG'}, optional
The mode to compute the normalizer.
The tensor shape.
mode : {'fan_in', 'fan_out', 'fan_avg'}, optional
The mode to compute fans.
scale : float, optional, default=3.0
The scale factor to distribution.
dtype : str, optional, default='float32'
The optional data type.
......@@ -315,7 +309,7 @@ def glorot_uniform(shape, scale=3, mode='FAN_IN', dtype='float32', **kwargs):
def ones(shape, dtype='float32', **kwargs):
r"""Return a tensor filled with ones.
.. math:: y \leftarrow 1
.. math:: \text{out} \leftarrow 1
```python
x = dragon.ones(shape=(2, 3), dtype='float32')
......@@ -324,7 +318,7 @@ def ones(shape, dtype='float32', **kwargs):
Parameters
----------
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
The tensor shape.
dtype : str, optional, default='float32'
The optional data type.
......@@ -338,9 +332,9 @@ def ones(shape, dtype='float32', **kwargs):
def ones_like(other, dtype='float32', **kwargs):
r"""Return a tensor shaping like another filled with ones.
r"""Return a tensor of ones with shape as the other.
.. math:: y \leftarrow 1
.. math:: \text{out} \leftarrow 1
Examples:
......@@ -384,18 +378,16 @@ def ones_like(other, dtype='float32', **kwargs):
def random_normal(shape, mean=0, std=1, dtype='float32', **kwargs):
r"""Return a tensor initialized from the normal distribution.
The **RandomNormal** distribution is defined as:
.. math:: X \sim N(\mu, \sigma)
.. math:: \text{out} \sim \mathcal{N}(\mu, \sigma)
Parameters
----------
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
The tensor shape.
mean : number, optional, default=0
The value of :math:`\mu`.
The value to :math:`\mu`.
std : number, optional, default=1
The value of :math:`\sigma`.
The value to :math:`\sigma`.
dtype : str, optional, default='float32'
The optional data type.
......@@ -424,20 +416,18 @@ def random_normal(shape, mean=0, std=1, dtype='float32', **kwargs):
@OpSchema.num_inputs(1)
def random_normal_like(other, mean=0, std=1, dtype='float32', **kwargs):
r"""Return a tensor shaping like another initialized from the normal distribution.
r"""Return a tensor initialized from the normal distribution with shape as the other.
The **RandomNormal** distribution is defined as:
.. math:: X \sim N(\mu, \sigma)
.. math:: \text{out} \sim \mathcal{N}(\mu, \sigma)
Parameters
----------
other : dragon.Tensor
The tensor to hint the shape.
mean : number, optional, default=0
The value of :math:`\mu`.
The value to :math:`\mu`.
std : number, optional, default=1
The value of :math:`\sigma`.
The value to :math:`\sigma`.
dtype : str, optional, default='float32'
The optional data type.
......@@ -472,18 +462,16 @@ def random_normal_like(other, mean=0, std=1, dtype='float32', **kwargs):
def random_uniform(shape, low=-1, high=1, dtype='float32', **kwargs):
r"""Return a tensor initialized from the uniform distribution.
The **RandomUniform** distribution is defined as:
.. math:: X \sim U(\alpha, \beta)
.. math:: \text{out} \sim \mathcal{U}(\alpha, \beta)
Parameters
----------
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
The tensor shape.
low : number, optional, default=-1
The value of :math:`\alpha`.
The value to :math:`\alpha`.
high : number, optional, default=1
The value of :math:`\beta`.
The value to :math:`\beta`.
dtype : str, optional, default='float32'
The optional data type.
......@@ -511,20 +499,18 @@ def random_uniform(shape, low=-1, high=1, dtype='float32', **kwargs):
@OpSchema.num_inputs(1)
def random_uniform_like(other, low=-1, high=1, dtype='float32', **kwargs):
r"""Return a tensor shaping like another initialized from the uniform distribution.
r"""Return a tensor initialized from the uniform distribution with shape as the other.
The **RandomUniform** distribution is defined as:
.. math:: X \sim U(\alpha, \beta)
.. math:: \text{out} \sim \mathcal{U}(\alpha, \beta)
Parameters
----------
other : dragon.Tensor
The tensor to hint the shape.
low : number, optional, default=-1
The value of :math:`\alpha`.
The value to :math:`\alpha`.
high : number, optional, default=1
The value of :math:`\beta`.
The value to :math:`\beta`.
dtype : str, optional, default='float32'
The optional data type.
......@@ -558,18 +544,16 @@ def random_uniform_like(other, low=-1, high=1, dtype='float32', **kwargs):
def truncated_normal(shape, mean=0, std=1, dtype='float32', **kwargs):
r"""Return a tensor initialized from the truncated normal distribution.
The **TruncatedNormal** distribution is defined as:
.. math:: X \sim TN(\mu, \sigma, \mu - 2\sigma, \mu + 2\sigma)
.. math:: \text{out} \sim \mathcal{TN}(\mu, \sigma, \mu - 2\sigma, \mu + 2\sigma)
Parameters
----------
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
The tensor shape.
mean : number, optional, default=0
The value of :math:`\mu`.
The value to :math:`\mu`.
std : number, optional, default=1
The value of :math:`\sigma`.
The value to :math:`\sigma`.
dtype : str, optional, default='float32'
The optional data type.
......@@ -599,7 +583,7 @@ def truncated_normal(shape, mean=0, std=1, dtype='float32', **kwargs):
def zeros(shape, dtype='float32', **kwargs):
r"""Return a tensor filled with zeros.
.. math:: y \leftarrow 0
.. math:: \text{out} \leftarrow 0
```python
x = dragon.zeros(shape=(2, 3), dtype='float32')
......@@ -608,7 +592,7 @@ def zeros(shape, dtype='float32', **kwargs):
Parameters
----------
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
The tensor shape.
dtype : str, optional, default='float32'
The optional data type.
......@@ -623,9 +607,9 @@ def zeros(shape, dtype='float32', **kwargs):
@OpSchema.num_inputs(1)
def zeros_like(other, dtype='float32', **kwargs):
r"""Return a tensor shaping like another filled with zeros.
r"""Return a tensor of zeros with shape as the other.
.. math:: y \leftarrow 0
.. math:: \text{out} \leftarrow 0
Examples:
......
......@@ -90,7 +90,7 @@ class GlorotNormal(Initializer):
def __init__(self, key, dev, **kwargs):
super(GlorotNormal, self).__init__(key, dev, **kwargs)
self.scale = kwargs.get('scale', 2.)
self.mode = kwargs.get('mode', 'FAN_IN')
self.mode = kwargs.get('mode', 'fan_in')
def attributes(self):
return {
......@@ -110,7 +110,7 @@ class GlorotUniform(Initializer):
def __init__(self, key, dev, **kwargs):
super(GlorotUniform, self).__init__(key, dev, **kwargs)
self.scale = kwargs.get('scale', 3.)
self.mode = kwargs.get('mode', 'FAN_IN')
self.mode = kwargs.get('mode', 'fan_in')
def attributes(self):
return {
......
......@@ -144,9 +144,9 @@ def axpby(inputs, outputs=None, alpha=1., beta=1., **kwargs):
outputs : Union[dragon.Tensor, Sequence[dragon.Tensor]], optional
The tensor :math:`y`.
alpha : number, optional, default=1.
The value of :math:`\alpha`.
The value to :math:`\alpha`.
beta : number, optional, default=1.
The value of :math:`\beta`.
The value to :math:`\beta`.
Returns
-------
......@@ -301,9 +301,9 @@ def clip(inputs, low=None, high=None, **kwargs):
inputs : dragon.Tensor
The tensor :math:`x`.
low : number, optional
The value of :math:`\text{low}`.
The value to :math:`\text{low}`.
high : number, optional
The value of :math:`\text{high}`.
The value to :math:`\text{high}`.
Returns
-------
......
......@@ -58,7 +58,7 @@ def batch_norm(
momentum : float, optional, default=0.9
The momentum for running average.
eps : float, optional, default=1e-5
The value of :math:`\epsilon`.
The value to :math:`\epsilon`.
use_stats : int, optional, default=-1
Whether to use estimated statistics or not.
......@@ -111,7 +111,7 @@ def group_norm(inputs, axis=-1, group=32, eps=1e-5, **kwargs):
group : int, optional, default=32
The group size.
eps : float, optional, default=1e-5
The value of :math:`\epsilon`.
The value to :math:`\epsilon`.
Returns
-------
......@@ -156,7 +156,7 @@ def instance_norm(inputs, axis=-1, eps=1e-5, **kwargs):
axis : int, optional, default=-1
The channel axis.
eps : float, optional, default=1e-5
The value of :math:`\epsilon`.
The value to :math:`\epsilon`.
Returns
-------
......@@ -201,7 +201,7 @@ def lp_normalize(inputs, axis=None, p=2, eps=1e-12, reduction='sum', **kwargs):
axis : Union[int, Sequence[int]], optional
The axis to compute the norm.
eps : float, optional, default=1e-12
The value of :math:`\epsilon`.
The value to :math:`\epsilon`.
reduction : {'sum', 'mean'}, optional
The reduction method for norm.
......@@ -260,7 +260,7 @@ def layer_norm(inputs, axis=-1, eps=1e-5, **kwargs):
axis : int, optional, default=-1
The channel axis.
eps : float, optional, default=1e-5
The value of :math:`\epsilon`.
The value to :math:`\epsilon`.
Returns
-------
......@@ -369,7 +369,7 @@ def sync_batch_norm(
momentum : float, optional, default=0.9
The momentum for average.
eps : float, optional, default=1e-5
The value of :math:`\epsilon`.
The value to :math:`\epsilon`.
use_stats : int, optional, default=-1
Whether to use estimated statistics or not.
process_group : ProcessGroup, optional
......
......@@ -24,9 +24,7 @@ from dragon.core.ops import array_ops
def add(self, other):
r"""Compute the element-wise addition.
.. math:: \text{out} = \text{self} + \text{other}
"""Compute the element-wise addition.
Parameters
----------
......@@ -36,11 +34,7 @@ def add(self, other):
Returns
-------
dragon.Tensor
The **y**.
See Also
--------
`dragon.math.add(...)`_ : Compute the element-wise addition.
The output tensor.
"""
return _binary_op(self, other, 'Add')
......@@ -90,9 +84,7 @@ def copy(self):
def div(self, other):
r"""Compute the element-wise division.
.. math:: \text{out} = \text{self} \div \text{other}
"""Compute the element-wise division.
Parameters
----------
......@@ -104,18 +96,12 @@ def div(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.div(...)`_ : Compute the element-wise division.
"""
return _binary_op(self, other, 'Div')
def ge(self, other):
r"""Compute element-wise greater-equal comparison.
.. math:: \text{out} = (\text{self} \geq \text{other})
"""Compute element-wise greater-equal comparison.
Parameters
----------
......@@ -127,35 +113,23 @@ def ge(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.greater_equal(...)`_ : Compute element-wise greater-equal comparison.
"""
return _binary_op(self, other, 'GreaterEqual')
def getitem(self, item):
"""Select the elements at the specific indices.
"""Select elements at the specific index.
Parameters
----------
item : Union[int, slice, dragon.Tensor]
The indices.
The index.
Returns
-------
dragon.Tensor
The output tensor.
See Also
--------
`dragon.slice(...)`_ : Select the elements according to the given sections.
See Also
--------
`dragon.masked_select(...)`_ : Select the elements where the given mask is 1.
"""
if isinstance(item, Tensor):
return _masked_select(self, item)
......@@ -165,21 +139,19 @@ def getitem(self, item):
def get_value(self):
"""Copy the data from storage.
"""Return the value of implementation.
Returns
-------
numpy.ndarray
The deep copied value.
The deep-copied value.
"""
return workspace.get_workspace().fetch_tensor(self)
def gt(self, other):
r"""Compute element-wise greater comparison.
.. math:: \text{out} = (\text{self} > \text{other})
"""Compute element-wise greater comparison.
Parameters
----------
......@@ -191,18 +163,12 @@ def gt(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.greater(...)`_ : Compute element-wise greater comparison.
"""
return _binary_op(self, other, 'Greater')
def le(self, other):
r"""Compute element-wise less-equal comparison.
.. math:: \text{out} = (\text{self} \leq \text{other})
"""Compute element-wise less-equal comparison.
Parameters
----------
......@@ -214,18 +180,12 @@ def le(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.less_equal(...)`_ : Compute element-wise less-equal comparison.
"""
return _binary_op(self, other, 'LessEqual')
def lt(self, other):
r"""Compute element-wise less comparison.
.. math:: \text{out} = (\text{self} < \text{other})
"""Compute element-wise less comparison.
Parameters
----------
......@@ -237,18 +197,12 @@ def lt(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.less(...)`_ : Compute element-wise less comparison.
"""
return _binary_op(self, other, 'Less')
def mul(self, other):
r"""Compute the element-wise multiplication.
.. math:: \text{out} = \text{self} \times \text{other}
"""Compute the element-wise multiplication.
Parameters
----------
......@@ -260,36 +214,24 @@ def mul(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.mul(...)`_ : Compute the element-wise multiplication.
"""
return _binary_op(self, other, 'Mul')
def neg(self):
r"""Compute the element-wise negative.
.. math:: y = -x
"""Compute the element-wise negative.
Returns
-------
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.negative(...)`_ : Compute the element-wise negative.
"""
return _unary_op(self, 'Neg')
def radd(self, other):
r"""Compute the element-wise addition.
.. math:: \text{out} = \text{other} + \text{self}
"""Compute the element-wise addition.
Parameters
----------
......@@ -301,18 +243,12 @@ def radd(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.add(...)`_ : Compute the element-wise addition.
"""
return _binary_op(other, self, 'Add')
def rdiv(self, other):
r"""Compute the element-wise division.
.. math:: \text{out} = \text{other} \div \text{self}
"""Compute the element-wise division.
Parameters
----------
......@@ -324,10 +260,6 @@ def rdiv(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.div(...)`_ : Compute the element-wise division.
"""
return _binary_op(other, self, 'Div')
......@@ -355,9 +287,7 @@ def reshape(self, shape):
def rmul(self, other):
r"""Compute the element-wise multiplication.
.. math:: \text{out} = \text{other} \times \text{self}
"""Compute the element-wise multiplication.
Parameters
----------
......@@ -369,18 +299,12 @@ def rmul(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.mul(...)`_ : Compute the element-wise multiplication.
"""
return _binary_op(other, self, 'Mul')
def rsub(self, other):
r"""Compute the element-wise subtraction.
.. math:: \text{out} = \text{other} - \text{self}
"""Compute the element-wise subtraction.
Parameters
----------
......@@ -392,31 +316,19 @@ def rsub(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.sub(...)`_ : Compute the element-wise subtraction.
"""
return _binary_op(other, self, 'Sub')
def setitem(self, key, value):
"""Set the value at the specific indices.
"""Set elements at the specific index.
Parameters
----------
key : Union[int, slice, dragon.Tensor]
The indices.
value : number or dragon.Tensor
The value.
See Also
--------
`dragon.assign(...)`_ : Assign the value to ref.
See Also
--------
`dragon.masked_assign(...)`_ : Assign the value to ref where mask is 1.
The index.
value : Union[dragon.Tensor, number]
The value to set.
"""
if isinstance(key, Tensor):
......@@ -427,12 +339,12 @@ def setitem(self, key, value):
def set_value(self, value):
"""Feed the const value to storage.
"""Set value to the implementation.
Parameters
----------
value : array_like
The const value.
The value to set.
Returns
-------
......@@ -445,9 +357,7 @@ def set_value(self, value):
def sub(self, other):
r"""Compute the element-wise subtraction.
.. math:: \text{out} = \text{self} - \text{value}
"""Compute the element-wise subtraction.
Parameters
----------
......@@ -459,10 +369,6 @@ def sub(self, other):
dragon.Tensor
The output tensor.
See Also
--------
`dragon.math.sub(...)`_ : Compute the element-wise subtraction.
"""
return _binary_op(self, other, 'Sub')
......@@ -547,7 +453,6 @@ Tensor.get_value = get_value
Tensor.reshape = reshape
Tensor.set_value = set_value
Tensor.__add__ = add
Tensor.__div__ = div
Tensor.__ge__ = ge
Tensor.__getitem__ = getitem
Tensor.__gt__ = gt
......@@ -556,7 +461,6 @@ Tensor.__lt__ = lt
Tensor.__mul__ = mul
Tensor.__neg__ = neg
Tensor.__radd__ = radd
Tensor.__rdiv__ = rdiv
Tensor.__rmul__ = rmul
Tensor.__rtruediv__ = rdiv
Tensor.__rsub__ = rsub
......
......@@ -23,7 +23,7 @@ from dragon.core.framework import workspace
def constant(value, dtype=None, shape=None, name='Const'):
r"""Return a tensor taking the value content.
"""Return a tensor initialized from the value.
Examples:
......@@ -47,7 +47,7 @@ def constant(value, dtype=None, shape=None, name='Const'):
Returns
-------
dragon.Tensor
The constant tensor.
The output tensor.
"""
if dtype is not None:
......@@ -82,7 +82,7 @@ def constant(value, dtype=None, shape=None, name='Const'):
else:
return TensorRef(
name=workspace.get_workspace().unique_name(
name, ':0', 'dragon.Tensor'),
name, ':0', 'Tensor'),
shape=list(value.shape),
dtype=str(value.dtype),
).set_value(value)
......@@ -47,7 +47,7 @@ def elu(x, alpha=1., **kwargs):
x : dragon.Tensor
The tensor :math:`x`.
alpha : float, optional, default=1.
The value of :math:`\alpha`.
The value to :math:`\alpha`.
Returns
-------
......@@ -141,7 +141,7 @@ def relu(x, alpha=0, max_value=None, **kwargs):
alpha : number, optional, default=0
The valve of :math:`\alpha`.
max_value : number, optional
The value of :math:`v_{max}`.
The value to :math:`v_{max}`.
"""
if max_value is not None:
......
......@@ -43,7 +43,7 @@ def Input(
x = tf.keras.Input(shape=(8,), batch_size=8, dtype='float32')
# Create a placeholder aliasing an existing tensor
x = dragon.Tensor('x', shape=(8,), dtype='float32').variable()
x = dragon.Tensor('x', shape=(8,), dtype='float32').constant()
xx = tf.keras.Input(tensor=x)
```
......
......@@ -49,7 +49,7 @@ class ELU(Layer):
Parameters
----------
alpha : float, optional, default=0.3
The value of :math:`\alpha`.
The value to :math:`\alpha`.
"""
super(ELU, self).__init__(**kwargs)
......@@ -92,7 +92,7 @@ class LeakyReLU(Layer):
Parameters
----------
alpha : float, optional, default=0.3
The value of :math:`\alpha`.
The value to :math:`\alpha`.
"""
super(LeakyReLU, self).__init__(**kwargs)
......@@ -135,9 +135,9 @@ class ReLU(Layer):
Parameters
----------
max_value : number, optional
The value of :math:`v_{max}`.
The value to :math:`v_{max}`.
negative_slope : float, optional, default=0.
The value of :math:`\alpha`.
The value to :math:`\alpha`.
"""
super(ReLU, self).__init__(**kwargs)
......
......@@ -26,6 +26,16 @@ class Loss(object):
"""The base class for loss criterion."""
def __init__(self, reduction=losses_utils.Reduction.MEAN, name=None):
"""Create a ``Loss`` criterion.
Parameters
----------
reduction : {'none', 'sum', 'mean', 'valid'}, optional
The reduction method.
name : str, optional
A optional name for the operation.
"""
losses_utils.Reduction.validate(reduction)
self.reduction = reduction
self.name = name
......
......@@ -21,7 +21,7 @@ from dragon.vm.tensorflow.core.keras.optimizer import optimizer
class Adam(optimizer.Optimizer):
r"""The optimizer which implements Adam algorithm.
r"""The optimizer to apply Adam algorithm.
`[Kingma & Ba, 2014] <https://arxiv.org/abs/1412.6980>`_.
The **Adam** update is defined as:
......
......@@ -21,7 +21,7 @@ from dragon.vm.tensorflow.core.keras.optimizer import optimizer
class SGD(optimizer.Optimizer):
r"""The optimizer which implements SGD algorithm.
r"""The optimizer to apply SGD algorithm.
Following SGD algorithms are supported:
......
......@@ -17,7 +17,7 @@ from dragon.vm.tensorflow.core.keras.optimizer import optimizer
class RMSprop(optimizer.Optimizer):
r"""The optimizer which implements RMSprop algorithm.
r"""The optimizer to apply RMSprop algorithm.
`[Hinton et.al, 2013] <http://www.cs.utoronto.ca/~bonner/courses/2016s/csc321/lectures/lec6.pdf>`_.
The **RMSprop** update is defined as:
......
......@@ -56,9 +56,9 @@ class L1L2(Regularizer):
Parameters
----------
l1 : float, optional, default=0.01
The value of :math:`\alpha`.
The value to :math:`\alpha`.
l2 : float, optional, default=0.01
The value of :math:`\beta`.
The value to :math:`\beta`.
"""
if l1 <= 0. or l2 <= 0.:
......@@ -90,7 +90,7 @@ def l1(l=0.01):
Parameters
----------
l : float, optional, default=0.01
The value of :math:`\alpha`.
The value to :math:`\alpha`.
Returns
-------
......@@ -111,9 +111,9 @@ def l1_l2(l1=0.01, l2=0.01):
Parameters
----------
l1 : float, optional, default=0.01
The value of :math:`\alpha`.
The value to :math:`\alpha`.
l2 : float, optional, default=0.01
The value of :math:`\beta`.
The value to :math:`\beta`.
Returns
-------
......@@ -134,7 +134,7 @@ def l2(l=0.01):
Parameters
----------
l : float, optional, default=0.01
The value of :math:`\beta`.
The value to :math:`\beta`.
Returns
-------
......
......@@ -19,5 +19,4 @@ from __future__ import print_function
from dragon.vm.tensorflow.core.ops.losses import loss_reduction
Reduction = loss_reduction.Reduction
......@@ -183,9 +183,9 @@ def expand_dims(input, axis, name=None):
def fill(dims, value=0, dtype=None, name=None):
r"""Return a tensor filled with the specific value.
r"""Return a tensor filled with the scalar value.
.. math:: y \leftarrow \text{Constant}
.. math:: \text{out} \leftarrow \text{value}
Examples:
......@@ -298,7 +298,7 @@ def identity(input, name=None):
def ones(shape, dtype='float32', name=None):
r"""Return a tensor filled with ones.
.. math:: y \leftarrow 1
.. math:: \text{out} \leftarrow 1
```python
x = tf.ones(shape=(2, 3), dtype=tf.float32)
......@@ -318,9 +318,9 @@ def ones(shape, dtype='float32', name=None):
def ones_like(input, dtype='float32', name=None):
r"""Return a tensor shaping like another filled with ones.
r"""Return a tensor of ones with shape as the other.
.. math:: y \leftarrow 1
.. math:: \text{out} \leftarrow 1
Examples:
......@@ -349,14 +349,14 @@ def one_hot(
off_value=0,
name=None,
):
r"""Return the one-hot representation from indices.
r"""Return the one-hot representation for input.
.. math::
y[i][j] =
\begin{cases}
\text{Val}_{off}, & \text{ if } \text{indices}[i] \neq j \\
\text{Val}_{on}, & \text{ otherwise }
\end{cases}
\text{out}[i][j] =
\begin{cases}
\text{off\_value}, & \text{ if } \text{input}[i] \neq j \\
\text{on\_value}, & \text{ otherwise }
\end{cases}
The max value of indices, i.e., the ``depth`` should be specified:
......@@ -374,7 +374,7 @@ def one_hot(
Parameters
----------
indices : dragon.Tensor
The tensor ``indices``.
The input tensor.
depth : int
The depth of representation.
on_value : int, optional, default=1
......@@ -390,7 +390,13 @@ def one_hot(
The output tensor.
"""
return array_ops.one_hot(indices, depth, on_value, off_value, name=name)
return array_ops.one_hot(
indices,
depth=depth,
on_value=on_value,
off_value=off_value,
name=name,
)
def pad(
......@@ -483,7 +489,7 @@ def placeholder(dtype=None, shape=None, name=None):
suffix=':0', namespace='Tensor'),
dtype=str(dtype) if dtype else dtype,
shape=shape,
).placeholder()
).constant()
def reshape(tensor, shape, name=None):
......@@ -752,7 +758,7 @@ def transpose(a, perm=None, name=None):
def zeros(shape, dtype='float32', name=None):
r"""Return a tensor filled with zeros.
.. math:: y \leftarrow 0
.. math:: \text{out} \leftarrow 0
```python
x = tf.zeros(shape=(2, 3), dtype=tf.float32)
......@@ -772,9 +778,9 @@ def zeros(shape, dtype='float32', name=None):
def zeros_like(input, dtype='float32', name=None):
r"""Return a tensor shaping like another filled with zeros.
r"""Return a tensor of zeros with shape as the other.
.. math:: y \leftarrow 0
.. math:: \text{out} \leftarrow 0
Examples:
......
......@@ -42,9 +42,9 @@ def clip_by_value(
t : dragon.Tensor
The tensor :math:`x`.
clip_value_min : number, optional
The value of :math:`\text{low}`.
The value to :math:`\text{low}`.
clip_value_max : number, optional
The value of :math:`\text{high}`.
The value to :math:`\text{high}`.
name : str, optional
A optional name for the operation.
......
......@@ -43,7 +43,11 @@ class Initializer(object):
class Constant(Initializer):
"""Fill tensors with a scalar."""
r"""Fill tensor with a scalar value.
.. math:: \text{tensor} \leftarrow \text{value}
"""
def __init__(self, value=0, dtype='float32'):
"""Create a ``Constant`` initializer.
......@@ -78,19 +82,27 @@ class Constant(Initializer):
return init_ops.fill(shape, value=self.value, dtype=dtype)
class Ones(Initializer):
"""Fill tensors with ones."""
class RandomNormal(Initializer):
r"""Fill tensor from a normal distribution.
def __init__(self, dtype='float32'):
"""Create a ``Ones`` initializer.
.. math:: \text{tensor} \sim \mathcal{N}(\mu, \sigma)
"""
def __init__(self, mean=0, stddev=1, dtype='float32'):
r"""Create a ``RandomNormal`` initializer.
Parameters
----------
mean : number, optional, default=0
The value to :math:`\mu`.
stddev : number, optional, default=1
The value to :math:`\sigma`.
dtype : str, optional, default='float32'
The data type to set as default.
"""
self.dtype = dtype
self.mean, self.stddev, self.dtype = mean, stddev, dtype
def __call__(self, shape, dtype=None, **kwargs):
"""Return a tensor initialized from the initializer.
......@@ -108,22 +120,30 @@ class Ones(Initializer):
The output tensor.
"""
dtype = str(self.dtype) if dtype is None else str(dtype)
return init_ops.fill(shape, value=1, dtype=str(dtype))
return init_ops.random_normal(
shape=shape,
mean=self.mean,
std=self.stddev,
dtype=str(self.dtype) if dtype is None else str(dtype),
)
class RandomUniform(Initializer):
"""Fill tensors according to a uniform distribution."""
r"""Fill tensor from an uniform distribution.
.. math:: \text{tensor} \sim \mathcal{U}(\alpha, \beta)
"""
def __init__(self, minval=0, maxval=1, dtype='float32'):
"""Create a ``RandomUniform`` initializer.
r"""Create a ``RandomUniform`` initializer.
Parameters
----------
minval : number, optional, default=0
The lower bound of distribution.
The value to :math:`\alpha`.
maxval : number, optional, default=1
The higher bound of distribution.
The value to :math:`\beta`.
dtype : str, optional, default='float32'
The data type to set as default.
......@@ -155,67 +175,22 @@ class RandomUniform(Initializer):
)
class RandomNormal(Initializer):
"""Fill tensors according to a normal distribution."""
def __init__(self, mean=0, stddev=1, dtype='float32'):
"""Create a ``RandomNormal`` initializer.
Parameters
----------
mean : number, optional, default=0
The mean of distribution.
stddev : number, optional, default=1
The stddev of distribution.
dtype : str, optional, default='float32'
The data type to set as default.
"""
self.mean, self.stddev, self.dtype = mean, stddev, dtype
def __call__(self, shape, dtype=None, **kwargs):
"""Return a tensor initialized from the initializer.
Parameters
----------
shape : Sequence[int]
The tensor shape.
dtype : str, optional
The optional data type.
Returns
-------
dragon.Tensor
The output tensor.
"""
return init_ops.random_normal(
shape=shape,
mean=self.mean,
std=self.stddev,
dtype=str(self.dtype) if dtype is None else str(dtype),
)
class TruncatedNormal(Initializer):
r"""Fill tensors according to a truncated normal distribution.
The **TruncatedNormal** distribution is defined as:
r"""Fill tensor from a truncated normal distribution.
.. math::
X \sim TN(\mu, \sigma, \mu - 2\sigma, \mu + 2\sigma)
.. math:: \text{tensor} \sim \mathcal{TN}(\mu, \sigma, \mu - 2\sigma, \mu + 2\sigma)
"""
def __init__(self, mean=0, stddev=1, dtype='float32'):
"""Create a ``TruncatedNormal`` initializer.
r"""Create a ``TruncatedNormal`` initializer.
Parameters
----------
mean : number, optional, default=0
The mean of distribution.
The value to :math:`\mu`.
stddev : number, optional, default=1
The stddev of distribution.
The value to :math:`\sigma`.
dtype : str, optional, default='float32'
The data type to set as default.
......@@ -247,11 +222,11 @@ class TruncatedNormal(Initializer):
class VarianceScaling(Initializer):
"""Fill tensors with the random values adapting to shape."""
"""Fill tensor from a scaled random distribution."""
def __init__(
self,
scale=1.,
scale=1.0,
mode='fan_in',
distribution='normal',
dtype='float32',
......@@ -260,8 +235,8 @@ class VarianceScaling(Initializer):
Parameters
----------
scale : float, optional, default=1.
The scale factor applied to distribution.
scale : float, optional, default=1
The scale factor to distribution.
mode : {'fan_in', 'fan_out', 'fan_avg'}, optional
The mode for adapting to shape.
distribution : {'normal', 'uniform'}, optional
......@@ -271,15 +246,13 @@ class VarianceScaling(Initializer):
"""
if scale <= 0.:
raise ValueError("`scale` must be positive float.")
if mode not in {"fan_in", "fan_out", "fan_avg"}:
raise ValueError("Invalid `mode` argument:", mode)
raise ValueError('<scale> must be positive float.')
mode = mode.lower()
if mode not in {'fan_in', 'fan_out', 'fan_avg'}:
raise ValueError('Invalid <mode> argument:', mode)
distribution = distribution.lower()
if distribution not in {"normal", "uniform"}:
if distribution not in {'normal', 'uniform'}:
raise ValueError("Invalid `distribution` argument:", distribution)
self.scale = scale
self.mode = mode
self.distribution = distribution
......@@ -304,21 +277,25 @@ class VarianceScaling(Initializer):
if self.distribution == 'normal':
return init_ops.glorot_normal(
shape=shape,
scale=self.scale * 2.,
mode=self.mode,
scale=self.scale * 2.0,
dtype=str(self.dtype) if dtype is None else str(dtype)
)
else:
return init_ops.glorot_uniform(
shape=shape,
scale=self.scale * 3.,
mode=self.mode,
scale=self.scale * 3.0,
dtype=str(self.dtype) if dtype is None else str(dtype)
)
class GlorotNormal(VarianceScaling):
"""Fill tensors according to a glorot normal distribution."""
r"""Fill tensor from a glorot normal distribution.
.. math:: \text{tensor} \sim \mathcal{N}(0, \sqrt{\frac{2}{\text{fan\_avg}}})
"""
def __init__(self, dtype='float32'):
"""Create a ``GlorotNormal`` initializer.
......@@ -330,7 +307,7 @@ class GlorotNormal(VarianceScaling):
"""
super(GlorotNormal, self).__init__(
scale=1.,
scale=1.0,
mode='fan_avg',
distribution='normal',
dtype=dtype,
......@@ -338,7 +315,12 @@ class GlorotNormal(VarianceScaling):
class GlorotUniform(VarianceScaling):
"""Fill tensors according to a glorot uniform distribution."""
r"""Fill tensor from a glorot uniform distribution.
.. math:: \text{tensor} \sim \mathcal{U}(-\sqrt{\frac{3}{\text{fan\_avg}}},
\sqrt{\frac{3}{\text{fan\_avg}}})
"""
def __init__(self, dtype='float32'):
"""Create a ``GlorotUniform`` initializer.
......@@ -357,8 +339,50 @@ class GlorotUniform(VarianceScaling):
)
class Ones(Initializer):
r"""Fill tensor with ones.
.. math:: \text{tensor} \leftarrow 1
"""
def __init__(self, dtype='float32'):
"""Create a ``Ones`` initializer.
Parameters
----------
dtype : str, optional, default='float32'
The data type to set as default.
"""
self.dtype = dtype
def __call__(self, shape, dtype=None, **kwargs):
"""Return a tensor initialized from the initializer.
Parameters
----------
shape : Sequence[int]
The tensor shape.
dtype : str, optional
The optional data type.
Returns
-------
dragon.Tensor
The output tensor.
"""
dtype = str(self.dtype) if dtype is None else str(dtype)
return init_ops.fill(shape, value=1, dtype=str(dtype))
class Zeros(Initializer):
"""Fill tensors with zeros."""
r"""Fill tensor with zeros.
.. math:: \text{tensor} \leftarrow 0
"""
def __init__(self, dtype='float32'):
"""Create a ``Zeros`` initializer.
......
......@@ -846,7 +846,7 @@ def pow(x, y, name=None):
def range(start, limit=None, delta=1, dtype='int64', name=None):
r"""Return a tensor with evenly spaced values within a interval.
r"""Return a tensor of evenly spaced values within a interval.
Specify ``start`` and ``limit`` to determine an interval:
......
......@@ -116,7 +116,7 @@ def l2_normalize(x, axis=None, epsilon=1e-12, name=None):
axis : Union[int, Sequence[int]], optional
The axis to compute norm.
epsilon : float, optional, default=1e-5
The value of :math:`\epsilon`.
The value to :math:`\epsilon`.
name : str, optional
A optional name for the operation.
......
......@@ -493,7 +493,7 @@ def elu(features, alpha=1., name=None, **kwargs):
features : dragon.Tensor
The tensor :math:`x`.
alpha : float, optional, default=1.
The value of :math:`\alpha`.
The value to :math:`\alpha`.
name : str, optional
A optional name for the operation.
......@@ -527,7 +527,7 @@ def leaky_relu(features, alpha=0.2, name=None, **kwargs):
features : dragon.Tensor
The input tensor.
alpha : number, optional, default=0.2
The value of :math:`\alpha`.
The value to :math:`\alpha`.
name : str, optional
A optional name for the operation.
......
......@@ -35,9 +35,9 @@ def random_normal(
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
mean : number, optional, default=0
The value of :math:`\mu`.
The value to :math:`\mu`.
stddev : number, optional, default=1
The value of :math:`\sigma`.
The value to :math:`\sigma`.
dtype : str, optional
The optional data type.
seed : int, optional
......@@ -74,9 +74,9 @@ def random_uniform(
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
minval : number, optional, default=0
The value of :math:`\alpha`.
The value to :math:`\alpha`.
maxval : number, optional, default=1
The value of :math:`\beta`.
The value to :math:`\beta`.
dtype : str, optional
The optional data type.
seed : int, optional
......@@ -114,9 +114,9 @@ def truncated_normal(
shape : Sequence[Union[int, dragon.Tensor]]
The shape of the tensor.
mean : number, optional, default=0
The value of :math:`\mu`.
The value to :math:`\mu`.
stddev : number, optional, default=1
The value of :math:`\sigma`.
The value to :math:`\sigma`.
dtype : str, optional
The optional data type.
seed : int, optional
......
......@@ -46,7 +46,7 @@ def leaky_relu(x, alpha=0.2, name="leaky_relu", **kwargs):
x : dragon.Tensor
The input tensor.
alpha : float, optional, default=0.2
The value of :math:`\alpha`.
The value to :math:`\alpha`.
name : str, optional
The optional operator name.
......
......@@ -50,9 +50,9 @@ class Initializer(object):
class Constant(Initializer):
r"""Fill tensors with a scalar value.
r"""Fill tensor with a scalar value.
.. math:: y \leftarrow \text{Value}
.. math:: \text{tensor} \leftarrow \text{value}
"""
......@@ -78,32 +78,30 @@ class Constant(Initializer):
class GlorotNormal(Initializer):
r"""Fill tensors according to a glorot normal distribution.
r"""Fill tensor from a glorot normal distribution.
The **GlorotNormal** distribution is defined as:
.. math:: X \sim N(0, \sqrt{\frac{\text{scale}}{\text{FAN}}})
.. math:: \text{tensor} \sim \mathcal{N}(0, \sqrt{\frac{\text{scale}}{\text{fan}}})
"""
def __init__(self, scale=2., mode='FAN_IN'):
def __init__(self, mode='fan_in', scale=2.0):
"""Create a ``GlorotNormal`` initializer.
Parameters
----------
scale : float, optional, default=2.
The scale factor of distribution.
mode : {'FAN_IN', 'FAN_OUT', 'FAN_AVG'}, optional
The mode to compute the normalizer.
mode : {'fan_in', 'fan_out', 'fan_avg'}, optional
The mode to compute the fans.
scale : float, optional, default=2.0
The scale factor to distribution.
"""
self.scale, self.mode = scale, mode
self.mode, self.scale = mode, scale
def __call__(self, shape, dtype='float32', **kwargs):
return self._getter(
init_ops.glorot_normal,
scale=self.scale,
mode=self.mode,
scale=self.scale,
shape=shape,
dtype=dtype,
**kwargs
......@@ -111,37 +109,32 @@ class GlorotNormal(Initializer):
class GlorotUniform(Initializer):
r"""Fill tensors according to a glorot uniform distribution.
r"""Fill tensor from a glorot uniform distribution.
The **GlorotUniform** distribution is defined as:
.. math::
X \sim U(
-\sqrt{\frac{\text{scale}}{\text{FAN}}},
\sqrt{\frac{\text{scale}}{\text{FAN}}}
)
.. math:: \text{tensor} \sim \mathcal{U}(-\sqrt{\frac{\text{scale}}{\text{fan}}},
\sqrt{\frac{\text{scale}}{\text{fan}}})
"""
def __init__(self, scale=3., mode='FAN_IN'):
def __init__(self, mode='fan_in', scale=3.0):
"""Create a ``GlorotUniform`` initializer.
Parameters
----------
scale : float, optional, default=3.
The scale factor of distribution.
mode : {'FAN_IN', 'FAN_OUT', 'FAN_AVG'}, optional
The mode to compute the normalizer.
mode : {'fan_in', 'fan_out', 'fan_avg'}, optional
The mode to compute the fans.
scale : float, optional, default=3.0
The scale factor to distribution.
"""
super(GlorotUniform, self).__init__()
self.scale, self.mode = scale, mode
self.mode, self.scale = mode, scale
def __call__(self, shape, dtype='float32', **kwargs):
return self._getter(
init_ops.glorot_uniform,
scale=self.scale,
mode=self.mode,
scale=self.scale,
shape=shape,
dtype=dtype,
**kwargs
......@@ -149,14 +142,14 @@ class GlorotUniform(Initializer):
class Ones(Initializer):
r"""Fill tensors with ones.
r"""Fill tensor with ones.
.. math:: y \leftarrow 0
.. math:: \text{tensor} \leftarrow 1
"""
def __init__(self):
"""Create a ``Zeros`` initializer."""
"""Create a ``Ones`` initializer."""
super(Ones, self).__init__()
def __call__(self, shape, dtype='float32', **kwargs):
......@@ -170,11 +163,9 @@ class Ones(Initializer):
class RandomNormal(Initializer):
r"""Fill tensors according to a random normal distribution.
The **RandomNormal** distribution is defined as:
r"""Fill tensor from a normal distribution.
.. math:: X \sim N(\mu, \sigma)
.. math:: \text{tensor} \sim \mathcal{N}(\mu, \sigma)
"""
......@@ -184,9 +175,9 @@ class RandomNormal(Initializer):
Parameters
----------
mean : number, optional, default=0.
The value of :math:`\mu`.
The value to :math:`\mu`.
stddev : number, optional, default=0.05
The value of :math:`\sigma`.
The value to :math:`\sigma`.
"""
self.mean, self.stddev = mean, stddev
......@@ -203,11 +194,9 @@ class RandomNormal(Initializer):
class RandomUniform(Initializer):
r"""Fill tensors according to a random uniform distribution.
r"""Fill tensors from an uniform distribution.
The **RandomUniform** distribution is defined as:
.. math:: X \sim U(\alpha, \beta)
.. math:: \text{tensor} \sim \mathcal{U}(\alpha, \beta)
"""
......@@ -217,9 +206,9 @@ class RandomUniform(Initializer):
Parameters
----------
minval : number, optional, default=-0.05
The value of :math:`\alpha`.
The value to :math:`\alpha`.
maxval : number, optional, default=0.05
The value of :math:`\beta`.
The value to :math:`\beta`.
"""
self.minval, self.maxval = minval, maxval
......@@ -236,12 +225,9 @@ class RandomUniform(Initializer):
class TruncatedNormal(Initializer):
r"""Fill tensors according to a truncated normal distribution.
The **TruncatedNormal** distribution is defined as:
r"""Fill tensor from a truncated normal distribution.
.. math::
X \sim TN(\mu, \sigma, \mu - 2\sigma, \mu + 2\sigma)
.. math:: \text{tensor} \sim \mathcal{TN}(\mu, \sigma, \mu - 2\sigma, \mu + 2\sigma)
"""
......@@ -251,9 +237,9 @@ class TruncatedNormal(Initializer):
Parameters
----------
mean : number, optional, default=0.
The value of :math:`\mu`.
The value to :math:`\mu`.
stddev : number, optional, default=0.05
The value of :math:`\sigma`.
The value to :math:`\sigma`.
"""
self.mean, self.stddev = mean, stddev
......@@ -270,9 +256,9 @@ class TruncatedNormal(Initializer):
class Zeros(Initializer):
r"""Fill tensors with zeros.
r"""Fill tensor with zeros.
.. math:: y \leftarrow 1
.. math:: \text{tensor} \leftarrow 0
"""
......
......@@ -46,7 +46,7 @@ class TestTensor(unittest.TestCase):
self.assertNotEqual(a.__hash__(), b.__hash__())
self.assertNotEqual(a.__repr__(), b.__repr__())
self.assertNotEqual(b.__repr__(), dragon.EagerTensor([2]).__repr__())
self.assertEqual(int(a.variable().placeholder().set_value(1)), 1)
self.assertEqual(int(a.constant().set_value(1)), 1)
self.assertEqual(float(dragon.Tensor.convert_to(1)), 1.)
self.assertEqual(int(b.set_value(1)), 1)
self.assertEqual(float(b), 1.)
......@@ -73,6 +73,8 @@ class TestTensor(unittest.TestCase):
with dragon.name_scope(''):
b.name = 'b'
self.assertEqual(b.name, 'b')
b.requires_grad = True
self.assertEqual(b.requires_grad, True)
def test_dlpack_converter(self):
data = np.array([0., 1., 2.], 'float32')
......@@ -101,14 +103,56 @@ class TestTensor(unittest.TestCase):
class TestWorkspace(unittest.TestCase):
"""Test the workspace class."""
def test_clear(self):
w = dragon.Workspace()
with w.as_default():
x = dragon.EagerTensor(1)
self.assertEqual(x.size, 1)
w.clear()
self.assertEqual(x.size, 0)
def test_feed_tensor(self):
w = dragon.Workspace()
with w.as_default():
v1, v2 = dragon.EagerTensor(1), np.array(2)
x = dragon.Tensor('test_feed_tensor/x')
w.feed_tensor(x, v1)
self.assertEqual(int(x), 1)
w.feed_tensor(x, v2)
self.assertEqual(int(x), 2)
def test_merge_form(self):
w1, w2 = dragon.Workspace(), dragon.Workspace()
with w1.as_default():
x = dragon.Tensor(str(id(w1))).set_value(0)
x = dragon.Tensor('test_merge_from/x').set_value(0)
w2.merge_from(w1)
with w2.as_default():
self.assertEqual(int(x), 0)
def test_register_alias(self):
w = dragon.Workspace()
with w.as_default():
x = dragon.EagerTensor(1)
w.register_alias(x.id, 'test_register_alias/y')
self.assertEqual(int(w.fetch_tensor('test_register_alias/y')), 1)
def test_reset_tensor(self):
w = dragon.Workspace()
with w.as_default():
x = dragon.EagerTensor(1)
self.assertEqual(x.size, 1)
w.reset_tensor(x)
self.assertEqual(x.size, 0)
def test_reset_workspace(self):
w = dragon.Workspace()
with w.as_default():
try:
dragon.reset_workspace()
except AssertionError:
pass
dragon.reset_workspace()
if __name__ == '__main__':
run_tests()
......@@ -2706,7 +2706,7 @@ class TestTensorOps(OpTestCase):
for a_shape, b_shape in self.binary_test_shapes:
data1, data2 = arange(a_shape, 1), arange(b_shape)
a, b = new_tensor(data1), new_tensor(data2)
self.assertEqual(a.__rdiv__(b), data2 / data1)
self.assertEqual(a.__rtruediv__(b), data2 / data1)
def test_reshape(self):
entries = [(0, 0), (0, -1)]
......
......@@ -43,7 +43,6 @@ from dragon.vm.torch.ops import tensorbind as _
from dragon.vm.torch.ops.array.functional import argmax
from dragon.vm.torch.ops.array.functional import argmin
from dragon.vm.torch.ops.array.functional import assign
from dragon.vm.torch.ops.array.functional import cast
from dragon.vm.torch.ops.array.functional import cat
from dragon.vm.torch.ops.array.functional import channel_normalize
from dragon.vm.torch.ops.array.functional import channel_shuffle
......@@ -72,12 +71,10 @@ from dragon.vm.torch.ops.array.functional import unsqueeze
from dragon.vm.torch.ops.array.functional import where
from dragon.vm.torch.ops.init.functional import arange
from dragon.vm.torch.ops.init.functional import eye
from dragon.vm.torch.ops.init.functional import normal
from dragon.vm.torch.ops.init.functional import ones
from dragon.vm.torch.ops.init.functional import ones_like
from dragon.vm.torch.ops.init.functional import rand
from dragon.vm.torch.ops.init.functional import randn
from dragon.vm.torch.ops.init.functional import uniform
from dragon.vm.torch.ops.init.functional import zeros
from dragon.vm.torch.ops.init.functional import zeros_like
from dragon.vm.torch.ops.math.functional import abs
......
......@@ -77,7 +77,7 @@ class Tape(object):
self._sources.add(tensor_id)
def merge_from(self, other):
"""Merge operations from another."""
"""Merge operations from the other."""
if other is not None:
self._operations = {**self._operations, **other._operations}
self._sources = self._sources.union(other._sources)
......
......@@ -1043,7 +1043,7 @@ def normalize(input, p=2, dim=1, eps=1e-12, out=None):
dim : int, optional, default=1
The dimension to reduce.
eps : float, optional, default=1e-12
The value of :math:`\epsilon`.
The value to :math:`\epsilon`.
out : dragon.vm.torch.Tensor, optional
The optional output tensor.
......
......@@ -53,7 +53,7 @@ class ELU(Module):
Parameters
----------
alpha : float, optional, default=1.
The value of :math:`\alpha`.
The value to :math:`\alpha`.
inplace : bool, optional, default=False
Whether to do the operation in-place.
......
......@@ -106,7 +106,7 @@ def assign(out, starts, sizes, input):
def cast(input, dtype='float32', inplace=False):
"""Cast the data type of input to another.
"""Cast the data type of input.
Parameters
----------
......
......@@ -27,7 +27,7 @@ def arange(
device=None,
requires_grad=False,
):
r"""Return a tensor with evenly spaced values within a interval.
"""Return a tensor of evenly spaced values within a interval.
Specify ``start`` and ``end`` to determine an interval:
......@@ -79,7 +79,7 @@ def arange(
device if device else cpp.device(),
num_args=len(slice_args),
dtype=dtype if dtype else 'int64',
).apply(slice_args)
).apply(slice_args, out)
out.requires_grad = requires_grad
return out
......@@ -145,51 +145,21 @@ def fill_like(out, shape_like, value):
.apply(out, [], shape_like)
def normal(*size, **kwargs):
"""Return a tensor with a normal distribution.
Parameters
----------
size : int...
The size(s) indicating the out shape.
mean : number, optional, default=0
The mean of distribution.
std : number, optional, default=1
The stddev of distribution.
out : dragon.vm.torch.Tensor, optional
The optional output tensor.
dtype : str, optional, default='float32'
The optional data type.
device : dragon.vm.torch.device, optional
The optional device of returned tensor.
requires_grad : bool, optional, default=False
**True** to record gradient for returned tensor.
Returns
-------
dragon.vm.torch.Tensor
The output tensor.
"""
out = kwargs.get('out', utils.new_leaf(size, kwargs))
return normal_fill(out, kwargs.get('mean', 0), kwargs.get('std', 1))
def normal_fill(out, mean=0, std=1):
"""Fill a tensor with a normal distribution."""
shape = out.shape
def normal_fill(input, mean=0, std=1):
"""Fill input from the normal distribution."""
shape = input.shape
return _functions.RandomNormal \
.instantiate(
out.device,
input.device,
ndim=len(shape),
mean=float(mean),
std=float(std),
dtype=out.dtype,
).apply(out, shape)
dtype=input.dtype,
).apply(input, shape)
def ones(*size, **kwargs):
r"""Return a tensor with value **1** filled.
r"""Return a tensor filled with ones.
.. math:: \text{out} \leftarrow 1
......@@ -217,7 +187,7 @@ def ones(*size, **kwargs):
def ones_like(input, **kwargs):
r"""Return a tensor with value **1** filled, shape as input.
r"""Return a tensor of ones with shape as the other.
.. math:: \text{out} \leftarrow 1
......@@ -245,7 +215,7 @@ def ones_like(input, **kwargs):
def rand(*size, **kwargs):
"""Return a float tensor with a uniform distribution of U(0, 1).
"""Return a tensor from the uniform distribution of U(0, 1).
Parameters
----------
......@@ -271,7 +241,7 @@ def rand(*size, **kwargs):
def randn(*size, **kwargs):
"""Return a float tensor with a normal distribution of N(0, 1).
"""Return a tensor from the normal distribution of N(0, 1).
Parameters
----------
......@@ -296,51 +266,21 @@ def randn(*size, **kwargs):
return normal_fill(out, 0, 1)
def uniform(*size, **kwargs):
"""Return a tensor with a normal distribution.
Parameters
----------
size : int...
The size(s) indicating the out shape.
low : number, optional, default=0
The low bound of distribution.
high : number, optional, default=1
The high bound of distribution.
out : dragon.vm.torch.Tensor, optional
The optional output tensor.
dtype : str, optional, default='float32'
The optional data type.
device : dragon.vm.torch.device, optional
The optional device of returned tensor.
requires_grad : bool, optional, default=False
**True** to record gradient for returned tensor.
Returns
-------
dragon.vm.torch.Tensor
The output tensor.
"""
out = kwargs.get('out', utils.new_leaf(size, kwargs))
return uniform_fill(out, kwargs.get('low', 0), kwargs.get('high', 1))
def uniform_fill(out, low=0, high=1):
"""Fill a tensor with a uniform distribution."""
shape = out.shape
def uniform_fill(input, low=0, high=1):
"""Fill input from the uniform distribution."""
shape = input.shape
return _functions.RandomUniform \
.instantiate(
out.device,
input.device,
ndim=len(shape),
low=float(low),
high=float(high),
dtype=out.dtype,
).apply(out, shape)
dtype=input.dtype,
).apply(input, shape)
def zeros(*size, **kwargs):
r"""Return a tensor with value **0** filled.
r"""Return a tensor filled with zeros.
.. math:: \text{out} \leftarrow 0
......@@ -368,7 +308,7 @@ def zeros(*size, **kwargs):
def zeros_like(input, **kwargs):
r"""Return a tensor with value **0** filled, shape as input.
r"""Return a tensor of zeros with shape as the other.
.. math:: \text{out} \leftarrow 0
......
......@@ -54,9 +54,9 @@ def axpby(input, alpha=1., beta=1., out=None):
input : dragon.vm.torch.Tensor
The input tensor.
alpha : float, optional, default=1.
The value of :math:`\alpha`.
The value to :math:`\alpha`.
beta : float, optional, default=1.
The value of :math:`\beta`.
The value to :math:`\beta`.
out : dragon.vm.torch.Tensor, optional
The optional output tensor.
......
......@@ -40,14 +40,14 @@ def abs(self):
return math_funcs.abs(self)
def add(self, value):
def add(self, other):
r"""Compute the element-wise addition.
.. math:: \text{out} = \text{self} + \text{value}
.. math:: \text{out} = \text{self} + \text{other}
Parameters
----------
value : Union[dragon.vm.torch.Tensor, number]
other : Union[dragon.vm.torch.Tensor, number]
The value to add.
Returns
......@@ -60,17 +60,17 @@ def add(self, value):
`torch.add(...)`_ : Compute the element-wise addition.
"""
return math_funcs.add(self, value)
return math_funcs.add(self, other)
def add_(self, value):
def add_(self, other):
r"""Compute the element-wise addition.
.. math:: \text{self} \mathrel{+}= \text{value}
.. math:: \text{self} \mathrel{+}= \text{other}
Parameters
----------
value : Union[dragon.vm.torch.Tensor, number]
other : Union[dragon.vm.torch.Tensor, number]
The value to add.
Returns
......@@ -83,7 +83,7 @@ def add_(self, value):
`torch.add(...)`_ : Compute the element-wise addition.
"""
return math_funcs.add(self, value, self)
return math_funcs.add(self, other, self)
def backward(self, gradient=None, retain_graph=False):
......@@ -398,14 +398,14 @@ def cumsum(self, dim):
return array_funcs.cumsum(self, dim)
def div(self, value):
def div(self, other):
r"""Compute the element-wise division.
.. math:: \text{out} = \text{self} \div \text{value}
.. math:: \text{out} = \text{self} \div \text{other}
Parameters
----------
value : Union[dragon.vm.torch.Tensor, number]
other : Union[dragon.vm.torch.Tensor, number]
The value to divide.
Returns
......@@ -418,17 +418,17 @@ def div(self, value):
`torch.div(...)`_ : Compute the element-wise division.
"""
return math_funcs.div(self, value)
return math_funcs.div(self, other)
def div_(self, value):
def div_(self, other):
r"""Compute the element-wise division.
.. math:: \text{self} \mathrel{\div}= \text{value}
.. math:: \text{self} \mathrel{\div}= \text{other}
Parameters
----------
value : Union[dragon.vm.torch.Tensor, number]
other : Union[dragon.vm.torch.Tensor, number]
The value to be divided.
Returns
......@@ -441,7 +441,7 @@ def div_(self, value):
`torch.div(...)`_ : Compute the element-wise division.
"""
return math_funcs.div(self, value, self)
return math_funcs.div(self, other, self)
def double(self):
......@@ -531,14 +531,14 @@ def expand(self, *sizes):
def fill_(self, value):
r"""Fill with the given constant value.
r"""Fill self with a scalar value.
.. math:: \text{self} \leftarrow \text{value}
Parameters
----------
value : number
The constant value.
The value to fill.
Returns
-------
......@@ -633,6 +633,19 @@ def ge(self, other):
def getitem(self, item):
"""Select elements at the specific index.
Parameters
----------
item : Union[int, slice, dragon.vm.torch.Tensor]
The index.
Returns
-------
dragon.vm.torch.Tensor
The output tensor.
"""
if isinstance(item, Tensor):
return self.masked_select(item)
else:
......@@ -935,14 +948,14 @@ def min(self, dim=None, keepdim=False):
return array_funcs.min(self, dim, keepdim)
def mul(self, value):
def mul(self, other):
r"""Compute the element-wise multiplication.
.. math:: \text{out} = \text{self} \times \text{value}
.. math:: \text{out} = \text{self} \times \text{other}
Parameters
----------
value : Union[dragon.vm.torch.Tensor, number]
other : Union[dragon.vm.torch.Tensor, number]
The value to multiply.
Returns
......@@ -955,17 +968,17 @@ def mul(self, value):
`torch.mul(...)`_ : Compute the element-wise multiplication.
"""
return math_funcs.mul(self, value)
return math_funcs.mul(self, other)
def mul_(self, value):
def mul_(self, other):
r"""Compute the element-wise multiplication.
.. math:: \text{self} \mathrel{\times}= \text{value}
.. math:: \text{self} \mathrel{\times}= \text{other}
Parameters
----------
value : Union[dragon.vm.torch.Tensor, number]
other : Union[dragon.vm.torch.Tensor, number]
The value to multiply.
Returns
......@@ -978,7 +991,7 @@ def mul_(self, value):
`torch.mul(...)`_ : Compute the element-wise multiplication.
"""
return math_funcs.mul(self, value, self)
return math_funcs.mul(self, other, self)
def multinomial(self, num_samples, eps=0.):
......@@ -1076,16 +1089,16 @@ def nonzero(self):
def normal_(self, mean=0, std=1):
r"""Fill self with a normal distribution.
r"""Fill self from a normal distribution.
.. math:: \text{self} \leftarrow N(\mu, \sigma)
.. math:: \text{self} \sim \mathcal{N}(\mu, \sigma)
Parameters
----------
mean : number, optional, default=0
The value of :math:`\mu`.
The value to :math:`\mu`.
std : number, optional, default=1
The value of :math:`\sigma`.
The value to :math:`\sigma`.
Returns
-------
......@@ -1302,6 +1315,16 @@ def rsqrt_(self):
def setitem(self, key, value):
"""Set elements at the specific index.
Parameters
----------
key : Union[int, slice, dragon.vm.torch.Tensor]
The index.
value : Union[dragon.vm.torch.Tensor, number]
The value to set.
"""
if isinstance(key, Tensor):
return self.masked_fill_(key, value)
else:
......@@ -1464,14 +1487,14 @@ def sum(self, dim=None, keepdim=False):
return array_funcs.sum(self, dim, keepdim)
def sub(self, value):
def sub(self, other):
r"""Compute the element-wise subtraction.
.. math:: \text{out} = \text{self} - \text{value}
.. math:: \text{out} = \text{self} - \text{other}
Parameters
----------
value : Union[dragon.vm.torch.Tensor, number]
other : Union[dragon.vm.torch.Tensor, number]
The value to subtract.
Returns
......@@ -1484,17 +1507,17 @@ def sub(self, value):
`torch.sub(...)`_ : Compute the element-wise subtraction.
"""
return math_funcs.sub(self, value)
return math_funcs.sub(self, other)
def sub_(self, value):
def sub_(self, other):
r"""Compute the element-wise subtraction.
.. math:: \text{self} \mathrel{-}= \text{value}
.. math:: \text{self} \mathrel{-}= \text{other}
Parameters
----------
value : Union[dragon.vm.torch.Tensor, number]
other : Union[dragon.vm.torch.Tensor, number]
The value to be subtracted.
Returns
......@@ -1507,7 +1530,7 @@ def sub_(self, value):
`torch.sub(...)`_ : Compute the element-wise subtraction.
"""
return math_funcs.sub(self, value, self)
return math_funcs.sub(self, other, self)
def type(self, dtype=None):
......@@ -1532,16 +1555,16 @@ def type(self, dtype=None):
def uniform_(self, low=0, high=1):
r"""Fill self with a uniform distribution.
r"""Fill self from a uniform distribution.
.. math:: \text{self} \leftarrow U(\alpha, \beta)
.. math:: \text{self} \sim \mathcal{U}(\alpha, \beta)
Parameters
----------
low : number, optional, default=0
The value of :math:`\alpha`.
The value to :math:`\alpha`.
high : number, optional, default=1
The value of :math:`\beta`.
The value to :math:`\beta`.
Returns
-------
......@@ -1741,7 +1764,6 @@ Tensor.unsqueeze_ = unsqueeze_
Tensor.where = where
Tensor.__getitem__ = getitem
Tensor.__radd__ = lambda self, value: math_funcs._binary_func(value, self, 'Add')
Tensor.__rdiv__ = lambda self, value: math_funcs._binary_func(value, self, 'Div')
Tensor.__rmul__ = lambda self, value: math_funcs._binary_func(value, self, 'Mul')
Tensor.__rsub__ = lambda self, value: math_funcs._binary_func(value, self, 'Sub')
Tensor.__rtruediv__ = lambda self, value: math_funcs._binary_func(value, self, 'Div')
......
......@@ -21,7 +21,7 @@ from dragon.vm.torch.optim.optimizer import Optimizer
class Adam(Optimizer):
r"""The optimizer which implements Adam algorithm.
r"""The optimizer to apply Adam algorithm.
`[Kingma & Ba, 2014] <https://arxiv.org/abs/1412.6980>`_.
The **Adam** update is defined as:
......
......@@ -21,7 +21,7 @@ from dragon.vm.torch.optim.optimizer import Optimizer
class RMSprop(Optimizer):
r"""The optimizer which implements RMSprop algorithm.
r"""The optimizer to apply RMSprop algorithm.
`[Hinton et.al, 2013] <http://www.cs.utoronto.ca/~bonner/courses/2016s/csc321/lectures/lec6.pdf>`_.
The **RMSprop** update is defined as:
......
......@@ -22,7 +22,7 @@ from dragon.vm.torch.optim.optimizer import required
class SGD(Optimizer):
r"""The optimizer which implements SGD algorithm.
r"""The optimizer to apply SGD algorithm.
Following SGD algorithms are supported:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!