Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Custom and Distributed Training with Tensorflow/Week 1 - Differentiation and Gradients/C2_W1_Lab_1_basic-tensors.ipynb
Views: 13372
Basic Tensors
In this ungraded lab, you will try some of the basic operations you can perform on tensors.
Imports
Exercise on basic Tensor operations
Lets create a single dimension numpy array on which you can perform some operation. You'll make an array of size 25, holding values from 0 to 24.
Now that you have your 1-D array, next you'll change that array into a tensor
. After running the code block below, take a moment to inspect the information of your tensor.
As the first operation to be performed, you'll square (element-wise) all the values in the tensor x
One feature of tensors is that they can be reshaped. When reshpaing, make sure you consider dimensions that will include all of the values of the tensor.
Notice that you'll get an error message if you choose a shape that cannot be exactly filled with the values of the given tensor.
Run the cell below and look at the error message
Try to change the tuple that is passed to
shape
to avoid an error.
Like reshaping, you can also change the data type of the values within the tensor. Run the cell below to change the data type from int
to float
Next, you'll create a single value float tensor by the help of which you'll see broadcasting
in action
Multiply the tensors x
and y
together, and notice how multiplication was done and its result.
Re-Initialize y
to a tensor having more values.
Add the tensors x
and y
together, and notice how addition was done and its result.
The shape parameter for tf.constant
When using tf.constant()
, you can pass in a 1D array (a vector) and set the shape
parameter to turn this vector into a multi-dimensional array.
The shape parameter for tf.Variable
Note, however, that for tf.Variable()
, the shape of the tensor is derived from the shape given by the input array. Setting shape
to something other than None
will not reshape a 1D array into a multi-dimensional array, and will give a ValueError
.