Learning optimal spike-based representations