testRNN: Coverage-guided Testing on Recurrent Neural Networks

06/20/2019 ∙ by Wei Huang, et al. ∙ 2

Recurrent neural networks (RNNs) have been widely applied to various sequential tasks such as text processing, video recognition, and molecular property prediction. We introduce the first coverage-guided testing tool, coined testRNN, for the verification and validation of a major class of RNNs, long short-term memory networks (LSTMs). The tool implements a generic mutation-based test case generation method, and it empirically evaluates the robustness of a network using three novel LSTM structural test coverage metrics. Moreover, it is able to help the model designer go through the internal data flow processing of the LSTM layer. The tool is available through: https://github.com/TrustAI/testRNN under the BSD 3-Clause licence.



There are no comments yet.


page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

I introduction

Development of recurrent neural networks (RNNs) has been focused predominantly on improving empirical accuracy, and far less has been done towards their verification and validation. Verification and validation (V&V) are independent procedures that are used together for checking that a product, service, or system meets requirements and specifications and that it fulfills its intended purpose [1]. There is a clear need therefore to move towards developing techniques to validate RNNs against their specifications. We focus on a major class of RNNs, i.e., long short-term memory networks (LSTMs), and an important specification, that is, their robustness. For robustness, it is required that a prediction made by an LSTM is invariant with respect to small perturbations of the input [2].

Our approach is based on coverage-guided testing [3]

, which has been shown successful in software fault detection. Coverage-guided testing has been extended to work with feed-forward neural networks (FNNs) in recent work such as

[4, 5]

, where a collection of test metrics and test case generation algorithms are proposed. These metrics are based on the structural information of the FNNs, such as the neurons

[4, 6], the relation between neurons in neighboring layers [7, 8], etc. However, when working with RNNs, whose internal structures are much more intricate, new test metrics and new test case generation methods are needed to take into account the additional structures and complexity.

In [9], three LSTM structural test metrics and a mutation-based test case generation method are proposed. In this work, we develop testRNN, a testing and debugging tool specially designed for RNNs, implementing the testing method from [9]

. The test metrics are designed to exploit different functional components of the LSTM networks, by considering both the one-step information change and the multi-step information evolution; the information is extracted from gate vectors and hidden state vectors. To refrain from “gaming against criteria”, the test case generation avoids using test metrics as targets, as recommended by Chilensky and Miller in 

[10]. In [11], RNNs are abstracted into state machines, based on which quantitative analysis metrics are applied. The metrics in [11] are not based on the structural information of RNNs, and the testing tool is not released.

testRNN is useful for both model designers and model users. Before putting their LSTM models into practical use, designers can apply coverage-guided testing to find adversarial examples, which can be used for model improvement via e.g., data augmentation. For users, they may refer to testRNN as a useful tool to see if the LSTM applications are sufficiently robust for the usage in critical circumstances.

Ii preliminaries

Ii-a Recurrent Neural Networks (RNNs)

RNNs work with sequential input data, and consists of at least one recurrent layer. The recurrent layer can be represented as a function such that for , where denotes the -th time step, is the cell state and acts as the intermediate memory, is the input, and is the output. Intuitively, the recurrent layer takes as inputs the current time input , the previous time memory state and the previous time output , updates the memory state into , and returns as the current time output. Initially, we let and be 0-valued vectors. For a (finite) sequence of inputs , the function is applied recursively over these inputs. For example, the popular long short-term memory (LSTM) layer can be represented with the following equations for time :



is the sigmoid function such that

for any , is the hyperbolic tangent function such that for any , , which are weight matrices,

are bias vectors,

are internal gate variables, is the hidden state variable (utilising ), and is the cell state variable. Normally, a fully-connected layer will receive the outputs from an RNN layer and further process these for the final classifications or predictions.

Ii-B Internal Information of LSTM Cells

Coverage metrics in [9] are based on the structural information extracted from the basic working units, i.e., cells of LSTM. Equations in (1) represent a set of mathematics operations inside a cell at time step . Based on them, the following information is captured in each cell.

Aggregate information of hidden states

The component value of varies between -1 and 1. It is thus reasonable to divide the possible information of the component into positive () and negative (). For the vector , we take the aggregate information [12] by summing up positive and negative component values, respectively, such that


Intuitively, represents the extent to which the hidden state contains positive information and represents the extent to which the hidden state contains negative information. To compare the hidden states of adjacent cells, we have


which compares the information of the current step with its previous step .

Abstract information of gates

