Written by Brian Herbert and Kevin J Anderson between 2002 - 2004 contains almost everything you describe, starting with:
- it's a Sci-Fi book about a war between humans and machines(AI) in space, and most likely published before the year 2004
The third book Dune: The Battle of Corrin published in 2004 contains the most parallels.
The trilogy deals with the fight between Humans and Machines across the Galaxy and fills in the history of the Dune series of books written by Brian's father Frank Herbert.
In the first book Dune: The Butlerian Jihad

We are introduced to the character of Vorian Atridies, the son of the Cymek (cyborg) Titan Agamemnon. Agamemnon had killed his 13 previous sons due to inadequacies and considered Vorian his perfect son.
The Cymeks are subservient to a machine AI they created called Omnius which controls the Machine Empire.
We also meet the independent robot Erasmus. Erasmus is fascinated with humans and carries out numerous experiments on slaves.
One slave in particular Serena Butler captures his attention, but when she gives birth to a baby boy Manion, Erasmus kills the baby thinking it was too much of a distraction for Serena.
This triggers the Butlerian Jihad the uprising of Humans against Machines.
In the second book Dune: The Machine Crusade

Erasmus had a bet with Omnius that he could raise a human child to be orderly and civilized like a machine. Omnius takes the most feral of the children he could find in the slave pits
"You do not fully appreciate the situation, Omnius. Begin with any healthy human. If taken at a formative age, when its mental systems remain pliable, any one of these poor humans can be trained. Given the opportunity, even the most bedraggled child could become brilliant, nearly our equal.
Hovering near Erasmus, the watcheye magnified its viewing mechanism for a closer look at the pens. "Any of them? That is doubtful."
"Nevertheless, I have found it to be true"
Additional watcheyes converged above the crowded pens where the feeding humans jostled each other. An image appeared on the watcheye lens by Erasmus, and Omnius said.
"Observe that boy closest to the fence - the one with straggly hair and ragged pants. He appears to be the wildest and most unkempt of all. See what you can do with that creature. I will wager that he remains an animal despite your best efforts"
Dune: The Machine Crusade
Erasmus names this child Gilbertus Albans and raises him to be the perfect logical being. He wins the challenge creating in the process the first Mentat.
Gilbertus was different from any human, his mind organised and efficient, as close to a computer's as was biologically possible
Dune: The Battle of Corrin
Gilbertus Albans therefore fits with:
- a machine has taken a male human baby and raised it to be the perfect human
Gilbertus then falls in love with a clone of Serena Butler.
Completely intent on a conversation with the latest Serena Butler clone, Gilbertus had not noticed his presence. his human ward seemed enrapture by her presence, though the robot couldn't understand why. Surely after twenty years Gilbertus would have wearied of his efforts to fashion her into a worthy mate.
Dune: The Battle of Corrin
In the final book Dune: The Battle of Corrin

The robot Erasmus places ~2 million humans in cargo ships creating a human-shield around the planet Corrin, called the Bridge of Hrethgir which is set to explode if the human fleet gets within range.
Which matches:
- the machines gathered up all the human slaves and placed them in a ship packed with explosives to use as both a shield, and a trap from the humans
Vorian Atreides and Abulurd Butler argue over whether to attack this human shield, with Vorian relieving Abulurd of command and advancing, thus starting the Atreides/Harkonnen feud.
Unbeknownst to either of them Erasmus had disabled the explosives after his "son" Gilbertus had snuck onto one of the cargo pods to be with a clone of Serena Butler.
But they were past the scrambler line, and the Bridge of Hrethgir should have detonated. Gilbertus turned away from the view outside. At least he was with Serena
Dune: The Battle of Corrin
This fits with:
- And the 'perfect human' had sneaked onto one of the ships that held a female slave he was in love with, forcing the machine 'father' to disarm the explosives to save his life.