Efficient and Flexible Inference for Stochastic Systems