Beispiel #1
0
 def call(self, inputs):
     self.grid = ops.convert_to_tensor(self.grid, dtype=self.dtype)
     self.bounds = ops.convert_to_tensor(self.bounds, dtype=self.dtype)
     inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
     queryPoints_ind = ((cast(shape(self.grid)[1:3], dtype=self.dtype)) -
                        constant(1.0)) * (inputs - self.bounds[0]) / (
                            self.bounds[1] - self.bounds[0])
     if common_shapes.rank(inputs) == 2:
         queryPoints_ind = expand_dims(queryPoints_ind, 0)
     output = interpolate(self.grid, queryPoints_ind)
     if common_shapes.rank(inputs) == 2:
         output = array_ops.reshape(output, (array_ops.shape(output)[1], ) +
                                    (array_ops.shape(output)[2], ))
     return output
Beispiel #2
0
    def call(self, inputs, params=None):

        if params[self.name + '/kernel:0'] is None:
            return super(layers.Dense, self).call(inputs)
        else:
            kernel = params.get(self.name + '/kernel:0')
            bias = params.get(self.name + '/bias:0')

        inputs = ops.convert_to_tensor(inputs)
        rank = common_shapes.rank(inputs)
        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = standard_ops.tensordot(inputs, kernel, [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            if not context.executing_eagerly():
                shape = inputs.shape.as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            # Cast the inputs to self.dtype, which is the variable dtype. We do not
            # cast if `should_cast_variables` is True, as in that case the variable
            # will be automatically casted to inputs.dtype.
            if not self._mixed_precision_policy.should_cast_variables:
                inputs = math_ops.cast(inputs, self.dtype)
            outputs = gen_math_ops.mat_mul(inputs, kernel)
        if self.use_bias:
            outputs = nn.bias_add(outputs, bias)
        if self.activation is not None:
            return self.activation(outputs)  # pylint: disable=not-callable
        return outputs
Beispiel #3
0
    def call(self, inputs):
        inputs = ops.convert_to_tensor(inputs)
        rank = common_shapes.rank(inputs)
        if rank > 2:
            raise ValueError(
                'mpusim_fc_base only supports tensors of rank <= 2')
        else:

            outputs = mpu_sim_mat_mul_lib.mpu_sim_mat_mul(
                inputs,
                self.kernel,
                activations_datatype_size_byte=self.
                activations_datatype_size_byte,
                weights_datatype_size_byte=self.weights_datatype_size_byte,
                results_datatype_size_byte=self.results_datatype_size_byte,
                systolic_array_height=self.systolic_array_height,
                systolic_array_width=self.systolic_array_width,
                activation_fifo_depth=self.activation_fifo_depth,
                accumulator_array_height=self.accumulator_array_height,
                log_file_output_dir=self.log_file_output_dir,
                model_name=self.model_name)

        if self.use_bias:
            outputs = nn.bias_add(outputs, self.bias)
        if self.activation is not None:
            return self.activation(outputs)
        return outputs
Beispiel #4
0
 def call(self, inputs):
     inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
     rank = common_shapes.rank(inputs)
     if rank is not 2:
         raise ValueError('`ParisLaw` only takes "rank 2" inputs.')
     output = self.kernel[0] * (inputs**self.kernel[1])
     return output
Beispiel #5
0
    def call(self, inputs):
        inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
        rank = common_shapes.rank(inputs)

        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = standard_ops.tensordot(inputs, self.kernel,
                                             [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            if not context.executing_eagerly():
                shape = inputs.get_shape().as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            outputs = gen_math_ops.mat_mul(inputs, self.kernel)

        w_norm = 0.5 * tf.linalg.norm(self.kernel, axis=0)**2
        x_norm = tf.expand_dims(0.5 * tf.linalg.norm(inputs, axis=-1)**2,
                                axis=-1)

        outputs = gen_math_ops.add(outputs, -w_norm)
        outputs = gen_math_ops.add(outputs, -x_norm)
        outputs /= (self.sigma**2)

        return self.activation(outputs)
Beispiel #6
0
def sn_fully_connected(inputs,
                       num_outputs,
                       scope,
                       activation_fn=tf.nn.relu,
                       weights_regularizer=None):
    with tf.variable_scope(scope):
        input_dim = inputs.shape[-1].value
        assert input_dim
        kernel = sn_kernel(shape=(input_dim, num_outputs), scope='kernel')
        bias = tf.get_variable(name='bias',
                               shape=(num_outputs, ),
                               initializer=tf.initializers.zeros)
        rank = common_shapes.rank(inputs)
        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = tf.tensordot(inputs, kernel, [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            # if not context.executing_eagerly():
            #    shape = inputs.get_shape().as_list()
            #    output_shape = shape[:-1] + [self.units]
            #    outputs.set_shape(output_shape)
        else:
            # Cast the inputs to self.dtype, which is the variable dtype. We do not
            # cast if `should_cast_variables` is True, as in that case the variable
            # will be automatically casted to inputs.dtype.
            outputs = tf.matmul(inputs, kernel)
        outputs = tf.nn.bias_add(outputs, bias)
        if activation_fn is not None:
            return activation_fn(outputs)  # pylint: disable=not-callable
        else:
            return outputs
Beispiel #7
0
    def call(self, inputs, reverse=False):
        inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
        rank = common_shapes.rank(inputs)

        kernel = self.kernel if not reverse else self.kernel_inverse

        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = standard_ops.tensordot(inputs, kernel, [[rank - 1], [0]])
            if not context.executing_eagerly():
                shape = inputs.get_shape().as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            outputs = gen_math_ops.mat_mul(inputs, kernel)

        if reverse:
            return outputs

        batch_size = tf.cast(tf.shape(inputs)[0], tf.float32)
        sequence_length = tf.cast(tf.shape(inputs)[1], tf.float32)
        # tf.logdet only works on Hermitian positive def matrices, maybe try tf.slogdet?
        log_det_W = batch_size * sequence_length * tf.log(
            tf.linalg.det(self.kernel))
        return outputs, log_det_W
Beispiel #8
0
    def call(self, inputs, params=None):

        if params[self.name + '/kernel:0'] is None:
            return super(layers.Dense, self).call(inputs)
        else:
            kernel = params.get(self.name + '/kernel:0')
            bias = params.get(self.name + '/bias:0')

        inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
        rank = common_shapes.rank(inputs)
        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = standard_ops.tensordot(inputs, kernel, [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            if not context.executing_eagerly():
                shape = inputs.get_shape().as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            outputs = gen_math_ops.mat_mul(inputs, kernel)
        if self.use_bias:
            outputs = nn.bias_add(outputs, bias)
        if self.activation is not None:
            return self.activation(outputs)  # pylint: disable=not-callable
        return outputs
Beispiel #9
0
 def call(self, inputs):
     """
         Apply a clipped matrix multiplication and sign activation function.
         Backpropagation is made on non-clipped and without sign expression.
     :param inputs:
     :return:
     """
     inputs = ops.convert_to_tensor(inputs)
     rank = common_shapes.rank(inputs)
     if rank > 2:
         # Broadcasting is required for the inputs.
         outputs = _clippedTensorDot(self.deviceName, inputs, self.kernel,
                                     rank)
         # Reshape the output back to the original ndim of the input.
         if not context.executing_eagerly():
             shape = inputs.get_shape().as_list()
             output_shape = shape[:-1] + [self.units]
             outputs.set_shape(output_shape)
     else:
         outputs = _clippedMatMul(self.deviceName, inputs, self.kernel)
     if self.use_bias:
         outputs = nn.bias_add(outputs, self.bias)
     if self.activation == tf.nn.softmax:
         outputs = self.activation(outputs)  # pylint: disable=not-callable
         #We must not clip the activation on either -1 or 1, because the activation is the softmax!!
         return outputs
     #We must clip the activation on either -1 or 1, use of sign function:
     with tf.device(self.deviceName):
         with ops.name_scope("signOp", [outputs]) as scope:
             return tf.add(
                 tf.stop_gradient(tf.sign(outputs) - outputs),
                 outputs,
                 name=scope
             )  #Use of stop_gradient enable rigorous backpropagation here
Beispiel #10
0
    def call(self, inputs):
        quant_kernel = tf.quantization.fake_quant_with_min_max_vars(
            self.kernel,
            min=tf.math.reduce_min(self.kernel),
            max=tf.math.reduce_max(self.kernel),
            num_bits=8,
            narrow_range=False,
            name=None)

        inputs = ops.convert_to_tensor(inputs)
        rank = common_shapes.rank(inputs)
        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = standard_ops.tensordot(inputs, quant_kernel,
                                             [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            if not context.executing_eagerly():
                shape = inputs.get_shape().as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            outputs = gen_math_ops.mat_mul(inputs, quant_kernel)
        if self.use_bias:
            outputs = nn.bias_add(outputs, self.bias)
        if self.activation is not None:
            return self.activation(outputs)  # pylint: disable=not-callable
        return outputs
Beispiel #11
0
    def call(self, inputs):
        inputs = tf.convert_to_tensor(inputs, dtype=self.dtype)
        with tf.name_scope("actQ"):
            tf.compat.v1.summary.histogram('prebinary_activations', inputs)
            if self.bits is not None:
                inputs = self.actQ(inputs, float(self.bits))
            else:
                inputs = self.actQ(inputs)
            tf.compat.v1.summary.histogram('binary_activations', inputs)
        with tf.name_scope("weightQ"):
            kernel = self.weightQ(self.kernel)
            tf.compat.v1.summary.histogram('weights', self.kernel)
            tf.compat.v1.summary.histogram('binary_weights', kernel)
        rank = common_shapes.rank(inputs)
        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = tf.tensordot(inputs, kernel, [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            if not context.executing_eagerly():
                shape = inputs.get_shape().as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            outputs = tf.matmul(inputs, kernel)

        if self.use_bias:
            outputs = tf.nn.bias_add(outputs, self.bias)

        if self.use_act and self.activation is not None:
            outputs = self.activation(outputs)  # pylint: disable=not-callable

        return outputs
    def call(self, inputs, training=None):
        if self.lr_mul == 1.0:
            W = self.coeff * self.kernel
        else:
            @custom_gradient
            def lr_multiplier(x):
                y = array_ops.identity(x)
                def grad(dy):
                    return dy * self.lr_mul
                return y, grad
            W = lr_multiplier(self.coeff * self.kernel)

        training = self._get_training_value(training)

        # Update singular vector by power iteration
        W_T = array_ops.transpose(W)
        u = array_ops.identity(self.u)
        for i in range(self.power_iter):
            v = nn_impl.l2_normalize(math_ops.matmul(u, W))  # 1 x filters
            u = nn_impl.l2_normalize(math_ops.matmul(v, W_T))
        # Spectral Normalization
        sigma_W = math_ops.matmul(math_ops.matmul(u, W), array_ops.transpose(v))
        # Backprop doesn't need in power iteration
        sigma_W = array_ops.stop_gradient(sigma_W)
        W_bar = W / array_ops.squeeze(sigma_W)

        # Assign new singular vector
        training_value = tf_utils.constant_value(training)
        if training_value is not False:
            def u_update():
                def true_branch():
                    return self._assign_singular_vector(self.u, u)
                def false_branch():
                    return self.u
                return tf_utils.smart_cond(training, true_branch, false_branch)
            self.add_update(u_update)

        # normal Dense using W_bar
        inputs = ops.convert_to_tensor(inputs)
        rank = common_shapes.rank(inputs)
        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = standard_ops.tensordot(inputs, W_bar, [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            if not context.executing_eagerly():
                shape = inputs.shape.as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            inputs = math_ops.cast(inputs, self._compute_dtype)
            outputs = math_ops.mat_mul(inputs, W_bar)
        if self.use_bias:
            outputs = nn.bias_add(outputs, self.bias)
        if self.activation is not None:
            return self.activation(outputs)  # pylint: disable=not-callable
        return outputs
Beispiel #13
0
    def call(self, inputs):
        if self.use_bias:
            raise ValueError()

        rank = common_shapes.rank(inputs)
        outputs = standard_ops.tensordot(inputs, self.kernel,
                                         [[rank - 1], [0]])
        shape = inputs.get_shape().as_list()
        output_shape = shape[:-1] + [self.units]
        outputs.set_shape(output_shape)
        return outputs
Beispiel #14
0
    def call(self, inputs):
        inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
        if common_shapes.rank(inputs) is not 2:
            raise ValueError(
                '`StressIntensityRange` only takes "rank 2" inputs.')

        output = gen_math_ops.mul(self.kernel * inputs[:, 1],
                                  gen_math_ops.sqrt(np.pi * inputs[:, 0]))
        output = array_ops.reshape(output, (array_ops.shape(output)[0], 1))

        # outputs should be (None, 1), so it is still rank = 2
        return output
Beispiel #15
0
    def call(self, inputs):
        inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
        if common_shapes.rank(inputs) is not 2:
            raise ValueError('`WalkerModel` only takes "rank 2" inputs.')

        sig = 1 / (1 + gen_math_ops.exp(self.kernel[0] * inputs[:, 1]))
        gamma = sig * self.kernel[1]
        C = self.kernel[2] / ((1 - inputs[:, 1])**(self.kernel[3] *
                                                   (1 - gamma)))
        output = C * (inputs[:, 0]**self.kernel[3])
        output = array_ops.reshape(output, (array_ops.shape(output)[0], 1))
        return output
    def call(self, inputs):
        rank = common_shapes.rank(inputs)

        # This is same as linear output of a normal dense layer but
        # it's not used as layer output. Instead, this value (key) is
        # used to calculate the actual weights.
        key = standard_ops.tensordot(inputs, self.key_kernel, [[rank - 1], [0]])
        
        if self.use_key_bias:
            key = key + self.key_bias
        
        return super(PolymorphicDense, self).call(key, inputs)
Beispiel #17
0
 def call(self, inputs):
     inputs = ops.convert_to_tensor(inputs)
     rank = common_shapes.rank(inputs)
     if rank > 2:
         # Broadcasting is required for the inputs.
         outputs = clippedTensorDot(inputs, self.kernel, rank,self.bias,self.rate, self.rateInhib)
         # Reshape the output back to the original ndim of the input.
         if not context.executing_eagerly():
             shape = inputs.get_shape().as_list()
             output_shape = shape[:-1] + [self.units]
             outputs.set_shape(output_shape)
     else:
         outputs = clippedMatMul(inputs, self.kernel,self.bias,self.rate, self.rateInhib)
     if self.activation is not None:
         return self.activation(outputs)  # pylint: disable=not-callable
     return outputs
Beispiel #18
0
 def call(self, inputs):
   inputs = ops.convert_to_tensor(inputs)
   rank = common_shapes.rank(inputs)
   if rank > 2:
     # Broadcasting is required for the inputs.
     outputs = standard_ops.tensordot(inputs, self.kernel, [[rank - 1], [0]])
     # Reshape the output back to the original ndim of the input.
     if not context.executing_eagerly():
       shape = inputs.get_shape().as_list()
       output_shape = shape[:-1] + [self.units]
       outputs.set_shape(output_shape)
   else:
     outputs = gen_math_ops.mat_mul(inputs, self.kernel)
   if self.use_bias:
     outputs = nn.bias_add(outputs, self.bias)
   if self.activation is not None:
     return self.activation(outputs)  # pylint: disable=not-callable
   return outputs
Beispiel #19
0
 def call(self, inputs):
   inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
   rank = common_shapes.rank(inputs)
   if rank > 2:
     # Broadcasting is required for the inputs.
     outputs = standard_ops.tensordot(inputs, self.kernel, [[rank - 1], [0]])
     # Reshape the output back to the original ndim of the input.
     if not context.executing_eagerly():
       shape = inputs.get_shape().as_list()
       output_shape = shape[:-1] + [self.units]
       outputs.set_shape(output_shape)
   else:
     outputs = gen_math_ops.mat_mul(inputs, self.kernel)
   if self.use_bias:
     outputs = nn.bias_add(outputs, self.bias)
   if self.activation is not None:
     return self.activation(outputs)  # pylint: disable=not-callable
   return outputs
Beispiel #20
0
 def call(self, inputs):
     hadamard_product = tf.math.multiply(self.mask, self.kernel)
     inputs = ops.convert_to_tensor(inputs)
     rank = common_shapes.rank(inputs)
     if rank > 2:
         output = tf.linalg.matmul(inputs, hadamard_product)
         shape = inputs.get_shape().as_list()
         output_shape = shape[:-1] + [self.units]
         output.set_shape(output_shape)
     else:
         if not self._mixed_precision_policy.should_cast_variables:
             inputs = math_ops.cast(inputs, self.dtype)
         output = tf.linalg.matmul(inputs, hadamard_product)
     if self.use_bias:
         output = K.bias_add(output, self.bias, data_format=None)
     # pass the result of the layer through the activ. function
     output = self.activation(output)
     return output
Beispiel #21
0
def fc_shared_weight(inputs,
                     vocab_size,
                     embeddings,
                     biases_initializer,
                     activation_fn=None):
    with tf.variable_scope('fc_shared_weights'):
        word_weight = tf.transpose(embeddings, (1, 0))
        embedding_dim = embeddings.shape[-1].value
        assert embedding_dim
        input_dim = inputs.shape[-1].value
        assert input_dim == embedding_dim
        blank_weight = tf.get_variable(
            name='blank_weight',
            initializer=tf.initializers.random_normal(0.05),
            dtype=tf.float32,
            shape=(embedding_dim, 1))
        weight = tf.concat([blank_weight, word_weight], axis=-1)
        print("fc_shared_weight: {}, {}, {}".format(blank_weight, word_weight,
                                                    weight))
        bias = tf.get_variable(name='bias',
                               initializer=biases_initializer,
                               shape=(vocab_size + 1, ),
                               dtype=tf.float32)
        rank = common_shapes.rank(inputs)
        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = tf.tensordot(inputs, weight, [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            # if not context.executing_eagerly():
            #    shape = inputs.get_shape().as_list()
            #    output_shape = shape[:-1] + [self.units]
            #    outputs.set_shape(output_shape)
        else:
            # Cast the inputs to self.dtype, which is the variable dtype. We do not
            # cast if `should_cast_variables` is True, as in that case the variable
            # will be automatically casted to inputs.dtype.
            outputs = tf.matmul(inputs, weight)
        outputs = tf.nn.bias_add(outputs, bias)
        if activation_fn is not None:
            return activation_fn(outputs)  # pylint: disable=not-callable
        else:
            return outputs
Beispiel #22
0
    def call(self, inputs, training=None):
        inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
        rank = common_shapes.rank(inputs)
        # Newly added lines for targeted dropout
        if training is None:
            training = K.learning_phase()
        if (training):
            if (self.targeted_dropout_type == "weight"):
                dropped_kernel = targeted_weight_dropout(
                    self.kernel, self.targeted_dropout_rate, self.dropout_rate,
                    K.learning_phase())
            elif (self.targeted_dropout_type == "unit"):
                dropped_kernel = targeted_unit_dropout(
                    self.kernel, self.targeted_dropout_rate, self.dropout_rate,
                    K.learning_phase())
            else:
                raise ValueError("Should be of 'weight' or 'unit'")

        if rank > 2:
            # Broadcasting is required for the inputs.
            if (training):
                outputs = standard_ops.tensordot(inputs, dropped_kernel,
                                                 [[rank - 1], [0]])
            else:
                outputs = standard_ops.tensordot(inputs, self.kernel,
                                                 [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            if not context.executing_eagerly():
                shape = inputs.get_shape().as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            if (training):
                outputs = gen_math_ops.mat_mul(inputs, dropped_kernel)
            else:
                outputs = gen_math_ops.mat_mul(inputs, self.kernel)

        if self.use_bias:
            outputs = nn.bias_add(outputs, self.bias)
        if self.activation is not None:
            return self.activation(outputs)  # pylint: disable=not-callable
        return outputs
Beispiel #23
0
    def call(self, inputs):
        inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)
        rank = common_shapes.rank(inputs)
        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = standard_ops.tensordot(inputs, self.kernel, [[rank - 1], [0]])
            if not context.executing_eagerly():
                shape = inputs.get_shape().as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            outputs = gen_math_ops.mat_mul(inputs, self.kernel)

        scale = self.scale / (tf.norm(self.kernel, 2, 0) + 1e-8)
        outputs = outputs * scale
        if self.use_bias:
            outputs = nn.bias_add(outputs, self.bias)
        if self.activation is not None:
            return self.activation(outputs)
        return outputs
Beispiel #24
0
 def call(self, inputs):
   inputs = ops.convert_to_tensor(inputs)
   rank = common_shapes.rank(inputs)
   if rank > 2:
     # Broadcasting is required for the inputs.
     outputs = standard_ops.tensordot(inputs, self.kernel, [[rank - 1], [0]])
     # Reshape the output back to the original ndim of the input.
     if not context.executing_eagerly():
       shape = inputs.shape.as_list()
       output_shape = shape[:-1] + [self.units]
       outputs.set_shape(output_shape)
   else:
     # Cast the inputs to self.dtype, which is the variable dtype. We do not
     # cast if `should_cast_variables` is True, as in that case the variable
     # will be automatically casted to inputs.dtype.
     if not self._mixed_precision_policy.should_cast_variables:
       inputs = math_ops.cast(inputs, self.dtype)
     outputs = gen_math_ops.mat_mul(inputs, self.kernel)
   if self.use_bias:
     outputs = nn.bias_add(outputs, self.bias)
   if self.activation is not None:
     return self.activation(outputs)  # pylint: disable=not-callable
   return outputs
Beispiel #25
0
    def call(self, inputs):
        inputs = ops.convert_to_tensor(inputs)
        rank = common_shapes.rank(inputs)

        softmax_kernel = self.kernel * tf.nn.softmax(
            logits=tf.abs(self.kernel) / self.temperature,
            axis=0,
        )

        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = standard_ops.tensordot(
                inputs,
                # that's the actual change
                softmax_kernel,
                [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            if not context.executing_eagerly():
                shape = inputs.shape.as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            # Cast the inputs to self.dtype, which is the variable dtype. We do not
            # cast if `should_cast_variables` is True, as in that case the variable
            # will be automatically casted to inputs.dtype.
            if not self._mixed_precision_policy.should_cast_variables:
                inputs = math_ops.cast(inputs, self.dtype)
            outputs = gen_math_ops.mat_mul(
                inputs,
                # that's the actual change
                softmax_kernel,
            )
        if self.use_bias:
            outputs = nn.bias_add(outputs, self.bias)
        if self.activation is not None:
            return self.activation(outputs)  # pylint: disable=not-callable
        return outputs
Beispiel #26
0
    def call(self, inputs):

        inputs = ops.convert_to_tensor(inputs)
        rank = common_shapes.rank(inputs)
        if rank > 2:
            # Broadcasting is required for the inputs.
            outputs = standard_ops.tensordot(inputs, self.kernel,
                                             [[rank - 1], [0]])
            # Reshape the output back to the original ndim of the input.
            if not context.executing_eagerly():
                shape = inputs.get_shape().as_list()
                output_shape = shape[:-1] + [self.units]
                outputs.set_shape(output_shape)
        else:
            outputs = gen_math_ops.mat_mul(inputs, self.kernel)

        if self.activation is not None:
            outputs = self.activation(outputs)  # pylint: disable=not-callable

        if self.verbose > 0:
            print(outputs.get_shape(), 'outputs before masking')

        if self.leaky_inputs:
            if self.verbose > 0:
                print('performing mask op')
            self.full_outputs = outputs
            # outputs_d = {}
            # for dim in range(outputs.get_shape()[1]):

            outputs_1d = tf.reshape(tf.transpose(outputs), [
                -1,
            ],
                                    name='outputs_1d_')

            # mask_array = self.mask_array['key_' + str(dim)]
            if self.verbose > 0:
                print('mask_array', self.mask_array.get_shape())

            mask_array_1d = tf.reshape(self.mask_array, [
                -1,
            ],
                                       name='mask_ph_1d_')

            if self.verbose > 0:
                print('mask_array_1d', mask_array_1d.get_shape())

            mask = tf.math.greater(mask_array_1d,
                                   tf.constant(0.0),
                                   name='masking_op_')

            outputs = outputs_1d[mask]
            outputs = tf.expand_dims(outputs, axis=1)
            # outputs_d['val_' + str(dim)] = outputs

            if self.verbose > 0:
                print(outputs.get_shape(), 'shape of output after masking')

            return outputs
        else:
            self.full_outputs = outputs

            if self.verbose > 0:
                print(outputs.get_shape(), 'shape of output without masking')

            return outputs
Beispiel #27
0
    def call(self, inputs):
        inputs = ops.convert_to_tensor(inputs, dtype=self.dtype)

        enable_quantop_dense = int(os.getenv('ENABLE_QUANTOP_DENSE', 0))
        if enable_quantop_dense == 1:
            inputs_qs = quantemu_ops.quantize_emu(
                inputs,
                data_format='unknown',
                allocate_copy=int(os.getenv('QUANTEMU_ALLOCATE_COPY_INPUTS',
                                            0)),
                data_type=int(os.getenv('QUANTEMU_DENSE_DATA_TYPE', 0)),
                precision=int(os.getenv('QUANTEMU_PRECISION_DENSE_INPUTS',
                                        23)),
                exponent_bits=int(os.getenv('QUANTEMU_EXPBITS', 5)),
                round_mode=int(os.getenv('QUANTEMU_RMODE_INPUTS', 0)))

            kernel_qs = quantemu_ops.quantize_emu(
                self.kernel,
                data_format='unknown',
                allocate_copy=int(
                    os.getenv('QUANTEMU_ALLOCATE_COPY_FILTERS', 0)),
                data_type=int(os.getenv('QUANTEMU_DENSE_DATA_TYPE', 0)),
                precision=int(os.getenv('QUANTEMU_PRECISION_DENSE_FILTERS',
                                        23)),
                exponent_bits=int(os.getenv('QUANTEMU_EXPBITS', 5)),
                round_mode=int(os.getenv('QUANTEMU_RMODE_FILTERS', 0)))
            rank = common_shapes.rank(inputs)
            if rank > 2:
                # Broadcasting is required for the inputs.
                outputs = standard_ops.tensordot(inputs_qs, kernel_qs,
                                                 [[rank - 1], [0]])
                # Reshape the output back to the original ndim of the input.
                if not context.executing_eagerly():
                    shape = inputs.get_shape().as_list()
                    output_shape = shape[:-1] + [self.units]
                    outputs.set_shape(output_shape)
            else:
                outputs = gen_math_ops.mat_mul(inputs_qs, kernel_qs)
            if self.use_bias:
                outputs = nn.bias_add(outputs, self.bias)
            if self.activation is not None:
                return self.activation(outputs)  # pylint: disable=not-callable
            return outputs

        else:  # No quantization

            rank = common_shapes.rank(inputs)
            if rank > 2:
                # Broadcasting is required for the inputs.
                outputs = standard_ops.tensordot(inputs, self.kernel,
                                                 [[rank - 1], [0]])
                # Reshape the output back to the original ndim of the input.
                if not context.executing_eagerly():
                    shape = inputs.get_shape().as_list()
                    output_shape = shape[:-1] + [self.units]
                    outputs.set_shape(output_shape)
            else:
                outputs = gen_math_ops.mat_mul(inputs, self.kernel)
            if self.use_bias:
                outputs = nn.bias_add(outputs, self.bias)
            if self.activation is not None:
                return self.activation(outputs)  # pylint: disable=not-callable
            return outputs