shortened the lines (#12214)

* Create README.md

* Update README.md

* Add files via upload

* Delete llm_experiments directory

* Create README.md

* Add files via upload

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add files via upload

* Delete neural_network/chatbot/main.py

* Delete neural_network/chatbot/llm_service.py

* Delete neural_network/chatbot/chatbot.py

* Delete neural_network/chatbot/db.py

* Update README.md

* Add files via upload

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add files via upload

made changes suggested by auto-checker

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add files via upload

* Add files via upload

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Delete neural_network/chatbot directory

* Add files via upload

* Add files via upload

* Add files via upload

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add files via upload

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* updating DIRECTORY.md

* Rename batch_size variable to _batch_size

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: cclauss <cclauss@users.noreply.github.com>
This commit is contained in:
Pritam Das
2026-09-23 15:46:57 +02:00
committed by GitHub
co-authored by pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Christian Clauss cclauss
parent c0fd8deab2
commit 692d38ff6f
2 changed files with 103 additions and 0 deletions
+2
View File
@@ -966,6 +966,7 @@
* [Happy Number](maths/special_numbers/happy_number.py)
* [Harshad Numbers](maths/special_numbers/harshad_numbers.py)
* [Hexagonal Number](maths/special_numbers/hexagonal_number.py)
* [Jacobsthal Number](maths/special_numbers/jacobsthal_number.py)
* [Kaprekar Constant](maths/special_numbers/kaprekar_constant.py)
* [Kaprekar Number](maths/special_numbers/kaprekar_number.py)
* [Krishnamurthy Number](maths/special_numbers/krishnamurthy_number.py)
@@ -1058,6 +1059,7 @@
* [Nesterov Accelerated Sgd](neural_network/optimizers/nesterov_accelerated_sgd.py)
* [Perceptron](neural_network/perceptron.py)
* [Simple Neural Network](neural_network/simple_neural_network.py)
* [Sliding Window Attention](neural_network/sliding_window_attention.py)
* [Two Hidden Layers Neural Network](neural_network/two_hidden_layers_neural_network.py)
## [Other](other)
+101
View File
@@ -0,0 +1,101 @@
"""
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
Name - - sliding_window_attention.py
Goal - - Implement a neural network architecture using sliding
window attention for sequence modeling tasks.
Detail: Total 5 layers neural network
* Input layer
* Sliding Window Attention Layer
* Feedforward Layer
* Output Layer
Author: Stephen Lee
Github: 245885195@qq.com
Date: 2024.10.20
References:
1. Choromanska, A., et al. (2020). "On the Importance of
Initialization and Momentum in Deep Learning." *Proceedings
of the 37th International Conference on Machine Learning*.
2. Dai, Z., et al. (2020). "Transformers are RNNs: Fast
Autoregressive Transformers with Linear Attention."
*arXiv preprint arXiv:2006.16236*.
3. [Attention Mechanisms in Neural Networks](https://en.wikipedia.org/wiki/Attention_(machine_learning))
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
"""
import numpy as np
class SlidingWindowAttention:
"""Sliding Window Attention Module.
This class implements a sliding window attention mechanism where
the model attends to a fixed-size window of context around each token.
Attributes:
window_size (int): The size of the attention window.
embed_dim (int): The dimensionality of the input embeddings.
"""
def __init__(self, embed_dim: int, window_size: int) -> None:
"""
Initialize the SlidingWindowAttention module.
Args:
embed_dim (int): The dimensionality of the input embeddings.
window_size (int): The size of the attention window.
"""
self.window_size = window_size
self.embed_dim = embed_dim
rng = np.random.default_rng()
self.attention_weights = rng.standard_normal((embed_dim, embed_dim))
def forward(self, input_tensor: np.ndarray) -> np.ndarray:
"""
Forward pass for the sliding window attention.
Args:
input_tensor (np.ndarray): Input tensor of shape (batch_size,
seq_length, embed_dim).
Returns:
np.ndarray: Output tensor of shape (batch_size, seq_length, embed_dim).
>>> x = np.random.randn(2, 10, 4) # Batch size 2, sequence
>>> attention = SlidingWindowAttention(embed_dim=4, window_size=3)
>>> output = attention.forward(x)
>>> output.shape
(2, 10, 4)
>>> (output.sum() != 0).item() # Check if output is non-zero
True
"""
_batch_size, seq_length, _ = input_tensor.shape
output = np.zeros_like(input_tensor)
for i in range(seq_length):
# Define the window range
start = max(0, i - self.window_size // 2)
end = min(seq_length, i + self.window_size // 2 + 1)
# Extract the local window
local_window = input_tensor[:, start:end, :]
# Compute attention scores
attention_scores = np.matmul(local_window, self.attention_weights)
# Average the attention scores
output[:, i, :] = np.mean(attention_scores, axis=1)
return output
if __name__ == "__main__":
import doctest
doctest.testmod()
# usage
rng = np.random.default_rng()
x = rng.standard_normal((2, 10, 4)) # Batch size 2,
attention = SlidingWindowAttention(embed_dim=4, window_size=3)
output = attention.forward(x)
print(output)