1 Introduction
One of the simplest questions one can ask of a set of data is whether or not a given query is contained within it. Is , our query, a member of , our chosen set of observations? This set membership query arises across many computing domains; from databases, network routing, and firewalls. One could query set membership by storing in its entirety and comparing against each element. However, more spaceefficient solutions exist.
The original and most widely implemented approximate set membership datastructure is the Bloom Filter (Bloom, 1970)
. It works by storing sparse distributed codes, produced from randomized hash functions, within a binary vector. The Bloomfilter trades off space for an allowed false positive rate, which arises due to hash collisions. However its error is onesided; if an element
is contained in then it will always be recognized. It never emits false negatives. One can find Bloom Filters embedded within a wide range of production systems; from network security (Geravand & Ahmadi, 2013), to block malicious IP addresses; databases, such as Google’s Bigtable (Chang et al., 2008), to avoid unnecessary disk lookups; cryptocurrency (Hearn & Corallo, 2012), to allow clients to filter irrelevant transactions; search, such as Facebook’s typeahead search (Adams, 2010), to filter pages which do not contain query prefixes; and program verification (Dillinger & Manolios, 2004), to avoid recomputation over previously observed states.While the main appeal of Bloom Filters is favourable compression, another important quality is the support for dynamic updates. New elements can be inserted in time. This is not the case for all approximate set membership data structures. For example, perfect hashing saves space over Bloom Filters but requires a preprocessing stage that is polynomialtime in the number of elements to store (Dietzfelbinger & Pagh, 2008). Whilst the static set membership problem is interesting, it limits the applicability of the algorithm. For example, in a database application that is serving a high throughput of write operations, it may be intractable to regenerate the full datastructure upon each batch of writes.
We thus focus on the data stream computation model (Muthukrishnan et al., 2005)
, where input observations are assumed to be ephemeral and can only be inspected a constant number of times — usually once. This captures many realworld applications: network traffic analysis, database query serving, and reinforcement learning in complex domains. Devising an approximate set membership data structure that is not only more compressive than Bloom Filters, but can be applied to either dynamic or static sets, could have a significant performance impact on modern computing applications. In this paper we investigate this problem using memoryaugmented neural networks and metalearning.
We build upon the recently growing literature on using neural networks to replace algorithms that are configured by heuristics, or do not take advantage of the data distribution. For example, Bloom Filters are indifferent to the data distribution. They have nearoptimal space efficiency when data is drawn uniformly from a universe set
(Carter et al., 1978) (maximalentropy case) but (as we shall show) are suboptimal when there is more structure. Prior studies on this theme have investigated compiler optimization (Cummins et al., 2017), computation graph placement (Mirhoseini et al., 2017), and data index structures such as btrees (Kraska et al., 2018). In the latter work, Kraska et al. (2018) explicitly consider the problem of static set membership. By training a neural network over a fixed (in their case, string inputs) along with heldout negative examples, they observe space reduction over a conventional Bloom Filter^{1}^{1}1The space saving increases to when an additional trick is incorporated, in discretizing and rescaling the classifier outputs and treating the resulting function as a hash function to a bitmap.. Crucially this requires iterating over the storage seta large number of times to embed its salient information into the weights of a neural network classifier. For a new
this process would have to be repeated from scratch.Instead of learning from scratch, we draw inspiration from the fewshot learning advances obtained by metalearning memoryaugmented neural networks (Santoro et al., 2016; Vinyals et al., 2016). In this setup, tasks are sampled from a common distribution and a network learns to specialize to (learn) a given task with few examples. This matches very well to applications where many Bloom Filters are instantiated over different subsets of a common data distribution. For example, a Bigtable database usually contains one Bloom Filter per SSTable file. For a large table that contains Petabytes of data, say, there can be over separate instantiated datastructures which share a common rowkey format and query distribution. Metalearning allows us to exploit this common redundancy. We design a database task with similar redundancy to investigate this exact application in Section 5.4.
The main contributions of this paper are (1) A new memoryaugmented neural network architecture, the Neural Bloom Filter, which learns to write to memory using a distributed write scheme, and (2) An empirical evaluation of the Neural Bloom Filter metalearned on oneshot approximate set membership problems of varying structure. We compare with the classical Bloom Filter alongside other memoryaugmented neural networks such as the Differentiable Neural Computer (Graves et al., 2016) and Memory Networks (Sukhbaatar et al., 2015). We find when there is no structure, that differentiates the query set elements and queries, the Neural Bloom Filter learns a solution similar to a Bloom Filter derivative — a Bloomg filter (Qiao et al., 2011) — but when there is a lot of structure the solution can be considerably more compressive (e.g. for a database task).
2 Background
2.1 Approximate Set Membership
The problem of exact set membership is to state whether or not a given query belongs to a set of distinct observations where are drawn from a universe set . By counting the number of distinct subsets of size it can be shown that any such exact set membership tester requires at least bits of space. To mitigate the space dependency on , which can be prohibitively large, one can relax the constraint on perfect correctness. Approximate set membership allows for a false positive rate of at most . Specifically we answer where and . It can be shown^{2}^{2}2By counting the minimal number of sets required to cover all . the space requirement for approximate set membership of uniformly sampled observations is at least bits (Carter et al., 1978) which can be achieved with perfect hashing. So for a false positive rate of , say, this amounts to
bits per element. In contrast to storing raw or compressed elements this can be a huge space saving, for example ImageNet images require
KB per image on average when compressed with JPEG, an increase of over four orders of magnitude.2.2 Bloom Filter
The Bloom Filter (Bloom, 1970) is a data structure which solves the dynamic approximate set membership problem with nearoptimal space complexity. It assumes access to k uniform hash functions such that independent of prior hash values or input . The Bloom Filter’s memory is a binary string of length which is initialized to zero. Writes are performed by hashing an input to locations in and setting the corresponding bits to , . For a given query the Bloom Filter returns true if all corresponding hashed locations are set to and returns false otherwise: . This incurs zero false negatives, as any previously observed input must have enabled the corresponding bits in , however there can be false positives due to hash collisions. To achieve a false positive rate of with minimal space one can set and , where is Euler’s number. The resulting space is a factor of from the optimal static lower bound given by Carter et al. (1978).
2.3 MemoryAugmented Neural Networks
Recurrent neural networks such as LSTMs retain a small amount of memory via the recurrent state. However this is usually tied to the number of trainable parameters in the model. There has been recent interest in augmenting neural networks with a larger external memory. The method for doing so, via a differentiable write and read interface, was first popularized by the Neural Turing Machine (NTM) (Graves et al., 2014) and its successor the Differentiable Neural Computer (DNC) (Graves et al., 2016) in the context of learning algorithms, and by Memory Networks (Sukhbaatar et al., 2015) in the context of question answering. Memory Networks store embeddings of the input in separate rows of a memory matrix . Reads are performed via a differentiable contentbased addressing operation. Given a query embedding we take some similarity measure
(e.g. cosine similarity, or negative euclidean distance) against each row in memory and apply a softmax to obtain a
soft address vector . A read is then a weighted sum over memory . The NTM and DNC use the same contentbased read mechanism, but also learns to write. These models can arbitrate whether to write to slots in memory with similar content (contentbased writes), temporally ordered locations, or unused memory.When it comes to capacity, there has been consideration to scaling both the DNC and Memory Networks to very large sizes using sparse read and write operations (Rae et al., 2016; Chandar et al., 2016). However another way to increase the capacity is to increase the amount of compression which occurs in memory. Memory Nets can create compressive representations of each input, but cannot compress jointly over multiple inputs because they are hardwired to write one slot per timestep. The NTM and DNC can compress over multiple slots in memory because they can arbitrate writes across multiple locations, but in practice seem to choose very sharp read and write addresses. The Kanerva Machine (Wu et al., 2018a, b) tackles memorywide compression using a distributed write scheme to jointly compose and compress its memory contents. The model uses contentbased addressing over a separate learnable addressing matrix , instead of the memory , and thus learns where to write. We take inspiration from this scheme.
3 Model
One approach to learning set membership in oneshot would be to use a recurrent neural network, such as an LSTM or DNC. Here, the model sequentially ingests the elements to store, answers a set of queries using the final state, and is trained by BPTT. Whilst this is a general training approach, and the model may learn a compressive solution, it does not scale well to larger number of elements. Even when
, backpropagating over a sequence of this length induces computational and optimization challenges. For larger values this quickly becomes intractable. Alternatively one could store an embedding of each element
in a slotbased Memory Network. This is more scalable as it avoids BPTT, because the gradients of each input can be calculated in parallel. However Memory Networks are not a space efficient solution (as shown in in Section 5) because there is no joint compression of inputs.This motivates the proposed memory model, the Neural Bloom Filter. Briefly, the network is augmented with a realvalued memory matrix. The network addresses memory by classifying which memory slots to read or write to via a softmax, conditioned on the input. We can think of this as a continuous analogue to the Bloom Filter’s hash function; because it is learned the network can colocate or separate inputs to improve performance. The network updates memory with a simple additive write operation — i.e. no multiplicative gating or squashing — to the addressed locations. An additive write operation can be seen as a continuous analogue to the the Bloom Filter’s logical OR write operation. Crucially, the additive write scheme allows us to train the model without BPTT — this is because gradients with respect to the write words can be computed in parallel. Reads involve a componentwise multiplication of address and memory (analogous to the selection of locations in the Bloom Filter via hashing), but instead of projecting this down to a scalar with a fixed function, we pass this through an MLP to obtain a scalar familiarity logit. The network is fully differentiable, allows for memories to be stored in a distributed fashion across slots, and is quite simple e.g. in comparison to DNCs.
The full architecture depicted in Figure 1 consists of a controller network which encodes the input to an embedding and transforms this to a write word and a query . The address over memory is computed via a softmax over the contentbased attention between and a learnable address matrix . Here, denotes a softmax. The network thus learns where to place elements or overlap elements based on their content, we can think of this as a soft and differentiable relaxation of the uniform hashing families incorporated by the Bloom Filter (see Appendix A.3 for further discussion).
A write is performed by running the controller to obtain a write word and address , and then additively writing to , weighted by the address , . The simple additive write ensures that the resulting memory is invariant to input ordering (as addition is commutative) and we do not have to backpropagatethroughtime (BPTT) over sequential writes — gradients can be computed in parallel. A read is performed by also running the controller network to obtain and and componentwise multiplying the address with , . The read words are fed through an MLP along with the residual inputs and and are projected to a single scalar logit, indicating the familiarity signal. We found this to be more powerful than the conventional read operation used by the DNC and Memory Networks, as it allows for nonlinear interactions between rows in memory at the time of read. See Algorithm 1 for an overview of the operations.
To give an example network configuration, we chose to be a 3layer CNN in the case of image inputs, and a hiddenunit LSTM in the case of text inputs. We chose and to be an MLP with a single hidden layer of size , followed by layer normalization, and
to be a 3layer MLP with residual connections. We used a leaky ReLU as the nonlinearity. Although the described model uses dense operations that scale linearly with the memory size
, we discuss how the model could be implemented for time reads and writes using sparse attention and read/write operations, in Appendix A.1. Furthermore the model’s relation to uniform hashing is discussed in Appendix A.3.4 Space Complexity
In this section we discuss space lower bounds for the approximate set membership problem when there is some structure to the storage or query set. This can help us formalise why and where neural networks may be able to beat classical lower bounds to this problem.
The lower bound from Carter et al. (1978) assumes that all subsets of size , and all queries
have equal probability. Whilst it is instructive to bound this maximumentropy scenario, which we can think of as ‘worst case’, most applications of approximate set membership e.g. web cache sharing, querying databases, or spellchecking, involve sets and queries that are not sampled uniformly. For example, the elements within a given set may be highly dependent, there may be a powerlaw distribution over queries, or the queries and sets themselves may not be sampled independently.
A more general space lower bound can be defined by an information theoretic argument from communication complexity (Yao, 1979). Namely, approximate set membership can be framed as a twoparty communication problem between Alice, who observes the set and Bob, who observes a query . They can agree on a shared policy in which to communicate. For given inputs they can produce a transcript which can be processed such that . BarYossef et al. (2004) shows that the maximum transcript size is greater than the mutual information between the inputs and transcript: . Thus we note problems where we may be able to use less space than the classical lower bound are cases where the entropy is small, e.g. our sets are highly nonuniform, or cases where is large, which signifies that many query and set pairs can be solved with the same transcript.
5 Experiments
Our experiments explore scenarios where set membership can be learned in oneshot with improved compression over the classical Bloom Filter. We consider tasks with varying levels of structure in the storage sets and queries . We compare the Neural Bloom Filter with three memoryaugmented neural networks, the LSTM, DNC, and Memory Network, that are all able to write storage sets in oneshot.
The training setup follows the memoryaugmented metalearning training scheme of Vinyals et al. (2016), only here the task is familiarity classification versus image classification. The network samples tasks which involve classifying familiarity for a given storage set. Metalearning occurs as a twospeed process, where the model quickly learns to recognize a given storage set within a training episode via writing to a memory or state, and the model slowly learns to improve this fastlearning process by optimizing the model parameters over multiple tasks. We detail the training routine in Algorithm 2.
For the RNN baselines (LSTM and DNC) the write operation corresponds to unrolling the network over the inputs and outputting the final state. For these models, the query network is simply an MLP classifier which receives the concatenated final state and query, and outputs a scalar logit. For the Memory Network, inputs are stored in individual slots and the familiarity signal is computed from the maximum contentbased attention value. The Neural Bloom Filter read and write operations are defined in Algorithm 1.
5.1 Space Comparison
We compared the space (in bits) of the model’s memory (or state) to a Bloom Filter at a given false positive rate and false negative rate. The false positive rate is measured empirically over a sample of queries for the learned models; for the Bloom Filter we employ the analytical false positive rate. Beating a Bloom Filter’s space usage with the analytical false positive rate implies better performance for any given Bloom Filter library version (as actual Bloom Filter hash functions are not uniform), thus the comparison is reasonable. For each model we sweep over hyperparameters relating to model size to obtain their smallest operating size at the desired false positive rate (for the full set, see Appendix D). Because the neural models can emit false negatives, we store these in a (ideally small) backup Bloom Filter, as proposed by Kraska et al. (2018); Mitzenmacher (2018a). We account for the space of this backup Bloom Filter, and add it to the space usage of the model’s memory for parity (See Appendix B for further discussion). The neural network must learn to output a small state in oneshot that can serve set membership queries at a given false positive rate, and emit a small enough number of false negatives such that the backup filter is also small, and the total size is considerably less than a Bloom Filter.
5.2 Sampling Strategies on MNIST
To understand what kinds of scenarios neural networks may be more (or less) compressive than classical Bloom Filters, we consider three simple set membership tasks that have a graded level of structure to the storage sets and queries. Concretely, they differ in the sampling distribution of storage sets and queries . However all problems are approximate set membership tasks that can be solved by a Bloom Filter. The tasks are (1) Classbased familiarity, a highly structured task where each set of images is sampled with the constraint that they arise from the same randomlyselected class. (2) Nonuniform instancebased familiarity
, a moderately structured task where the images are sampled without replacement from an exponential distribution.
(3) Uniform instancebased familiarity, a completely unstructured task where each subset contains images sampled uniformly without replacement. For each task we varied the size of the sample set to store, and calculated the space (in bits) of each model’s state at a fixed false positive rate of and a false negative rate of . We used relatively small storage set sizes (e.g. ) to start with, as this highlights that some RNNbased approaches struggle to train over larger set sizes, before progressing to larger sets in subsequent sections. See Appendix E for further details on the task setup. In the classbased sampling task we see in Figure 2a that the DNC, LSTM and Neural Bloom Filter are able to significantly outperform the classical Bloom Filter when images are sampled by class. The Memory Network is able to solve the task with a word size of only , however this corresponds to a far greater number of bits per element, versus the Bloom Filter’s (to a total size of kb), and so the overall size was prohibitive. The DNC, LSTM, and Neural Bloom Filter are able to solve the task with a storage set size of at kb , b, and b; a , , and saving respectively. For the nonuniform sampling task in Figure 2b we see the Bloom Filter is preferable for less than stored elements, but is overtaken thereafter. At elements the DNC, LSTM, and Neural Bloom Filter consume kb, kb, and kb respectively which corresponds to a , , and reduction over the kb Bloom Filter. In the uniform sampling task shown in Figure 2c, there is no structure to the sampling of . The two architectures which rely on BPTT essentially fail to solve the task at some threshold of storage size. The Neural Bloom Filter solves it with kb (using a memory size of and word size of). The overall conclusion from these sets of experiments is that the classical Bloom Filter works best when there is no structure to the data, however when there is (e.g. skewed data, or highly dependent sets that share common attributes) we do see significant space savings.
5.3 Memory Access Analysis
We wanted to understand how the Neural Bloom Filter uses its memory, and in particular how its learned solutions may correspond to classical algorithms. We inspected the memory contents (what was stored to memory) and addressing weights (where it was stored) for a small model of memory slots and a word size of , trained on the MNIST classbased familiarity task. We plot this for each class label, and compare the pattern of memory usage to two other models that use increasingly simpler read and write operations: (1) an ablated model with constant write words , and (2) an ablated model with and a linear read operator .
The full model, shown in Figure 3a learns to place some classes in particular slots, e.g. class slot , however most are distributed. Inspecting the memory contents, it is clear the write word encodes a unique D token for each class. This solution bears resemblance with Bloomg Filters (Qiao et al., 2011) where elements are spread across a smaller memory with the same hashing scheme as Bloom Filters, but a unique token is stored in each slot instead of a constant bit value. With the model ablated to store only s in Figure 3b we see it uses semantic addressing codes for some classes (e.g. and ) and distributed addresses for other classes. E.g. for class the model prefers to uniformly spread its writes across memory slot , , and . The model solution is similar to that of Bloom Filters, with distributed addressing codes as a solution — but no information in the written words themselves. When we force the read operation to be linear in Figure 3c, the network maps each input class to a unique slot in memory. This solution has a correspondence with perfect hashing. In conclusion, with small changes to the read/write operations we see the Neural Bloom Filter learn different algorithmic solutions.
5.4 Database Queries
5%  1%  0.1%  
Neural Bloom Filter  871b  1.5kb  24.5kb 

