Tensor basics Flashcards

(12 cards)

1
Q

What is the difference between the functions below?

tf.constant([10, 9])
tf.Variable([10, 9])

A

constant object does not support item assignment once created (IMMUTABLE), while Variable does support changing its elemens this through the .assign() procedural attribute/method (MUTABLE).

Variable is not fully flexible though as it doesn’t allow changing its shape after creation.

Remember: constant means unchangeable while Variable means it can vary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two fundamental attributes from a tf.Tensor object?

A

Tensor.shape: tells you the size of the tensor along each of its axes.
Tensor.dtype: tells you the type of all the elements in the tensor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain how the random.shuffle() function works.

shuffled_tensor = tf.random.shuffle(not_shuffled_tensor)

A

random.shuffle() will shuffle the not_shuffle_tensor along its first dimension, which is the rows.

ATTENTION: It will shuffle all elements within the first column. Then, it move to the second column and so on.

It won’t shuffle elements within the first row, then move on to the other rows. This would correspond to shuffle across along the second dimension, which are the columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Imagine we want to create a tensor from a numpy array with shape (3,8). What is something we need to watch out for in terms of the shape of the tensor?

A

The shapes of the array and tensor must be compatible.

Given that the array has a shape of (3,8), it has 24 elements. We can only create tensors with a shape that also results in 24 elements.

For example, we can reshape the array into a (3, 4, 2) tensor. By multiplying the number of elements in each dimension of the shape, we get 3 * 4 * 2 = 24.
t1 = tf.constant(arr, shape=(3,4,2))

However, we cannot reshape it to a (3, 4, 5) tensor since these would have 3 * 4 * 5 = 60 elements and we only have 24 elements. Non-retangular tensors, with “holes” within it, are not allowed.
t2 = tf.constant(arr, shape=(3,4,5))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe the main attributes of tensors below:
* Rank
* Size
* Shape
* Axis or dimension

A

Shape: The length (number of elements) of each of the axes of a tensor.

Rank: Number of tensor axes. A scalar has rank 0, a vector has rank 1, a matrix is rank 2.

Axis or Dimension: A particular dimension of a tensor.

Size: The total number of items in the tensor, the product of the shape vector’s elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is keyboard shorcut for showing the docstring of a function inside a Jupyter Notebook?

A

Shift + Tab

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Suppose we have two tensors A and B. How can we do matrix and element-wise multiplication?

A

Element-wise multiplication
A * B

Matrix multiplication
tf.linalg.matmul(A,B)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is one function that we can use to change the shape of an existing tensor?

A
tf.reshape(tensor, shape, name=None)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can we do one-hot enconding in TensorFlow?

A

Use the tf.one_hot() function.

We need to pass in the the data to be enconded and the depth that the number values to encoded this data.

For example, if we have three categories (Good, Neutral, Bad), then we should set depth = 3.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can we convert a tensor to a numpy array?

A

Use the .numpy method in tensor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Assume you were given the tensor shown below. When and how can you change the value of the element highlighted to 99?

A

This change is only possible if the tensor was created as a Variable. In contrast, it if were created as a constant, then you cannot change its value anymore.

In the former case, we can use the assign method as shown below.

tensor_name[1,0,2].assign(99)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Assume t1 and t2 are tensors. What is the result from the code below
t1@t2

A

The @ operator will perform matrix multiplication between t1 and t2.

The number of columns in t1 must match the number of rows in t2 in order for the multiplication to be possible.

The resulting tensor will have the number of rows from t1 and the number of columns from t2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly