Klein-Gordon equation from its classical Hamiltonian I don't have much experience in classical field theory and have been trying to study it for the past week. However, I don't know if my understanding of the equations of motion for the fields are correct. Specifically, the Hamiltonian of the Klein-Gordon field is given by: $$ H[\pi,\phi] = \int\frac{1}{2} (\pi^2 + |\nabla \phi|^2+m^2\phi^2) \text{ } d^3x, $$
where $\pi$ is the conjugate momentum to the field. And we have the usual Hamiltonian equations:
$$\begin{align}\dot\pi &= -\frac{\delta H}{\delta \phi}\cr 
\dot\phi&= \frac{\delta H}{\delta \pi}.\end{align}$$
This seems pretty straight forward from here but the Hamiltonian is supposed to be a function of $\phi$ and $\pi$ but we also have terms that depend on the spatial derivatives of the fields. If my memory is accurate, I don't remember having this problem in classical mechanics. Should the spatial derivatives of the fields be treated as being dependent on the on the field itself?
If so, how do we compute the term $\frac{\delta H}{\delta\phi}$?
 A: Assuming real fields (the complexification is an easy exercise), @Chandrahas's point is that$$\int\tfrac12\nabla\phi\nabla\phi d^3x=-\int\tfrac12\phi\nabla^2\phi d^3x$$up to a boundary term. This is the $3$-dimensional equivalent of how we use $4$-dimensional integration by parts in Lagrangian field theory, when deriving an Euler-Lagrange equation from the stationary action principle. So$$-\dot{\pi}=\frac{\delta H}{\delta\phi}=m^2\phi-\nabla^2\phi.$$As a sanity check, substituting $\pi=\dot{\phi}$ gives us the usual second-order equation expected from the Lagrangian approach, viz.$$\nabla^2\phi-\ddot{\phi}=m^2\phi.$$
A: What you have appearing in the equations of motion is a variational derivative. That is, the change of the functional $H[\phi,\pi]$ with respect to a variation of $\phi$ at some point $x^i_0$. Skipping the mathematical details, you can compute the variational details by the following trick
$$\dot{\pi}(x^i_0) = -\frac{\delta H}{\delta \phi(x^i_0)} \equiv \left(\frac{\partial}{\partial \epsilon} H[\phi+\epsilon \delta_{x_0},\pi]\right)\Big|_{\epsilon=0}$$
where $\delta_{x_0} = \delta^{(3)}(x^i - x^i_0)$ is the delta function at $x^i_0$. Of course, sometimes you may have to deal with the derivatives of the delta function in there. When $H$ is just a volume integral of a Hamiltonian density dependent only on first-order derivatives $\mathcal{H}(\phi,\partial_i \phi, \pi,...)$, then the variational derivative will have a form that you may recognize as very much analogous to Lagrangian field theory.