Bloom Filter  31.2kb  47.9kb  72.2kb 
Cuckoo Filter  33.1kb  45.3kb  62.6kb 
We look at a task inspired by database interactions. NoSQL databases, such as Bigtable and Cassandra, use a single stringvalued rowkey, which is used to index the data. The database is comprised of a union of files (e.g. SSTables) storing contiguous rowkey chunks. Bloom Filters are used to determine whether a given query lies within the stored set. We emulate this setup by constructing a universe of strings, that is alphabetically ordered, and by sampling contiguous ranges (to represent a given SSTable). Queries are sampled uniformly from the universe set of strings. We choose the unique tokens in the GigaWord v5 news corpus to be our universe as this consists of structured natural data and some noisy or irregular strings.
We consider the task of storing sorted string sets of size . We train the Neural Bloom Filter to several desired false positive rates () and used a backup Bloom Filter to guarantee false negative rate. We also trained LSTMs and DNCs for comparison, but they failed to learn a solution to the task after several days of training; optimizing insertions via BPTT over a sequence of length did not result in a remotely usable solution. The Neural Bloom Filter avoids BPTT via its simple additive write scheme, and so it learned to solve the task quite naturally. As such, we compare the Neural Bloom Filter solely to classical data structures: Bloom Filters and Cuckoo Filters. In Table 1 we see a significant space reduction of , where the margin grows with increasing permitted false positive rates. Since memory is an expensive component within production databases (in contrast to disk, say), this memory space saving could translate to a nontrivial cost reduction. We note that a storage size of may appear small, but is relevant to the NOSQL database scenario where disk files (e.g. SSTables) are typically sharded to be several megabytes in size, to avoid issues with compaction. E.g. if the stored values were of size kB per row, we would expect unique keys or less in an average Bigtable SSTable.
One further consideration for production deployment is the ability to extrapolate to larger storage set sizes during evaluation. We investigate this for the Neural Bloom Filter on the same database task, and compare it to an LSTM. To ensure both models train, we set the maximum training storage set size to and evaluate up to sizes , a modest size increase. We find that the Neural Bloom Filter uses up to less space than the LSTM and the neural models are able to extrapolate to larger set sizes than those observed during training (see Appendix F Figure 4). Whilst the performance eventually degrades when the training limit size is exceeded, it is not catastrophic for either the LSTM or Neural Bloom Filter.
5.5 Timing benchmark
Query + Insert Latency  Query Throughput (QPS)  Insert Throughput (IPS)  
CPU  GPU  CPU  GPU  CPU  GPU  
Bloom Filter*  0.02ms    61K    61K   
Neural Bloom Filter  5.1ms  13ms  3.5K  105K  3.2K  101K 
LSTM  5.0ms  13ms  3.1K  107K  2.4K  4.6K 
Learned Index (Kraska et al., 2018)  780ms  1.36s  3.1K  107K  25  816 
We have principally focused on space comparisons in this paper, we now consider speed for the database task described in the prior section. We measure latency as the wallclock time to complete a single insertion or query of a rowkey string of length . We also measure throughput as the reciprocal wallclock time of inserting or querying
strings. We use a common encoder architecture for the neural models, a 128hiddenunit character LSTM. We benchmark the models on the CPU (Intel(R) Xeon(R) CPU E51650 v2 @ 3.50GHz) and on the GPU (NVIDIA Quadro P6000) with models implemented in TensorFlow without any modelspecific optimizations. We compare to empirical timing results published in a queryoptimized Bloom Filter variant
(Chen et al., 2007). We include the Learned Index from (Kraska et al., 2018) to contrast timings with a model that is not oneshot. The architecture is simply the LSTM character encoder; inserts are performed via gradient descent. The number of gradientdescent steps to obtain convergence is domaindependent, we chose steps in our timing benchmarks. The Learned Index queries are obtained by running the character LSTM over the input and classifying familiarity — and thus query metrics are identical to the LSTM baseline.We see in Table 2. that the combined query and insert latency of the Neural Bloom Filter and LSTM sits at ms on the CPU, around slower than the classical Bloom Filter. The Learned Index contains a much larger latency of ms due to the sequential application of gradients. For all neural models, latency is not improved when operations are run on the GPU. However when multiple queries are received, the throughput of GPUbased neural models surpasses the classical Bloom Filter due to efficient concurrency of the dense linear algebra operations. This leads to the conclusion that a Neural Bloom Filter could be deployed in scenarios with high query load without a catastrophic decrease in throughput, if GPU devices are available. For insertions we see a bigger separation between the oneshot models: the LSTM and Neural Bloom Filter. Whilst all neural models are uncompetitive on the CPU, the Neural Bloom Filter surpasses the Bloom Filter’s insertion throughput when placed on the GPU, with insertions per second (IPS). The LSTM runs at IPS, one order of magnitude slower, because writes are serial, and the Learned Index structure is two orders of magnitude slower at IPS due to sequential gradient computations. The benefits of the Neural Bloom Filter’s simple write scheme are apparent here.
6 Related Work
There have been a large number of Bloom Filter variants published; from Counting Bloom Filters which support deletions (Fan et al., 2000), Bloomier Filters which store functions vs sets (Chazelle et al., 2004), Compressed Bloom Filters which use arithmetic encoding to compress the storage set (Mitzenmacher, 2002), and Cuckoo Filters which use cuckoo hashing to reduce redundancy within the storage vector (Fan et al., 2014). Although some of these variants focus on better compression, they do not achieve this by specializing to the data distribution.
One of the few works which address datadependence are Weighted Bloom Filters (Bruck et al., 2006; Wang et al., 2015)
. They work by modulating the number of hash functions used to store or query each input, dependent on its storage and query frequency. This requires estimating a large number of separate storage and query frequencies. This approach can be useful for imbalanced data distributions, such as the nonuniform instancebased MNIST familiarity task. However it cannot take advantage of dependent sets, such as the classbased MNIST familiarity task, or the database query task. We see the Neural Bloom Filter is more compressive in all settings.
Sterne (2012) proposes a neurallyinspired set membership datastructure that works by replacing the randomized hash functions with a randomlywired computation graph of OR and AND gates. The false positive rate is controlled analytically by modulating the number of gates and the overall memory size. However there is no learning or specialization to the data with this setup. Bogacz & Brown (2003) investigates a learnable neural familiarity module, which serves as a biologically plausible model of familiarity mechanisms in the brain, namely within the perirhinal cortex. However this has not shown to be empirically effective at exact matching.
Kraska et al. (2018) consider the use of a neural network to classify the membership of queries to a fixed set . Here the network itself is more akin to a perfect hashing setup where multiple epochs are required to find a succinct holistic representation of the set, which is embedded into the weights of the network. In their case this search is performed by gradientbased optimization. We emulate their experimental comparison approach but instead propose a memory architecture that represents the set as activations in memory, versus weights in a network.
Mitzenmacher (2018a) discusses the benefits and drawbacks of a learned Bloom Filter; distinguishing the empirical false positive rate over the distribution of sets versus the conditional false positive rate of the model given a particular set . In this paper we focus on the empirical false positive rate because we wish to exploit redundancy in the data and query distribution. Mitzenmacher (2018b) also considers an alternate way to combine classical and learned Bloom Filters by ‘sandwiching’ the learned model with prefilter and postfilter classical Bloom Filters to further reduce space.
7 Conclusion
In many situations neural networks are not a suitable replacement to Bloom Filters and their variants. The Bloom Filter is robust to changes in data distribution because it delivers a bounded false positive rate for any sampled subset. However in this paper we consider the questions, “When might a singleshot neural network provide better compression than a Bloom Filter?”. We see that a model which uses an external memory with an adaptable capacity, avoids BPTT with a feedforward write scheme, and learns to address its memory, is the most promising option in contrast to popular memory models such as DNCs and LSTMs. We term this model the Neural Bloom Filter due to the analogous incorporation of a hashing scheme, commutative write scheme, and multiplicative read mechanism.
The Neural Bloom Filter relies on settings where we have an offline dataset (both of stored elements and queries) that we can metalearn over. In the case of a large database we think this is warranted, a database with K separate set membership data structures will benefit from a single (or periodic) metalearning training routine that can run on a single machine and sample from the currently stored data, generating a large number of efficient datastructures. We envisage the space cost of the network to be amortized by sharing it across many neural Bloom Filters, and the timecost of executing the network to be offset by the continuous acceleration of dense linear algebra on modern hardware, and the ability to batch writes and queries efficiently. A promising future direction would be to investigate the feasibility of this approach in a production system.
Acknowledgments
We thank Peter Dayan, Yori Zwols, Yan Wu, Joel Leibo, Greg Wayne, Andras Gyorgy, Charles Blundell, Daan Weirstra, Pushmeet Kohli, and Tor Lattimor for their insights during this project.
References
 Adams (2010) Adams, K. The life of a typeahead query, 2010. URL https://www.facebook.com/Engineering/videos/432864835468/. [Online, accessed 01August2018].
 BarYossef et al. (2004) BarYossef, Z., Jayram, T. S., Kumar, R., and Sivakumar, D. An information statistics approach to data stream and communication complexity. Journal of Computer and System Sciences, 68(4):702–732, 2004.
 Bloom (1970) Bloom, B. H. Space/time tradeoffs in hash coding with allowable errors. Communications of the ACM, 13(7):422–426, 1970.
 Bogacz & Brown (2003) Bogacz, R. and Brown, M. W. Comparison of computational models of familiarity discrimination in the perirhinal cortex. Hippocampus, 13(4):494–524, 2003.
 Bruck et al. (2006) Bruck, J., Gao, J., and Jiang, A. Weighted bloom filter. In Information Theory, 2006 IEEE International Symposium on, pp. 2304–2308. IEEE, 2006.

