{'fraction_non_alphanumeric': 0.03010815551008477, 'fraction_numerical': 0.013227126571178019, 'mean_word_length': 4.935588809368901, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Oscillatory Fourier Neural Network: A Compact and Efficient Architecture for Sequential Processing

Tremendous progress has been made in sequential processing with the recent advances in recurrent neural networks. However, recurrent architectures face the challenge of exploding/vanishing gradients during training, and require significant computational resources to execute back-propagation through time. Moreover, large models are typically needed for executing complex sequential tasks. To address these challenges, we propose a novel neuron model that has cosine activation with a time varying component for sequential processing. The proposed neuron provides an efficient building block for projecting sequential inputs into spectral domain, which helps to retain long-term dependencies with minimal extra model parameters and computation. A new type of recurrent network architecture, named Oscillatory Fourier Neural Network, based on the proposed neuron is presented and applied to various types of sequential tasks. We demonstrate that recurrent neural network with the proposed neuron model is mathematically equivalent to a simplified form of discrete Fourier transform applied onto periodical activation. In particular, the computationally intensive back-propagation through time in training is eliminated, leading to faster training while achieving the state of the art inference accuracy in a diverse group of sequential tasks. For instance, applying the proposed model to sentiment analysis on IMDB review dataset reaches 89.4% test accuracy within 5 epochs, accompanied by over 35x reduction in the model size compared to LSTM. The proposed novel RNN architecture is well poised for intelligent sequential processing in resource constrained hardware.


Introduction
Recently, artificial neural networks, especially various types of recurrent neural networks (RNN) have demonstrated significant success in sequential processing tasks such as natural language processing (NLP) (Yin et al. 2017). However, complex sequential processing tasks desires increasingly large RNN models, and the training of RNNs encounters the notorious challenge of exploding or vanishing gradients when information across long time need to be preserved and processed. In particular, back-propagation through time (BPTT) applied on weight matrices associated with hidden states such as W hh need to unroll gradient calculations Copyright © 2021, Association for the Advancement of Artificial Intelligence (www.aaai.org). All rights reserved. throughout large number of time steps. BPTT is not only prone to exponentially decaying or growing of the propagated values but also consuming significant computation resources over the recurrent steps. Although mitigating architectures, such as long-short term memory (LSTM) and gated recurrent units (GRU) have been proposed, these modified RNN models typically contain more training parameters, consumes longer time to train, and may still suffer the loss of gradients in presence of very long input sequences. On the other hand, it has been observed that extracting and understanding meaningful patterns at different time scales can be important for sequential processing tasks such as text sentiment analysis or body activity recognition. For instances, patterns in movie reviews could emerge at the word level, sentence/clause level, and paragraph level that altogether determine the outcome of the final sentiment of a review. In order to learn the features at various scales from sequential data, incorporating spectral analysis such as Fourier Transform in RNNs have received growing attention (Lee-Thorp et al. 2021), (Tamkin, Jurafsky, and Goodman 2020). Typically, the input of temporal series can be fed into certain type of spectral processing such as Fourier transform, where frequency-domain outcome can be obtained and further analyzed by the subsequent neural network blocks.
In this work, we propose a new type of neuron with time varying cosine activation (termed TV-Cosine neuron), and construct an RNN architecture, named oscillatory Fourier neural network (O-FNN), for efficient learning for sequential tasks. Intuitively, the proposed neuron model projects sequential input data into a phase of oscillating neuron, in contrast to conventional neurons such as ReLU or tanh that modulate the magnitude of activation depending on the input. Moreover, each neuron in the proposed architecture is a cosine activation applied onto a superposition of input signal and time dependent rotating term of certain frequency. The hidden state vector of the O-FNN accumulates the outcome of cosine activation across all time steps before being fed into the final layer of the network. Following this approach, no backpropagation through time is needed during the backward pass of training, and both forward and backward pass can process the sequential data in a fully parallel manner. We will show that after accumulating the cosine activation through time, the hidden states will equivalently represent a modified discrete Fourier transform of sine and cosine neu-ral activation. The major contribution is summarized as follows: • We propose a new type of neuron with time varying cosine activation for sequential processing. We demonstrate that RNN with such time-varying activation is mathematically equivalent to a simplified form of discrete Fourier transform. Due to the usage of cosine activation, the transform of data into frequency domain is more computationally efficient compared to applying Fourier transform on ReLU or sigmoidal neurons. • We propose a new type of RNN architecture, O-FNN, that is fully parallelizable in both forward and backward passes. In particular, since the backward propagation is not unrolled in time, the issue of exponential explosion or decay of the gradient values across long range of time steps with conventional activation functions is eliminated. • We show that O-FNN architecture is capable of achieving better performance on a plethora of sequential processing tasks with more compact models and high computational efficiency in comparison with regular RNN/LSTM. The characteristics of smaller memory footprint and faster training make the O-FNN especially suitable for deployment in resource-constrained hardware such as IOT and battery-powered edge devices.

Related Work
While periodic activation functions have been proposed as early as 1980s (Lapedes and Farber 1987;McCaughan 1997), learning with such periodic activation has received limited attention from the research community (Sopena, Romero, and Alquezar 1999;Wong, Leung, and Chang 2002). In (Sopena, Romero, and Alquezar 1999), the authors show that with proper range of initial weight values, a multi-layer perceptron using sinusoidal neurons in the form of sin (W X + b) improves accuracy and trains faster compared to its sigmoidal counterpart on some small datasets.
Recently, sinusoidal activation function in implicit neural representations demonstrated improved capability of processing complex spatial-temporal signals and their derivative (Sitzmann et al. 2020). Authors in (Ramachandran, Zoph, and Le 2017) conduct network architecture search and identify neuron with partial sinusoidal behavior as one of the top candidates for activation functions for typical image classification tasks. Note that training networks with periodical activation may be challenging due to the possibility of having numerous local minima in the landscape of loss function (Sopena, Romero, and Alquezar 1999;Parascandolo, Huttunen, and Virtanen 2017). Regarding applying periodical activation in RNNs, (Sopena and Alquezar 1994) reports improvement in learning when sine instead of sigmoid activation function is used in the last fully connected layer of a simple RNN trained for a next-character prediction task. The authors of (Parascandolo, Huttunen, and Virtanen 2017) also observe that the periodical activation can be beneficial for training RNNs and LSTMs on some algorithmic tasks, showing faster learning and possibly better accuracy. Moreover, oscillatory neuron dynamics is exploited in implementing coupled oscilla-tor recurrent neural networks in (Rusch and Mishra 2021), demonstrating great potential of oscillating neurons for processing complex sequential data.
Usage of periodical activation is related to Fourier transforms which also involve extraction of information and features in the frequency domains. In particular, Fourier transforms, especially discrete Fourier Transforms (DFT), have been adopted successfully for RNNs to execute sequential processing (Koplon and Sontag 1997) or make predictions (Zhang and Chan 2000). Fourier transforms are typically done on input signals to facilitate the learning of spectral features. More recently, the idea of leveraging Fourier transforms for extracting spectral information is also explored in Natural Language Processing (NLP) tasks. Authors in (Zhang et al. 2018) proposes using Fourier basis to summarize the statistics of hidden states through past time steps in RNNs. (Tamkin, Jurafsky, and Goodman 2020) applies spectral filters similar to DFT to the activations of individual neurons in BERT (Devlin et al. 2018) language model, aiming for extracting information changing at different time scales in texts.

Fully Connected Layer
cos( ! ) × Figure 1: (a) Time-Varying Cosine neuron with fullyconnected input layer. (b) One dimensional convolution layer can also be used as input layer. (c) TV-Cosine neuron is mathematically equivalent to projecting sine and cosine activations respectively onto sin ω i t and cos ω i t channels of discrete frequencies.

Time-Varying Cosine (TV-Cosine) Neuron for Sequential Processing
We propose temporal artificial neuron named Time-Varying Cosine (TV-Cosine) neuron. As shown in Fig.1(a), the proposed neuron has a cosine activation, which has an input θ t i obtained from a superposition of a fully connected layer's output φ t and a time-varying phase modulating term ω i t. Mathematically, each TV-Cosine neuron is equivalent to a sine neuron and a cosine neuron respectively, projected to a sine and a cosine oscillating functions, as shown in Fig.1(c). Each TV-Cosine neuron has a specific oscillating frequency ω i , where the subscript i indicates the frequency channel.
The frequencies for different channels ω i are hyper parameters of the proposed model. In sequential processing tasks, the fully connected input layer in Fig.1(a) can also be replaced with one dimensional convolutional layer as shown in Fig.1(b), in which, d consecutive input vectors are fed to the TV-Cosine neuron at each time-step. We observed that using 1D convolutional layer with a small slicing window (d = 3) and small stride (s = 1) slightly improves the accuracy, at the cost of slightly increasing the number of weight parameters.

Forward pass in O-FNN
We propose O-FNN architecture consisting of TV-Cosine neurons for efficient learning of sequential tasks. A simple example of O-FNN containing four TV-Cosine neurons with distinctive channel frequencies is shown in Fig.2. The forward flow of O-FNN is unrolled in time as shown along the horizontal direction. Each channel traces and accumulates outputs of one TV-Cosine neuron across all time-steps. The final hidden states of all neurons are concatenated before further processing by a fully connected read-out layer. As discussed earlier, each TV-Cosine neuron equivalently projects activations of sine and cosine activations onto oscillating sine and cosine channels with various frequencies.
Hence, the forward flow of O-FNN in Fig.2 is mathematically equivalent to a special form of Discrete Fourier Transform (DFT). TV-Cosine neurons in O-FNN can be categorized into one DC neuron and a few AC neurons depending on the neuron input. For example, at each time-step t, an input phase φ t is computed as shown by Eq.1. Input to DC neuron equals φ t subtracts a constant value 1 4 π as shown by Eq.2. Inputs to AC neurons equal φ t subtracts a time-driven phase mod-ulating term ω i t as shown by Eq.3. The angular velocity ω i of different AC neurons are computed by Eq.4, in which f is a hyper-parameter used to determine the running speed of the clock in each AC neuron.
The forward flow of O-FNN is a fully parallelizable architecture as shown in Fig.2. Concretely, neuronal activation of all time steps can be computed in parallel if the whole input sequence is already known. The process of forward propagation can be explained as follows. First, all hidden states are initialized to zero. Next, input at each time step can be fed into the parallelizable architecture, and the cosine activation from each time step can be computed following Eq.1 -4. Subsequently, the final states of DC and AC neurons in O-FNN are computed by summing and averaging of cosine neurons' outputs over all time steps, following Eq.5(a) and Eq.6(a). The summation for the AC-channel neurons outputs is equivalent to combining a sine neuron's DST and cosine neuron's DCT terms. Finally, the resulting hidden coefficients are concatenated and fed to the final read-out layer as shown by Eqs.7 and 8. Note that, by subtracting a constant phase 1 4 π in Eq.2 (and multiplying a coefficient of √ 2), we manage to obtain a unified formal expression of hidden states of both DC and AC channels.
The compact model size and high computational efficiency stem from the fact that O-FNN does not require the square matrix W hh and the associated matrix multiplication that occur at every time-step in regular RNNs. The superior accuracy of O-FNN can be attributed to operating in the frequency domain. The DC and low frequency AC channels efficiently capture long time dependencies from sequential data, whereas the high frequency AC channels provide shortterm memory. Compared to approaches that perform spectrum analysis on raw input data or ReLU/Sigmoid neurons, our approach eliminates multiplication operations required to perform the transformation from time to frequency domain. For example, we can perform a regular DFT on neuron with activation function f (ReLU or Sigmoid). However, as described by Eqs.9 (a) and (b), at least one multiplication is required at each time-step to complete the transformation. On the other hand, only addition operations are used in our approach to perform the special form of DFT in Fig.2 using TV-Cosine neurons and O-FNN architecture in Fig.2. The required cosine computation can be implemented using a look-up table. The required extra memory space O(N ) (N is sequence length) is negligible compared to the trainable parameters.
The number of AC neurons (channels) used in O-FNN is a hyper-parameter. According to our simulation results, using only two to three AC neurons are sufficient to obtain the SOTA accuracies across various datasets we tested. Increasing the number of AC neurons beyond four causes accuracy degradation due to over-fitting. As can be seen in the result section, O-FNN requires far fewer neurons and smaller memory footprint while achieving superior accuracy than today's SOTA models.

Backward pass in O-FNN
The back-propagation of O-FNN is shown in Fig.3. During forward propagation, the input sequence x 1 , ..., x N , the concatenated final hidden states h N cat ∈ R 4n×1 , and the inputs θ t i to all TV-Cosine neurons at all time-steps have been cached and will be used in back-propagation. Gradient w.r.t. output ∂L ∂ y ∈ R d×1 first propagates through the fully connected read-out layer and the parameters in this layer can be updated using Formulas 10 and 11, where η is learning rate. Next as shown in Formula 12, the weight matrix W y ∈ R d×4n of the read-out layer is decomposed into four sub-matrices W y[i] ∈ R d×n , where (i = 0, ..., 3). the parameters of input layer are updated using the averaged gradients across all time-steps and channels as indicated by 15 and 16.
By eliminating hidden state weight matrix W hh and the time consuming BPTT in the backward pass, training O-FNN can be faster with considerable improvement in energy efficiency. Moreover, the occurrence of exploding and vanishing gradients due to multiplicative errors across numerous time steps is also prevented in absence of BPTT. Unlike GRUs/LSTMs, which employ extra states and gates to retain memory for long-term dependencies, O-FNN resorts to transforming the input sequences to the frequency domain, and focuses on training the network based on the spectral information. Since O-FNN is designed to accumulate equal contribution from all time-steps, the updates of parameters is determined by the averaged gradients, leading to an equivalent "mini-batch" effect during training. Such equivalent "mini-batch gradient descent" based training in O-FNN might be able to explain the fast convergence with O-FNN compared to other SOTA sequential models, as is demonstrated in the Results and Discussion Section.

Results and Discussion
We will discuss the tuning of hyper-parameters for optimizing the network architecture. Specifically we investigate the impact of the base frequency f and the total number of channels (#chs) on the model's learning capability. Then we will present the results of applying the proposed O-FNN to a diverse group of learning tasks. We conducted experiments on an NVIDIA GeForce GTX 1080 GPU. We perform hyperparameters tuning based on a grid search. The best performing results is based on the highest mean values of validation accuracy averaged over 10 random initialization of trainable parameters. We used an exponential decaying learning with an initial learning rate 1e-3 for all the experiments. For the IMDB task, an exponential decaying factor 0.3 is used, and 0.7 is used for other tasks.

Hyper-parameters Tuning and Understanding
We start with optimizing hyper-parameters for the IMDB sentiment classification task. IMDB dataset contains 50K movie reviews from IMDB users. The sentiments of the reviews written by IMDB users are labeled as either positive ("1") or negative ("0") (Maas et al. 2011). The binary sentiment classification task is to differentiate a user review being positive or negative. The 50K dataset is split evenly for training and testing (25K for each). As for word embedding, we apply the pretrained 50d Glove to vectorize the vocabulary (Brochier, Guille, and Velcin 2019). As is shown in Fig.4(a), adding AC channels of various base frequency f improves the learning performance compared to the case of using DC channel only, corroborating the importance of capturing AC features for learning the long sequences of texts. For IMDB dataset, it is found that base frequency of f around 1.0 works well. Increasing f above 2.0 hurts the performance, possibly due to the loss of low-frequency information which is critical for learning long-time dependencies. It is also observed that increasing the total number of channels from 3 to 5 further improve the test accuracy with a more stabilized learning curve versus number of epochs. Fig.4(b) summarizes the results of hyper-parameter tuning for IMDB dataset. The high efficiency of the Time-Varying Cosine neuron is clearly demonstrated, as only 3-5 channels are sufficient to extract a rich set of features in the time-domain. Adding more channels (above 5) shows no benefit while increasing the model size.
In addition to text classifications, we also evaluate the tuning of hyper-parameters for the task of image classification. We look into digit classification on permuted sequential MNIST dataset (psMNIST). The psMNIST is a modification of sequential MNIST (Le, Jaitly, and Hinton 2015) (sM-NIST), where a fixed permutation is applied to the stream of individual pixels based on MNIST digits (Lecun et al. 1998). The psMNIST provides sequences of pixels with a sequence length T = 784, which is a few times longer than the sequence length of IMDB reviews (averaged at about 250 words). As is shown in Fig.4(d), we observe improvement of performance as AC channels are added into the network, similarly to the observation from the IMDB experiment. Moreover, it is found that higher base frequency is desirable to cope with the increased sequence length in psM-NIST. As is further discussed in the following, the choice of optimized base frequency is dependent on the characteristics of dataset such as the sequence length and the ratio of pattern length to total sequence length. As is summarized in Fig.4(e), the best performance occurs when number of channels equals 3 with the base AC frequency f =2.0. The observed degradation of test accuracy for number of channels higher than 3 is due to overfitting, as a large number of neurons are connected to process single-pixel sequences.

Results on Various Datasets
We apply the O-FNN to various sequential learning tasks, with hyper-parameters optimized for each dataset and task. In addition to IMDB and permuted sequential MNIST as discussed above, we further apply the O-FNN model to process noise padded CIFAR-10 (Chang et al. 2019) and human activity recognition (HAR-2) datasets (Anguita et al. 2012) (Kusupati et al. 2018). The results of O-FNN on the four datasets are summarized in Table 1. For each task, we compare the O-FNN with a few baseline models such as various types of LSTM ((Pandey 2020)) and GRU ((Dey and Salem 2017)) as well as some recent results from the literature (such as coRNN(Rusch and Mishra 2021)) for comparison. As is shown in Fig.4(c) and Fig.4(f), the proposed O-FNN reaches the highest test accuracy with faster convergence on both IMDB and sMNIST benchmark experiments. For IMDB, we focus the comparison with models that have a relatively small number (128) of hidden units. We observe that O-FNN achieves significantly higher accuracy with 32× fewer neurons, 35× fewer parameters and being 10× faster in training. As for the psMNIST benchmark, our proposed model reaches higher than 93% test accuracy within 3 epochs, outperforming both LSTM (van der Westhuizen and Lasenby 2018) (Helfrich, Willmott, and Ye 2018) and GRU (Hafner et al. 2017) (Chang et al. 2017). The proposed O-FNN reaches the state of the art performance at sMNIST, while outperforming all single-layer RNN architectures at processing the challenging permuted sMNIST.
The noise padded CIFAR-10 experiment poses great challenges for models to learn temporal dependence over the large number of time steps, due to the fact that the padded input sequences contain Gaussian noise in 968 out of 1000 time steps while only 32 time steps contain the serialized CIFAR10 images (Krizhevsky and Hinton 2009). From Table 1, we observe that O-FNN outperforms other RNN architectures on this benchmark, while requiring only 65k parameters. We found that a higher base frequency (f =8.0) is 60% 65% 70% 75% 80% 85% 90% 0 1 2 3 4 5 6 7 8 9 10 test accuracy training epochs    (Rusch and Mishra 2021) and (Kag, Zhang, and Saligrama 2020)  Noise padded CIFAR-10 LSTM (Kag, Zhang, and Saligrama 2020) 11.6 128 64k -Incremental RNN (Kag, Zhang, and Saligrama 2020) 54.5 128 11.5k -Lipshitz RNN (Erichson et al. 2020  needed for the model to learn informative features in presence of highly concentrated input data over a small frac-tion of total sequence (i.e. only first 32 actual image input from 1000 total steps). The intuition is that high frequency channels are more capable of extracting localized pattern formed by input from adjacent time steps, while low frequency channels tend to get an averaged effect from input over large numbers of steps. To this effect, it is challenging for low frequency channels to learn meaningful features since noises take a dominating proportion in those long sequences. Our study demonstrates that tuning the hyperparameter f can provide a powerful knob for accommodating the imbalanced distribution of input information among noise padding in long sequences.
As for the HAR-2 dataset, we can see that O-FNN can reach close to SOTA accuracy with fewer parameters compared to most of the references. Morevoer, we observe that even the compact version of O-FNN remain to perform at about 95% test accuracy with less than 1K parameters. Therefore, the compact O-FNN model can be well poised for applications in edge applications and IOT devices. Figure 5 summarizes the accuracy and number of parameters of the proposed O-FNN model in comparison with recent references. In plots of model test accuracy vs model size, a trade-off is typically found when the accuracy of a model increases with number of parameters. Therefore, intrinsic gain in the capability of a model will be demonstrated if the position of the model in this plot can shift up vertically. Specifically, we observe that O-FNN is overall on the top/left side of most references across all four datasets.The overall high accuracy across various tasks can be attributed to the capability of learning features at various time scales through different frequency channels. Based on the trade-off plots, O-FNN is significantly more efficient than all the references at IMDB sentiment analysis task. As for permuted sMNIST and noise-padded CIFAR10, O-FNN outperforms the most recent references, while the separation is smaller. On HAR-2, both O-FNN and the recently proposed coRNN perform well, suggesting a good potential of utilizing neurons with oscillatory dynamics in processing temporal signals. The compact size of O-FNN is partially attributed to the absence of the W hh matrix in regular RNNs, as well as the fact that our simplified Discrete Fourier Transform requires only a few frequency channels each associated with one Time-Varying Cosine neuron. The observation of fast convergence in training across various tasks can be attributed to O-FNN's fully parallelizable architecture, which makes the error back-propagation more direct than regular RNNs that requires recursive computations through time.

Conclusion
A novel RNN architecture (O-FNN) based on a Time-Varying Cosine neuron model is proposed for sequential processing. Compared to conventional Fourier transform on raw input data and ReLU/sigmoidal activations, the proposed architecture provides a computationally efficient approach to extracting frequency-domain information from sequential data. Moreover, the O-FNN by design have full parallelism in both forward and backward passes, leading to significant simplification in training while eliminating the issue of exploding/vanishing gradients encountered by RNNs. In contrast with GRU/LSTM models, which require various types of gates and additional memory/cell states to retain long-term memory, our proposed architecture has extremely compact model size, while achieving the retention of information over long sequences through the learning of frequency-domain feature. We show that O-FNN is capable of handling long time dependencies with significantly smaller model size and lower computational cost, while retaining superior accuracy, faster convergence, and better error resiliency than the State-of-The-Art across various types of sequential tasks. Future avenues of work could extend the current O-FNN to address many-to-many tasks such as machine translation, or image captioning. Leveraging the potential of fast training and having compact models, O-FNN can also be further explored to process complex tasks such as video analysis.