Say you have three functions, $f, g, h$: $$f(x): \mathbb{R}^k\rightarrow\mathbb{R}$$ $$g(y): \mathbb{R}^k\rightarrow\mathbb{R}^k$$ $$h(z): \mathbb{R}^n\rightarrow\mathbb{R}^k$$
I want to take the derivative of $f(g(h(z)))$ with respect to $z\in\mathbb{R}^n$. If I apply the chain rule, I find that $$\frac{\partial f}{\partial z} = \frac{\partial f}{\partial g}\frac{\partial g}{\partial h}\frac{\partial h}{\partial z}$$ But here's where I'm confused. I think that $\frac{\partial f}{\partial g} \in \mathbb{R}^{k\times 1}$, $\frac{\partial g}{\partial h}\in\mathbb{R}^{k\times k}$, and $\frac{\partial h}{\partial z}\in\mathbb{R}^{n\times k}$, and these dimensions clearly don't match up in the product. I was expecting that $\frac{\partial f}{\partial z}\in\mathbb{R}^{n\times 1}$. What am I missing? Thanks!