0
0
TensorFlowml~10 mins

Numpy interoperability 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 convert a TensorFlow tensor to a NumPy array.

TensorFlow
import tensorflow as tf

tensor = tf.constant([1, 2, 3])
array = tensor.[1]()
Drag options to blanks, or click blank then click option'
Aarray
Btolist
Cnumpy
Dconvert
Attempts:
3 left
💡 Hint
Common Mistakes
Using tolist() returns a Python list, not a NumPy array.
Using array() or convert() are not valid TensorFlow tensor methods.
2fill in blank
medium

Complete the code to create a TensorFlow tensor from a NumPy array.

TensorFlow
import tensorflow as tf
import numpy as np

np_array = np.array([4, 5, 6])
tensor = tf.[1](np_array)
Drag options to blanks, or click blank then click option'
Acreate_tensor
Bfrom_numpy
Ctensor_from_array
Dconvert_to_tensor
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_numpy or tensor_from_array are not TensorFlow functions.
create_tensor is not a valid TensorFlow function.
3fill in blank
hard

Fix the error in the code to correctly convert a TensorFlow tensor to a NumPy array.

TensorFlow
import tensorflow as tf

tensor = tf.constant([7, 8, 9])
array = tensor.[1]
Drag options to blanks, or click blank then click option'
Anumpy()
Bnumpy
Cto_numpy()
Dtoarray()
Attempts:
3 left
💡 Hint
Common Mistakes
Using numpy without parentheses returns the method itself, not the array.
to_numpy() and toarray() are not TensorFlow tensor methods.
4fill in blank
hard

Fill both blanks to create a TensorFlow tensor from a NumPy array and then convert it back to a NumPy array.

TensorFlow
import tensorflow as tf
import numpy as np

np_array = np.array([10, 11, 12])
tensor = tf.[1](np_array)
back_to_np = tensor.[2]()
Drag options to blanks, or click blank then click option'
Aconvert_to_tensor
Bnumpy
Dfrom_numpy
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_numpy instead of convert_to_tensor.
Using numpy without parentheses for conversion.
5fill in blank
hard

Fill all three blanks to create a NumPy array, convert it to a TensorFlow tensor, and then get its shape.

TensorFlow
import tensorflow as tf
import numpy as np

np_array = np.array([[1], [2], [3]])
tensor = tf.convert_to_tensor(np_array)
shape = tensor.shape
Drag options to blanks, or click blank then click option'
A1
B2
C3
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-integer or missing values in the array.
Not matching the number of blanks with the number of elements.