how do I import numpy and tensorflow to python
import numpy as np
import tensorflow as tf
how do we tell tensorflow to compute the grandient descent
with tf.GradientTape() as tape:
this will do the backprop and the gradients but in reverse
what is the main part of tensorflow code
the main part is the cost function, that is the thing we’ll try to optimize
what is GradientTape doing
is recording the computational graph to then do the same backwards for back propagation
what is a tf.Tensor
this is the equivalent to an Array in numpy which contains information about the computational graph
how do we store the state of the variables in TensorFlow
with tf.Variable, also it has an argument for dtype
what’s the name of the datasets in TensorFlow
HDF5
how can we know the shape and dtype of a dataset in TensorFlow
using the element_spect attribute
how do we input a new image or input into a tensor dataset
it must be changed and normalized with some functions to a tensor structure, for example in the case of an image:
def normalize(image):
image = tf.cast(image, tf.float32) /255
image = tf.reshape(image, [-1,])
return image
and now after finishing we can return the image that is now normalized and reshaped
Then after doing that, we use the map method in tensorflow for the whole dataset, for example:
new_train = x_train.map(normalize)
new_test = x_test.map(normalize)
in the following equation, which ones are matrices and which ones vectors
Y = WX + b
W and X are matrices and b is a vector
all of them are random
how do we create variables and constants in tensorflow
with tf.constant()
and tf.Variable()
In both cases we need to state first the shape and then the name.
Variables can be changed but constants cannot be changed
how can I cast z into a tensor of type float32
tf.cast(z, tf.float32)
how do I implement sigmoid or softmax in tensorflow
If I have for example z and want an activation using sigmoid or softmax, I can use
tf.keras.activations.sigmoid(z)
or
tf.keras.activations.softmax(z)
what is a one hot encoding
is when a vector for example [1 2 3 0 2 1] is turned into a converted representation of 0 and 1 in which each column represents one of the number but only one of the rows what represent the classes has a 1 per column
how can we do a one hot encoding in tensorflow
tf.one_hot(labels, depth, axis=0)
axis = 0 indicates the new axis is created at dimension 0
how do we initialize with zeros or ones in tensorflow
tf.zeros()
tf.ones()
What is a Glorot initializer
it’s an initializer in which we sample from the normal distribution centered at 0 but truncated
how do we use an initializer in tensor flow
the easiest way to do it is to just assign as a variable the initializer like this
initializer = tf.keras.initializers.GlorotNormal(seed=1) or similar
so then when creating a variable we can do the following:
tf.Variable(initializer(shape = (32,1) ) )
basically we put inside () of the tf.Variable what we want and in this case we want to use the initializer using the specific function so we just provide the shape
what are the steps to build a neural network using tensorflow
we have to do 2 things:
1. implement the forward propagation
2. retrieve the gradients and train the model