0
0
TensorFlowml~10 mins

Tensor math operations in TensorFlow - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a tensor filled with zeros of shape (3, 3).

TensorFlow
import tensorflow as tf
zero_tensor = tf.[1](shape=(3, 3))
print(zero_tensor)
Drag options to blanks, or click blank then click option'
Aones
Bzeros
Crandom_uniform
Dconstant
Attempts:
3 left
💡 Hint
Common Mistakes
Using tf.ones instead of tf.zeros
Forgetting to specify the shape argument
2fill in blank
medium

Complete the code to add two tensors element-wise.

TensorFlow
import tensorflow as tf
x = tf.constant([1, 2, 3])
y = tf.constant([4, 5, 6])
sum_tensor = tf.[1](x, y)
print(sum_tensor)
Drag options to blanks, or click blank then click option'
Aadd
Bsubtract
Cmultiply
Ddivide
Attempts:
3 left
💡 Hint
Common Mistakes
Using tf.multiply instead of tf.add
Using the '+' operator instead of tf.add (which also works but here we want the function)
3fill in blank
hard

Fix the error in the code to compute the matrix multiplication of two tensors.

TensorFlow
import tensorflow as tf
A = tf.constant([[1, 2], [3, 4]])
B = tf.constant([[5, 6], [7, 8]])
result = tf.[1](A, B)
print(result)
Drag options to blanks, or click blank then click option'
Amatmul
Bmultiply
Cadd
Dsubtract
Attempts:
3 left
💡 Hint
Common Mistakes
Using tf.multiply which does element-wise multiplication
Using tf.add which adds tensors
4fill in blank
hard

Fill both blanks to create a tensor of shape (2, 3) filled with ones and then multiply it by 5.

TensorFlow
import tensorflow as tf
ones_tensor = tf.[1](shape=(2, 3))
scaled_tensor = ones_tensor [2] 5
print(scaled_tensor)
Drag options to blanks, or click blank then click option'
Aones
B*
C+
Dzeros
Attempts:
3 left
💡 Hint
Common Mistakes
Using tf.zeros instead of tf.ones
Using '+' instead of '*' for scaling
5fill in blank
hard

Fill both blanks to compute its square, and then find the mean of the squared values.

TensorFlow
import tensorflow as tf
x = tf.constant([1, 2, 3, 4])
squared = tf.math.[1](x)
mean_val = tf.reduce_[2](squared)
print(mean_val.numpy())  # Output the mean as a number
Drag options to blanks, or click blank then click option'
Asquare
Bmean
Csum
Dabs
Attempts:
3 left
💡 Hint
Common Mistakes
Using tf.math.abs instead of tf.math.square
Using tf.reduce_sum instead of tf.reduce_mean