How to do it...

  1. We'll start by loading the necessary libraries for the script:
import os 
import numpy as np 
import matplotlib.pyplot as plt 
import tensorflow as tf 
  1. Next, we'll set the parameters of the genetic algorithm. Here, we will have 100 individuals, each with a length of 50. The selection percentage will be 20% (keeping the top 20 individuals). The mutation will be set to be the inverse of the number of features, a common place to start for the mutation. This means that we expect one feature per child solution to change. We will run the genetic algorithm for 200 generations:
pop_size = 100 features = 50 selection = 0.2 mutation = 1./ features generations = 200 num_parents = int(pop_size*selection) num_children = pop_size - num_parents ...

Get TensorFlow Machine Learning Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.