How Convolution 2D layers work in TF learn

How Convolution 2D layers work in TF learn

Recipe Objective

This recipe explains how convolution 2D layer works in TFLayer.

Convolution 2D

Its syntax is: tflearn.layers.conv.conv_2d (incoming, nb_filter, filter_size, strides=1, padding='same', activation='linear', bias=True, weights_init='uniform_scaling', bias_init='zeros', regularizer=None, weight_decay=0.001, trainable=True, restore=True, reuse=False, scope=None, name='Conv2D')

def conv_2d(incoming, nb_filter, filter_size, strides=1, padding='same', activation='linear', bias=True, weights_init='uniform_scaling', bias_init='zeros', regularizer=None, weight_decay=0.001, trainable=True, restore=True, reuse=False, scope=None, name="Conv2D"):

    ip = utils.get_incoming_shape(incoming)

    assert len(ip) == 2, "Incoming Tensor shape must be 2-D, not %d-D" % len(ip)

    filter_size = utils.autoformat_filter_conv2d(filter_size, ip[-1], nb_filter)

    strides = utils.autoformat_kernel_2d(strides)

    padding = utils.autoformat_padding(padding)

    with tf.variable_scope(scope, default_name=name, values=[incoming], reuse=reuse) as scope:

       name = scope.name

       if isinstance(weights_init, str):

          weights_init = initializations.get(weights_init)()

       elif type(weights_init) in [tf.Tensor, np.ndarray, list]:

          filter_size = None

       reg = None

       if regularizer is not None:

          reg = lambda y: regularizers.get(regularizer)(y, weight_decay)

       w = vs.variable('w', shape=filter_size, regularizer=reg, initializer=W_init, trainable=trainable, restore=restore)

       # Track per layer variables

       tf.add_to_collection(tf.GraphKeys.LAYER_VARIABLES + '/' + name, w)

       a = None

       if bias:

          a_shape = [nb_filter]

          if isinstance(bias_init, str):

             bias_init = initializations.get(bias_init)()

          elif type(bias_init) in [tf.Tensor, np.ndarray, list]:

             a_shape = None

          a = vs.variable('a', shape=a_shape, initializer=bias_init,        trainable=trainable, restore=restore)

          # Track per layer variables

          tf.add_to_collection(tf.GraphKeys.LAYER_VARIABLES + '/' + name, a)

       inf = tf.nn.conv2d(incoming, w, strides, padding)

       if a is not None:

          inf = tf.nn.bias_add(inf, a)

       if activation:

          if isinstance(activation, str):

             inf = activations.get(activation)(inf)

          elif hasattr(activation, '__call__'):

             inf = activation(inf)

          else:

             raise ValueError("Invalid Activation.")

       # Track activations.

       tf.add_to_collection(tf.GraphKeys.ACTIVATIONS, inf)

    # Add attributes to Tensor to easy access weights.

    inference.scope = scope

    inference.w = w

    inference.a = a

    # Track output tensor.

    tf.add_to_collection(tf.GraphKeys.LAYER_TENSOR + '/' + name, inf)

    return inf

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.