Carter et al. (1978)
Carter, L., Floyd, R., Gill, J., Markowsky, G., and Wegman, M.
Exact and approximate membership testers.
In
Proceedings of the tenth annual ACM symposium on Theory of computing
, pp. 59–65. ACM, 1978.  Chandar et al. (2016) Chandar, S., Ahn, S., Larochelle, H., Vincent, P., Tesauro, G., and Bengio, Y. Hierarchical memory networks. arXiv preprint arXiv:1605.07427, 2016.
 Chang et al. (2008) Chang, F., Dean, J., Ghemawat, S., Hsieh, W. C., Wallach, D. A., Burrows, M., Chandra, T., Fikes, A., and Gruber, R. E. Bigtable: A distributed storage system for structured data. ACM Transactions on Computer Systems (TOCS), 26(2):4, 2008.
 Chazelle et al. (2004) Chazelle, B., Kilian, J., Rubinfeld, R., and Tal, A. The bloomier filter: an efficient data structure for static support lookup tables. In Proceedings of the fifteenth annual ACMSIAM symposium on Discrete algorithms, pp. 30–39. Society for Industrial and Applied Mathematics, 2004.
 Chen et al. (2007) Chen, Y., Kumar, A., and Xu, J. J. A new design of bloom filter for packet inspection speedup. In Global Telecommunications Conference, 2007. GLOBECOM’07. IEEE, pp. 1–5. IEEE, 2007.

