Murphy's book inverts a trained classifier by taking gradients with respect to the input instead of the weights. Here the classifier is a tiny one-hidden-layer ReLU network on a flattened h×w grey image x∈RD, D=hw:
z=W1x+b1,a=ReLU(z),logits=W2a+b2,p(y∣x)=softmax(logits)
params is a dict with W1 (H, D), b1 (H,), W2 (K, H), b2 (K,). Use ReLU′(0)=0. Implement:
visualize_class(params, c, x0, eps1, eps2, steps, prior, shape): run the deterministic update (the Langevin update with ϵ3=0)
xt+1=xt+ϵ1∇xlogp(xt)+ϵ2∇xlogp(y=c∣xt)
for steps steps. With prior="gauss", logp(x)=−21∥x∥2. With prior="tv", logp(x)=−TV(x), where TV is the sum over the h×w image (x.reshape(shape)) of squared differences between vertically and horizontally adjacent pixels. Return (x_final, probs) where probs lists p(y=c∣xt) for t=0,…,steps.
deep_dream(params, x0, lr, steps): gradient ascent on the mean hidden activation L(x)=H1∑jaj(x), xt+1=xt+lr∇xL(xt). Return (x_final, values) with L(xt) for t=0,…,steps.