Language models, retrieval, prompting, and language adaptation · Reasoning, decoding, and discrete diffusion language · LNG071 · Algorithm
Chain-of-thought prompting
Encourages a language model to generate intermediate reasoning text before a final answer, often through worked examples. It changes inference conditioning, not the architecture or model weights by itself.
In CodeSplash
No method yet, but the reference's example runs here as a notebook.
Do it in CodeSplash Open the reference's example as a Python or R notebook.
- Availability
- Runs as a notebook
- Code example
- Complete calculation
- 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
Encourages a language model to generate intermediate reasoning text before a final answer, often through worked examples. It changes inference conditioning, not the architecture or model weights by itself.
Question / objective
Does prompting for intermediate reasoning improve final-answer accuracy on the target task?
Architecture / computational structure
The base generator is unchanged; rationale tokens become part of the generated context before the answer. No weights are learned by prompting alone; demonstrations, wording, temperature, and budget are inference choices.
Assumptions
Fluent rationales may be incorrect or unfaithful to the model's actual computation.
Formula / computational definition
Treat rationale z as an intermediate sequence, yielding p(y|x)=sum_z p(y,z|x). Ordinary prompted decoding samples or selects one rationale rather than exactly marginalizing the enormous rationale space.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Demonstrations, instruction wording, rationale format, decoding temperature, token budget, and final-answer extraction rule.
Fitting, fine-tuning, and adaptation
There is no fitting step for basic prompting. Select prompts on development tasks and keep test prompts fixed; optional rationale SFT is a separate learned adaptation requiring labeled traces and retention checks. Compare final-answer correctness at equal token budgets, and do not treat rationale fluency as verification.
How to use and calibrate
Measure final-answer accuracy, output-token cost, prompt sensitivity, and reasoning-step validity separately; use held-out examples and avoid leaking answers in demonstrations. 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
Solve arithmetic word problems while separately checking the final numeric answer.
Interpretation / pitfalls
Watch out
Extra text increases cost and can introduce errors; gains depend on task and model, not a universal reasoning guarantee. 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: Any supported text-generation interface can run structured prompts; R can use reticulate or a documented HTTP interface. The native example marginalizes two toy rationale paths, illustrating the probabilistic interpretation rather than invoking or reproducing a reasoning model.
Example scope: Exact marginalization over two toy rationale paths; ordinary chain-of-thought does not enumerate this sum.
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.
Complete calculation — Exact marginalization over two toy rationale paths; ordinary chain-of-thought does not enumerate this sum.
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)
p_rationale = [0.6,0.4]; p_answer_given_rationale = [0.9,0.3]
print(dot(p_rationale,p_answer_given_rationale))
Complete calculation — Exact marginalization over two toy rationale paths; ordinary chain-of-thought does not enumerate this sum.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
p_rationale <- c(0.6,0.4); p_answer_given_rationale <- c(0.9,0.3)
print(sum(p_rationale*p_answer_given_rationale))
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG071.