We also consider information represented in the gates , , and . Intuitively, these gates have their dedicated meanings. That is, the forget gate, , controls to what extent information passes through the cell, the input gate, , determines how much new information will be added to the cell state, and the output gate, , determines the level of impact the cell state has on the hidden output.

Let be the value of the gate at time when the input is . We use forget rate to denote the extent to which information passes through the cell, at time , to be memorised, when the input is . Formally, we have


It is easy to see that is within the range , since all components in have their values bounded in .

Iii The testRNN Tool

Fig. 1: Architecture of the RNN testing tool: testRNN

The architecture of testRNN is shown in Figure 1. Given an input LSTM Model, and the Training Dataset, testRNN will generate a Test Suite, together with a test report that logs the update of Coverage Rate, Adversarial Examples, and detailed testing information including the average perturbation of adversarial examples.

Iii-a Test Metrics

testRNN currently supports three structure-based test metrics [9] to exploit the behaviours of a LSTM model: cell coverage, gate coverage and sequence coverage.

Cell coverage aims at covering significant hidden state changes at each time step. When a cell value is greater than , a user defined threshold parameter, the cell is activated and covered by the test case. The coverage is then used to measure the percentage of cells activated at least once by the generated test cases.

The gate coverage is similar to cell coverage, but instead information is extracted from the gates of a LSTM cell. In particular, our tool focuses on the forget gate coverage. The forget gate value indicates how much information can be inherited from the last cell. Since LSTM is well known for its long term memory ability, it is meaningful to check if a cell throws away a suitable amount of information learned from previous inputs. When using forget gate coverage, a threshold value is thus required.

Sequence coverage captures the sequential information passing through the hidden states. It comprises positive sequence coverage, based on , and negative sequence coverage, based on . A sequence such as is contained in symbols. In testRNN, the user is able to decide the number of symbols to be used in the symbolic representation. We use both 2 and 3 in our experiments. Based on symbolic representation, we can collect a set of layer-wise memory patterns from a set of test cases, and define the coverage rate as the percentage of possible patterns covered by the generated test cases. Since the number of patterns increases exponentially with respect to the length of sequence, we allow the users to define a specific range of cells to be considered. For example, one could take the range within the full range to study the last 50 time steps of LSTM processing.

Iii-B Mutation-based Test Case Generation

The Mutation module in Figure 1 implements the test generation algorithm in [9]. It mutates the input dataset for a higher coverage rate and production of more adversarial examples. There are two kinds of mutations: continuous input mutation, and discrete input mutation.

For continuous inputs, such as images, the tool provides a Stochastic Gradient Ascent (SGA) engine. The engine is in principle gradient search [13], mutating an input based on two parameters: a gradient magnitude , and a gradient steps , both of which are randomly selected from pre-specified ranges. Thus, it is easy to use SGA to generate multiple mutations from one input seed from the training dataset.

For discrete input problems, a series of customised mutation operations is very often needed. testRNN

 also provides several default mutation methods for commonly-seen discrete problems. For example, for Natural Language Processing (NLP), available mutation operations includes (1) synonym replacement, (2) random insertion, (3) random swap, (4) random deletion.

Iii-C Parameter Settings

In addition to setting thresholds for test metrics, the user should specify either a level of coverage rate, or a number of test cases, as the stopping criterion. These are required input parameters for the tool. The user can change the pseudo-random number in the Input Seeds Selection module to select different sets of seeds from the training database. The minimal test suite generation is optional for Test Suite. If it is not specified, all the mutation samples will be added into the final test suite.

Iii-D Oracle

Similar as in [5, 7, 6], an oracle is employed to automatically determine if a mutated test case is meaningful. We consider an oracle as a set of Euclidean norm balls with the seed inputs as centers. Only those test cases that fall within such norm balls are valid, among which the misclassified ones are considered faulty, i.e., adversarial examples [2]. The radius of norm balls is configurable.

Iii-E Generated Test Suite

The tool testRNN outputs a test suite that also includes adversarial examples. In addition, the user can ask for a minimal test suite to satisfy the specified test coverage.

Iii-F Coverage Report and Empirical Robustness Evaluation

A coverage report tracks the update of all coverage statistics during the test case generation. The quantity and quality of adversarial examples are also recorded. The quality of adversarial examples is measured by the average perturbation to the original inputs. testRNN includes a post-processing script (namely readfile.py) that reads the report and automatically extracts the running statistics into a .MAT file, to be plotted via Matlab.

