Exploiting Non-idealities of Resistive Switching Memories for Efficient Machine Learning