Stimulation of neural activity is an important scientific and clinical tool, causally testing hypotheses and treating neurodegenerative and neuropsychiatric diseases. However, current stimulation approaches cannot flexibly control the pattern of activity in populations of neurons. To address this, we developed an adaptive, closed-loop stimulation (ACLS) system that uses patterned, multi-site electrical stimulation to control the pattern of activity in a population of neurons. Importantly, ACLS is a learning system; it monitors the response to stimulation and iteratively updates the stimulation pattern to produce a specific neural response. In silico and in vivo experiments showed ACLS quickly learns to produce specific patterns of neural activity (~15 minutes) and was robust to noise and drift in neural responses. In visual cortex of awake mice, ACLS learned electrical stimulation patterns that produced responses similar to the natural response evoked by visual stimuli. Similar to how repetition of a visual stimulus causes an adaptation in the neural response, the response to electrical stimulation was adapted when it was preceded by the associated visual stimulus. Altogether, our results show ACLS can learn, in real-time, to generate specific patterns of neural activity, providing a framework for using closed-loop learning to control neural activity.