text: # -*- coding: utf-8 -*-
  """
  An implementation of the policyValueNet in Tensorflow
  Tested in Tensorflow 1.4 and 1.5
  
  @author: Xiang Zhong
  """
  
  import numpy as np
  import tensorflow as tf
  
  
  class PolicyValueNet():
      def __init__(self, board_width, board_height, model_file=None):
          self.board_width = board_width
          self.board_height = board_height
  
          # Define the tensorflow neural network
          # 1. Input:
          self.input_states = tf.placeholder(
                  tf.float32, shape=[None, 5, board_height, board_width])
          self.input_state = tf.transpose(self.input_states, [0, 2, 3, 1])
  
          # 在AlphaGo Zero中，输入局面首先通过了20或40个基于卷积的残差网络模块，然后再分别接上2层或3层网络得到策略和价值输出，整个网络的层数有40多或80多层，训练和预测的时候都十分缓慢。
          # 在我们的实现中，对这个网络结构进行了极大的简化，整个策略价值网络的深度只有5~6层，训练和预测都相对比较快。
          # 最开始是公共的3层全卷积网络，分别使用32、64和128个 3x3 的filter，使用ReLu激活函数。
          # 2. Common Networks Layers
          self.conv1 = tf.layers.conv2d(inputs=self.input_state, filters=32,
                                        kernel_size=[3, 3], padding="same",
                                        data_format="channels_last",
                                        activation=tf.nn.relu)
          self.conv2 = tf.layers.conv2d(inputs=self.conv1, filters=64,
                                        kernel_size=[3, 3], padding="same",
                                        data_format="channels_last",
                                        activation=tf.nn.relu)
          self.conv3 = tf.layers.conv2d(inputs=self.conv2, filters=128,
                                        kernel_size=[3, 3], padding="same",
                                        data_format="channels_last",
                                        activation=tf.nn.relu)
          # 然后再分成policy和value两个输出
          # 在policy这一端，先使用4个 1x1 的filter进行降维，再接一个全连接层，使用softmax非线性函数直接输出棋盘上每个位置的落子概率
          # 3-1 Action Networks
          self.action_conv = tf.layers.conv2d(inputs=self.conv3, filters=4,
                                              kernel_size=[1, 1], padding="same",
                                              data_format="channels_last",
                                              activation=tf.nn.relu)
          # Flatten the tensor
          self.action_conv_flat = tf.reshape(
                  self.action_conv, [-1, 4 * board_height * board_width])
          # 3-2 Full connected layer, the output is the log probability of moves
          # on each slot on the board
          self.action_fc = tf.layers.dense(inputs=self.action_conv_flat,
                                           units=board_height * board_width,
                                           activation=tf.nn.log_softmax)
          # 在value这一端，先使用2个 1x1 的filter进行降维，再接一个64个神经元的全连接层，最后再接一个全连接层，使用tanh非线性函数直接输出 [-1,1] 之间的局面评分。
          # 4 Evaluation Networks
          self.evaluation_conv = tf.layers.conv2d(inputs=self.conv3, filters=2,
                                                  kernel_size=[1, 1],
                                                  padding="same",
                                                  data_format="channels_last",
                                                  activation=tf.nn.relu)
          self.evaluation_conv_flat = tf.reshape(
                  self.evaluation_conv, [-1, 2 * board_height * board_width])
          self.evaluation_fc1 = tf.layers.dense(inputs=self.evaluation_conv_flat,
                                                units=64, activation=tf.nn.relu)
          # output the score of evaluation on current state
          self.evaluation_fc2 = tf.layers.dense(inputs=self.evaluation_fc1,
                                                units=1, activation=tf.nn.tanh)
  
          # Define the Loss function
          # 1. Label: the array containing if the game wins or not for each state
          self.labels = tf.placeholder(tf.float32, shape=[None, 1])
          # 2. Predictions: the array containing the evaluation score of each state
          # which is self.evaluation_fc2
          # 3-1. Value Loss function
          self.value_loss = tf.losses.mean_squared_error(self.labels,
                                                         self.evaluation_fc2)
          # 3-2. Policy Loss function
          self.mcts_probs = tf.placeholder(
                  tf.float32, shape=[None, board_height * board_width])
          self.policy_loss = tf.negative(tf.reduce_mean(
                  tf.reduce_sum(tf.multiply(self.mcts_probs, self.action_fc), 1)))
          # 3-3. L2 penalty (regularization)
          l2_penalty_beta = 1e-4
          vars = tf.trainable_variables()
          l2_penalty = l2_penalty_beta * tf.add_n(
              [tf.nn.l2_loss(v) for v in vars if 'bias' not in v.name.lower()])
          # 3-4 Add up to be the Loss function
          self.loss = self.value_loss + self.policy_loss + l2_penalty
  
          # Define the optimizer we use for training
          self.learning_rate = tf.placeholder(tf.float32)
          self.optimizer = tf.train.AdamOptimizer(
                  learning_rate=self.learning_rate).minimize(self.loss)
  
          # Make a session
          self.session = tf.Session()
  
          # calc policy entropy, for monitoring only
          self.entropy = tf.negative(tf.reduce_mean(
                  tf.reduce_sum(tf.exp(self.action_fc) * self.action_fc, 1)))
  
          # Initialize variables
          init = tf.global_variables_initializer()
          self.session.run(init)
  
          # For saving and restoring
          self.saver = tf.train.Saver()
          if model_file is not None:
              self.restore_model(model_file)
  
      def policy_value(self, state_batch):
          """
          input: a batch of states
          output: a batch of action probabilities and state values
          """
          log_act_probs, value = self.session.run(
                  [self.action_fc, self.evaluation_fc2],
                  feed_dict={self.input_states: state_batch}
                  )
          act_probs = np.exp(log_act_probs)
          return act_probs, value
  
      def policy_value_fn(self, board):
          """
          input: board
          output: a list of (action, probability) tuples for each available
          action and the score of the board state
          """
          legal_positions = board.availables
          current_state = np.ascontiguousarray(board.current_state().reshape(
                  -1, 5, self.board_width, self.board_height))
          act_probs, value = self.policy_value(current_state)
          act_probs = zip(legal_positions, act_probs[0][legal_positions])
          return act_probs, value
  
      def train_step(self, state_batch, mcts_probs, winner_batch, lr):
          """perform a training step"""
          winner_batch = np.reshape(winner_batch, (-1, 1))
          loss, entropy, _ = self.session.run(
                  [self.loss, self.entropy, self.optimizer],
                  feed_dict={self.input_states: state_batch,
                             self.mcts_probs: mcts_probs,
                             self.labels: winner_batch,
                             self.learning_rate: lr})
          return loss, entropy
  
      def save_model(self, model_path):
          self.saver.save(self.session, model_path)
  
      def restore_model(self, model_path):
          self.saver.restore(self.session, model_path)
  