Cummins et al. (2017)
Cummins, C., Petoumenos, P., Wang, Z., and Leather, H.
Endtoend deep learning of optimization heuristics.
In Parallel Architectures and Compilation Techniques (PACT), 2017 26th International Conference on, pp. 219–232. IEEE, 2017.  Datar et al. (2004) Datar, M., Immorlica, N., Indyk, P., and Mirrokni, V. S. Localitysensitive hashing scheme based on pstable distributions. In Proceedings of the twentieth annual symposium on Computational geometry, pp. 253–262. ACM, 2004.
 Dietzfelbinger & Pagh (2008) Dietzfelbinger, M. and Pagh, R. Succinct data structures for retrieval and approximate membership. In International Colloquium on Automata, Languages, and Programming, pp. 385–396. Springer, 2008.
 Dillinger & Manolios (2004) Dillinger, P. C. and Manolios, P. Bloom filters in probabilistic verification. In International Conference on Formal Methods in ComputerAided Design, pp. 367–381. Springer, 2004.
 Fan et al. (2014) Fan, B., Andersen, D. G., Kaminsky, M., and Mitzenmacher, M. D. Cuckoo filter: Practically better than bloom. In Proceedings of the 10th ACM International on Conference on emerging Networking Experiments and Technologies, pp. 75–88. ACM, 2014.
 Fan et al. (2000) Fan, L., Cao, P., Almeida, J., and Broder, A. Z. Summary cache: a scalable widearea web cache sharing protocol. IEEE/ACM Transactions on Networking (TON), 8(3):281–293, 2000.
 Geravand & Ahmadi (2013) Geravand, S. and Ahmadi, M. Bloom filter applications in network security: A stateoftheart survey. Computer Networks, 57(18):4047–4064, 2013.
 Graves et al. (2014) Graves, A., Wayne, G., and Danihelka, I. Neural turing machines. arXiv preprint arXiv:1410.5401, 2014.
 Graves et al. (2016) Graves, A., Wayne, G., Reynolds, M., Harley, T., Danihelka, I., GrabskaBarwińska, A., Colmenarejo, S. G., Grefenstette, E., Ramalho, T., Agapiou, J., et al. Hybrid computing using a neural network with dynamic external memory. Nature, 538(7626):471, 2016.
 Hearn & Corallo (2012) Hearn, M. and Corallo, M. BIPS: Connection Bloom filtering, 2012. URL https://github.com/bitcoin/bips/blob/master/bip0037.mediawiki. [Online; accessed 01August2018].

