<jupyter_start><jupyter_text>0. ref * https://pypi.org/project/keras-self-attention/* https://stackoverflow.com/questions/58356868/how-visualize-attention-lstm-using-keras-self-attention-package  1. basic usage<jupyter_code>import keras
from keras_self_attention import SeqSelfAttention


model = keras.models.Sequential()
model.add(keras.layers.Embedding(input_dim=100,
                                 output_dim=30,
                                 mask_zero=True))
model.add(keras.layers.Bidirectional(keras.layers.LSTM(units=32,
                                                       return_sequences=True)))
model.add(SeqSelfAttention(attention_activation='sigmoid'))
model.add(keras.layers.Dense(units=5))
model.compile(
    optimizer='adam',
    loss='categorical_crossentropy',
    metrics=['categorical_accuracy'],
)
model.summary()

# random data for test
import numpy as np

x_data = np.random.rand(50,100)#50 samples
y_data = np.random.randint(2, size=(50, 100,5))
print(x_data.shape, y_data.shape)
model.fit(x_data,y_data)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup