You are on page 1of 5

Tensorflow

Basic Basic

Resources to follow https://bit.ly/36MlpJt

Status Ongoing

Define deep learning?


Deep learning takes data e.g images etc and turn them into numbers and then find
patterns into those numbers or Deep learning is used to simulate the functioning of
human brain by adding 3 to more layers. We can use machine learning for anything
as long as we convert inputs into numbers it could be anything.

Supervised, Unsupervised, Reinforcement/Semi-supervised Learning and


Transfer learning:
In supervised learning we give the input data with label and classes which can be
easy to identify, Unsupervised is used to classifiy the data based on their similarities
in the data. Semi-supervised works by using reward and punish approach. Transfer
learning is use the knowledge of one algorithm by another without training it on
another dataset.

GPU vs TPU:

Tensorflow 1
GPU is used to do parrallel task more efficiently than CPU and can be used for
image, video and natural language processing where TPU can be used to do the
multiplication of matrix upto hundred of TFLOPS (Trillion Floating Point Operations
per Second).

What is a tensor?
Tensor is a mathematical object or ndarray in which we represent a physical model
to the computer in the form of matrix. e.g. When we gives input to the model it
converts that input of image/video/voice into tensors (in numbers) which can be
easily understood by the neural network.
By default, TensorFlow creates tensors with either an  int32  or  float32  datatype.
The more it’s precise the more it takes space on memory.

tensor = tf.constant([[[1, 2, 3],


[4, 5, 6]],
[[7, 8, 9],
[10, 11, 12]],
[[13, 14, 15],
[16, 17, 18]]])
tensor.ndim

This is known as a rank 3 tensor (3-dimensions), however a tensor can have an


arbitrary (unlimited) amount of dimensions.
For example, you might turn a series of images into tensors with shape (224, 224, 3,
32), where:

224, 224 (the first 2 dimensions) are the height and width of the images in pixels.

3 is the number of colour channels of the image (red, green blue).

32 is the batch size (the number of images a neural network sees at any one
time).

The difference between  tf.Variable() and  tf.constant()  is tensors created


with  tf.constant()  are immutable (can't be changed, can only be used to create a
new tensor), where as, tensors created with  tf.Variable()  are mutable (can be
changed).

We can change the value of tensor created with tf.Variable() using assign() method.

Tensorflow 2
changeable =tf.Variable([7,3])
unchangeable = tf.constant([7,3])
changeable, unchangeable
changeable[0].assign(17)
unchangeable[0].assign(70) #We can't do this because it's constant
changeable

Tensors vs Arrays
The only difference b/w both of these are tensors can be run on GPUs. We can
reshape 3D, 4D... arrays or tensors into 1D by putting -1 inside reshape method e.g.
take a look at below code

arr = np.array([[[ 1, 2, 3],


[ 4, 5, 6]],

[ [ 7, 8, 9],
[10, 11, 12]
]])

change_dim.ndim
change_dim.reshape(-1)

# For converting tensor into 1D array


tensor = tf.constant([[[ 1, 2, 3],
[ 4, 5, 6]],

[ [ 7, 8, 9],
[10, 11, 12]
]])
tensor
tensor_new = tf.reshape(tensor,shape=(-1))
tensor_new
# For iterating through all the elements in a tensor we can use nditor method
for item in np.nditer(tensor):
print(item)
# Or we can do the long version of it.
for x in tensor:
for y in x:
for z in y:
print(z.numpy())

Why we need to shuffle tensors are images in a dataset?


By shuffling tensor or images prevent the model from overfitting e.g. Let’s say we
have 5,000 of cat images and 10,000 of dogs images and training the model on it

Tensorflow 3
will make it overfit by looking to the order of the dataset that’s why we need to shuffle
the dataset so that every image in it is in random order.

Why we need SparseTensor?


When a tensor contain many zeros like below tensor then it's called
sparseTensor.

Tensor are the simple tensor which contain some numerical values.

SparseTensor is needed when we have an image which contain lots of dark pixel
which is detoned by zero so we simply mitigate those dark pixel denoted by zero
and take the actual values and their corresponding indices and shape which
reduce computation time of our GPU instead of calculating the zeros values
also.

Hyperparameter Typical value

Same shape as number of features (e.g. 3 for #


Input layer shape bedrooms, # bathrooms, # car spaces in
housing price prediction)
Problem specific, minimum = 1, maximum =
Hidden layer(s)
unlimited

Neurons per hidden layer Problem specific, generally 10 to 100

Same shape as desired prediction shape (e.g. 1


Output layer shape
for house price)

Hidden activation Usually ReLU (rectified linear unit)

Output activation None, ReLU, logistic/tanh

Tensorflow 4
Hyperparameter Typical value

MSE (mean square error) or MAE (mean


Loss function absolute error)/Huber (combination of
MAE/MSE) if outliers

Optimizer SGD (stochastic gradient descent), Adam

Tensorflow 5

You might also like