Jouppi et al. (2017)
Jouppi, N. P., Young, C., Patil, N., Patterson, D., Agrawal, G., Bajwa, R.,
Bates, S., Bhatia, S., Boden, N., Borchers, A., et al.
Indatacenter performance analysis of a tensor processing unit.
In Computer Architecture (ISCA), 2017 ACM/IEEE 44th Annual International Symposium on, pp. 1–12. IEEE, 2017.  Kaiser et al. (2017) Kaiser, Ł., Nachum, O., Roy, A., and Bengio, S. Learning to remember rare events. In International Conference on Learning Representations, 2017.
 Kraska et al. (2018) Kraska, T., Beutel, A., Chi, E. H., Dean, J., and Polyzotis, N. The case for learned index structures. In Proceedings of the 2018 International Conference on Management of Data, pp. 489–504. ACM, 2018.

Mirhoseini et al. (2017)
Mirhoseini, A., Pham, H., Le, Q. V., Steiner, B., Larsen, R., Zhou, Y., Kumar,
N., Norouzi, M., Bengio, S., and Dean, J.
Device placement optimization with reinforcement learning.
In
Proceedings of the 34th International Conference on Machine Learning
, volume 70, pp. 2430–2439. PMLR, 2017.  Mitzenmacher (2002) Mitzenmacher, M. Compressed bloom filters. IEEE/ACM Transactions on Networking (TON), 10(5):604–612, 2002.
 Mitzenmacher (2018a) Mitzenmacher, M. A model for learned bloom filters and related structures. arXiv preprint arXiv:1802.00884, 2018a.
 Mitzenmacher (2018b) Mitzenmacher, M. Optimizing learned bloom filters by sandwiching. arXiv preprint arXiv:1803.01474, 2018b.
 Muthukrishnan et al. (2005) Muthukrishnan, S. et al. Data streams: Algorithms and applications. Foundations and Trends in Theoretical Computer Science, 1(2):117–236, 2005.
 Qiao et al. (2011) Qiao, Y., Li, T., and Chen, S. One memory access bloom filters and their generalization. In INFOCOM, 2011 Proceedings IEEE, pp. 1745–1753. IEEE, 2011.
 Rae et al. (2016) Rae, J. W., Hunt, J. J., Danihelka, I., Harley, T., Senior, A. W., Wayne, G., Graves, A., and Lillicrap, T. P. Scaling memoryaugmented neural networks with sparse reads and writes. In Advances in Neural Information Processing Systems, pp. 3621–3629, 2016.
 Santoro et al. (2016) Santoro, A., Bartunov, S., Botvinick, M., Wierstra, D., and Lillicrap, T. Metalearning with memoryaugmented neural networks. In Proceedings of the 33rd International Conference on Machine Learning, pp. 1842–1850, 2016.
 Shazeer et al. (2017) Shazeer, N., Mirhoseini, A., Maziarz, K., Davis, A., Le, Q., Hinton, G., and Dean, J. Outrageously large neural networks: The sparselygated mixtureofexperts layer. arXiv preprint arXiv:1701.06538, 2017.
 Sterne (2012) Sterne, P. Efficient and robust associative memory from a generalized bloom filter. Biological cybernetics, 106(45):271–281, 2012.
 Sukhbaatar et al. (2015) Sukhbaatar, S., Szlam, A., Weston, J., and Fergus, R. Endtoend memory networks. In Proceedings of the 28th International Conference on Neural Information Processing SystemsVolume 2, pp. 2440–2448. MIT Press, 2015.
 Vinyals et al. (2016) Vinyals, O., Blundell, C., Lillicrap, T., Kavukcuoglu, K., and Wierstra, D. Matching networks for one shot learning. In Proceedings of the 30th International Conference on Neural Information Processing Systems, pp. 3637–3645. Curran Associates Inc., 2016.
 Wang et al. (2015) Wang, X., Ji, Y., Dang, Z., Zheng, X., and Zhao, B. Improved weighted bloom filter and space lower bound analysis of algorithms for approximated membership querying. In International Conference on Database Systems for Advanced Applications, pp. 346–362. Springer, 2015.
 Wu et al. (2018a) Wu, Y., Wayne, G., Graves, A., and Lillicrap, T. The kanerva machine: A generative distributed memory. In International Conference on Learning Representations, 2018a.
 Wu et al. (2018b) Wu, Y., Wayne, G., Gregor, K., and Lillicrap, T. Learning attractor dynamics for generative memory. In Advances in Neural Information Processing Systems, pp. 9401–9410, 2018b.
 Yao (1979) Yao, A. C.C. Some complexity questions related to distributive computing (preliminary report). In Proceedings of the eleventh annual ACM symposium on Theory of computing, pp. 209–213. ACM, 1979.
