Variable
- class csdl_alpha.src.graph.variable.Variable(shape=None, name=None, value=None, tags=None, hierarchy=None)[source]
- Attributes
valueThe value of the variable used for inline evaluation
Methods
T()Invert the axes of a tensor.
alias of
float64expand(out_shape[, action])Expands the input scalar/tensor to the specified out_shape by repeating the tensor along certain axes determined fom the action argument.
flatten()Returns a 1D version of the variable.
get(slices)Similar to __getitem__ but only accepts a Slice object.
info()returns a string containing information about the node
inner(other)Inner product of two tensors x and y.
print_on_update([string])Prints the variable value when the value is updated along with the string provided.
print_trace([tab])Prints the trace of the node.
reshape(*shape)Returns a reshaped version of the variable.
save()Sets variable to be saved
set(slices, value)Sets a sliced selection of the variable to a new value.
set_as_design_variable([upper, lower, ...])set_hierarchy(hierarchy)set_value(value)Sets the value of a variable.
Slice
add_name
add_tag
get_base_str
post_init
set_as_constraint
set_as_objective
- T()[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.]])
- __getitem__(indices)[source]
Returns a sliced selection of the variable as a new variable. The slicing can be specified by a csdl Slice object or a tuple of lists of integers, integers, and slices. The slicing rules are similar to Numpy’s tensor indexing rules with some restrictions. See examples for more information.
- Parameters
- indicesUnion[Slice, tuple[list[int], int, slice]]
The indices to slice the variable by. See examples for more information.
- Returns
- out: Variable
a new variable that is a indexed selection of the original variable.
Examples
Integer indexing allows a selection of a single element in a dimension and removes that dimension in the output.
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> x[0].shape (1,) >>> x[0].value array([1.]) >>> x = csdl.Variable(value = np.arange(6).reshape(2,3)) >>> x[0].shape # removes the first dimension in the output (3,) >>> x[0].value array([0., 1., 2.]) >>> x[1,2].shape # returns a single element (1,) >>> x[1,2].value array([5.])
Slicing allows a selection of a range of elements in a dimension using slice notation and keeps that dimension in the output.
>>> x[1:2].shape # keeps the first dimension in the output (1, 3) >>> x[1:2].value array([[3., 4., 5.]]) >>> x[:].shape (2, 3) >>> np.all(x[:].value == x.value) True >>> x[1:2,:-1].shape (1, 2) >>> x[1:2,:-1].value array([[3., 4.]])
Integer lists allows for selecting a coordinate of elements across multiple dimensions and compresses them to one one dimension.
>>> x[[0,1]].shape (2, 3) >>> x[[0,1]].value array([[0., 1., 2.], [3., 4., 5.]]) >>> x[[0,1],[0,1]].shape # outputs x[0,0] and x[1,1] in a 1D array (2,) >>> x[[0,1],[0,1]].value # outputs x[0,0] and x[1,1] in a 1D array array([0., 4.])
All three types of indexing can be combined.
>>> x = csdl.Variable(value = np.arange(24).reshape(4,2,3)) >>> x[[0,1],1:].shape (2, 1, 3) >>> x[[0,1],1:].value array([[[ 3., 4., 5.]], [[ 9., 10., 11.]]]) >>> x[0,[0,1],[1,0]].shape (2,) >>> x[0,[0,1],[1,0]].value array([1., 3.])
- __init__(shape=None, name=None, value=None, tags=None, hierarchy=None)[source]
Initialize a Variable object.
- Parameters
- shapetuple, optional
The shape of the variable. If not provided, it will be inferred from the value.
- namestr, optional
The name of the variable.
- valueUnion[np.ndarray, float, int], optional
The initial value of the variable.
- tagslist[str], optional
A list of tags associated with the variable.
- hierarchyint, optional
The hierarchy level of the variable.
- Attributes
- hierarchyint
The hierarchy level of the variable.
- shapetuple
The shape of the variable.
- sizeint
The size of the variable.
- nameslist[str]
A list of names associated with the variable.
- valueUnion[np.ndarray, float, int]
The value of the variable.
- tagslist[str]
A list of tags associated with the variable.
- dtype
alias of
float64
- expand(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.]]])
- flatten()[source]
Returns a 1D version of the variable.
- Parameters
- selfVariable
- 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])) >>> x.flatten().value # reshapes to 1 dimension array([1., 2., 3., 4.])
- get(slices)[source]
Similar to __getitem__ but only accepts a Slice object.
- Parameters
- slicesSlice
- Returns
- out: Variable
- inner(other)[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
- selfVariable
First input tensor.
- otherVariableLike
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])) >>> x.inner(y).value array([32.]) >>> a = csdl.Variable(value = np.array([[1, 2], [3, 4]])) >>> b = csdl.Variable(value = np.array([[5, 6], [7, 8]])) >>> a.inner(b).value array([70.])
- print_on_update(string=None)[source]
Prints the variable value when the value is updated along with the string provided.
- Parameters
- stringstr, optional
additional string to print along with the value, by default prints the name of the node
- reshape(*shape)[source]
Returns a reshaped version of the variable.
- Parameters
- selfVariable
- 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.reshape(2,2).value # same thing as above array([[1., 2.], [3., 4.]]) >>> x.reshape(4).value # optionally pass in integers array([1., 2., 3., 4.])
- set(slices, value)[source]
Sets a sliced selection of the variable to a new value. The slicing must be specified by a csdl Slice object. See examples for more information.
- Parameters
- indicesSlice
The indices to slice the variable by. See examples for more information.
- valueVariableLike
The value to set the sliced selection of the variable to.
- Returns
- out: Variable
A new variable that represents the original variable with the sliced selection set to the new value.
Examples
The set method creates a new variable with the sliced selection set to the new value. The original variable is not modified.
>>> recorder = csdl.Recorder(inline = True) >>> recorder.start() >>> x = csdl.Variable(value = np.array([1.0, 2.0, 3.0])) >>> x1 = x.set(csdl.slice[0], 0.0) >>> x1.value array([0., 2., 3.])
Use the csdl.slice slicer object when using slices.
>>> x1 = x.set(csdl.slice[1:3], csdl.Variable(value = np.array([4.0, 5.0]))) >>> x1.value array([1., 4., 5.]) >>> x = csdl.Variable(value = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])) >>> x1 = x.set(csdl.slice[1, 1:3], csdl.Variable(value = np.array([10.0, 11.0]))) >>> x1.value array([[ 1., 2., 3.], [ 4., 10., 11.]])
The slicing conventions are identical to those in the __getitem__ method and broadcasting from a scalar is also supported.
>>> x1 = x.set(csdl.slice[[0,1], [1,2]], csdl.Variable(value = np.array([10.0, 11.0]))) >>> x1.value array([[ 1., 10., 3.], [ 4., 5., 11.]]) >>> x1 = x.set(csdl.slice[0, 1:], 10.0) >>> x1.value array([[ 1., 10., 10.], [ 4., 5., 6.]]) >>> x1 = x.set(csdl.slice[:, [0, 2]], 11.0) >>> x1.value array([[11., 2., 11.], [11., 5., 11.]])
Slicing with CSDL variables is also supported in some cases when the slice size is constant.
>>> start = csdl.Variable(value = 0) >>> x1 = x.set(csdl.slice[start:start+2, 1], 10.0) >>> x1.value array([[ 1., 10., 3.], [ 4., 10., 6.]])
Get the same behaviour of in-place modification by returning the same variable (it is recommended to combine slices to one .set call when possible to reduce the number of operations).
>>> x = x.set(csdl.slice[0,0], 10.0) >>> x = x.set(csdl.slice[1,0], 11.0) >>> x = x.set(csdl.slice[1,2], 12.0) >>> x.value array([[10., 2., 3.], [11., 5., 12.]])
- set_value(value)[source]
Sets the value of a variable.
- Parameters
- valueUnion[np.ndarray, float, int]
Value for the variable
- property value
The value of the variable used for inline evaluation