3. TensorFlow Development
Activity 3.01: Using TensorBoard to Visualize Tensor Transformations
Solution:
- Import the TensorFlow library and set a seed:
import tensorflow as tf tf.random.set_seed(42)
- Set the log directory and initialize a file writer object to write the trace:
logdir = 'logs/' writer = tf.summary.create_file_writer(logdir)
- Create a TensorFlow function to multiply two tensors and add a value of
1
to all elements in the resulting tensor using theones_like
function to create a tensor of the same shape as the result of the matrix multiplication. Then, apply a sigmoid function to each value of the tensor:@tf.function def my_func(x, y): r1 = tf.matmul(x, y) r2 = r1 + tf.ones_like(r1) r3 = tf.keras.activations.sigmoid(r2) return r3
- Create two tensors with the shape
5x5x5
:x = tf.random.uniform((5, 5, 5)) y = tf.random.uniform((5, 5, 5))
- Turn...