local({
## forward and backward pass at theta0, as in nn_pois
W <- matrix(theta0[1:(J * p)], J, p); w0 <- theta0[J * p + 1:J]
v <- theta0[J * p + J + 1:J]; v0 <- theta0[J * p + 2 * J + 1]
H <- tanh(sweep(X %*% t(W), 2, w0, "+"))
lam <- as.vector(exp(v0 + H %*% v))
d_lam <- 1 - ytrue / lam # delta at lambda
d_eta <- lam - ytrue # delta at eta
d_h <- outer(d_eta, v) # delta at h_j
d_a <- d_h * (1 - H^2) # delta at a_j
## layout: one column per layer, units stacked top to bottom
y3 <- c(10, 5.4, 0.8)
x_xy <- cbind(1, y3); a_xy <- cbind(8, y3); h_xy <- cbind(11.2, y3)
eta_xy <- c(14.6, 5.4); lam_xy <- c(17.2, 5.4); L_xy <- c(19.8, 5.4)
r <- 0.9 # node radius
sq <- 0.3 # half-width of a constant node
up <- 2.4 # height of a constant node above its unit
op <- par(mar = c(0, 0, 0, 0)); on.exit(par(op))
plot(NA, xlim = c(0, 20.8), ylim = c(-0.3, 13.8), asp = 1,
axes = FALSE, xlab = "", ylab = "")
## an edge label: the weight, and its gradient in green, on a white box
edge_label <- function(xy, lab, g, col) {
rect(xy[1] - 0.8, xy[2] - 0.4, xy[1] + 0.8, xy[2] + 0.4,
col = "white", border = "grey70")
text(xy[1], xy[2] + 0.18, parse(text = lab), col = col, cex = 0.7)
text(xy[1], xy[2] - 0.2, sprintf("%.4f", g), col = "darkgreen", font = 2, cex = 0.7)
}
## a node: its label and the first three of its values
node <- function(xy, lab, vals, bg) {
symbols(xy[1], xy[2], circles = r, inches = FALSE, add = TRUE, bg = bg)
text(xy[1], xy[2] + 0.55, parse(text = lab), cex = 0.95)
text(xy[1], xy[2] - 0.25, paste(c(sprintf("%.3f", vals[1:3]), "..."), collapse = "\n"),
cex = 0.6)
}
wlab <- function(nm, w) sprintf("%s == %s", nm, sprintf("%.3f", w))
## edges
for (k in 1:p) for (j in 1:J) segments(x_xy[k, 1], x_xy[k, 2], a_xy[j, 1], a_xy[j, 2])
for (j in 1:J) {
segments(a_xy[j, 1], a_xy[j, 2], h_xy[j, 1], h_xy[j, 2])
segments(h_xy[j, 1], h_xy[j, 2], eta_xy[1], eta_xy[2])
segments(a_xy[j, 1], a_xy[j, 2] + up, a_xy[j, 1], a_xy[j, 2])
}
segments(eta_xy[1], eta_xy[2] + up, eta_xy[1], eta_xy[2])
segments(eta_xy[1], eta_xy[2], L_xy[1], L_xy[2])
## edge labels; each w_jk sits near the a_j end of its edge
for (k in 1:p) for (j in 1:J)
edge_label(x_xy[k, ] + 0.74 * (a_xy[j, ] - x_xy[k, ]),
wlab(sprintf("w[%d%d]", j, k), W[j, k]),
back[sprintf("w%d%d", j, k)], "steelblue4")
bias_mid <- (r + up - sq) / 2
for (j in 1:J) {
edge_label(a_xy[j, ] + c(0, bias_mid), wlab(sprintf("w[%d*0]", j), w0[j]),
back[sprintf("w%d0", j)], "steelblue4")
edge_label((h_xy[j, ] + eta_xy) / 2, wlab(sprintf("v[%d]", j), v[j]),
back[sprintf("v%d", j)], "firebrick4")
text(mean(c(a_xy[j, 1], h_xy[j, 1])), a_xy[j, 2] + 0.3, "tanh", cex = 0.75)
}
edge_label(eta_xy + c(0, bias_mid), wlab("v[0]", v0), back["v0"], "firebrick4")
text(mean(c(eta_xy[1], lam_xy[1])), eta_xy[2] + 0.3, "exp", cex = 0.75)
text(mean(c(lam_xy[1], L_xy[1])), eta_xy[2] + 0.3, "Poisson", cex = 0.75)
## constant nodes
const_xy <- rbind(a_xy, eta_xy) + cbind(0, rep(up, J + 1))
rect(const_xy[, 1] - sq, const_xy[, 2] - sq, const_xy[, 1] + sq, const_xy[, 2] + sq,
col = "lightblue1")
text(const_xy[, 1], const_xy[, 2], "1", cex = 0.85)
## nodes: covariates at the inputs, deltas elsewhere
for (k in 1:p) node(x_xy[k, ], sprintf("x[i]^{(%d)}", k), X[, k], "grey90")
for (j in 1:J) {
node(a_xy[j, ], sprintf("delta[a[i]^{(%d)}]", j), d_a[, j], "honeydew")
node(h_xy[j, ], sprintf("delta[h[i]^{(%d)}]", j), d_h[, j], "lightyellow")
}
node(eta_xy, "delta[eta[i]]", d_eta, "grey90")
node(lam_xy, "delta[lambda[i]]", d_lam, "grey90")
symbols(L_xy[1], L_xy[2], circles = 0.7, inches = FALSE, add = TRUE, bg = "white")
text(L_xy[1], L_xy[2], expression(L[i]))
text(c(1, 9.6, 14.6, 17.2, 19.8), 13.6,
c("Input", "Hidden (tanh)", "Output", "Mean", "Loss"), font = 2)
})