docstr: Check that the agent updates for specified actions and rewards.
docstr_tok: ["Check","that","the","agent","updates","for","specified","actions","and","rewards","."]
code: def testLinearAgentUpdateWithMaskedActions(
      self,
      batch_size,
      context_dim,
      exploration_policy,
      dtype,
      use_eigendecomp=False,
      set_example_weights=False,
  ):
      """Check that the agent updates for specified actions and rewards."""
  
      del use_eigendecomp  # Unused in this test.
  
      # Construct a `Trajectory` for the given action, observation, reward.
      num_actions = 5
      (
          initial_step,
          final_step,
      ) = _get_initial_and_final_steps_with_action_mask(
          batch_size, context_dim, num_actions=num_actions
      )
      action = np.random.randint(
          num_actions, size=batch_size, dtype=np.int32
      )
      action_step = _get_action_step(action)
      experience = _get_experience(
          initial_step, action_step, final_step
      )
  
      # Construct an agent and perform the update.
      observation_spec = (
          tensor_spec.TensorSpec([context_dim], tf.float32),
          tensor_spec.TensorSpec([num_actions], tf.int32),
      )
      time_step_spec = time_step.time_step_spec(observation_spec)
      action_spec = tensor_spec.BoundedTensorSpec(
          dtype=tf.int32, shape=(), minimum=0, maximum=num_actions - 1
      )
  
      def observation_and_action_constraint_splitter(obs):
          return obs[0], obs[1]
  
      agent = linear_agent.LinearBanditAgent(
          exploration_policy=exploration_policy,
          time_step_spec=time_step_spec,
          action_spec=action_spec,
          observation_and_action_constraint_splitter=(
              observation_and_action_constraint_splitter
          ),
          dtype=dtype,
      )
      self.evaluate(agent.initialize())
      weights = (
          tf.linspace(start=1.5, stop=10.5, num=batch_size)
          if set_example_weights
          else None
      )
      loss_info = agent.train(experience, weights)
      self.evaluate(loss_info)
      final_a = self.evaluate(agent.cov_matrix)
      final_b = self.evaluate(agent.data_vector)
  
      # Compute the expected updated estimates.
      reshaped_observation = tf.reshape(
          observation_and_action_constraint_splitter(
              experience.observation
          )[0],
          [batch_size, -1],
      )
      reshaped_reward = tf.reshape(experience.reward, [batch_size])
      observation, reward = _maybe_weight_observation_and_reward(
          reshaped_observation, reshaped_reward, weights
      )
      observations_list = tf.dynamic_partition(
          data=observation,
          partitions=tf.convert_to_tensor(action),
          num_partitions=num_actions,
      )
      rewards_list = tf.dynamic_partition(
          data=reward,
          partitions=tf.convert_to_tensor(action),
          num_partitions=num_actions,
      )
      expected_a_updated_list = []
      expected_b_updated_list = []
      for _, (observations_for_arm, rewards_for_arm) in enumerate(
          zip(observations_list, rewards_list)
      ):
          num_samples_for_arm_current = tf.cast(
              tf.shape(rewards_for_arm)[0], tf.float32
          )
          num_samples_for_arm_total = num_samples_for_arm_current
  
          # pylint: disable=cell-var-from-loop
          def true_fn():
              a_new = tf.matmul(
                  observations_for_arm,
                  observations_for_arm,
                  transpose_a=True,
              )
              b_new = bandit_utils.sum_reward_weighted_observations(
                  rewards_for_arm, observations_for_arm
              )
              return a_new, b_new
  
          def false_fn():
              return tf.zeros([context_dim, context_dim]), tf.zeros(
                  [context_dim]
              )
  
          a_new, b_new = tf.cond(
              tf.squeeze(num_samples_for_arm_total) > 0,
              true_fn,
              false_fn,
          )
  
          expected_a_updated_list.append(self.evaluate(a_new))
          expected_b_updated_list.append(self.evaluate(b_new))
  
      # Check that the actual updated estimates match the expectations.
      self.assertAllClose(expected_a_updated_list, final_a)
      self.assertAllClose(expected_b_updated_list, final_b)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import os
  from typing import Optional, Tuple
  from absl import flags
  from absl.testing import parameterized
  import numpy
  import tensorflow
  import tensorflow_probability
  from tf_agents.bandits.agents import lin_ucb_agent
  from tf_agents.bandits.agents import linear_bandit_agent
  from tf_agents.bandits.agents import linear_thompson_sampling_agent
  from tf_agents.bandits.agents import utils
  from tf_agents.bandits.drivers import driver_utils
  from tf_agents.bandits.specs import utils
  from tf_agents.policies import utils
  from tf_agents.specs import tensor_spec
  from tf_agents.trajectories import policy_step
  from tf_agents.trajectories import time_step
  from tf_agents.typing import types
  from tf_agents.utils import common
label_desc: Use image node_id 10 for calling the LinearBanditAgentTest obj's underlying member method code with example usage: obj.testLinearAgentUpdateWithMaskedActions(batch_size, context_dim, exploration_policy, dtype, use_eigendecomp, set_example_weights) and returns: obs,  obs, a_new,  b_new, tf,  tf