Appendix A Further Model Details
a.1 Efficient addressing
We discuss some implementation tricks that could be employed for a production system.
Firstly the original model description defines the addressing matrix to be trainable. This ties the number of parameters in the network to the memory size. It may be preferable to train the model at a given memory size and evaluate for larger memory sizes. One way to achieve this is by allowing the addressing matrix to be nontrainable. We experiment with this, allowing
to be a fixed sample of Gaussian random variables. We can think of these as point on a sphere in high dimensional space, the controller network must learn to organize inputs into separate buckets across the surface of the sphere.
To make the addressing more efficient for larger memory sizes, we experiment with sparsification of the addressing softmax by preserving only the top k components. We denote this sparse softmax . When using a sparse address, we find the network can fixate on a subset of rows. This observation is common to prior sparse addressing work (Shazeer et al., 2017). We find sphering the query vector, often dubbed whitening, remedies this (see Appendix G for an ablation). The modified sparse architecture variant is illustrated in Algorithm 3.
One can avoid the lineartime distance computation in the addressing operation by using an approximate knearest neighbour index, such as localitysensitive hashing (Datar et al., 2004), to extract the nearest neighbours from in time. The use of an approximate nearest neighbour index has been empirically considered for scaling memoryaugmented neural networks (Rae et al., 2016; Kaiser et al., 2017) however this was used for attention on directly. As
is dynamic the knn requires frequent rebuilding as memories are stored or modified. This architecture is simpler —
is fixed and so the approximate knn can be built once.To ensure the serialized size of the network (which can be shared across many memory instantiations) is independent of the number of slots in memory we can avoid storing . In the instance that it is not trainable, and is simply a fixed sample of random variables that are generated from a deterministic random number generator — we can instead store a set of integer seeds that can be used to regenerate the rows of . We can let the th seed , say represented as a 16bit integer, correspond to the set of rows with indices . If these rows need to be accessed, they can be regenerated onthefly by . The total memory cost of is thus bits, where is the number of memory slots^{3}^{3}3One can replace with if there are more than one million slots.
Putting these two together it is possible to query and write to a Neural Bloom Filter with memory slots in time, where the network consumes space. It is worth noting, however, the Neural Bloom Filter’s memory is often much smaller than the corresponding classical Bloom Filter’s memory, and in many of our experiments is even smaller than the number of unique elements to store. Thus dense matrix multiplication can still be preferable  especially due to its acceleration on GPUs and TPUs (Jouppi et al., 2017)  and a dense representation of is not inhibitory. As model optimization can become applicationspecific, we do not focus on these implementation details and use the model in its simplest setting with dense matrix operations.
a.2 Moving ZCA
The moving ZCA was computed by taking moving averages of the first and second moment, calculating the ZCA matrix and updating a moving average projection matrix
. This is only done during training, at evaluation time is fixed. We describe the update below for completeness.(1)  
1st moment EMA  (2)  
2nd moment EMA  (3)  
Singular values  (4)  
ZCA matrix  (5)  
ZCA EMA  (6)  
Projected query  (7) 
In practice we do not compute the singular value decomposition at each time step to save computational resources, but instead calculate it and update
every steps. We scale the discount in this case .a.3 Relation to uniform hashing
We can think of the decorrelation of , along with the sparse contentbased attention with , as a hash function that maps to several indices in . For moderate dimension sizes of (, say) we note that the Gaussian samples in lie close to the surface of a sphere, uniformly scattered across it. If , the decorrelated query, were to be Gaussian then the marginal distribution of nearest neighbours rows in will be uniform. If we chose the number of nearest neighbours then this implies the slots in are selected independently with uniform probability. This is the exact hash function specification that Bloom Filters assume. Instead we use a continuous (as we choose ) approximation (as we decorrelate vs Gaussianize) to this uniform hashing scheme, so it is differentiable and the network can learn to shape query representations.
Appendix B Space Comparison
For each task we compare the model’s memory size, in bits, at a given false positive rate — usually chosen to be . For our neural networks which output a probability one could select an operating point such that the false positive rate is . In all of our experiments the neural network outputs a memory (state) which characterizes the storage set. Let us say SPACE(f, ) is the minimum size of , in bits, for the network to achieve an average false positive rate of . We could compare SPACE(f,) with SPACE(Bloom Filter,) directly, but this would not be a fair comparison as our network can emit false negatives.
To remedy this, we employ the same scheme as Kraska et al. (2018) where we use a ‘backup’ Bloom Filter with false positive rate to store all false negatives. When we query the backup Bloom Filter. Because the overall false positive rate is , to achieve a false positive rate of at most (say ) we can set . The number of elements stored in the backup bloom filter is equal to the number of false negatives, denoted . Thus the total space can be calculated, TOTAL_SPACE(f,) = SPACE(f,) + * SPACE(Bloom Filter,). We compare this quantity for different storage set sizes.
Appendix C Model Size
For the MNIST experiments we used a
layer convolutional neural network with
filters followed by a twolayer feedforward network with hiddenlayers respectively. The number of trainable parameters in the Neural Bloom Filter (including the encoder) is which amounts to Mb at bit precision. We did not optimize the encoder architecture to be lean, as we consider it part of the library in a sense. For example, we do not count the size of the hashing library that an implemented Bloom Filter relies on, which may have a chain of dependencies, or the package size of TensorFlow used for our experiments. Nevertheless we can reason that when the Neural Bloom Filter is kb smaller than the classical, such as for the nonuniform instancebased familiarity in Figure 2b, we would expect to see a net gain if we have a collection of at least datastructures. We imagine this could be optimized quite significantly, by using bit precision and perhaps using more convolution layers or smaller feedforward linear operations.For the database experiments we used an LSTM character encoder with hidden units followed by another feedforward layer. The number of trainable parameters in the Neural Bloom Filter which amounts to Mb. One could imagine optimizing this by switching to a GRU or investigating temporal convolutions as encoders.
Appendix D HyperParameters
We swept over the following hyperparameters, over the range of memory sizes displayed for each task. We computed the best model parameters by selecting those which resulted in a model consuming the least space as defined in Appendix B. This depends on model performance as well as state size. The Memory Networks memory size was fixed to equal the input size (as the model does not arbitrate what inputs to avoid writing).
Memory Size (DNC, NBF)  {2, 4, 8, 16, 32, 64} 