The coverage rate and the percentage of adversarial examples in the test suite generated are empirical statistics for the robustness evaluation, under the specified coverage metric. Finally, users can (1) compare different coverage metrics for the same model and (2) analyse the impact of threshold values on the coverage rates with the help of plotted figures.

Iii-G Additional usage

testRNN provides a lot of useful features for LSTM testing. As an example, it records the number of times a test-condition is covered, according to the generated test suite. In Section V, we show that a plot on coverage times can help users understand the learning mechanism behind the LSTM model.

In conclusion, there are at least the following advantages for using testRNN. First, it generates test cases and can find a number of adversarial examples complying with the specified test-conditions. Second, users can use testRNN to compare the robustness of different LSTMs. Third, the adversarial examples can be utilized to retrain or improve a model. Last but not least, the plots on coverage times can give intuitive explanations on how a model learns from a dataset.

Iv Usage Example

testRNN is written in Python. It provides users a range of convenient command line options. An example is given below.

python main.py --model network
--TestCaseNum 2000 --threshold_CC 6
--threshold_GC 0.8 --symbols_SQ 2
--seq [16,20] --minimalTest 0 --mode test
--output log_folder/record.txt

In this case, testRNN reads the input LSTM model network (--model network) and generates a test suite of test cases (--TestCaseNum 2000). The thresholds for cell and forget gate coverage are 6 and 0.8 (--threshold_CC 6 --threshold_GC 0.8), respectively. For sequence coverage, 2 symbols are used for the symbolic representation (--symbols_SQ 2) of sequence patterns in cell [19, 24] (--seq [16,20]). The option --minimalTest 0 stands for not generating the minimal test suite, i.e., all the valid mutated samples will be added to the test suite. A log file record.txt is saved to the folder log_folder.

Fig. 2:

The structure of an LSTM network trained on MNIST database.

V Experiments

We demonstrate the utility of testRNN using a simple LSTM model trained for MNIST handwritten digit recognition. Exemplar usages of testRNN

 on two other LSTM models, including a sentiment analysis model and another molecular prediction model can be found online

111https://github.com/TrustAI/testRNN.The structure of the MNIST model is shown in Figure 2. Each input image is of size

and the rows of an image are taken as a sequential input. The first two layers are LSTM layers, followed by two fully-connected layers with ReLU and SoftMax activation functions respectively, to process the extracted feature information and output the classification result. The model achieves

accuracy in training dataset ( samples) and accuracy in test dataset ( samples).

In our experiment, as shown in Fig. 2, we focus on the LSTM 2 layer. The testing process will terminate when test cases are generated. The hyper-parameters set for three test metrics are: , , and . Since each MNIST image has rows as a sequential input of length , the total number of test-conditions for one-step coverage, including both cell coverage and gate coverage, is . For sequence coverage, for experimental purposes, we let testRNN test on a range of cells, . The experiment results in the log file can be visualised as in Figure 3 (left) by running the following script:

python readfile.py --metrcis all
      --output log_folder/record.txt

It generates the plots of the testing results of all metrics in the report file log_folder/record.txt.

Fig. 3: testRNN testing results for the MNIST model.

Apart from the coverage results for all test metrics, the plot on the right in Figure 3 presents the relationship between the number of adversarial examples in the test suite and the oracle radius. We may use the “area under the curve” to compare the robustness of networks. Figure 4 gives several adversarial examples.

Fig. 4: Adversarial Examples For MNIST

We can also count the coverage times of all test-conditions for cell and gate coverage. The results displayed in Figure 5 indicate the learning pattern of LSTM for an MNIST model. For example, the cells which are covered more often than others in the left of Figure 5, such as the 4th cell, may contribute more towards the final classification.

Fig. 5: 2000 test cases are used to demonstrate the coverage times of 28 features in second LSTM layer of the MNIST model.

Vi Conclusions

testRNN is a tool for testing and debugging LSTM networks. We believe that a worthy application of such a white-box testing tool is to assess the internal structures of an LSTM network, and that this helps inform safety and robustness arguments for LSTM networks.


This document is an overview of UK MOD (part) sponsored research and is released for informational purposes only. The contents of this document should not be interpreted as representing the views of the UK MOD, nor should it be assumed that they reflect any current or future UK MOD policy. The information contained in this document cannot supersede any statutory or contractual requirements or liabilities and is offered without prejudice or commitment.

Crown Copyright (2019), Dstl. This material is licensed under the terms of the Open Government Licence except where otherwise stated. To view this licence, visit http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3 or write to the Information Policy Team, The National Archives, Kew, London TW9 4DU, or email: psi@nationalarchives.gsi.gov.uk.