示例#1
0
  def test_gradients_numerical(self):
    with spectral_ops_test_util.fft_kernel_label_map(), (
        self.test_session(use_gpu=True)):
      # Tuples of (signal_length, frame_length, frame_step, fft_length,
      # stft_bound, inverse_stft_bound).
      # TODO(rjryan): Investigate why STFT gradient error is so high.
      test_configs = [
          (64, 16, 8, 16),
          (64, 16, 16, 16),
          (64, 16, 7, 16),
          (64, 7, 4, 9),
          (29, 5, 1, 10),
      ]

      for (signal_length, frame_length, frame_step, fft_length) in test_configs:
        signal_shape = [signal_length]
        signal = random_ops.random_uniform(signal_shape)
        stft_shape = [max(0, 1 + (signal_length - frame_length) // frame_step),
                      fft_length // 2 + 1]
        stft = spectral_ops.stft(signal, frame_length, frame_step, fft_length,
                                 pad_end=False)
        inverse_stft_shape = [(stft_shape[0] - 1) * frame_step + frame_length]
        inverse_stft = spectral_ops.inverse_stft(stft, frame_length, frame_step,
                                                 fft_length)
        stft_error = test.compute_gradient_error(signal, [signal_length],
                                                 stft, stft_shape)
        inverse_stft_error = test.compute_gradient_error(
            stft, stft_shape, inverse_stft, inverse_stft_shape)
        self.assertLess(stft_error, 2e-3)
        self.assertLess(inverse_stft_error, 5e-4)
  def test_gradients_numerical(self):
    with spectral_ops_test_util.fft_kernel_label_map(), (
        self.test_session(use_gpu=True)):
      # Tuples of (signal_length, frame_length, frame_step, fft_length,
      # stft_bound, inverse_stft_bound).
      # TODO(rjryan): Investigate why STFT gradient error is so high.
      test_configs = [
          (64, 16, 8, 16),
          (64, 16, 16, 16),
          (64, 16, 7, 16),
          (64, 7, 4, 9),
          (29, 5, 1, 10),
      ]

      for (signal_length, frame_length, frame_step, fft_length) in test_configs:
        signal_shape = [signal_length]
        signal = random_ops.random_uniform(signal_shape)
        stft_shape = [max(0, 1 + (signal_length - frame_length) // frame_step),
                      fft_length // 2 + 1]
        stft = spectral_ops.stft(signal, frame_length, frame_step, fft_length,
                                 pad_end=False)
        inverse_stft_shape = [(stft_shape[0] - 1) * frame_step + frame_length]
        inverse_stft = spectral_ops.inverse_stft(stft, frame_length, frame_step,
                                                 fft_length)
        stft_error = test.compute_gradient_error(signal, [signal_length],
                                                 stft, stft_shape)
        inverse_stft_error = test.compute_gradient_error(
            stft, stft_shape, inverse_stft, inverse_stft_shape)
        self.assertLess(stft_error, 2e-3)
        self.assertLess(inverse_stft_error, 5e-4)
    def testMaximumSpanningTreeGradientError(self):
        """Numerically validates the max score gradient."""
        with self.test_session():
            # The maximum-spanning-tree-score function, as a max of linear functions,
            # is piecewise-linear (i.e., faceted).  The numerical gradient estimate
            # may be inaccurate if the epsilon ball used for the estimate crosses an
            # edge from one facet to another.  To avoid spurious errors, we manually
            # set the sample point so the epsilon ball fits in a facet.  Or in other
            # words, we set the scores so there is a non-trivial margin between the
            # best and second-best trees.
            scores_raw = [[[0, 0, 0, 0], [1, 0, 0, 0], [1, 2, 0, 0],
                           [1, 2, 3, 4]],
                          [[4, 3, 2, 9], [0, 0, 2, 9], [0, 0, 0, 9],
                           [9, 9, 9, 9]]]  # pyformat: disable

            # Use 64-bit floats to reduce numerical error.
            scores = constant_op.constant(scores_raw, dtypes.float64)
            init_scores = np.array(scores_raw)

            num_nodes = constant_op.constant([4, 3], dtypes.int32)
            max_scores = mst_ops.max_spanning_tree(num_nodes,
                                                   scores,
                                                   forest=False)[0]

            gradient_error = test.compute_gradient_error(
                scores, [2, 4, 4], max_scores, [2], init_scores)
            self.assertIsNot(gradient_error, None)
 def test_gradient_numerical(self):
     with self.test_session(use_gpu=True):
         signal_shape = (2, 128)
         signal = array_ops.ones(signal_shape)
         frame_length = 33
         frame_step = 9
         frames = shape_ops.frame(signal, frame_length, frame_step)
         error = test.compute_gradient_error(signal, signal_shape, frames,
                                             frames.shape.as_list())
         self.assertLess(error, 2e-5)
示例#5
0
 def test_gradient_numerical(self):
     with self.session(use_gpu=True):
         shape = (2, 10, 10)
         framed_signal = array_ops.zeros(shape)
         frame_hop = 10
         reconstruction = reconstruction_ops.overlap_and_add(
             framed_signal, frame_hop)
         error = test.compute_gradient_error(framed_signal, shape,
                                             reconstruction, [2, 100])
         self.assertLess(error, 2e-5)
 def test_gradient_numerical(self):
   with self.session(use_gpu=True):
     shape = (2, 10, 10)
     framed_signal = array_ops.zeros(shape)
     frame_hop = 10
     reconstruction = reconstruction_ops.overlap_and_add(
         framed_signal, frame_hop)
     error = test.compute_gradient_error(
         framed_signal, shape, reconstruction, [2, 100])
     self.assertLess(error, 2e-5)
示例#7
0
 def test_gradient_numerical(self):
   with self.test_session(use_gpu=True):
     signal_shape = (2, 128)
     signal = array_ops.ones(signal_shape)
     frame_length = 33
     frame_step = 9
     frames = shape_ops.frame(signal, frame_length, frame_step)
     error = test.compute_gradient_error(
         signal, signal_shape, frames, frames.shape.as_list())
     self.assertLess(error, 2e-5)
示例#8
0
 def test_gradient_numerical(self):
     if context.executing_eagerly():
         return
     with self.session():
         signal_shape = (2, 128)
         signal = array_ops.ones(signal_shape)
         frame_length = 33
         frame_step = 9
         frames = shape_ops.frame(signal, frame_length, frame_step)
         error = test.compute_gradient_error(signal, signal_shape, frames,
                                             frames.shape.as_list())
         self.assertLess(error, 2e-5)
示例#9
0
 def test_gradient_numerical(self):
     # TODO(rjryan): Eager gradient tests.
     if context.executing_eagerly():
         return
     with self.session(use_gpu=True):
         shape = (2, 10, 10)
         framed_signal = array_ops.zeros(shape)
         frame_hop = 10
         reconstruction = reconstruction_ops.overlap_and_add(
             framed_signal, frame_hop)
         error = test.compute_gradient_error(framed_signal, shape,
                                             reconstruction, [2, 100])
         self.assertLess(error, 2e-5)
示例#10
0
    def _gradients(self, data_format='NCHW', use_gpu=False):

        batch, channels, height, width = 2, 3, 5, 6
        input_a = np.random.randn(batch, channels, height, width)
        input_b = np.random.randn(batch, channels, height, width)

        kernel_size = 1
        max_displacement = 2
        stride_1 = 1
        stride_2 = 2
        pad = 4

        if data_format == 'NHWC':
            input_a = input_a.transpose(0, 2, 3, 1)
            input_b = input_b.transpose(0, 2, 3, 1)

        with self.test_session(use_gpu=use_gpu, force_gpu=use_gpu):

            input_a_op = ops.convert_to_tensor(input_a, dtype=dtypes.float32)
            input_b_op = ops.convert_to_tensor(input_b, dtype=dtypes.float32)

            call_op = correlation_cost
            actual_op = call_op(input_a_op,
                                input_b_op,
                                kernel_size=kernel_size,
                                max_displacement=max_displacement,
                                stride_1=stride_1,
                                stride_2=stride_2,
                                pad=pad,
                                data_format=data_format)

            err_a = test.compute_gradient_error([input_a_op, input_b_op],
                                                [input_a.shape, input_b.shape],
                                                actual_op,
                                                actual_op.shape.as_list())

            self.assertLess(err_a, 1e-4)