Skip to content

Conversation

devvratpathak
Copy link

Describe your change:

This PR adds a comprehensive Vision Transformer (ViT) implementation to the computer_vision folder for image classification tasks, implementing the architecture from "An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale" (Dosovitskiy et al., 2020).

The implementation includes patch embedding, positional encoding, attention mechanism, layer normalization, feed-forward network, transformer encoder blocks, and the complete ViT pipeline. All functions have comprehensive docstrings, type hints, doctests, and pass all ruff checks.

Fixes #13326

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

…features section

- Add comprehensive table of contents for easy navigation
- Include detailed installation steps with virtual environment setup
- Add usage examples showing how to run and import algorithms
- Create features section listing all algorithm categories
- Add explicit license section with MIT License information
- Expand contributing section with quick start guide
- Add about section explaining repository purpose

Fixes TheAlgorithms#13111
…features section

- Add comprehensive table of contents for easy navigation
- Include detailed installation steps with virtual environment setup
- Add usage examples showing how to run and import algorithms
- Create features section listing all algorithm categories
- Add explicit license section with MIT License information
- Expand contributing section with quick start guide
- Add about section explaining repository purpose

Fixes TheAlgorithms#13111
…ation

- Implement complete ViT architecture with patch embedding
- Add positional encoding with learnable CLS token
- Include scaled dot-product attention mechanism
- Implement transformer encoder blocks with layer normalization
- Add feed-forward network with GELU activation
- Include comprehensive docstrings and type hints
- Add doctests for all functions
- Provide example usage demonstrating the complete pipeline

Fixes TheAlgorithms#13326
- Replace Optional with X | None syntax (UP045)
- Use np.random.Generator instead of legacy np.random methods (NPY002)
- Fix line length violations (E501)
- Assign f-string literals to variables in exceptions (EM102)
- Remove unused variables and parameters (RUF059, F841)
- Add noqa comment for intentionally unused API parameter
- All doctests still pass successfully
@algorithms-keeper algorithms-keeper bot added documentation This PR modified documentation files require descriptive names This PR needs descriptive function and/or variable names labels Oct 7, 2025
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

return output, attention_weights


def layer_norm(x: np.ndarray, epsilon: float = 1e-6) -> np.ndarray:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

return (x - mean) / (std + epsilon)


def feedforward_network(x: np.ndarray, hidden_dim: int = 3072) -> np.ndarray:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x



def transformer_encoder_block(
x: np.ndarray, num_heads: int = 12, hidden_dim: int = 3072 # noqa: ARG001

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 7, 2025
- Rename 'x' to 'embeddings' in layer_norm, feedforward_network,
  and transformer_encoder_block functions
- Update all docstring examples to use 'embeddings'
- Improves code readability per algorithms-keeper bot feedback
- Fix noqa comment placement for unused num_heads parameter
- All doctests and ruff checks pass
@devvratpathak devvratpathak reopened this Oct 7, 2025
@algorithms-keeper algorithms-keeper bot removed the require descriptive names This PR needs descriptive function and/or variable names label Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed documentation This PR modified documentation files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Vision Transformer code for image classification

2 participants