class_docstr: Convolutional Neural Network for image classification.
  
  It assumes the inputs are images of shape width x height x channels.
  The precise architecture follows the Tensorflow CNN
  tutorial at https://www.tensorflow.org/tutorials/images/deep_cnn.
  Note that this CNN works both for the agreement and classification models.
  In the agreement case, the provided inputs will be a tuple
  (inputs_src, inputs_target), which are aggregated into one input after the
  convolution layers, right before the fully connected network that makes the
  final prediction.
  
  Attributes:
    output_dim: Integer representing the number of classes.
    channels: Integer representing the number of channels in the input images
      (e.g., 1 for black and white, 3 for RGB).
    aggregation: String representing an aggregation operation, that is applied
      on the two inputs of the agreement model, after they are encoded through
      the convolution layers. See superclass attributes for details.
    activation: An activation function to be applied to the outputs of each
      fully connected layer of the aggregation network.
    is_binary_classification: Boolean specifying if this is model for
      binary classification. If so, it uses a different loss function and
      returns predictions with a single dimension, batch size.
    name: String representing the model name.
class_docstr_tok: ["Convolutional","Neural","Network","for","image","classification",".","It","assumes","the","inputs","are","images","of","shape","width","x","height","x","channels",".","The","precise","architecture","follows","the","Tensorflow","CNN","tutorial","at","https",":","\/\/www.tensorflow.org\/tutorials\/images\/deep_cnn",".","Note","that","this","CNN","works","both","for","the","agreement","and","classification","models",".","In","the","agreement","case",",","the","provided","inputs","will","be","a","tuple","(","inputs_src",",","inputs_target",")",",","which","are","aggregated","into","one","input","after","the","convolution","layers",",","right","before","the","fully","connected","network","that","makes","the","final","prediction",".","Attributes",":","output_dim",":","Integer","representing","the","number","of","classes",".","channels",":","Integer","representing","the","number","of","channels","in","the","input","images","(","e.g.",",","1","for","black","and","white",",","3","for","RGB",")",".","aggregation",":","String","representing","an","aggregation","operation",",","that","is","applied","on","the","two","inputs","of","the","agreement","model",",","after","they","are","encoded","through","the","convolution","layers",".","See","superclass","attributes","for","details",".","activation",":","An","activation","function","to","be","applied","to","the","outputs","of","each","fully","connected","layer","of","the","aggregation","network",".","is_binary_classification",":","Boolean","specifying","if","this","is","model","for","binary","classification",".","If","so",",","it","uses","a","different","loss","function","and","returns","predictions","with","a","single","dimension",",","batch","size",".","name",":","String","representing","the","model","name","."]
docstr: Creates all hidden layers of the model, before the prediction layer.
  
  Args:
    inputs: A tensor containing the model inputs. The first dimension is the
      batch size.
  
  Returns:
    A tuple containing the encoded representation of the inputs and a
    dictionary of regularization parameters.
docstr_tok: ["Creates","all","hidden","layers","of","the","model",",","before","the","prediction","layer",".","Args",":","inputs",":","A","tensor","containing","the","model","inputs",".","The","first","dimension","is","the","batch","size",".","Returns",":","A","tuple","containing","the","encoded","representation","of","the","inputs","and","a","dictionary","of","regularization","parameters","."]
code: def _construct_layers(self, inputs):
      """Creates all hidden layers of the model, before the prediction layer.
  
      Args:
        inputs: A tensor containing the model inputs. The first dimension is the
          batch size.
  
      Returns:
        A tuple containing the encoded representation of the inputs and a
        dictionary of regularization parameters.
      """
  
      # A dictionary of parameters on top of which we add weight decay.
      reg_params = {}
  
      # Convolution 1.
      with tf.variable_scope("conv1") as scope:
          kernel = tf.get_variable(
              "kernel",
              shape=[5, 5, self.channels, 64],
              initializer=tf.truncated_normal_initializer(
                  stddev=5e-2, dtype=tf.float32
              ),
              dtype=tf.float32,
          )
          conv = tf.nn.conv2d(
              inputs, kernel, [1, 1, 1, 1], padding="SAME"
          )
          biases = tf.get_variable(
              "biases",
              [64],
              initializer=tf.constant_initializer(0.0),
              dtype=tf.float32,
          )
          pre_activation = tf.nn.bias_add(conv, biases)
          conv1 = tf.nn.relu(pre_activation, name=scope.name)
  
      # Max pooling 1.
      pool1 = tf.nn.max_pool(
          conv1,
          ksize=[1, 3, 3, 1],
          strides=[1, 2, 2, 1],
          padding="SAME",
          name="pool1",
      )
      # Local Response Normalization 1.
      norm1 = tf.nn.lrn(
          pool1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name="norm1"
      )
      # Convolution 2.
      with tf.variable_scope("conv2") as scope:
          kernel = tf.get_variable(
              "kernel",
              shape=[5, 5, 64, 64],
              initializer=tf.truncated_normal_initializer(
                  stddev=5e-2, dtype=tf.float32
              ),
              dtype=tf.float32,
          )
          conv = tf.nn.conv2d(
              norm1, kernel, [1, 1, 1, 1], padding="SAME"
          )
          biases = tf.get_variable(
              "biases",
              [64],
              initializer=tf.constant_initializer(0.1),
              dtype=tf.float32,
          )
          pre_activation = tf.nn.bias_add(conv, biases)
          conv2 = tf.nn.relu(pre_activation, name=scope.name)
  
      # Local Response Normalization 2.
      norm2 = tf.nn.lrn(
          conv2, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name="norm2"
      )
      # Max pooling 2.
      pool2 = tf.nn.max_pool(
          norm2,
          ksize=[1, 3, 3, 1],
          strides=[1, 2, 2, 1],
          padding="SAME",
          name="pool2",
      )
  
      # Layer 3.
      with tf.variable_scope("local3") as scope:
          # Move everything into depth so we can perform a single matrix multiply.
          reshape = tf.keras.layers.Flatten()(pool2)
          dim = reshape.get_shape()[1].value
          weights = tf.get_variable(
              "weights",
              shape=[dim, 384],
              initializer=tf.truncated_normal_initializer(
                  stddev=0.04, dtype=tf.float32
              ),
              dtype=tf.float32,
          )
          reg_params[weights.name] = weights
          biases = tf.get_variable(
              "biases",
              [384],
              initializer=tf.constant_initializer(0.1),
              dtype=tf.float32,
          )
          local3 = tf.nn.relu(
              tf.matmul(reshape, weights) + biases, name=scope.name
          )
  
      # Layer 4.
      with tf.variable_scope("local4") as scope:
          weights = tf.get_variable(
              "weights",
              shape=[384, 192],
              initializer=tf.truncated_normal_initializer(
                  stddev=0.04, dtype=tf.float32
              ),
              dtype=tf.float32,
          )
          reg_params[weights.name] = weights
          biases = tf.get_variable(
              "biases",
              [192],
              initializer=tf.constant_initializer(0.1),
              dtype=tf.float32,
          )
          local4 = tf.nn.relu(
              tf.matmul(local3, weights) + biases, name=scope.name
          )
  
      return local4, reg_params
  
file_path: neural-structured-learning/research/gam/gam/models/cnn.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from .models_base import Model
  import tensorflow
label_desc: Use image node_id 2 for calling the ImageCNNAgreement obj's underlying member method code with example usage: obj._construct_layers(inputs) and returns: local4,  reg_params