Ejemplo n.º 1
0
    def inverse(self,
                x,
                objective,
                yy=None,
                nlf0=None,
                nlf1=None,
                iso=None,
                cam=None):
        z = x
        squeeze_factor = self.hps.squeeze_factor

        for i in range(self.n_levels):
            z = squeeze2d(z, squeeze_factor, self.hps.squeeze_type)
            if yy is not None:
                yy = squeeze2d(yy, squeeze_factor, self.hps.squeeze_type)
            for bijector in self.model[i]:
                if type(bijector) in [
                        AffineCouplingCondY, AffineCouplingCondXY,
                        AffineCouplingFitSdnGain2, AffineCouplingCondYG,
                        AffineCouplingCamSdn, AffineCouplingCondXYG,
                        AffineCouplingSdnGain, AffineCouplingSdn,
                        AffineCouplingGain, AffineCouplingGainEx1,
                        AffineCouplingGainEx2, AffineCouplingGainEx3,
                        AffineCouplingSdnEx1, AffineCouplingSdnEx2,
                        AffineCouplingSdnEx3, AffineCouplingSdnEx4,
                        AffineCouplingGainEx4, AffineCouplingSdnEx5,
                        AffineCouplingSdnEx6
                ]:
                    try:
                        z, log_abs_det_J_inv = \
                            bijector._inverse_and_log_det_jacobian(z, yy, nlf0, nlf1, iso, cam)
                    except Exception as e:
                        print(e)
                        z = bijector._inverse(z, yy, nlf0, nlf1, iso, cam)
                        log_abs_det_J_inv = bijector._inverse_log_det_jacobian(
                            z, yy, nlf0, nlf1, iso, cam)
                else:
                    try:
                        z, log_abs_det_J_inv = \
                            bijector._inverse_and_log_det_jacobian(z)
                    except Exception as e:
                        print(e)
                        z = bijector._inverse(z)
                        log_abs_det_J_inv = bijector._inverse_log_det_jacobian(
                            z)
                objective += log_abs_det_J_inv
            if i < self.n_levels - 1:
                z, objective = split2d("pool{}".format(i), z, objective)
        return z, objective
Ejemplo n.º 2
0
 def _forward_and_log_det_jacobian(self,
                                   x,
                                   yy,
                                   nlf0=None,
                                   nlf1=None,
                                   iso=None,
                                   cam=None):
     if self._last_layer:
         x = tf.reshape(x, (-1, self.i0, self.i1, self.ic))
         yy = tf.reshape(yy, (-1, self.i0, self.i1, self.ic))
     if 2 * x.shape[1] == yy.shape[1]:
         yy = squeeze2d(yy, 2)
     x0 = x[:, :, :, :self.ic // 2]
     x1 = x[:, :, :, self.ic // 2:]
     x0yy = tf.concat([x0, yy], axis=-1)
     shift, log_scale = self._shift_and_log_scale_fn(x0yy, iso)
     log_scale = self.scale * tf.tanh(log_scale)
     y1 = x1
     if shift is not None:
         y1 -= shift
     if log_scale is not None:
         y1 *= tf.exp(-log_scale)
     y = tf.concat([x0, y1], axis=-1)
     if log_scale is None:
         log_abs_det_J = tf.constant(0., dtype=x.dtype, name="fldj")
     else:
         log_abs_det_J = -tf.reduce_sum(log_scale, axis=[1, 2, 3])
     return y, log_abs_det_J
Ejemplo n.º 3
0
 def _forward(self, x, yy, nlf0=None, nlf1=None, iso=None, cam=None):
     if self._last_layer:
         x = tf.reshape(x, (-1, self.i0, self.i1, self.ic))
         yy = tf.reshape(yy, (-1, self.i0, self.i1, self.ic))
     if yy.shape[1] == 2 * x.shape[1]:  # needs squeezing
         yy = squeeze2d(yy, 2)
     shift, log_scale = self._shift_and_log_scale_fn(yy, iso)
     log_scale = self.scale * tf.tanh(log_scale)
     y = x  # x[:, :, :, self.ic // 2:]
     if shift is not None:
         y -= shift
     if log_scale is not None:
         y *= tf.exp(-log_scale)
     return y
Ejemplo n.º 4
0
    def _forward(self, x, yy, nlf0=None, nlf1=None, iso=None, cam=None):
        if self._last_layer:
            x = tf.reshape(x, (-1, self.i0, self.i1, self.ic))
            yy = tf.reshape(yy, (-1, self.i0, self.i1, self.ic))

        if yy.shape[1] == 2 * x.shape[1]:  # needs squeezing
            yy = squeeze2d(yy, 2)

        scale = sdn_model_params_ex4(yy, iso, self.gain_init)
        shift = 0.0

        y = x
        if scale is not None:
            y *= scale
        if shift is not None:
            y += shift
        return y
Ejemplo n.º 5
0
    def _forward(self, x, yy, nlf0=None, nlf1=None, iso=None, cam=None):
        if self._last_layer:
            x = tf.reshape(x, (-1, self.i0, self.i1, self.ic))
            yy = tf.reshape(yy, (-1, self.i0, self.i1, self.ic))

        if yy.shape[1] == 2 * x.shape[1]:  # needs squeezing
            yy = squeeze2d(yy, 2)

        beta1, beta2 = sdn_iso_model_params_3(iso)
        scale = tf.sqrt(beta1 * yy + beta2)
        shift = 0.0

        y = x
        if scale is not None:
            y *= scale
        if shift is not None:
            y += shift
        return y
Ejemplo n.º 6
0
 def _forward_log_det_jacobian(self,
                               x,
                               yy,
                               nlf0=None,
                               nlf1=None,
                               iso=None,
                               cam=None):
     if self._last_layer:
         x = tf.reshape(x, (-1, self.i0, self.i1, self.ic))
         yy = tf.reshape(yy, (-1, self.i0, self.i1, self.ic))
     if 2 * x.shape[1] == yy.shape[1]:
         yy = squeeze2d(yy, 2)
     x0 = x[:, :, :, :self.ic // 2]
     x0yy = tf.concat([x0, yy], axis=-1)
     _, log_scale = self._shift_and_log_scale_fn(x0yy, iso)
     log_scale = self.scale * tf.tanh(log_scale)
     if log_scale is None:
         return tf.constant(0., dtype=x.dtype, name="fldj")
     return -tf.reduce_sum(log_scale, axis=[1, 2, 3])
Ejemplo n.º 7
0
 def _forward(self, x, yy, nlf0=None, nlf1=None, iso=None, cam=None):
     # print('_forward-------')
     # import pdb
     # pdb.set_trace()
     if self._last_layer:
         x = tf.reshape(x, (-1, self.i0, self.i1, self.ic))
         yy = tf.reshape(yy, (-1, self.i0, self.i1, self.ic))
     if 2 * x.shape[1] == yy.shape[1]:
         yy = squeeze2d(yy, 2)
     x0 = x[:, :, :, :self.ic // 2]
     x1 = x[:, :, :, self.ic // 2:]
     x0yy = tf.concat([x0, yy], axis=-1)
     shift, log_scale = self._shift_and_log_scale_fn(x0yy, iso)
     log_scale = self.scale * tf.tanh(log_scale)
     y1 = x1
     if shift is not None:
         y1 -= shift
     if log_scale is not None:
         y1 *= tf.exp(-log_scale)
     y = tf.concat([x0, y1], axis=-1)
     return y