Example #1
0
    def test_sin(test_case):
        input = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)
        of_out = flow.sin(input)
        np_out = np.sin(input.numpy())
        test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))
        test_case.assertTrue(
            np.allclose(input.sin().numpy(), np_out, 1e-5, 1e-5))

        arr = np.array([-0.5461, 0.1347, -2.7266, -0.2746])
        input2 = flow.Tensor(arr, dtype=flow.float32)
        np_out2 = np.array([-0.51935846, 0.13429303, -0.40318328, -0.27116194])
        of_out2 = flow.sin(input2)
        test_case.assertTrue(np.allclose(of_out2.numpy(), np_out2, 1e-5, 1e-5))
Example #2
0
    def forward(self, waveforms):
        """
        Parameters
        ----------
        waveforms : `oneflow.Tensor` (batch_size, 1, n_samples)
            Batch of waveforms.
        Returns
        -------
        features : `oneflow.Tensor` (batch_size, out_channels, n_samples_out)
            Batch of sinc filters activations.
        """
        self.n_ = self.n_.to(waveforms.device)

        self.window_ = self.window_.to(waveforms.device)

        low = self.min_low_hz + flow.abs(self.low_hz_)

        high = flow.clamp(
            low + self.min_band_hz + flow.abs(self.band_hz_),
            self.min_low_hz,
            self.sample_rate / 2,
        )
        band = (high - low)[:, 0]

        f_times_t_low = flow.matmul(low, self.n_)
        f_times_t_high = flow.matmul(high, self.n_)

        band_pass_left = (
            (flow.sin(f_times_t_high) - flow.sin(f_times_t_low)) / (self.n_ / 2)
        ) * self.window_
        band_pass_center = 2 * band.reshape(-1, 1)
        band_pass_right = flow.flip(band_pass_left, dims=[1])

        band_pass = flow.cat([band_pass_left, band_pass_center, band_pass_right], dim=1)

        band_pass = band_pass / (2 * band[:, None])

        self.filters = (band_pass).reshape(self.out_channels, 1, self.kernel_size)

        output = F.conv1d(
            waveforms,
            self.filters,
            stride=[self.stride],
            padding=[self.padding],
            dilation=[self.dilation],
            bias=None,
            groups=1,
        )
        return output
Example #3
0
def sinc(band, t_right):
    y_right = flow.sin(
        2 * math.pi * band * t_right) / (2 * math.pi * band * t_right)
    y_left = flip(y_right, 0)

    y = flow.cat([y_left, flow.ones(1).to("cuda"), y_right])

    return y
Example #4
0
    def __init__(self, d_model, dropout=0.1, max_len=5000):
        super(PositionalEncoding, self).__init__()
        self.dropout = nn.Dropout(p=dropout)

        pe = flow.zeros((max_len, d_model))
        position = flow.arange(0, max_len, dtype=flow.float).unsqueeze(1)
        div_term = flow.exp(
            flow.arange(0, d_model, 2).to(flow.float) * (-math.log(10000.0) / d_model)
        ).unsqueeze(0)
        pe[:, 0::2] = flow.sin(position * div_term)
        pe[:, 1::2] = flow.cos(position * div_term)
        pe = pe.unsqueeze(0).transpose(0, 1)
        self.pe = flow.nn.Parameter(pe, requires_grad=False)
Example #5
0
 def __init__(self, d_model, max_len=5000):
     super(PositionalEncoding, self).__init__()
     # Compute the positional encodings once in log space.
     pe = flow.zeros(max_len, d_model, requires_grad=False)
     position = flow.arange(0, max_len).unsqueeze(1).to(dtype=flow.float32)
     div_term = flow.exp(
         flow.arange(0, d_model, 2).to(dtype=flow.float32)
         * -(math.log(10000.0) / d_model)
     )
     pe[:, 0::2] = flow.sin(position * div_term)
     pe[:, 1::2] = flow.cos(position * div_term)
     pe = pe.unsqueeze(0)
     self.register_buffer("pe", pe)
Example #6
0
 def _embedding_from_positions(self, position):
     """get absolute pos embedding based position.
     Args:
         position (torch.Tensor): Input. Its shape is (b, t)
     Returns:
         posemb (torch.Tensor): Encoded tensor. Its shape is (b, time, emb_dim)
     """
     batch_size, time_step = position.size()
     posemb = flow.zeros(batch_size,
                         time_step,
                         self.emb_dim,
                         device=position.device)
     div_term = flow.exp(
         flow.arange(
             0, self.emb_dim, 2, device=position.device, dtype=flow.float32)
         * -(math.log(10000.0) / self.emb_dim))
     posemb[:, :,
            0::2] = flow.sin(position.float().unsqueeze(-1) * div_term)
     posemb[:, :,
            1::2] = flow.cos(position.float().unsqueeze(-1) * div_term)
     return posemb
Example #7
0
def _sin(self):
    return flow.sin(self)
Example #8
0
 def test_sin(test_case):
     input = flow.Tensor(np.random.randn(2, 6, 5, 3))
     of_out = flow.sin(input)
     np_out = np.sin(input.numpy())
     test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))