Algorithm and Hardware Co-design for Learning On-a-chip