def test(self, inputs, outputs, fitness_callback, to_test='gen_best'):
        if self.network_params['network'] == 'feedforward':
            model = Network(0, self.gen_best)
            with tf.Session() as sess:
                actions = sess.run(model.prediction,
                                   feed_dict={model.X: inputs})
                print('test score: {0:.2f}%'.format(
                    fitness_callback(actions, outputs)))

            tf.reset_default_graph()

        # built using tf.keras
        else:
            # reshape data for each network type
            if self.network_params['network'] == 'convolutional':
                inputs = inputs[:, :, np.newaxis]
            elif self.network_params['network'] == 'recurrent':
                timesteps = self.network_params['timesteps']
                inputs = np.array([
                    inputs[i - timesteps:i]
                    for i in range(timesteps, inputs.shape[0] + 1)
                ])
                outputs = outputs[timesteps - 1:]

            actions = self.gen_best.model.prediction.predict(inputs)
            print('test score: {0:.2f}%'.format(
                fitness_callback(actions, outputs)))
    def test(self, inputs, outputs, fitness_callback, to_test='gen_best'):
        model = Network(0, self.gen_best)
        with tf.Session() as sess:
            actions = sess.run(model.prediction, feed_dict={model.X: inputs})
            print('test score: {0:.2f}%\t(hold: {1:.2f}%)'.format(
                fitness_callback(actions, outputs),
                (outputs[-1] / outputs[0] - 1) * 100))

        tf.reset_default_graph()
예제 #3
0
    def __init__(self,
                 id,
                 network_params,
                 mutation_scale,
                 w_mutation_rate,
                 b_mutation_rate,
                 parent_1=None,
                 parent_2=None):

        # fitness is score normalized
        self.fitness = 0
        self.actions = None
        self.prices = []
        self.score = 0
        self.id = id

        # keep track of how genome came to be
        self.mutated = False
        self.bred = False

        self.inputs = network_params['input']
        self.hidden = network_params['hidden']
        self.outputs = network_params['output']
        self.network = network_params['network']

        self.w_mutation_rate = w_mutation_rate
        self.b_mutation_rate = b_mutation_rate
        self.mutation_scale = mutation_scale

        self.weights = None
        self.biases = None

        # two parents available for breeding
        if parent_2 is not None:
            self.weights = copy.deepcopy(parent_1.weights)
            self.biases = copy.deepcopy(parent_1.biases)
            self.breed(parent_2)
            self.bred = True

        # mutate if only one parent available
        elif parent_1 is not None:
            self.weights = copy.deepcopy(parent_1.weights)
            self.biases = copy.deepcopy(parent_1.biases)
            self.mutate_w_feedforward()
            self.mutate_b_feedforward()
            self.mutated = True

        # initial values when population is first created
        else: self.init_w_b()
        self.model = Network(self.id, self)
예제 #4
0
    def test(self, inputs, outputs, fitness_callback, to_test='gen_best'):
        # built using tf.keras
        if self.network_params['network'] == 'convolutional':

            # add extra dimension for conv1D channel
            inputs = inputs[:,:, np.newaxis]

            actions = self.gen_best.model.prediction.predict(inputs)
            print('test score: {0:.2f}%'.format(fitness_callback(actions, outputs)))

        else:
            model = Network(self.gen_best)
            with tf.Session() as sess:
                actions = sess.run(model.prediction, feed_dict={model.X: inputs})
                print('test score: {0:.2f}%'.format(fitness_callback(actions, outputs)))

            tf.reset_default_graph()
예제 #5
0
    def __init__(self,
                 id,
                 network_params,
                 mutation_scale,
                 w_mutation_rate,
                 b_mutation_rate,
                 parent_1=None,
                 parent_2=None,
                 load_keras=None):

        # fitness is score normalized
        self.fitness = 0
        self.score = 0
        self.id = id

        # keep track of how genome came to be
        self.mutated = False
        self.bred = False

        self.inputs = network_params['input']
        self.hidden = network_params['hidden']
        self.outputs = network_params['output']
        self.network = network_params['network']
        self.model = None
        self.timesteps = None

        try:
            if self.network == 'recurrent':
                self.timesteps = network_params['timesteps']
        except:
            raise AttributeError(
                'Must specify timesteps for recurrent network')

        self.w_mutation_rate = w_mutation_rate
        self.b_mutation_rate = b_mutation_rate
        self.mutation_scale = mutation_scale

        self.weights = None
        self.biases = None

        # two parents available for breeding
        if parent_2 is not None:
            self.weights = copy.deepcopy(parent_1.weights)
            self.biases = copy.deepcopy(parent_1.biases)
            self.breed(parent_2)
            self.bred = True

        # mutate if only one parent available
        elif parent_1 is not None:
            self.weights = copy.deepcopy(parent_1.weights)
            self.biases = copy.deepcopy(parent_1.biases)
            self.mutate_w_feedforward()
            self.mutate_b_feedforward()
            self.mutated = True

        elif load_keras is not None:
            self.model = Network(self.id, self, load_keras=load_keras)
            self.mutate_w_cnn()

        # initial values when population is first created
        else:
            self.init_w_b()

        # pass genome to network object
        if load_keras is None: self.model = Network(self.id, self)