Get startedGet started for free

Part 1: Enter to win amazing prizes

In this exercise, you will learn about the Dense layer. Why not do that with a fun exercise? Imagine there's a game show where prizes are determined by a neural network. The contestant enters

  • the number of siblings,
  • the number of coffees had today and
  • if they like tomatoes or not,

and the model predicts what the contestant will win.

To implement this, you will be using Keras. You will need to create a model with an input layer which accepts three features (the number of siblings as an integer, the number of coffees as an integer and if they like tomatoes or not as a 0 or 1). Then the input goes through a Dense layer which outputs 3 probabilities (i.e. probabilities of winning a car, a gift voucher or nothing).

Input and Dense layers as well as a Model object from Keras are already imported. You are also provided a weight initializer called init to initialize the Dense layer.

This exercise is part of the course

Machine Translation with Keras

View Course

Exercise instructions

  • Define an input layer which only accepts 3 contestants (batch size), where each contestant has 3 inputs: sibling count, number of coffees and tomato preference (input size).
  • Define a Dense layer which has 3 outputs, softmax activation and init as the initializer.
  • Compute the model predictions for x using the defined model.
  • Get the most probable prize (as an integer) for each contestant.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define an input layer with batch size 3 and input size 3
inp = Input(____=(____,____))
# Get the output of the 3 node Dense layer
pred = ____(____, ____=____, kernel_initializer=init, bias_initializer=init)(inp)
model = Model(inputs=inp, outputs=pred)

names = ["Mark", "John", "Kelly"]
prizes = ["Gift voucher", "Car", "Nothing"]
x = np.array([[5, 0, 1], [0, 3, 1], [2, 2, 1]])
# Compute the model prediction for x
y = ____.____(____)
# Get the most probable class for each sample
classes = np.____(____, ____)
print("\n".join(["{} has probabilities {} and wins {}".format(n,p,prizes[c]) \
                 for n,p,c in zip(names, y, classes)]))
Edit and Run Code