Word Size (MemNets, DNC, NBF)  {2, 4, 6, 8, 10} 
Hidden Size (LSTM)  {2, 4, 8, 16, 32, 64} 
Sphering Decay (NBF)  {0.9, 0.95, 0.99} 
Learning Rate (all)  {1e4, 5e5} 
Appendix E Experiment Details
For the classbased familiarity task, and uniform sampling task, the model was trained on the training set and evaluated on the test set. For the classbased task sampling, a class is sampled at random and is formed from a random subset of images from that class. The queries are chosen uniformly from either or from images of a different class.
For the nonuniform instancebased familiarity task we sampled images from an exponential distribution. Specifically we used a fix permutation of the training images, and from that ordering chose for the images to store. The query images were selected uniformly. We used a fixed permutation (or shuffle) of the images to ensure most probability mass was not placed on images of a certain class. I.e. by the natural ordering of the dataset we would have otherwise almost always sampled images. This would be confounding task nonuniformity for other latent structure to the sets. Because the network needed to relate the image to its frequency of occurence for task, the models were evaluated on the training set. This is reasonable as we are not wishing for the model to visually generalize to unseen elements in the setting of this exactfamiliarity task. We specifically want the network weights to compress a map of image to probability of storage.
For the database task a universe of unique tokens were extracted from GigaWord v5. We shuffled the tokens and placed M in a training set and K in a test set. These sets were then sorted alphabetically. A random subset, representing an SSTable, was sampled by choosing a random start index and selecting the next elements, which form our set . Queries are sampled uniformly at random from the universe set. Models are trained on the training set and evaluated on the test set.
Appendix F Database Extrapolation Task
We investigate whether neural models are able to extrapolate to larger test sizes. Using the database task setup, where each set contains a contiguous set of sorted strings; we train both the Neural Bloom Filter and LSTM on sets of sizes 2  200. We then evaluate on sets up to 250, i.e. a 25% increase over what is observed during training. This is to emulate the scenario that we train on a selection of databse tablets, but during evaluation we may observe some tablets that are slightly larger than those in the training set. Both the LSTM and Neural Bloom Filter are able to solve the task, with the Neural Bloom Filter using significantly less space for the larger allowed false positive rate of 5% and 1%. We do see the models’ error increase as it surpasses the maximum training set size, however it is not catastrophic. Another interesting trend is noticeable; the neural models have higher utility for larger allowed false positive rates. This may be because of the difficulty in training the models to an extremely low accuracy.
Appendix G Effect of Sphering
We see the benefit of sphering in Figure 5 where the converged validation performance ends up at a higher state. Investigating the proportion of memory filled after all elements have been written in Figure 6, we see the model uses quite a small proportion of its memory slots. This is likely due to the network fixating on rows it has accessed with sparse addressing, and ignoring rows it has otherwise never touched — a phenomena noted in Shazeer et al. (2017). The model finds a local minima in continually storing and accessing the same rows in memory. The effect of sphering is that the query now appears to be Gaussian (up to the first two moments) and so the nearest neighbour in the address matrix A (which is initialized to Gaussian random variables) will be close to uniform. This results in a more uniform memory access (as seen in Figure 6) which significantly aids performance (as seen in Figure 5).
Appendix H Timing Benchmark
We use the Neural Bloom Filter network architecture for the large database task (Table 1). The network uses an encoder LSTM with hidden units over the characters, and feeds this through a fully connected layer to encode the input. A twolayer hiddenunit MLP is used as the query architecture. The memory and word size is and respectively, and so the majority of the compute is spent in the encoder and query network. We compare this with an LSTM containing hidden units. We benchmark the singlequery latency of the network alongside the throughput of a batch of queries, and a batch of inserts. The Neural Bloom Filter and LSTM is implemented in TensorFlow without any custom kernels or specialized code. We benchmark it on the CPU (Intel(R) Xeon(R) CPU E51650 v2 @ 3.50GHz) and a GPU (NVIDIA Quadro P6000). We compare to empirical timing results published in a queryoptimized Bloom Filter variant (Chen et al., 2007).
It is worth noting, in several Bloom Filter applications, the actual query latency is not in the critical path of computation. For example, for a distributed database, the network latency and disk access latency for one tablet can be orders of magnitude greater than the inmemory latency of a Bloom Filter query. For this reason, we have not made runtime a point of focus in this study, and it is implicitly assumed that the neural network is trading off greater latency for less space. However it is worth checking whether runtime could be prohibitive.