Skip to content

Language models, retrieval, prompting, and language adaptation · Instruction, preference, and parameter-efficient adaptation · LNG078 · Training method

Soft prompt tuning

Learns continuous input prompt embeddings for a frozen language model. It differs from ordinary text prompt editing and from layer-wise prefix tuning because the optimized representation is introduced at the input…

Explained here Component demo, not a trained model CodeSplash's own run

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Learns continuous input prompt embeddings for a frozen language model. It differs from ordinary text prompt editing and from layer-wise prefix tuning because the optimized representation is introduced at the input embedding level.

Question / objective

Can trainable input embeddings adapt a frozen model with very few task parameters?

Architecture / computational structure

A learned m-by-d prompt matrix is prepended at the embedding level; the base network is frozen. Prompt values are learned parameters, while length, initialization, task format, and optimizer schedule are hyperparameters.

Assumptions

Learned prompts are tied to the base model's embedding space and may fail when the checkpoint changes.

Formula / computational definition

P∈Rm×d,min⁡P−log⁡pθ(y∣[P;E(x)]),θ frozen.P\in\mathbb R^{m\times d},\qquad\min_P-\log p_\theta(y\mid[P;E(x)]),\qquad\theta\ \text{frozen}.

Concatenate learned prompt embeddings P in R^(m by d) with embedded input E(x), freeze theta, and minimize -log p_theta(y|[P;E(x)]). Only P and any explicitly unfrozen components receive gradients.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Prompt length m, initialization, learning rate, base-model scale, task formatting, and optional head tuning.

Fitting, fine-tuning, and adaptation

Optimize prompts with the task's supervised likelihood while freezing all base tensors except explicitly named heads. Tune length/learning rate across seeds and validate against discrete prompting and full updates. Preserve base-version compatibility and test task retention when switching prompts; learned prompts do not decode into reliable human instructions.

How to use and calibrate

Compare accuracy, parameter/storage cost, domain transfer, seed variability, and capability retention with discrete prompts, prefix tuning, and full fine-tuning. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Store lightweight task-specific conditioning for a fixed text-to-text model.

Interpretation / pitfalls

Watch out

Limited trainable capacity can hurt difficult shifts; prompt embeddings are not directly readable natural-language instructions. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: PEFT documents prompt-tuning support for compatible models. R can call that implementation via reticulate. The native example concatenates toy prompt/input embeddings and counts trainable prompt parameters, not a trained soft prompt.

Example scope: Soft prompt input-embedding concatenation and trainable parameter count; no optimization.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Soft prompt input-embedding concatenation and trainable parameter count; no optimization.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
prompt = [[0.1,0.2],[0.3,-0.1]]; input_embeddings = [[1,0],[0,1],[0.5,0.5]]
combined = prompt+input_embeddings
print({"input_length":len(combined),"prompt_parameters":len(prompt)*len(prompt[0])})
R

Component demo, not a trained model — Soft prompt input-embedding concatenation and trainable parameter count; no optimization.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
prompt <- rbind(c(0.1,0.2),c(0.3,-0.1)); input_embeddings <- rbind(c(1,0),c(0,1),c(0.5,0.5))
combined <- rbind(prompt,input_embeddings)
print(c(input_length=nrow(combined),prompt_parameters=length(prompt)))

Sources

From the CodeSplash reference, entry LNG078.