The architecture of STraTS is illustrated in Figure 3. Unlike most of the existing approaches which
take a time-series matrix as input, STraTS defines its input as a set of observation triplets. Each
observation triplet in the input is embedded using the Initial Triplet Embedding module. The
initial triplet embeddings are then passed through a Contextual Triplet Embedding module which
utilizes the Transfomer architecture to encode the context for each triplet. The Fusion Self-attention
module then combines these contextual embeddings via self-attention mechanism to generate an
embedding for the input time-series which is concatenated with demographics embedding and
passed through a feed-forward network to make the final prediction. The notations used in the
paper are summarized in Table 1.
3.2.1 Initial Triplet Embedding. Given an input time-series T = {(𝑡𝑖
, 𝑓𝑖
, 𝑣𝑖)}𝑛
𝑖=1
, the initial embedding
for the 𝑖
𝑡ℎ triplet ei ∈ R
𝑑
is computed by summing the following component embeddings: (i) Feature
embedding e
f
i
∈ R
𝑑
, (ii) Value embedding e
v
i
∈ R
𝑑
, and (iii) Time embedding e
t
i
∈ R
𝑑
. In other words,
ei = e
f
i
e
v
i
e
t
i
∈ R
𝑑
. Feature embeddings e
f
(·) are obtained from a simple lookup table similar to
word embeddings. Since feature values and times are continuous unlike feature names which are
categorical objects, we cannot use a lookup table to embed these continuous values unless they
are categorized. Some researchers [30, 32] have used sinusoidal encodings to embed continuous
ACM Trans. Knowl. Discov. Data., Vol. 1, No. 1, Article . Publication date: February 2022.
Self-Supervised Transformer for Sparse and Irregularly Sampled Multivariate Clinical Time-Series 7
Table 1. Notations used in this paper.
Notation Definition
𝑁 # Time-series for target task
𝑁
′
Time-series for forecasting task
d ∈ R
𝐷 Demographics vector
F Set of clinical variables
𝑡𝑖 ∈ R≥0 Time of 𝑖
𝑡ℎ observation
𝑓𝑖 ∈ F Variable of 𝑖
𝑡ℎ observation
𝑣𝑖 ∈ R Value of 𝑖
𝑡ℎ observation
(𝑡, 𝑓 , 𝑣) Observation triplet
T = {(𝑡𝑖
, 𝑓𝑖
, 𝑣𝑖)}𝑛
𝑖=1 Multivariate time-series
𝑦,𝑦˜ ∈ {0, 1} True and predicted outputs for target task
z, z˜ ∈ R
|F | True and predicted outputs for forecasting task
m ∈ {0, 1}
|F | Forecast mask
e
𝑡
𝑖
, e
𝑣
𝑖
∈ R
𝑑 CVE for time and value
e
𝑓
𝑖
∈ R
𝑑 Variable embedding
e𝑖 ∈ R
𝑑
Initial triplet embedding
e
𝑇 ∈ R
𝑑 Time-series embedding
e
𝑑 ∈ R
𝑑 Demographics embedding
values. We propose a novel continuous value embedding (CVE) technique using a one-to-many
Feed-Forward Network (FFN) with learnable parameters i.e., e
v
i
= 𝐹 𝐹𝑁𝑣
(𝑣𝑖), and e
t
i
= 𝐹 𝐹𝑁𝑡
(𝑡𝑖).
Both FFNs have one input neuron and 𝑑 output neurons and a single hidden layer with ⌊
√
𝑑⌋
neurons and 𝑡𝑎𝑛ℎ(·) activation. They are of the form 𝐹 𝐹𝑁 (𝑥) = 𝑈 𝑡𝑎𝑛ℎ(𝑊 𝑥 + 𝑏) where the
dimensions of weights {𝑊 , 𝑏,𝑈 } can be inferred from the size of hidden and output layers of the
FFN. Unlike sinusoidal encodings with fixed frequencies, this technique offers more flexibility
by allowing end-to-end learning of continuous value and time embeddings without the need to
categorize them.
3.2.2 Contextual Triplet Embedding. The initial triplet embeddings {e1, ..., e𝑛} are then passed
through a Transformer architecture [30] with 𝑀 blocks, each containing a Multi-Head Attention
(MHA) layer with ℎ attention heads and a FFN with one hidden layer. Each block takes 𝑛 input
embeddings E ∈ R
𝑛×𝑑
and outputs the corresponding 𝑛 output embeddings C ∈ R
𝑛×𝑑
that capture
the contextual information. MHA layers use multiple attention heads to attend to information
contained in different embedding projections in parallel. The computations of the MHA layer are
given by
H𝑗 = 𝑠𝑜 𝑓 𝑡𝑚𝑎𝑥
(EW𝑞
𝑗
) (EW𝑘
𝑗
)
𝑇
√︁
𝑑/ℎ
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
(EW𝑣
𝑗
) 𝑗 = 1, ..., ℎ (1)
𝑀𝐻𝐴(E) = (H1 ◦ ... ◦ Hℎ) W𝑐 (2)
Each head projects the input embeddings into query, key, and value subspaces using matrices
{W
𝑞
𝑗
, W𝑘
𝑗
, W𝑣
𝑗
} ⊂ R
𝑑×𝑑ℎ . The queries and keys are then used to compute the attention weights
which are used to compute weighted averages of value (different from value in observation triplet)
vectors. Finally, the outputs of all heads are concatenated and projected to original dimension with
ACM Trans. Knowl. Discov. Data., Vol. 1, No. 1, Article . Publication date: February 2022.
8 Sindhu Tipirneni and Chandan K Reddy
Fig. 3. The overall architecture of the proposed STraTS model. The Input Triplet Embedding module embeds
each observation triplet, the Contextual Triplet Embedding module encodes contextual information for the
triplets, the Fusion Self-Attention module computes time-series embedding which is concatenated with
demographics embedding and passed through a dense layer to generate predictions for target and self supervision (forecasting) tasks.
W𝑐 ∈ R
ℎ𝑑ℎ×𝑑
. The FFN layer takes the form
F(X) = 𝑅𝑒𝐿𝑈 (XW𝑓
1
b
𝑓
1
) W
𝑓
2
b
𝑓
2
(3)
with weights Wf
1
∈ R
𝑑×2𝑑
, b
f
1
∈ R
2𝑑
, Wf
2
∈ R
2𝑑×𝑑
, b
f
2
∈ R
𝑑
. Dropout, residual connections, and
layer normalization are added for every MHA and FFN layer. Also, attention dropout randomly
masks out some positions in the attention matrix before the softmax computation during training.
The output of each block is fed as input to the succeeding one, and the output of the last block
gives the contextual triplet embeddings {c1, ..., c𝑛}.
3.2.3 Fusion Self-attention. After computing contextual embeddings using a Transformer, we
fuse them using a self-attention layer to compute time-series embedding e
𝑇 ∈ R
𝑑
. This layer first
computes attention weights {𝛼1, ..., 𝛼𝑛} by passing each contextual embedding through a FFN and
computing a softmax over all the FFN outputs.
𝑎𝑖 = u
𝑇
𝑎
𝑡𝑎𝑛ℎ(W𝑎c𝑖 + b𝑎) (4)
𝛼𝑖 =
𝑒𝑥𝑝(𝑎𝑖)
Í𝑛
𝑗=1
𝑒𝑥𝑝(𝑎𝑗)
∀𝑖 = 1, ..., 𝑛 (5)
W𝑎 ∈ R
𝑑𝑎×𝑑
, b𝑎 ∈ R
𝑑𝑎
, ua ∈ R
𝑑𝑎 are the weights of this attention network which has 𝑑𝑎 neurons in
the hidden layer. The time-series embedding is then computed as
e
𝑇 =
∑︁𝑛
𝑖=1
𝛼𝑖ci
(6)
ACM Trans. Knowl. Discov. Data., Vol. 1, No. 1, Article . Publication date: February 2022.
Self-Supervised Transformer for Sparse and Irregularly Sampled Multivariate Clinical Time-Series 9
3.2.4 Demographics Embedding. We realize that demographics can be encoded as triplets with
a default value for time. However, we found that the prediction models performed better in our
experiments when demographics are processed separately by passing d through a FFN as shown
below. The demographics embedding is thus obtained as
e
𝑑 = 𝑡𝑎𝑛ℎ(W𝑑
2
𝑡𝑎𝑛ℎ(W𝑑
1
d + b
𝑑
1
) + b
𝑑
2
) ∈ R
𝑑
(7)
where the hidden layer has a dimension of 2𝑑.
3.2.5 Prediction Head. The final prediction for target task is obtained by passing the concatenation
of demographics and time-series embeddings through a dense layer with weights w𝑇
𝑜 ∈ R
𝑑
, 𝑏𝑜 ∈ R
and sigmoid activation.
𝑦˜ = 𝑠𝑖𝑔𝑚𝑜𝑖𝑑 (w
𝑇
𝑜
[e
𝑑
◦ e
𝑇
] + 𝑏𝑜 ) (8)
The model is trained on the target task using cross-entropy loss.
3.2.6 Self-supervision. We experimented with both masking and forecasting as pretext tasks for
providing self-supervision and found that forecasting improved the results on target tasks. The
forecasting task uses the same architecture as the target task except for the prediction layer i.e.,
z˜ = W𝑠 [e
𝑑
◦ e
𝑇
] + b𝑠 ∈ R
|F | (9)
A masked MSE loss is used for training on the forecasting task to account for missing values in the
forecast outputs. Thus, the loss for self-supervision is given by
L𝑠𝑠 =
1
|𝑁 ′
|
𝑁
∑︁′
𝑘=1
∑︁
|F |
𝑗=1
𝑚
𝑘
𝑗
(z˜
𝑘
𝑗 − z
𝑘
𝑗
)
2
(10)
where m𝑘
𝑗
= 1 (or m𝑘
𝑗
= 0) if the ground truth forecast z
𝑘
𝑗
is available (or unavailable) for 𝑗
𝑡ℎ variable
in 𝑘
𝑡ℎ sample. The model is first pretrained on the self-supervision task and is then fine-tuned on
the target task.
from argparse import Namespace
from models import TimeSeriesModel
import torch.nn as nn
from transformers import BertModel, BertConfig
import torch
import torch.nn.functional as F
import numpy as np
x = torch.unsqueeze(x, -1)
x = torch.matmul(x,self.W1) + self.b1[None,None,:] # bsz,max_len,int_dim
x = self.activation(x)
x = torch.matmul(x,self.W2) #bsz,max_len,hid_dim
return x
att = torch.matmul(x,self.W) + self.b[None,None,:] # bsz,max_len,int_dim
att = self.activation(att)
att = torch.matmul(att,self.u)[:,:,0] # bsz,max_len
att = att + (1-mask)torch.finfo(att.dtype).min
att = torch.softmax(att,dim=-1) # bsz,max_len
return att
if self.pretrain:
return self.forecast_final(ts_demo_emb, forecast_values, forecast_mask)
logits = self.binary_head(self.forecast_head(ts_demo_emb))[:,0] if self.finetune else self.binary_head(ts_demo_emb)[:,0]
return self.binary_cls_final(logits, labels)
i am trying to use NOTEEVENTS on STraTS exclusively. time would be minutes, value would be the embedded text data from NOTEEVENTS, and features would be the embeddings of a concated desc and category. this is to be done without modifying the architecture is this feasible? ask me if you have any doubts regarding on anything need any form of clarification.
what is the best course of action with reference to the above?
It seems you are attempting to adapt the STraTS model to work exclusively with clinical notes from the MIMIC-III dataset's NOTEEVENTS, where:
Time (t_i): Timestamp of the note event in minutes.
Feature (f_i): Embeddings of a concatenation of the note's description and category.
Value (v_i): Embedded text data from the note itself.
You wish to incorporate this data into the STraTS model without modifying its architecture.
Feasibility Analysis:
The STraTS model is designed to handle sparse and irregularly sampled multivariate time-series data by processing observation triplets ((t_i, f_i, v_i)), where:
Time Embedding (e_{t_i}): Embeds continuous time values using a small feed-forward network.
Feature Embedding (e_{f_i}): Embeds categorical feature names (variables) using an embedding lookup table.
Value Embedding (e_{v_i}): Embeds continuous scalar values (measurements) using a Continuous Value Embedding (CVE), which is a small feed-forward network.
These embeddings are summed to create the initial triplet embeddings.
Challenges:
The Value Embedding (e_{v_i}) in STraTS is designed for scalar continuous values (e.g., lab measurements, vital signs). It uses a feed-forward network to embed these scalar values.
In your case, v_i is a text embedding, which is a vector, not a scalar.
The current CVE module cannot directly handle vector inputs like text embeddings without modification.
Options and Recommendations:
Modifying the Value Embedding Module:
Feasibility: To embed text data as v_i, you would need to adjust the Value Embedding module to accept vector inputs (text embeddings) instead of scalars.
Required Changes: Modify the CVE (Continuous Value Embedding) to process vector inputs. This could involve changing the input layer to accept higher-dimensional inputs or replacing the CVE with a different mechanism.
Impact: This constitutes a modification to the architecture, which you wish to avoid.
Using Feature Embeddings to Incorporate Text Data:
Idea: Since the Feature Embedding (e_{f_i}) uses an embedding lookup table for categorical variables, you might consider representing each note as a unique "feature" and embedding it using this mechanism.
Challenges:
The number of unique notes is large, which makes embedding them via a lookup table impractical.
It doesn't capture the semantic information within the text.
Concatenating Text Embeddings with Value Embeddings:
Feasibility: Pass the text embeddings through a feed-forward network to reduce their dimensionality to match the existing d dimension, then use them as e_{v_i}.
Required Changes: This still requires modifying the CVE to handle vector inputs or introducing an additional feed-forward network before the CVE.
Impact: Minor architectural changes are necessary.
Alternative Approach Without Modifying the Architecture:
Idea: Represent the entire text data as a sequence of scalar values (e.g., via averaging word embeddings) to produce a scalar value for v_i.
Challenges: This leads to significant loss of information and doesn't leverage the richness of the text data.
Accepting Minimal Architectural Changes:
Given the constraints, it seems unavoidable to make some modifications to the architecture to effectively incorporate text data as v_i.
Recommendation: Modify the Value Embedding module to accept text embeddings. This could involve:
Replacing the CVE with a pre-trained language model (e.g., BERT) to obtain contextual embeddings of the text.
Ensuring the output dimension of the text embeddings matches the model's hidden dimension d.
Justification: This modification is minimal and localized to the Value Embedding component. The overall architecture of STraTS remains largely unchanged.
Best Course of Action:
Based on the above analysis, here's what I recommend:
Modify the Value Embedding Module to Accept Text Embeddings:
Replace or augment the CVE (FFN_v) with a mechanism that can process text embeddings.
Use a pre-trained language model (e.g., BERT, BioBERT, or ClinicalBERT) to obtain embeddings for the text data.
Ensure these embeddings are mapped to the model's hidden dimension d, possibly using a linear layer.
Integration Steps:
Time Embedding (e_{t_i}): Remains unchanged, as time is still a scalar (minutes).
Feature Embedding (e_{f_i}): Since you have embeddings of concatenated descriptions and categories, pre-process these to create a lookup table or use a separate embedding layer to get embeddings matching dimension d.
Value Embedding (e_{v_i}): Replace the CVE with a text embedding module (e.g., BERT). You can:
Pass the note text through BERT to obtain contextual embeddings.
Optionally, use a linear layer to adjust the embedding dimension to match d.
Justify Minimal Architectural Changes:
The modifications are confined to the embedding layer (e_{v_i}).
The rest of the STraTS architecture (Transformer layers, Fusion Self-Attention, Prediction Head) remains untouched.
This approach allows you to leverage the power of STraTS while effectively incorporating textual data.
Implementation Tips:
Batch Processing Text Data: Ensure that the text embeddings are efficiently computed, possibly by pre-processing or using batch operations.
Handling Variable Lengths: Text data can have variable lengths. Decide whether to use padding/truncation or pooling mechanisms to produce fixed-size embeddings.
Resource Considerations: Text embeddings, especially from models like BERT, can be computationally intensive. Ensure that your hardware resources can handle the additional load.
Experiment and Evaluate:
Test the modified model to ensure it performs as expected.
Compare performance with and without the text data to validate the effectiveness of the approach.
Monitor for overfitting, given the richness of text data.
Conclusion:
While your goal is to adapt STraTS without modifying its architecture, effectively incorporating rich textual data from NOTEEVENTS as the Value (v_i) in the triplets necessitates some changes to the Value Embedding component. By making minimal and localized modifications to accept text embeddings, you can maintain the core architecture of STraTS while leveraging the valuable information in clinical notes.
Next Steps:
Proceed with modifying the Value Embedding module to process text embeddings.
Ensure alignment of embedding dimensions across all components.
Maintain the rest of the architecture as is to preserve the benefits of STraTS.
Feel free to ask if you need further clarification or assistance with specific implementation details.