示例#1
0
    def run_many(self, positions, use_random_symmetry=True):
        """Compute the policy and value output for given positions.

    Args:
      positions: A list of positions for go board status
      use_random_symmetry: Apply random symmetry (defined in symmetries.py) to
        the extracted features (defined in features.py) of the given positions

    Returns:
      probabilities, value: The policy and value outputs (defined in
        dualnet_model.py)
    """
        def _extract_features(positions):
            return features.extract_features(self.hparams.board_size,
                                             positions)

        processed = list(map(_extract_features, positions))
        # processed = [
        #  features.extract_features(self.hparams.board_size, p) for p in positions]
        if use_random_symmetry:
            syms_used, processed = symmetries.randomize_symmetries_feat(
                processed)
        # feed_dict is a dict object to provide the input examples for the step of
        # inference. sess.run() returns the inference predictions (indicated by
        # self.inference_output) of the given input as outputs
        outputs = self.sess.run(self.inference_output,
                                feed_dict={self.inference_input: processed})
        probabilities, value = outputs['policy_output'], outputs[
            'value_output']
        if use_random_symmetry:
            probabilities = symmetries.invert_symmetries_pi(
                self.hparams.board_size, syms_used, probabilities)
        return probabilities, value
    def run_many(self, positions, use_random_symmetry=True):
        processed = list(map(features.extract_features, positions))
        # print(processed[0].shape)
        if use_random_symmetry:
            syms_used, processed = symmetries.randomize_symmetries_feat(
                processed)
        # processed: list [] of (18, N, N)
        processed = [
            np.reshape(item, (1, 18, go.N, go.N)) for item in processed
        ]
        processed = np.concatenate(processed, axis=0).astype(np.float32)
        if len(processed.shape) == 3:
            processed = np.expand_dims(processed, 0)

        batch = torch.from_numpy(processed)
        if self.cuda:
            batch = batch.cuda()
        outputs = self.model(batch)
        probabilities, value = outputs['pi'], outputs['V']

        if self.cuda:
            probabilities = probabilities.cpu()
            value = value.cpu()
        probabilities = probabilities.detach().numpy()
        value = value.detach().numpy()
        if use_random_symmetry:
            probabilities = symmetries.invert_symmetries_pi(
                syms_used, probabilities)

        return probabilities, value.flatten()
示例#3
0
  def run_many(self, positions, use_random_symmetry=True):
    """Compute the policy and value output for given positions.

    Args:
      positions: A list of positions for go board status
      use_random_symmetry: Apply random symmetry (defined in symmetries.py) to
        the extracted features (defined in features.py) of the given positions

    Returns:
      probabilities, value: The policy and value outputs (defined in
        dualnet_model.py)
    """
    def _extract_features(positions):
      return features.extract_features(self.hparams.board_size, positions)
    processed = list(map(_extract_features, positions))
    # processed = [
    #  features.extract_features(self.hparams.board_size, p) for p in positions]
    if use_random_symmetry:
      syms_used, processed = symmetries.randomize_symmetries_feat(processed)
    # feed_dict is a dict object to provide the input examples for the step of
    # inference. sess.run() returns the inference predictions (indicated by
    # self.inference_output) of the given input as outputs
    outputs = self.sess.run(
        self.inference_output, feed_dict={self.inference_input: processed})
    probabilities, value = outputs['policy_output'], outputs['value_output']
    if use_random_symmetry:
      probabilities = symmetries.invert_symmetries_pi(
          self.hparams.board_size, syms_used, probabilities)
    return probabilities, value
示例#4
0
    def run_batch_test(self, position, batch_size, use_random_symmetry=True):
        """Test batch inference execution to ensure proper IPU setup without C++
        """
        positions = [position]
        processed = list(map(features_lib.extract_features, positions))

        if FLAGS.use_random_symmetry:
            syms_used, processed = symmetries.randomize_symmetries_feat(
                processed)

        position = processed[0]
        s = position.shape

        input_shape = [s[0], s[1], s[2]]
        x_data = np.zeros(input_shape, np.float32)

        outputs = self.sess.run(
            self.inference_output,
            feed_dict={self.inference_input: [x_data] * batch_size})
        probabilities, value = outputs['policy_output'], outputs[
            'value_output']

        if use_random_symmetry:
            probabilities = symmetries.invert_symmetries_pi(
                syms_used, probabilities)
        return probabilities, value
示例#5
0
 def run_many(self, positions, use_random_symmetry=True):
     processed = list(map(features.extract_features, positions))
     if use_random_symmetry:
         syms_used, processed = symmetries.randomize_symmetries_feat(
             processed)
     outputs = self.sess.run(self.inference_output,
                             feed_dict={self.inference_input['pos_tensor']: processed})
     probabilities, value = outputs['policy_output'], outputs['value_output']
     if use_random_symmetry:
         probabilities = symmetries.invert_symmetries_pi(
             syms_used, probabilities)
     return probabilities, value
示例#6
0
 def run_many(self, positions, use_random_symmetry=True):
     processed = list(map(features.extract_features, positions))
     if use_random_symmetry:
         syms_used, processed = symmetries.randomize_symmetries_feat(
             processed)
     outputs = self.sess.run(self.inference_output,
                             feed_dict={self.inference_input: processed})
     probabilities, value = outputs['policy_output'], outputs['value_output']
     if use_random_symmetry:
         probabilities = symmetries.invert_symmetries_pi(
             syms_used, probabilities)
     return probabilities, value
示例#7
0
 def run_many(self, positions):
     f = get_features()
     processed = [features_lib.extract_features(p, f) for p in positions]
     if FLAGS.use_random_symmetry:
         syms_used, processed = symmetries.randomize_symmetries_feat(
             processed)
     outputs = self.sess.run(self.inference_output,
                             feed_dict={self.inference_input: processed})
     probabilities, value = outputs['policy_output'], outputs['value_output']
     if FLAGS.use_random_symmetry:
         probabilities = symmetries.invert_symmetries_pi(
             syms_used, probabilities)
     return probabilities, value
示例#8
0
    def run_many(self, positions, use_random_symmetry=True):
        processed = list(map(features.extract_features, positions))
        if use_random_symmetry:
            syms_used, processed = symmetries.randomize_symmetries_feat(
                processed)

        processed = np.array(processed)
        processed = np.moveaxis(processed, -1, 1)
        processed = torch.from_numpy(processed)
        probabilities, value, logits = self.model(processed.float())
        probabilities = probabilities.detach().cpu().numpy()
        value = value.detach().cpu().numpy()
        value = np.squeeze(value, axis=1)
        if use_random_symmetry:
            probabilities = symmetries.invert_symmetries_pi(
                syms_used, probabilities)

        return probabilities, value