Car NOT Goat

Programming Snapshot – Neural Networks

© Lead Image © Frantisek Hojdysz, 123RF.com

© Lead Image © Frantisek Hojdysz, 123RF.com

Article from Issue 207/2018
Author(s):

The well-known Monty Hall game show problem can be a rewarding maiden voyage for prospective statisticians. But is it possible to teach a neural network to choose between goats and cars with a few practice sessions?

Here's the problem: In a game show, a candidate has to choose from three closed doors; waiting behind these doors is a car, which is the main prize, a goat, and yet another goat (Figure 1). The candidate first picks a door, and then the presenter opens another, behind which there is a bleating goat. How is the candidate most likely to win the grand prize: Sticking with their choice or switching to the remaining closed door?

Figure 1: Monty Hall problem on Wikipedia.

As has been shown [1] [2], the candidate is better off switching, because then they double their chances of winning. But how does a neural network learn the optimal game strategy while being rewarded for wins and punished for losses?

Human Model

The input and output data must be professionally manipulated – as is always the case with machine learning. An artificial intelligence (AI) system is not a cauldron into which you throw problems and then ready-made solutions just bubble up. In fact, AI algorithms only solve a small number of precisely defined problems.

To solve the problem, the algorithm used in this article is a multilayer neural network that takes three input parameters: the door the candidate selected, the door the presenter opened, and the remaining locked door.

In the perceptron's hidden middle layer, each artificial neuron is connected to each input neuron of the first layer. Even though a neural network does not quite work like a human brain, you can still interpret these massive links as a reflection of the human design. In turn, each of the hidden layer's inner neurons fires pulses to all the output layer's neurons.

Carrot and Stick

In the training phase, we want the network to learn a strategy from played game shows to predict those that indicated winning the prize on the output neuron, depending on the current door constellation. In a few thousand rounds, the script feeds the three input parameters into the AI system and compares the door value at the output with the actual door leading to the car. If the system has predicted the correct door, it is rewarded. If it is wrong, it has to adjust its neurons' parameters via a feedback mechanism.

In AI jargon, the training runs are called episodes. It is often helpful not to adjust the neuron parameters with every dataset, but instead just after a batch of input values. This saves computing time and prevents the system from balancing the weights in wild swings, which often leads to unstable conditions that don't converge into a solution.

1,000 Shows Recorded

Listing 1 records the results of 1,000 game shows in which the prize is placed behind a random door, and then the presenter opens a door that neither leads to the main prize nor is already open. Game results go to a file in CSV format (Figure 2). It numbers the doors from   to 2 and logs the indices of the following doors in each line in this order: the door that is chosen by the game show candidate, the door that the presenter opens, the remaining door, and the one leading to the prize.

Listing 1

monty

01 #!/usr/bin/env python3
02 from random import shuffle, randrange
03
04 class Door(object):
05   def __init__(self,prize):
06     self.prize = prize
07
08 class Show(object):
09   def __init__(self):
10     self.picked  = None
11     self.revealed  = None
12     self.alternate = None
13
14       # hide prize behind random door
15     self.prizes = [1,0,0]
16     shuffle(self.prizes)
17
18     self.doors = []
19     for prize in self.prizes:
20       self.doors.append(Door(prize))
21
22   def pick(self):
23       # candidate picks a door
24     idx = randrange(0,len(self.doors))
25     self.picked = idx
26
27       # moderator reveals another door
28     for idx,door in \
29         enumerate(self.doors):
30       if door.prize or \
31           self.picked == idx:
32         continue
33       if self.revealed is None:
34         self.revealed = idx
35       # determine remaining door
36     for idx,door in \
37         enumerate(self.doors):
38       if self.picked != idx and \
39           self.revealed != idx:
40         self.alternate = idx
41
42   def prize_idx (self):
43     for idx,door in \
44         enumerate(self.doors):
45       if door.prize:
46         return idx
47
48 print("picked,revealed,alternate,prize");
49
50 for i in range(1000):
51   show = Show()
52   show.pick()
53   show.reveal()
54
55   print("{0},{1},{2},{3}".format(
56     show.picked, show.revealed,
57     show.alternate,show.prize_idx()))
Figure 2: A random generator generates results of game shows and outputs them in CSV format for a subsequent training session with a neural network.

For example, if the neural network encounters the [1,2,0,0] combination, such as in the first line in the file displayed in Figure 2, it knows that the candidate has chosen the second door (index 1), the presenter has then opened the third (index 2), and the first is still closed (index 0). The main prize was randomly hidden and ended up behind the first door (index 0). With these parameters, to win the game, the neural network must pick the first door.

Listing 1 defines two classes, Door for individual doors and Show for a world with three doors and the rules of the TV show. Door objects are initialized either with or without a main prize; line 15 places the prize behind the first door and then lets line 16 shuffle the doors, so that the prize randomly ends up somewhere. Using the pick() method from line 22, randrange() simulates the candidate picking a random door. The Show object remembers the selected door's index in the picked instance variable.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Neural Networks

    3, 4, 8, 11… ? A neural network can complete this series without knowledge of the underlying algorithm – by a kind of virtual gut feeling. We’ll show you how neural networks solve problems by simulating the behavior of a human brain.

  • Spam-Detecting Neural Network

    Build a neural network that uncovers spam websites.

  • Deep Learning

    Deep learning isn't just for industrial automation tasks. With a little help from Gimp and some special neural network tools, you can add color to your old black and white images.

  • FAQ

    Welcome our new artificial intelligence overlords by tinkering with their gray matter.

  • Calculating Probability

    To tackle mathematical problems with conditional probabilities, math buffs rely on Bayes' formula or discrete distributions, generated by short Perl scripts.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News