Aan de slagGa gratis aan de slag

Entropy playground

If you haven't encountered the concept of entropy before, it is useful to wrap your head around it with an example.

You will build a function plot_probabilities taking a list of probabilities as argument. It calculates the entropy and plots the probabilities in a bar chart.

Playing around with entropy, you should find that entropy is higher when the probability distribution is spread across many actions.

The torch.distribution.Categorical class is loaded in your environment as Categorical; this class has a method, .entropy(), which returns the entropy in nats.

Deze oefening maakt deel uit van de cursus

Deep Reinforcement Learning in Python

Cursus bekijken

Oefeninstructies

  • Obtain the entropy of the probability distribution in nats.
  • For convenience, convert the entropy from nats to bits.
  • Try using another list as input for the function.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

def plot_probabilities(probs):
  dist = Categorical(torch.tensor(probs))
  # Obtain the entropy in nats
  entropy = dist.____
  # Convert the entropy to bits
  entropy = entropy / math.log(____)
  print(f"{'Probabilities:':>15} {[round(prob, 3) for prob in dist.probs.tolist()]}")
  print(f"{'Entropy:':>15} {entropy:.2f}\n")
  plt.figure()
  plt.bar([str(x) for x in range(len(dist.probs))], dist.probs, color='skyblue', edgecolor='black')
  plt.ylabel('Probability'); plt.xlabel('Action index'); plt.ylim(0, 1)
  plt.show()
  
plot_probabilities([.25, .25, .25, .25])
plot_probabilities([.1, .15, .2, .25, .3])
# Try with your own list
plot_probabilities(____)
Code bewerken en uitvoeren