Basic Operations: Arithmetic
Arithmetic Operations
TensorFlow provides numerous arithmetic operations for tensor manipulations. These operations and many others in TensorFlow support broadcasting, making it easier to perform element-wise operations on tensors of different shapes.
Addition
For tensors addition we can use tf.add()
, .assign_add()
methods and a plus +
sign. Also we can use broadcasting with the plus sign +
or with the tf.add()
method.
Broadcasting makes it possible to carry out element-wise operations on tensors of different, but compatible, shapes by virtually expanding the smaller tensor to match the shape of the larger tensor.
Note
For the inplace method, the fundamental element must be a mutable
Variable
type rather than a constant.
Subtraction
We have analogues of all methods for subtraction as for addition:
tf.add()
changes intotf.subtract()
;- Plus sign
+
changes into minus sign-
; .assign_add()
changes into.assign_sub()
.
Multiplication (Element-wise)
For multiplication, there isn't an inplace method since matrix multiplication inherently results in a new object. However, other operations have their counterparts:
tf.add()
corresponds totf.multiply()
;- The plus sign
+
corresponds to the asterisk sign*
.
Division
Similar to multiplication, but with tf.divide()
and /
sign.
Broadcasting
Broadcasting is the term used to describe how tensors with different shapes are automatically and implicitly treated during arithmetic operations so that they appear as if they have the same shape. It allows operations to be performed on tensors of different sizes without explicitly resizing them first.
Завдання
Given a set of matrices, perform the following operations:
- Inplace addition of a 2x2 matrix.
- Subtraction using
tf.subtract()
method for a 2x3 matrix. - Broadcasting multiplication of a 3x2 matrix with another 1x2 matrix.
- Broadcasting division between two matrices, one of size 2x3 and the other 2x1.
Note
Note the broadcasting behavior in the multiplication and division operations. In multiplication, it's like multiplying
[[1, 2], [3, 4], [5, 6]]
with the[[2, 4], [2, 4], [2, 4]]
. In division, it's like dividing[[2, 4, 6], [4, 8, 12]]
by[[2, 2, 2], [4, 4, 4]]
.In the first case, broadcasting expands matrix along the 0th axis (first parameter of shape), while in the second case, matrix is expanded along the 1st axis (second parameter of shape). It depends on the shape of the matrices.
Все було зрозуміло?
Зміст курсу
Introduction to TensorFlow
2. Basics of TensorFlow
Introduction to TensorFlow
Basic Operations: Arithmetic
Arithmetic Operations
TensorFlow provides numerous arithmetic operations for tensor manipulations. These operations and many others in TensorFlow support broadcasting, making it easier to perform element-wise operations on tensors of different shapes.
Addition
For tensors addition we can use tf.add()
, .assign_add()
methods and a plus +
sign. Also we can use broadcasting with the plus sign +
or with the tf.add()
method.
Broadcasting makes it possible to carry out element-wise operations on tensors of different, but compatible, shapes by virtually expanding the smaller tensor to match the shape of the larger tensor.
Note
For the inplace method, the fundamental element must be a mutable
Variable
type rather than a constant.
Subtraction
We have analogues of all methods for subtraction as for addition:
tf.add()
changes intotf.subtract()
;- Plus sign
+
changes into minus sign-
; .assign_add()
changes into.assign_sub()
.
Multiplication (Element-wise)
For multiplication, there isn't an inplace method since matrix multiplication inherently results in a new object. However, other operations have their counterparts:
tf.add()
corresponds totf.multiply()
;- The plus sign
+
corresponds to the asterisk sign*
.
Division
Similar to multiplication, but with tf.divide()
and /
sign.
Broadcasting
Broadcasting is the term used to describe how tensors with different shapes are automatically and implicitly treated during arithmetic operations so that they appear as if they have the same shape. It allows operations to be performed on tensors of different sizes without explicitly resizing them first.
Завдання
Given a set of matrices, perform the following operations:
- Inplace addition of a 2x2 matrix.
- Subtraction using
tf.subtract()
method for a 2x3 matrix. - Broadcasting multiplication of a 3x2 matrix with another 1x2 matrix.
- Broadcasting division between two matrices, one of size 2x3 and the other 2x1.
Note
Note the broadcasting behavior in the multiplication and division operations. In multiplication, it's like multiplying
[[1, 2], [3, 4], [5, 6]]
with the[[2, 4], [2, 4], [2, 4]]
. In division, it's like dividing[[2, 4, 6], [4, 8, 12]]
by[[2, 2, 2], [4, 4, 4]]
.In the first case, broadcasting expands matrix along the 0th axis (first parameter of shape), while in the second case, matrix is expanded along the 1st axis (second parameter of shape). It depends on the shape of the matrices.
Все було зрозуміло?