CNN With Tensor Flow
CNN With Tensor Flow
Descent Optimization
Stochastic Gradient Descend (SGD)
Stochastically sample “mini-batches” from dataset D
The size of Bj can contain even just 1 sample
route”
● Assumption: Test data will be no different than training
data
For ML we cannot make this assumption ==> test data are
always different
Gradient Descent (GD) vs
Stochastic Gradient Descent (SGD)
Stochastic Gradient Descent
● SGD is preferred to Gradient Descend
● (A bit) Noisy gradients act as regularization
[X] [W] b
1*4
3*4
1*4
7*3 Repeated 7 times
7*4
[X] * [W] [X] * [W] + b
7*4 7*4
tf.zeros
tf.zeros(shape, dtype=tf.float32, name=None)
Args:
● shape: Either a list of integers, or a 1-D Tensor of type int32.
● dtype: The type of an element in the resulting Tensor.
● name: A name for the operation (optional).
● tf.zeros([3, 4], tf.int32) ==> [ [0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0] ]
● tf.zeros(4) ==> [0, 0, 0, 0]
tf.nn.conv2d
tf.nn.conv2d (input, filter, strides, padding,
use_cudnn_on_gpu=None, data_format=None, name=None)
Computes a 2-D convolution given 4-D input and filter tensors.
Given an input tensor of shape [batch, in_height, in_width,
in_channels] and a filter / kernel tensor of shape [filter_height,
filter_width, in_channels, out_channels], this op performs the
following:
● Flattens the filter to a 2-D matrix with shape [filter_height *
filter_width * in_channels, output_channels].
● Extracts image patches from the input tensor to form a
virtual tensor of shape [batch, out_height, out_width,
filter_height * filter_width * in_channels].
tf.nn.conv2d
tf.nn.conv2d (input, filter, strides, padding, use_cudnn_on_gpu=None,
data_format=None, name=None)
Args:
● input: A Tensor. Must be one of the following types: half, float32, float64.
● filter: A Tensor. Must have the same type as input.
● strides: A list of ints. 1-D of length 4. The stride of the sliding window for each
dimension of input. Must be in the same order as the dimension specified with
format.
● padding: A string from: "SAME", "VALID". The type of padding algorithm to use.
● use_cudnn_on_gpu: An optional bool. Defaults to True.
● data_format: An optional string from: "NHWC", "NCHW". Defaults to "NHWC".
Specify the data format of the input and output data. With the default format
"NHWC", the data is stored in the order of: [batch, in_height, in_width,
in_channels]. Alternatively, the format could be "NCHW", the data storage order
of: [batch, in_channels, in_height, in_width].
tf.nn.max_pool
tf.nn.max_pool(value, ksize, strides, padding,
data_format='NHWC', name=None)
Args:
● value: A 4-D Tensor with shape [batch, height, width, channels]
and type tf.float32.
● ksize: A list of ints that has length >= 4. The size of the window for
each dimension of the input tensor.
● strides: A list of ints that has length >= 4. The stride of the sliding
window for each dimension of the input tensor.
● padding: A string, either 'VALID' or 'SAME'. The padding algorithm.
● data_format: A string. 'NHWC' and 'NCHW' are supported.
● name: Optional name for the operation.
●
tf.reshape
tf.reshape(tensor, shape, name=None)
Reshapes a tensor.
Given tensor, this operation returns a tensor that has the
same values as tensor with shape shape.
If one component of shape is the special value -1, the size
of that dimension is computed so that the total size remains
constant. In particular, a shape of [-1] flattens into 1-D.
At most one component of shape can be -1.
If x size is 117600, then
tf.reshape(x, [-1, 28, 28, 3]) will generate 50
images each of size 28*28*3 channels
tf.nn.dropout
tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None)
Args:
● x: A tensor.
● keep_prob: A scalar Tensor with the same type as x. The probability that
each element is kept.
● noise_shape: A 1-D Tensor of type int32, representing the shape for
randomly generated keep/drop flags.
● seed: A Python integer. Used to create random seeds.
● name: A name for this operation (optional).
With probability keep_prob, outputs the input element scaled up by 1 /
keep_prob, otherwise outputs 0. The scaling is so that the expected sum is
unchanged.
By default, each element is kept or dropped independently.
Input Image
28*28*3 Input Image 28*28 pixels, 3 Channels
32 Filters
Convolute with 32 Filters,
[5*5] *3 [5*5] *3 [5*5] *3
Each is 5*5*3
32 Feature map
O/P is 32 images
28*28 28*28 Feature Map 28*28 Each is 28*28 (use padding)
Training Labels
Parameters Calculations
Input values
28*28*3 Input Image 28*28*3
32 Filters
Filter parameters
[5*5] *3 [5*5] *3 [5*5] *3
(5*5*3) * 32 + 32 {bias}
32 Feature map
64 Filters
…………………………………………..definitial
bias_variable(shape):
= tf.constant(0.1,shape=shape)
return tf.Variable(initial)
…………………………………………..definitial
bias_variable(shape):
= tf.constant(0.1,shape=shape)
return tf.Variable(initial)
Training Labels
Train the Network
● Feed Forward, Calculate Estimated output
● Apply “Softmax” on Output
● Loss Calculation (Cross_Entropy)
● Optimization (Minimize Loss and Update
Weights)
64 feature map
y_conv =
7*7 7*7 7*7 7*7 tf.matmul(h_fc1_drop, W_fc2) + b_fc2
7x7x64 values
cross_entropy =
…………………………………………..
tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
10
O/P layer
y_conv
Softmax
y_
Training Labels
64 feature map
…………………………………………..
Softmax
y_conv
Cross Entropy Error
y_
Training Labels
Model Evaluation
(Training Data / Test Data)
Start
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
Useful Operations
in Tensorflow
Operation Description
tf.add sum
tf.sub substraction
tf.mul multiplication
tf.div division
tf.mod
tf.abs
module
return the absolute value
Some
tf.neg return negative value Useful
tf.sign
tf.inv
return the sign
returns the inverse
Arithmetic
tf.square calculates the square Operations
tf.round returns the nearest integer
tf.sqrt calculates the square root
tf.pow calculates the power
tf.exp calculates the exponential
tf.log calculates the logarithm
tf.maximum returns the maximum
tf.minimum returns the minimum
tf.cos calculates the cosine
tf.sin calculates the sine
Some useful Matrix Operations
Operation Description