Standard Operations API
|
Elementwise addition of two tensors x and y. |
|
Elementwise multiplication of two tensors x and y. |
|
Elementwise subtraction of two tensors x and y. |
The elementwise squares of the input tensor. |
|
|
Elementwise addition of two tensors x and y. |
|
Computes the power of the first input with exponent as the second input. |
The elementwise square roots of the input tensor. |
|
Elementwise exponential of the input tensor or scalar. |
|
|
Computes the natural logarithm of all entries in the input tensor if base argument is not provided. |
|
Computes the sum of all entries in the input tensor if a single argument is provided. |
|
Computes the product of all entries in the input tensor if a single argument is provided. |
|
Computes the average of all entries in the input tensor if a single argument is provided. |
|
Computes the maximum entry in the input tensor if a single argument is provided. |
|
Computes the minimum entry in the input tensor if a single argument is provided. |
|
Elementwise absolute values of the input tensor. |
Compute -1*x of a variable x |
|
Return a copy of the input variable x. |
|
|
|
|
doc strings |
Elementwise sine of a CSDL Variable |
|
Elementwise cosine of a CSDL Variable |
|
Elementwise tangent of a CSDL Variable |
|
Elementwise arcsine of a CSDL Variable |
|
Elementwise arccosine of a CSDL Variable |
|
Elementwise arctangent of a CSDL Variable |
|
Elementwise hyperbolic sine of a CSDL Variable |
|
Elementwise hyperbolic cosine of a CSDL Variable |
|
Elementwise hyperbolic tangent of a CSDL Variable |
|
|
Computes the cross product of two arrays of 2D or 3D vectors. |
Assemble a block matrix from a list or list of lists of matrices. |
|
|
Computes the even p-norm of all entries in the input tensor if a single argument is provided. |
|
Dot product of two vectors x and y. |
|
matrix-vector multiplication A*x. |
|
matrix-matrix multiplication A*B. |
|
Solve a linear system of equations Ax = b for x. |
|
Outer product of two tensors x and y. |
|
Inner product of two tensors x and y. |
|
Computes the tensor dot product of two tensors x and y along the specified axes. |
|
Reshape a tensor x to a new shape. |
Invert the axes of a tensor. |
|
|
Expands the input scalar/tensor to the specified out_shape by repeating the tensor along certain axes determined fom the action argument. |
|
Reorders the axes of the input tensor as per the specified action. |
|
Einstein summation of a list of Variables according to the specified action. |
|
Elementwise bessel function of a tensor, uses scipy's bessel functions. |
|
(TEMPORARY) sparse matrix-vector multiplication A*x. |
|
(TEMPORARY) sparse matrix-matrix multiplication A*x. |
|
Computes the derivatives of the output variables with respect to the input variables in CSDL. |
|
Stack arrays in a sequence vartically (row wise). |
|
concatenate arrays along an axis. |
|
Elementwise linear combination of two tensors. |
- csdl_alpha.add(x, y)[source]
Elementwise addition of two tensors x and y.
- Parameters
- xVariable
- yVariable
- Returns
- out: Variable
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.Variable(value = np.array([4.0, 5.0, 6.0])) >>> csdl.add(x, y).value array([5., 7., 9.]) >>> (x + y).value # equivalent to the above array([5., 7., 9.]) >>> (x + 2.0).value # broadcasting is also supported array([3., 4., 5.])
- csdl_alpha.mult(x, y)[source]
Elementwise multiplication of two tensors x and y.
- Parameters
- xVariable
- yVariable
- Returns
- out: Variable
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.Variable(value = np.array([4.0, 5.0, 6.0])) >>> csdl.mult(x, y).value array([ 4., 10., 18.]) >>> (x * y).value # equivalent to the above array([ 4., 10., 18.]) >>> (x * 2.0).value # broadcasting is also supported array([2., 4., 6.])
- csdl_alpha.sub(x, y)[source]
Elementwise subtraction of two tensors x and y.
- Parameters
- xVariable
- yVariable
- Returns
- out: Variable
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([4.0, 5.0, 6.0])) >>> y = csdl.Variable(value = np.array([3.0, 2.0, 1.0])) >>> csdl.sub(x, y).value array([1., 3., 5.]) >>> (x - y).value # equivalent to the above array([1., 3., 5.]) >>> (x - 2.0).value # broadcasting is also supported array([2., 3., 4.])
- csdl_alpha.square(x)[source]
The elementwise squares of the input tensor.
- Parameters
- xVariable, np.ndarray, float, or int
Input tensor to take the square of.
- Returns
- Variable
Elementwise squares of the input tensor.
- csdl_alpha.div(x, y)[source]
Elementwise addition of two tensors x and y.
- Parameters
- xVariable
- yVariable
- Returns
- out: Variable
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.Variable(value = np.array([4.0, 5.0, 6.0])) >>> csdl.div(x, y).value array([0.25, 0.4 , 0.5 ]) >>> (x/y).value # equivalent to the above array([0.25, 0.4 , 0.5 ]) >>> (x/2.0).value # broadcasting is also supported array([0.5, 1. , 1.5])
- csdl_alpha.power(x, y)[source]
Computes the power of the first input with exponent as the second input. If one of the inputs is a scalar, it is broadcasted to the shape of the other input.
- Parameters
- xVariable, np.ndarray, float or int
Input tensor whose power needs to be computed.
- yVariable, np.ndarray, float or int
Power to which the first input tensor needs to be raised.
- Returns
- Variable
Power of the first input with exponent as the second input.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y1 = csdl.power(x, 2) >>> y1.value array([1., 4., 9.]) >>> y2 = x ** 2 >>> y2.value array([1., 4., 9.])
Power raised to a tensor variable exponent
>>> z = csdl.Variable(value = 3.0 * np.ones((3,))) >>> y2 = x ** z >>> y2.value array([ 1., 8., 27.])
- csdl_alpha.sqrt(x)[source]
The elementwise square roots of the input tensor.
- Parameters
- xVariable, np.ndarray, float, or int
Input tensor to take the square root of.
- Returns
- Variable
Elementwise square roots of the input tensor.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.sqrt(x) >>> y.value array([1. , 1.41421356, 1.73205081])
- csdl_alpha.exp(x)[source]
Elementwise exponential of the input tensor or scalar.
- Parameters
- xVariableLike
Input tensor to take the exponential of.
- Returns
- Variable
Elementwise exponential of the input tensor.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.exp(x) >>> y.value array([ 2.71828183, 7.3890561 , 20.08553692])
- csdl_alpha.log(x, base=None)[source]
Computes the natural logarithm of all entries in the input tensor if base argument is not provided. Otherwise, computes the logarithm of all entries in the input tensor with respect to the specified base. If one of the inputs is a scalar, it is broadcasted to the shape of the other input.
- Parameters
- xVariable, np.ndarray, float or int
Input tensor whose logarithm needs to be computed.
- baseVariable, np.ndarray, float or int, default=np.e
Base of the logarithm. If not provided, natural logarithm is computed.
- Returns
- Variable
Logarithm of the first input with base as the second input.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y1 = csdl.log(x) >>> y1.value array([0. , 0.69314718, 1.09861229])
Logarithm with a specified base
>>> y2 = csdl.log(x, 2) >>> y2.value array([0. , 1. , 1.5849625])
Logarithm with a specified tensor variable base
>>> b = csdl.Variable(value = 2.0 * np.ones((3,))) >>> y3 = csdl.log(x, b) >>> y3.value array([0. , 1. , 1.5849625])
- csdl_alpha.sum(*args, axes=None)[source]
Computes the sum of all entries in the input tensor if a single argument is provided. Computes the sum of all entries along the specified axes if axes argument is given. Computes the elementwise sum of multiple variables of the same shape, if multiple arguments are provided. Axes argument is not allowed in this case.
- Parameters
- *argstuple of Variable or np.ndarray objects
Input tensor/s whose sum/s needs to be computed.
- axestuple of int, default=None
Axes along which to compute the sum of the input tensor, if there’s only one input tensor.
- Returns
- Variable
Sum of all entries in the input tensor if a single argument is provided. Sum of entries along the specified axes if axes argument is given. Elementwise sum of multiple variables of the same shape, if multiple arguments are provided.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y1 = csdl.sum(x) >>> y1.value array([6.])
Sum of a single tensor variable along a specified axis
>>> x_val = np.arange(6).reshape(2,3) >>> x = csdl.Variable(value = x_val) >>> y2 = csdl.sum(x, axes=(1,)) >>> y2.value array([ 3., 12.])
Elementwise sum of multiple tensor variables
>>> y3 = csdl.sum(x, 2 * np.ones((2,3)), np.ones((2,3))) >>> y3.value array([[3., 4., 5.], [6., 7., 8.]])
- csdl_alpha.product(*args, axes=None)[source]
Computes the product of all entries in the input tensor if a single argument is provided. Computes the product of all entries along the specified axes if axes argument is given. Computes the elementwise product of multiple variables of the same shape, if multiple arguments are provided. Axes argument is not allowed in this case.
- Parameters
- *argstuple of Variable or np.ndarray objects
Input tensor/s whose product/s needs to be computed.
- axestuple of int, default=None
Axes along which to compute the product of the input tensor, if there’s only one input tensor.
- Returns
- Variable
Product of all entries in the input tensor if a single argument is provided. Product of entries along the specified axes if axes argument is given. Elementwise product of multiple variables of the same shape, if multiple arguments are provided.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y1 = csdl.product(x) >>> y1.value array([6.])
Product of a single tensor variable along a specified axis
>>> x_val = np.array([[1, 2, 3], [4, 5, 6]]) >>> x = csdl.Variable(value = x_val) >>> y2 = csdl.product(x, axes=(1,)) >>> y2.value array([ 6., 120.])
Elementwise product of multiple tensor variables
>>> y3 = csdl.product(x, 2 * np.ones((2,3)), np.ones((2,3))) >>> y3.value array([[ 2., 4., 6.], [ 8., 10., 12.]])
- csdl_alpha.average(*args, axes=None)[source]
Computes the average of all entries in the input tensor if a single argument is provided. Computes the average of all entries along the specified axes if axes argument is given. Computes the elementwise average of multiple variables of the same shape, if multiple arguments are provided. Axes argument is not allowed in this case.
- Parameters
- *argstuple of Variable or np.ndarray objects
Input tensor/s whose average/s needs to be computed.
- axestuple of int, default=None
Axes along which to compute the average of the input tensor, if there’s only one input tensor.
- Returns
- Variable
Average of all entries in the input tensor if a single argument is provided. Average of entries along the specified axes if axes argument is given. Elementwise average of multiple variables of the same shape, if multiple arguments are provided.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y1 = csdl.average(x) >>> y1.value array([2.])
Average of a single tensor variable along a specified axis
>>> x_val = np.arange(6).reshape(2,3) >>> x = csdl.Variable(value = x_val) >>> y2 = csdl.average(x, axes=(1,)) >>> y2.value array([1., 4.])
Elementwise average of multiple tensor variables
>>> y3 = csdl.average(x, 2 * np.ones((2,3)), np.ones((2,3))) >>> y3.value array([[1. , 1.33333333, 1.66666667], [2. , 2.33333333, 2.66666667]])
- csdl_alpha.maximum(*args, axes=None, rho=20.0)[source]
Computes the maximum entry in the input tensor if a single argument is provided. Computes the maximum entries along the specified axes if axes argument is given. Computes the elementwise maximum of multiple variables of the same shape, if multiple arguments are provided. Axes argument is not allowed in this case.
- Parameters
- *argstuple of Variable or np.ndarray objects
Input tensor/s whose maximum needs to be computed.
- axestuple of int, default=None
Axes along which to compute the maximum of the input tensor, if there’s only one input tensor.
- rhofloat, default=20.
Smoothing parameter for the maximum function.
- Returns
- Variable
Maximum entry in the input tensor if a single argument is provided. Maximum entries along the specified axes if axes argument is given. Elementwise maximum of multiple variables of the same shape, if multiple arguments are provided.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x_val = np.arange(6).reshape(2,3) >>> x = csdl.Variable(value = x_val) >>> y1 = csdl.maximum(x) >>> y1.value array([5.])
Maximum of a single tensor variable along a specified axis
>>> y2 = csdl.maximum(x, axes=(1,)) >>> y2.value array([2., 5.])
Elementwise maximum of multiple tensor variables
>>> y3 = csdl.maximum(x, 2 * np.ones((2,3)), np.ones((2,3))) >>> y3.value array([[2. , 2. , 2.03465736], [3. , 4. , 5. ]])
Note that y3.value[0,2] is not exactly 2.0 due to the smoothing term. It can be made closer to 2.0 by increasing the value of the smoothing parameter rho as shown below.
>>> y = csdl.maximum(x, 2 * np.ones((2,3)), np.ones((2,3)), rho=200) >>> y.value array([[2. , 2. , 2.00346574], [3. , 4. , 5. ]])
- csdl_alpha.minimum(*args, axes=None, rho=20.0)[source]
Computes the minimum entry in the input tensor if a single argument is provided. Computes the minimum entries along the specified axes if axes argument is given. Computes the elementwise minimum of multiple variables of the same shape, if multiple arguments are provided. Axes argument is not allowed in this case.
- Parameters
- *argstuple of Variable or np.ndarray objects
Input tensor/s whose minimum needs to be computed.
- axestuple of int, default=None
Axes along which to compute the minimum of the input tensor, if there’s only one input tensor.
- rhofloat, default=20.
Smoothing parameter for the minimum function.
- Returns
- Variable
Minimum entry in the input tensor if a single argument is provided. Minimum entries along the specified axes if axes argument is given. Elementwise minimum of multiple variables of the same shape, if multiple arguments are provided.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x_val = np.arange(6).reshape(2,3) >>> x = csdl.Variable(value = x_val) >>> y1 = csdl.minimum(x) >>> y1.value array([-1.03057685e-10])
Note that the value of y1 is not exactly 0.0 due to the smoothing term. The value of y1 can be made closer to 0.0 by increasing the value of the smoothing parameter rho as shown below.
>>> y = csdl.minimum(x, rho=200) >>> y.value array([-0.])
Minimum of a single tensor variable along a specified axis
>>> y2 = csdl.minimum(x, axes=(1,)) >>> y2.value array([-1.03057685e-10, 3.00000000e+00])
Elementwise minimum of multiple tensor variables
>>> y3 = csdl.minimum(x, 2 * np.ones((2,3)), np.ones((2,3))) >>> y3.value array([[-1.03057685e-10, 9.65342641e-01, 1.00000000e+00], [ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00]])
- csdl_alpha.absolute(x, rho=20.0)[source]
Elementwise absolute values of the input tensor.
- Parameters
- xVariable, np.ndarray, float, or int
Input tensor to take the absolute values of.
- rhofloat, default=20.
Smoothing parameter for the absolute function.
- Returns
- Variable
Elementwise absolute of the input tensor.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([-1.0, 2.0, -3.0])) >>> y = csdl.absolute(x) >>> y.value array([1., 2., 3.]) >>> y = csdl.absolute(0.0) >>> y.value array([0.03465736])
Note that the value of y is not exactly 0.0 due to the smoothing term. The value of y can be made closer to 0.0 by increasing the value of the smoothing parameter rho.`
>>> y = csdl.absolute(0.0, rho=200) >>> y.value array([0.00346574])
- csdl_alpha.negate(x)[source]
Compute -1*x of a variable x
- Parameters
- xVariable
- Returns
- out: Variable
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0, 4.0])) >>> (csdl.negate(x)).value array([-1., -2., -3., -4.]) >>> (-x).value # equivalent to the above array([-1., -2., -3., -4.])
- csdl_alpha.copyvar(x)[source]
Return a copy of the input variable x.
- Parameters
- xVariableLike
- Returns
- out: Variable
A new variable that represents the same value as x
- csdl_alpha.sin(x)[source]
Elementwise sine of a CSDL Variable
- Parameters
- xVariable
CSDL Variable to take the sine of
- Returns
- y: Variable
The elementwise sine of x
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.sin(x) >>> y.value array([0.84147098, 0.90929743, 0.14112001])
- csdl_alpha.cos(x)[source]
Elementwise cosine of a CSDL Variable
- Parameters
- xVariable
CSDL Variable to take the cosine of
- Returns
- y: Variable
The elementwise cosine of x
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.cos(x) >>> y.value array([ 0.54030231, -0.41614684, -0.9899925 ])
- csdl_alpha.tan(x)[source]
Elementwise tangent of a CSDL Variable
- Parameters
- xVariable
CSDL Variable to take the tangent of
- Returns
- y: Variable
The elementwise tangent of x
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.tan(x) >>> y.value array([ 1.55740772, -2.18503986, -0.14254654])
- csdl_alpha.arcsin(x)[source]
Elementwise arcsine of a CSDL Variable
- Parameters
- xVariable
CSDL Variable to take the sine of
- Returns
- y: Variable
The elementwise sine of x
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, -0.5, 0.5])) >>> y = csdl.arcsin(x) >>> y.value array([ 1.57079633, -0.52359878, 0.52359878])
- csdl_alpha.arccos(x)[source]
Elementwise arccosine of a CSDL Variable
- Parameters
- xVariable
CSDL Variable to take the cosine of
- Returns
- y: Variable
The elementwise cosine of x
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, -0.5, 0.5])) >>> y = csdl.arccos(x) >>> y.value array([0. , 2.0943951 , 1.04719755])
- csdl_alpha.arctan(x)[source]
Elementwise arctangent of a CSDL Variable
- Parameters
- xVariable
CSDL Variable to take the tangent of
- Returns
- y: Variable
The elementwise tangent of x
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.arctan(x) >>> y.value array([0.78539816, 1.10714872, 1.24904577])
- csdl_alpha.sinh(x)[source]
Elementwise hyperbolic sine of a CSDL Variable
- Parameters
- xVariable
CSDL Variable to take the hyperbolic sine of
- Returns
- y: Variable
The elementwise hyperbolic sine of x
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.sinh(x) >>> y.value array([ 1.17520119, 3.62686041, 10.01787493])
- csdl_alpha.cosh(x)[source]
Elementwise hyperbolic cosine of a CSDL Variable
- Parameters
- xVariable
CSDL Variable to take the hyperbolic cosine of
- Returns
- y: Variable
The elementwise hyperbolic cosine of x
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.cosh(x) >>> y.value array([ 1.54308063, 3.76219569, 10.067662 ])
- csdl_alpha.tanh(x)[source]
Elementwise hyperbolic tangent of a CSDL Variable
- Parameters
- xVariable
CSDL Variable to take the hyperbolic tangent of
- Returns
- y: Variable
The elementwise hyperbolic tangent of x
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y = csdl.tanh(x) >>> y.value array([0.76159416, 0.96402758, 0.99505475])
- csdl_alpha.cross(x, y, axis=None)[source]
Computes the cross product of two arrays of 2D or 3D vectors.
- Parameters
- xVariable or np.ndarray
First input tensor of shape (l,…,2,…,n) or (l,…,3,…,n).
- yVariable or np.ndarray
Second input tensor of the same shape as x.
- axisint, default=None
Axis along which the 2D or 3D vectors are stored in the input tensors. Need not be specified if the input tensors are 1D vectors of size 2 or 3. Needs to be specified if the input tensors are 2D or higher dimensional tensors. Axis must be a non-negative integer less than the number of dimensions in the input tensors.
- Returns
- Variable
Tensor containing the cross product (x × y) of vectors in the input tensors.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([3.0, 4.0, 5.0])) >>> y = csdl.Variable(value = np.array([4.0, 5.0, 6.0])) >>> csdl.cross(x, y).value array([-1., 2., -1.])
>>> x = csdl.Variable(value = 3.0 * np.ones((2,3))) >>> y_val = np.arange(6).reshape(2,3) >>> csdl.cross(x, y_val, axis=1).value array([[ 3., -6., 3.], [ 3., -6., 3.]])
- csdl_alpha.blockmat(l)[source]
Assemble a block matrix from a list or list of lists of matrices.
- Parameters
- llist or list of lists of Variable or np.ndarray objects
List or a list of lists of matrices to assemble into a block matrix.
- Returns
- Variable
Block matrix assembled from the input list.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x_val = 3.0*np.ones((2,3)) >>> z_val = np.ones((1,5)) >>> x = csdl.Variable(name = 'x', value = x_val) >>> z = csdl.Variable(name = 'z', value = z_val)
Create a block row matrix
>>> b1 = csdl.blockmat([x, np.zeros((2,2))]) >>> b1.value array([[3., 3., 3., 0., 0.], [3., 3., 3., 0., 0.]])
Create a block matrix with block rows and columns
>>> b2 = csdl.blockmat([[x, np.zeros((2,2))], [z]]) >>> b2.value array([[3., 3., 3., 0., 0.], [3., 3., 3., 0., 0.], [1., 1., 1., 1., 1.]])
- csdl_alpha.norm(*args, axes=None, ord=2)[source]
Computes the even p-norm of all entries in the input tensor if a single argument is provided. Computes the even p-norm of all entries along the specified axes if axes argument is given. Computes the elementwise even p-norm of multiple variables of the same shape, if multiple variable arguments are provided. axes argument is not allowed in this case.
- Parameters
- *argstuple of Variable or np.ndarray objects
Input tensor/s whose even p-norm/s needs to be computed.
- axestuple of int, default=None
Axes along which to compute the even p-norm of the input tensor, if there’s only one input tensor.
- ordint (even), default=2
Order of norm to compute. Currently only even p-norms are supported.
- Returns
- Variable
p-norm of all entries in the input tensor if a single argument is provided. p-norm of entries along the specified axes if axes argument is given. Elementwise p-norm of multiple variables of the same shape, if multiple variable arguments are provided.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([3.0, 4.0])) >>> y1 = csdl.norm(x) >>> y1.value array([5.]) >>> y2 = csdl.norm(x, ord=4) >>> y2.value array([4.28457229])
Norm of a single tensor variable along a specified axis
>>> x_val = np.arange(6).reshape(2,3) >>> x = csdl.Variable(value = x_val) >>> y3 = csdl.norm(x, axes=(1,)) >>> y3.value array([2.23606798, 7.07106781])
Elementwise norm of multiple tensors
>>> y4 = csdl.norm(x, 2 * np.ones((2,3)), np.ones((2,3))) >>> y4.value array([[2.23606798, 2.44948974, 3. ], [3.74165739, 4.58257569, 5.47722558]])
- csdl_alpha.vdot(x, y)[source]
Dot product of two vectors x and y. The result is a scalar of shape (1,).
- Parameters
- xVariable
1D vector.
- yVariable
1D vector.
- Returns
- out: Variable
Scalar dot product of x and y.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1, 2, 3])) >>> y = csdl.Variable(value = np.array([4, 5, 6])) >>> csdl.vdot(x, y).value array([32.])
- csdl_alpha.matvec(A, x)[source]
matrix-vector multiplication A*x. The number of columns of A must be equal to the number of rows of x. If x is 1D, reshaped to 2D.
- Parameters
- AVariable
2D matrix
- xVariable
1D or 2D vector
- Returns
- y: Variable
1D or 2D vector
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> A = csdl.Variable(value = np.array([[1, 2], [3, 4], [5, 6]])) >>> x = csdl.Variable(value = np.array([1, 2])) >>> csdl.matvec(A, x).value array([ 5., 11., 17.])
- csdl_alpha.matmat(A, B)[source]
matrix-matrix multiplication A*B. The number of columns of A must be equal to the number of rows of x.
- Parameters
- AVariable
2D matrix
- BVariable
2D matrix (or 1D vector, in which case matvec is called instead)
- Returns
- C: Variable
2D matrix
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> A = csdl.Variable(value = np.array([[1, 2], [3, 4], [5, 6]])) >>> B = csdl.Variable(value = np.array([[1, 2], [3, 4]])) >>> (A @ B).value array([[ 7., 10.], [15., 22.], [23., 34.]])
- csdl_alpha.solve_linear(A, b, solver=<csdl_alpha.src.operations.linalg.linear_solvers.direct_solver.DirectSolver object>)[source]
Solve a linear system of equations Ax = b for x.
- Parameters
- AVariableLike
2D matrix
- bVariableLike
1D or 2D vector
- Returns
- x: Variable
1D or 2D vector
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> A = csdl.Variable(value = np.array([[1, 2], [3, 4]])) >>> b = csdl.Variable(value = np.array([5, 6])) >>> csdl.solve_linear(A, b).value array([-4. , 4.5]) >>> recorder.stop()
Specify different solvers:
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> A = csdl.Variable(value = np.array([[1, 2], [3, 4]])) >>> b = csdl.Variable(value = np.array([5, 6])) >>> csdl.solve_linear(A, b, solver = csdl.linear_solvers.DirectSolver()).value array([-4. , 4.5]) >>> recorder.stop()
- csdl_alpha.outer(x, y)[source]
Outer product of two tensors x and y. The result is a tensor with shape (x.shape + y.shape). For example, if x has shape (3,2) and y has shape (4,5), the output will have shape (3,2,4,5).
- Parameters
- xVariableLike
First input tensor.
- yVariableLike
Second input tensor.
- Returns
- out: Variable
Outer product of x and y.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1, 2, 3])) >>> y = csdl.Variable(value = np.array([4, 5])) >>> csdl.outer(x, y).value array([[ 4., 5.], [ 8., 10.], [12., 15.]]) >>> z = csdl.Variable(value = np.array([[1, 2], [3, 4]])) >>> csdl.outer(x, z).value array([[[ 1., 2.], [ 3., 4.]], [[ 2., 4.], [ 6., 8.]], [[ 3., 6.], [ 9., 12.]]])
- csdl_alpha.inner(x, y)[source]
Inner product of two tensors x and y. The result is a scalar of shape (1,). The input tensors must have the same shape.
- Parameters
- xVariableLike
First input tensor.
- yVariableLike
Second input tensor.
- Returns
- out: Variable
Scalar inner product of x and y.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1, 2, 3])) >>> y = csdl.Variable(value = np.array([4, 5, 6])) >>> csdl.inner(x, y).value array([32.]) >>> a = csdl.Variable(value = np.array([[1, 2], [3, 4]])) >>> b = csdl.Variable(value = np.array([[5, 6], [7, 8]])) >>> csdl.inner(a, b).value array([70.])
- csdl_alpha.tensordot(x, y, axes=None)[source]
Computes the tensor dot product of two tensors x and y along the specified axes. The axes argument is a tuple of two lists, where the corresponding axes of x and y to multiply and sum over are specified. If axes is specified, the resulting tensor will have shape equal to the concatenation of the shapes of x and y, with the axes specified removed. For example, if x has shape (3,2) and y has shape (2,5), and axes = ([1],[0]), the result will have shape (3,5).
The tensor dot product is a generalization of the inner and outer product operations. If no axes is specified, the resulting tensor is the outer product of x and y having shape (x.shape + y.shape). If x and y have same shape, and the axes is set to ([0,1,…,rank_x], [0,1,…,rank_y]), the result is the scalar inner product of x and y. Note that the rank_x = rank_y = len(x.shape) = len(y.shape).
- Parameters
- xVariableLike
First input tensor.
- yVariableLike
Second input tensor.
- axestuple of 2 lists, default=None
Axes along which to compute the tensor dot product of the input tensors. If not specified, the outer product of x and y is computed. If specified, the axes must be a tuple of 2 lists. The axes must be unique within each list. The axes must be non-negative integers within each list. Each list in the tuple must have the same length. Each corresponding pair of axes for x and y in the 2 lists specified must have equal lengths.
- Returns
- Variable
Tensor dot product of x and y.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1, 2, 3])) >>> y = csdl.Variable(value = np.array([4, 5]))
Outer product of x and y:
>>> csdl.tensordot(x, y).value array([[ 4., 5.], [ 8., 10.], [12., 15.]])
Outer product of x and z:
>>> z = csdl.Variable(value = np.array([[1, 2], [3, 4]])) >>> csdl.tensordot(x, z).value array([[[ 1., 2.], [ 3., 4.]], [[ 2., 4.], [ 6., 8.]], [[ 3., 6.], [ 9., 12.]]])
Dot product of y and z along one axis (same at matrix product z @ y):
>>> csdl.tensordot(y, z, axes=([0], [1])).value array([14., 32.])
Inner product of z and t:
>>> t_np = np.array([[5, 6], [7, 8]]) >>> csdl.tensordot(z, t_np, axes=([0,1], [0,1])).value array([70.])
- csdl_alpha.reshape(x, shape)[source]
Reshape a tensor x to a new shape.
- Parameters
- xVariable
- shapetuple[int]
- Returns
- out: Variable
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0, 4.0])) >>> csdl.reshape(x, (2,2)).value array([[1., 2.], [3., 4.]]) >>> x.reshape((2,2)).value # same thing as above array([[1., 2.], [3., 4.]]) >>> x.flatten().value # reshapes to 1 dimension array([1., 2., 3., 4.])
- csdl_alpha.transpose(x)[source]
Invert the axes of a tensor. The shape of the output is the reverse of the input shape.
- Parameters
- xVariableLike
- Returns
- out: Variable
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])) >>> csdl.transpose(x).value array([[1., 4.], [2., 5.], [3., 6.]]) >>> x.T().value # equivalent to the above array([[1., 4.], [2., 5.], [3., 6.]])
- csdl_alpha.expand(x, out_shape, action=None)[source]
Expands the input scalar/tensor to the specified out_shape by repeating the tensor along certain axes determined fom the action argument. For example, action=’i->ijk’ will expand a 1D tensor to a 3D tensor by repeating the input tensor along two new axes. The action argument is optional if the input is a scalar since the scalar will be simply broadcasted to the specified out_shape.
- Parameters
- xVariableLike
Input scalar/tensor that needs to be expanded.
- out_shapetuple of int
Desired shape of the expanded output tensor.
- actionstr, default=None
Specifies the action to be taken when expanding the tensor, e.g.,`’i->ij’` expands a vector to a matrix by repeating the input vector rowwise.
- Returns
- Variable
Expanded output tensor as per the specified out_shape and action.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = 3.0) >>> y1 = csdl.expand(x, out_shape=(2,3)) >>> y1.value array([[3., 3., 3.], [3., 3., 3.]]) >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> y2 = csdl.expand(x, out_shape=(2,3), action='i->ji') >>> y2.value array([[1., 2., 3.], [1., 2., 3.]]) >>> y3 = csdl.expand(x, out_shape=(3,2), action='i->ij') >>> y3.value array([[1., 1.], [2., 2.], [3., 3.]]) >>> y4 = csdl.expand(x, out_shape=(4,3,2), action='i->lij') >>> y4.value array([[[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]], [[1., 1.], [2., 2.], [3., 3.]]])
- csdl_alpha.reorder_axes(x, action)[source]
Reorders the axes of the input tensor as per the specified action. For example, action=’ijk->kji’ will transpose the input 3D tensor. The action argument is optional if the input is a scalar since the scalar will be simply broadcasted to the specified out_shape.
- Parameters
- xVariable or np.ndarray
Input tensor that needs to have its axes reordered.
- actionstr
Specifies how the axes of the input tensor needs to be reordered, e.g.,`’ij->ji’` transposes the input matrix.
- Returns
- Variable
Axes-reordered output tensor as per specfied action.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x_val = np.array([[1., 2., 3.], [4., 5., 6.]]) >>> x = csdl.Variable(value = x_val) >>> y1 = csdl.reorder_axes(x, action='ij->ji') >>> y1.value array([[1., 4.], [2., 5.], [3., 6.]])
Reorder the axes of a 3D tensor:
>>> x_val = np.arange(24).reshape(2,3,4) >>> x_val array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) >>> y2 = csdl.reorder_axes(x_val, action='ijk->kij') >>> y2.value array([[[ 0., 4., 8.], [12., 16., 20.]], [[ 1., 5., 9.], [13., 17., 21.]], [[ 2., 6., 10.], [14., 18., 22.]], [[ 3., 7., 11.], [15., 19., 23.]]])
- csdl_alpha.einsum(*args, action=None)[source]
Einstein summation of a list of Variables according to the specified action. The action needs to be a string that explicitly specifies the input and output subscripts separated by ‘->’. The string must contain the explicit indicator ‘->’ to specify the output form. For example, if the input Variables are A and B, and the action is ‘ij,jk->ik’, the output will be the matrix product of A and B.
- Parameters
- argslist of Variables or np.ndarray objects
Input Variables for Einstein summation.
- actionstr
String specifying the input and output subscripts separated by ‘->’. The input subscripts are separated by commas. There must be exactly one output subscript. For example, ‘ij,jk->ik’ specifies the matrix product of two matrices.
- Returns
- Variable
Result of Einstein summation of the input Variables according to the specified action.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1, 2, 3])) >>> y = csdl.Variable(value = np.array([4, 5]))
Outer product of x and y:
>>> csdl.einsum(x, y, action='i,j->ij').value array([[ 4., 5.], [ 8., 10.], [12., 15.]])
Outer product of x and z:
>>> z = csdl.Variable(value = np.array([[1, 2], [3, 4]])) >>> csdl.einsum(x, z, action='i,jk->ijk').value array([[[ 1., 2.], [ 3., 4.]], [[ 2., 4.], [ 6., 8.]], [[ 3., 6.], [ 9., 12.]]])
Outer product of y and z reordered:
>>> csdl.einsum(x, z, action='i,jk->kij').value array([[[ 1., 3.], [ 2., 6.], [ 3., 9.]], [[ 2., 4.], [ 4., 8.], [ 6., 12.]]])
Dot product of y and z along one axis (same at matrix product z @ y):
>>> csdl.einsum(y, z, action='j,ij->i').value array([14., 32.])
Inner product of z and t:
>>> t_np = np.array([[5, 6], [7, 8]]) >>> csdl.einsum(z, t_np, action='ij,ij->').value array([70.])
Sum of all the elements of z:
>>> csdl.einsum(z, action='ij->').value array([10.])
Matrix product z @ t:
>>> csdl.einsum(z, t_np, action='ij,jk->ik').value array([[19., 22.], [43., 50.]])
Matrix product z.T @ t:
>>> csdl.einsum(z, t_np, action='ji,jk->ik').value array([[26., 30.], [38., 44.]])
- csdl_alpha.bessel(x, kind=1, order=1)[source]
Elementwise bessel function of a tensor, uses scipy’s bessel functions. Supports both J and Y Bessel functions by specifying kind = 1 or kind = 2 respectively.
- Parameters
- xVariableLike
Input tensor to evaluate bessel function.
- kindint
The kind of Bessel function. The options are 1 (J) or 2 (Y)
- order: int
Order of the Bessel function
- Returns
- Variable
Elementwise bessel function of the input tensor.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> csdl.bessel(x).value array([0.44005059, 0.57672481, 0.33905896])
specify kind and order:
>>> csdl.bessel(x, kind = 2, order = 3).value array([-5.82151761, -1.12778378, -0.53854162])
- csdl_alpha.sparse.matvec(A, x)
(TEMPORARY) sparse matrix-vector multiplication A*x. The number of columns of A must be equal to the number of rows of x.
- Parameters
- Ascipy sparse matrix
2D matrix
- xVariable
- Returns
- Variable
Examples
>>> import scipy.sparse as sp >>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> data = np.array([1, 2, 3, 4, 5, 6]) >>> row = np.array([0, 1, 2, 3, 4, 5]) >>> col = np.array([5, 4, 3, 2, 1, 0]) >>> A = sp.csr_matrix((data, (row, col)), shape=(6,6)) >>> x_val = np.arange(6).reshape((6,1)) >>> x = csdl.Variable(value = x_val) >>> csdl.sparse.matvec(A, x).value array([[5.], [8.], [9.], [8.], [5.], [0.]])
- csdl_alpha.sparse.matmat(A, x)
(TEMPORARY) sparse matrix-matrix multiplication A*x. The number of columns of A must be equal to the number of rows of x.
- Parameters
- Ascipy sparse matrix
2D matrix
- xVariable
- Returns
- Variable
Examples
>>> import scipy.sparse as sp >>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> data = np.array([1, 2, 3, 4, 5, 6]) >>> row = np.array([0, 1, 2, 3, 4, 5]) >>> col = np.array([5, 4, 3, 2, 1, 0]) >>> A = sp.csr_matrix((data, (row, col)), shape=(6,6)) >>> x_val = np.arange(12).reshape((6,2)) >>> x = csdl.Variable(value = x_val) >>> csdl.sparse.matmat(A, x).value array([[10., 11.], [16., 18.], [18., 21.], [16., 20.], [10., 15.], [ 0., 6.]])
- csdl_alpha.derivative(ofs, wrts, mode='reverse', as_block=False, graph=None, loop=True, concatenate_ofs=False, elementwise=False)[source]
Computes the derivatives of the output variables with respect to the input variables in CSDL.
- Parameters
- ofsUnion[Variable, list[Variable]]
Variables to take derivatives of.
- wrtsUnion[Variable, list[Variable]]
Variables to take derivatives with respect to.
- modestr, optional
‘forward’ or ‘reverse’ to forward or reverse mode differentiation, by default ‘reverse’
- as_blockbool, optional
If True, returns the derivatives as a block matrix, by default False
- graphGraph, optional
Which graph to take derivatives of, by default the current active graph
- loopbool, optional
If True, uses a csdl loop to compute the derivatives. If false, batches the derivatives. by default True
- concatenate_ofs: bool, optional
If True, concatenates the output variables into one variable before taking the derivative. This reduces the size of the graph and may be less efficient. by default False
- elementwisebool, optional
If True, assumes diagonal derivatives, by default False. (WARNING: The output will be incorrect if this is not the case.)
- Returns
- Union[dict[Variable], dict[Variable,Variable], Variable]
Returns the derivatives as CSDL variables. - If both ofs and wrts are lists, returns a dictionary of dictionaries. - If only one is a list, returns a dictionary. - If neither are lists, returns a single variable.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = 3.0) >>> y = csdl.Variable(value = 4.0) >>> z = x*y >>> dz = csdl.derivative(ofs = z, wrts = [x, y]) >>> dz_dx, dz_dy = dz[x], dz[y] >>> dz_dx.value array([[4.]]) >>> dz_dy.value array([[3.]])
Take derivatives of derivatives
>>> dz2_dx2 = csdl.derivative(ofs = dz_dx, wrts = x) >>> dz2_dx2.value array([[0.]])
- csdl_alpha.vstack(arrays)[source]
Stack arrays in a sequence vartically (row wise).
- Parameters
- arraystuple of arrays to stack. Each array must have the same shape in all but the first dimension.
1D arrays must have the same length.
- Returns
- Variable
The stacked array - at least 2D.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x_val = 3.0*np.ones((3,)) >>> z_val = np.ones((3,)) >>> x = csdl.Variable(name = 'x', value = x_val) >>> z = csdl.Variable(name = 'z', value = z_val) >>> y = csdl.vstack((x, z)) >>> y.value array([[3., 3., 3.], [1., 1., 1.]])
- csdl_alpha.concatenate(arrays, axis=0)[source]
concatenate arrays along an axis.
- Parameters
- arraystuple of arrays to stack. Each array must have the same shape in all but the specified dimension.
- axisint, optional
The axis along which to concatenate the arrays. The default is 0.
- Returns
- Variable
The concatenated array
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x_val = 3.0*np.ones((3,2)) >>> z_val = np.ones((1,2)) >>> x = csdl.Variable(name = 'x', value = x_val) >>> z = csdl.Variable(name = 'z', value = z_val) >>> y = csdl.concatenate((x, z), axis = 0) >>> y.value array([[3., 3.], [3., 3.], [3., 3.], [1., 1.]]) >>> y = csdl.concatenate((x.flatten(), z.flatten())) >>> y.value array([3., 3., 3., 3., 3., 3., 1., 1.])
- csdl_alpha.linear_combination(start, stop, num_steps, start_weights=None, stop_weights=None)[source]
Elementwise linear combination of two tensors.
- Parameters
- startVariableLike
First tensor to be linearly combined.
- stopVariableLike
Second tensor to be linearly combined.
- start_weightsnp.ndarray
Weights for the first tensor in the linear combination.
- stop_weightsnp.ndarray
Weights for the second tensor in the linear combination.
- num_stepsint
Number of steps in the linear combination.
- Returns
- Variable
Elementwise linear combination of the two tensors.
Examples
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> start = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> stop = csdl.Variable(value = np.array([4.0, 5.0, 6.0])) >>> csdl.linear_combination(start, stop, 3).value array([[4. , 4.5, 4. ], [2.5, 3.5, 4.5], [1. , 2. , 3. ]])