Learning States for Detecting Anomalies in Time Series