Optical Neural Network in Free-Space and Nanophotonics