# Implementing Deep Q-Networks (DQN) algorithm

**URL:** <https://community.wolfram.com/t/implementing-deep-q-networks-dqn-algorithm/18033>\
**Category:** Staff Picks\
**Tags:** data-science, graphics-and-visualization, interface-construction, machine-learning, staff-pick, publication-materials, neural-networks, artificial-intelligence\
**Created:** [April 28, 2021, 9:16pm UTC](https://community.wolfram.com/t/implementing-deep-q-networks-dqn-algorithm/18033 "2021-04-28T21:16:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![landajuelala1](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/landajuelala1/32/25446_2.png) [@landajuelala1](https://community.wolfram.com/u/landajuelala1)\
**Post date:** [April 28, 2021, 9:16pm UTC](https://community.wolfram.com/t/implementing-deep-q-networks-dqn-algorithm/18033/1 "2021-04-28T21:16:18Z")

</div>

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/ea625124-d11f-47af-a7f6-00d4a32f2ef0)

[Minimal\_DQN\_in\_Mathematica.nb](https://community.wolfram.com/uploads/short-url/aQnIeQSpXc5ZbUuDqOLVCiRMP9o.nb) (41.8 KB)

---

<div class="post-metadata">

**Author:** ![wolframcommunity](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wolframcommunity/32/8335_2.png) [@wolframcommunity](https://community.wolfram.com/u/wolframcommunity)\
**Post date:** [April 29, 2021, 9:04pm UTC](https://community.wolfram.com/t/implementing-deep-q-networks-dqn-algorithm/18033/2 "2021-04-29T21:04:38Z")

</div>

![news-congrads-kkluyshnik-02-04-19.jpg](https://global.discourse-cdn.com/wolfram/original/3X/6/a/6a310c2d0904616faf4e6e148260c9f29ab9f4eb.jpeg) – you have earned _ **Featured Contributor Badge** _ ![2015-11-02_10-07-11.png](https://global.discourse-cdn.com/wolfram/original/3X/5/0/50e59c9ead62f729e7130d3ea1482555a75602b6.png)  
Your exceptional post has been selected for our editorial column _ **Staff Picks** _ [http://wolfr.am/StaffPicks](http://wolfr.am/StaffPicks) and [Your Profile](http://community.wolfram.com/web/landajuelala1) is now distinguished by a _ **Featured Contributor Badge** _ and is displayed on the [Featured Contributor Board](https://community.wolfram.com/people). Thank you!

---

<div class="post-metadata">

**Author:** ![jonathan3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jonathan3/32/5643_2.png) [@jonathan3](https://community.wolfram.com/u/jonathan3)\
**Post date:** [May 5, 2021, 9:57pm UTC](https://community.wolfram.com/t/implementing-deep-q-networks-dqn-algorithm/18033/3 "2021-05-05T21:57:29Z")

</div>

I’ve tried running this on a couple of different (powerful) PCs, but failed in each case. It seems to hang part way through the NetTrain evaluation.

---

<div class="post-metadata">

**Author:** ![pbarendse](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/pbarendse/32/30486_2.png) [@pbarendse](https://community.wolfram.com/u/pbarendse)\
**Post date:** [November 4, 2022, 6:23pm UTC](https://community.wolfram.com/t/implementing-deep-q-networks-dqn-algorithm/18033/4 "2022-11-04T18:23:16Z")

</div>

I get some immediate errors when training, but if I change this line:

```
<|"Measurement"->Function[N[#BatchData["score"]][[1]]],"Key"->"Score"|>

```

to this:

```
<|"Measurement"->Function[N[#BatchData["score"]]],"Key"->"Score"|>

```

it works.  
(and it seems pretty clear from the code above that the “score” is a real number)

However, the trained agent doesn’t seem to do very well on my 2018 MacBook pro.  
Twice it hung (although after manually stopping, the agent does pretty well), and once it finished but failed to hold up the cart pole (image attached).

 ![ScreenShot2022-11-04at2.21.16PM.jpg](https://global.discourse-cdn.com/wolfram/original/3X/f/6/f6f2bba391d5cc564d3cd2e35603cb5fd3da5946.jpeg)
