Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Operations: Arithmetic | Tensors
Introduction to TensorFlow

Basic Operations: ArithmeticBasic 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 into tf.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 to tf.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.

For a deeper understanding of broadcasting, you can refer to the official NumPy documentation page on this subject.

Tarea

Given a set of matrices, perform the following operations:

  1. Inplace addition of a 2x2 matrix.
  2. Subtraction using tf.subtract() method for a 2x3 matrix.
  3. Broadcasting multiplication of a 3x2 matrix with another 1x2 matrix.
  4. 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.

¿Todo estuvo claro?

Sección 1. Capítulo 8
toggle bottom row
course content

Contenido del Curso

Introduction to TensorFlow

Basic Operations: ArithmeticBasic 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 into tf.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 to tf.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.

For a deeper understanding of broadcasting, you can refer to the official NumPy documentation page on this subject.

Tarea

Given a set of matrices, perform the following operations:

  1. Inplace addition of a 2x2 matrix.
  2. Subtraction using tf.subtract() method for a 2x3 matrix.
  3. Broadcasting multiplication of a 3x2 matrix with another 1x2 matrix.
  4. 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.

¿Todo estuvo claro?

Sección 1. Capítulo 8
toggle bottom row
some-alt