BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Paper implementations20 of 28 in topic

847. NEFTune noisy embeddings

EasyPaper implementations

Why it matters NEFTune is a one-line change that the paper reports lifts AlpacaEval scores. It is a good example of a paper whose whole method fits in one formula.

Paper: "NEFTune: Noisy Embeddings Improve Instruction Finetuning", Jain et al., 2023, arXiv 2310.05914.

During instruction fine-tuning, NEFTune adds uniform noise to the token embeddings. The noise is drawn from Uniform(-1, 1) and scaled by alpha / sqrt(L * d), where L is the sequence length and d is the embedding dimension.

Write neftune(embeddings, uniform_noise, alpha). The noise sample is passed in so the result is deterministic.

Inputs:
- embeddings: a list or ndarray of shape (L, d) or (batch, L, d). Use np.asarray(embeddings, dtype=float).
- uniform_noise: same shape, values in [-1, 1].
- alpha: a float >= 0. alpha = 0 returns the embeddings unchanged.

L and d are always the last two axes. Return an ndarray of floats with the input shape. No rounding.

Functiondef neftune(embeddings, uniform_noise, alpha)

Example 1

Input: neftune([[1.0, 2.0], [3.0, 4.0]], [[1.0, -1.0], [0.5, 0.0]], 4.0)

Output: [[3.0, 0.0], [4.0, 4.0]]

Example 2

Input: embeddings = [[0.0, 0.0, 0.0]], uniform_noise = [[1.0, 1.0, -1.0]], alpha = 3.0

Output: [[1.732051, 1.732051, -1.732051]]

Example 3

Input: embeddings = [[[1.0], [1.0], [1.0], [1.0]]], uniform_noise = [[[1.0], [-1.0], [0.5], [0.0]]], alpha = 2.0

Output: [[[2.0], [0.0], [1.5], [1.0]]]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

Read L and d from the last two axes with shape[-2] and shape[-1].

Hint 2

The scale is alpha / sqrt(L * d), one number for the whole sequence.

Hint 3

Return embeddings + scale * noise.

Common mistakes
  • Using sqrt(d) only.
  • Taking L from axis 0 when there is a batch axis.
  • Scaling the embeddings instead of adding noise.
Understand the idea

The scale alpha / sqrt(L d) keeps the expected noise norm roughly constant as sequences get longer or wider. For a (2, 2) input with alpha 4 the scale is 4 / 2 = 2, so noise [1, -1] turns [1, 2] into [3, 0].

Shapes and data flow
embeddings and noise: (..., L, d). Output: same shape.
Topics
Paper implementations
Similar questions
  • word2vec skip-gram negative sampling loss (Mikolov et al., 2013)Easy
  • Plan a training run: compute-optimal model size and tokens (Chinchilla rule)Easy
  • Evaluate the Chinchilla scaling lawEasy
  • Build a global magnitude pruning maskEasy
All Paper implementations problems
Report
847 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def neftune(embeddings, uniform_noise, alpha):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS