@@ -15,6 +15,13 @@ SigNetworks is available on PyPI and can be installed with pip:
1515pip install sig_networks
1616```
1717
18+ Note that currently ` sig_networks ` only supports Python 3.8 since it relies on
19+ the [ ` signatory ` ] ( https://github.com/patrick-kidger/signatory ) library. However,
20+ it is possible to install ` signatory ` with more recent Python and PyTorch
21+ versions if you install it from source. See the installation guide in the
22+ [ signatory documentation] ( https://signatory.readthedocs.io/en/latest/pages/usage/installation.html )
23+ for more details.
24+
1825### Signatory/Torch
1926
2027SigNetworks depends on the
@@ -36,6 +43,10 @@ pip install signatory==1.2.6.1.9.0
3643pip install sig_networks
3744```
3845
46+ If you encounter any issues with the installation of ` signatory ` , please see the
47+ FAQs in the
48+ [ signatory documentation] ( https://signatory.readthedocs.io/en/latest/pages/miscellaneous/faq.html ) .
49+
3950## Usage
4051
4152The key components in the _ signature-window_ models presented in (see
@@ -64,10 +75,107 @@ fashion. The key components are:
6475- The SeqSigNet-Attention-BiLSTM model:
6576 [ ` sig_networks.SeqSigNetAttentionBiLSTM ` ] ( src/sig_networks/seqsignet_attention_bilstm.py )
6677
78+ ### Using the SWNU and SWMHAU modules
79+
80+ The Signature Window units (SWNU and SWMHAU) accept a batch of streams and
81+ returns a batch of feature representations. For example:
82+
6783``` python
68- ...
84+ from sig_networks.swnu import SWNU
85+ import torch
86+
87+ # initialise a SWNU object
88+ swnu = SWNU(
89+ input_channels = 10 ,
90+ hidden_dim = 5 ,
91+ log_signature = False ,
92+ sig_depth = 3 ,
93+ pooling = " signature" ,
94+ BiLSTM = True ,
95+ )
96+
97+ # create a three-dimensional tensor of batched streams
98+ # shape [batch, length, channels] where channels = 10
99+ streams = torch.randn(2 , 20 , 10 )
100+
101+ # pass the streams through the SWNU
102+ features = swnu(streams)
103+
104+ # features is a two-dimensional tensor of shape [batch, signature_channels]
105+ features.shape
69106```
70107
108+ The SWMHAU is similar to the SWNU, but rather than having an LSTM to process the
109+ signature streams, we have a multihead-attention layer. For example:
110+
111+ ``` python
112+ from sig_networks.swmhau import SWMHAU
113+ import torch
114+
115+ # initialise a SWMHAU object
116+ swmhau = SWMHAU(
117+ input_channels = 10 ,
118+ output_channels = 5 ,
119+ log_signature = False ,
120+ sig_depth = 3 ,
121+ num_heads = 5 ,
122+ num_layers = 1 ,
123+ dropout_rate = 0.1 ,
124+ pooling = " signature" ,
125+ )
126+
127+ # create a three-dimensional tensor of batched streams
128+ # shape [batch, length, channels] where channels = 10
129+ streams = torch.randn(2 , 20 , 10 )
130+
131+ # pass the streams through the SWMHAU
132+ features = swmhau(streams)
133+
134+ # features is a two-dimensional tensor of shape [batch, signature_channels]
135+ features.shape
136+ ```
137+
138+ Note in the above, we used the ` pooling="signature" ` option. This means that at
139+ the end of the SWNU/SWMHAU, we will take a final signature of the streams to get
140+ a fixed-length feature representation for each item in the batch. There are
141+ other options such as taking the final LSTM hidden state for SWNU (set
142+ ` pooling="lstm" ` ), or using a CLS pooling for SWMHAU (set ` pooling="cls" ` ).
143+ There is another option where ` pooling=None ` which means that the SWNU/SWMHAU
144+ where no pooling is applied at the end of the SWNU/SWMHAU and the output is a
145+ three-dimensional tensor of shape ` [batch, length, hidden_dim] ` .
146+
147+ ### Using the network models
148+
149+ The library also has the SWNU-Network and SeqSigNet models as introduced in
150+ [ Sequential Path Signature Networks for Personalised Longitudinal Language Modeling] ( https://aclanthology.org/2023.findings-acl.310/ ) .
151+
152+ Since then, there have been developments of other models which utilise the SWNUs
153+ and SWMHAUs discussed above. Each of these models are avaliable as PyTorch
154+ modules which can be initialised and trained in the usual way.
155+
156+ For SWNU-Network and SWMHAU-Network models, they expect two inputs:
157+
158+ 1 . ` path ` : a batch of streams of shape ` [batch, length, channels] ` - these get
159+ processed by the SWNU/SWMHAU
160+ 2 . ` features ` : a batch of features of shape ` [batch, features] ` - these get
161+ concatenated with the output of the SWNU/SWMHAU to be fed into a FFN layer
162+
163+ For SeqSigNet models (e.g. SeqSigNet, SeqSigNet-Attention-Encoder,
164+ SeqSigNet-Attention-BiLSTM), they also expect two inputs but the path is
165+ slightly different:
166+
167+ 1 . ` path ` : a batch of streams of shape ` [batch, units, length, channels] ` - each
168+ of the units for each batch will get processed by the SWNU/SWMHAU.
169+ Afterwards, there is a global network to process the outputs of the
170+ SWNU/SWMHAU in order to pool the outputs into a single fixed-length feature
171+ represenation for the history. The global network can either be a BiLSTM (in
172+ the case of SeqSigNet and SeqSigNet-Attention-BiLSTM) or a Transformer
173+ Encoder (in the case of SeqSigNet-Attention-Encoder).
174+ 2 . ` features ` : a batch of features of shape ` [batch, features] ` - these get
175+ concatenated with the output of the global network (either BiLSTM or a
176+ Transformer Encoder) that processes the outputs of SWNU and SWMHAU to be fed
177+ into a FFN layer
178+
71179## Pre-commit and linters
72180
73181To take advantage of ` pre-commit ` , which will automatically format your code and
0 commit comments