def _initialize_loss(self):

    def fake_array(tensor):
        shape = tensor.shape.as_list()
        shape = [(s if (s is not None) else 1) for s in shape]
        return np.zeros(shape, dtype=tensor.dtype.as_numpy_dtype)
    dummy_batch = {SampleBatch.CUR_OBS: fake_array(self._obs_input), SampleBatch.NEXT_OBS: fake_array(self._obs_input), SampleBatch.DONES: np.array([False], dtype=np.bool), SampleBatch.ACTIONS: fake_array(ModelCatalog.get_action_placeholder(self.action_space)), SampleBatch.REWARDS: np.array([0], dtype=np.float32)}
    if self._obs_include_prev_action_reward:
        dummy_batch.update({SampleBatch.PREV_ACTIONS: fake_array(self._prev_action_input), SampleBatch.PREV_REWARDS: fake_array(self._prev_reward_input)})
    state_init = self.get_initial_state()
    state_batches = []
    for (i, h) in enumerate(state_init):
        dummy_batch['state_in_{}'.format(i)] = np.expand_dims(h, 0)
        dummy_batch['state_out_{}'.format(i)] = np.expand_dims(h, 0)
        state_batches.append(np.expand_dims(h, 0))
    if state_init:
        dummy_batch['seq_lens'] = np.array([1], dtype=np.int32)
    for (k, v) in self.extra_compute_action_fetches().items():
        dummy_batch[k] = fake_array(v)
    self._sess.run(tf.global_variables_initializer())
    postprocessed_batch = self.postprocess_trajectory(SampleBatch(dummy_batch))
    self.model(self._input_dict, self._state_in, self._seq_lens)
    if self._obs_include_prev_action_reward:
        train_batch = UsageTrackingDict({SampleBatch.PREV_ACTIONS: self._prev_action_input, SampleBatch.PREV_REWARDS: self._prev_reward_input, SampleBatch.CUR_OBS: self._obs_input})
        loss_inputs = [(SampleBatch.PREV_ACTIONS, self._prev_action_input), (SampleBatch.PREV_REWARDS, self._prev_reward_input), (SampleBatch.CUR_OBS, self._obs_input)]
    else:
        train_batch = UsageTrackingDict({SampleBatch.CUR_OBS: self._obs_input})
        loss_inputs = [(SampleBatch.CUR_OBS, self._obs_input)]
    for (k, v) in postprocessed_batch.items():
        if (k in train_batch):
            continue
        elif (v.dtype == np.object):
            continue
        elif ((k == 'seq_lens') or k.startswith('state_in_')):
            continue
        shape = ((None,) + v.shape[1:])
        dtype = (np.float32 if (v.dtype == np.float64) else v.dtype)
        placeholder = tf.placeholder(dtype, shape=shape, name=k)
        train_batch[k] = placeholder
    for (i, si) in enumerate(self._state_in):
        train_batch['state_in_{}'.format(i)] = si
    train_batch['seq_lens'] = self._seq_lens
    if log_once('loss_init'):
        logger.debug('Initializing loss function with dummy input:\n\n{}\n'.format(summarize(train_batch)))
    self._loss_input_dict = train_batch
    loss = self._do_loss_init(train_batch)
    for k in sorted(train_batch.accessed_keys):
        if ((k != 'seq_lens') and (not k.startswith('state_in_'))):
            loss_inputs.append((k, train_batch[k]))
    TFPolicy._initialize_loss(self, loss, loss_inputs)
    if self._grad_stats_fn:
        self._stats_fetches.update(self._grad_stats_fn(self, train_batch, self._grads))
    self._sess.run(tf.global_variables_initializer())
def _initialize_loss(self):

    def fake_array(tensor):
        shape = tensor.shape.as_list()
        shape = [(s if (s is not None) else 1) for s in shape]
        return np.zeros(shape, dtype=tensor.dtype.as_numpy_dtype)
    dummy_batch = {SampleBatch.CUR_OBS: fake_array(self._obs_input), SampleBatch.NEXT_OBS: fake_array(self._obs_input), SampleBatch.DONES: np.array([False], dtype=np.bool), SampleBatch.ACTIONS: fake_array(ModelCatalog.get_action_placeholder(self.action_space)), SampleBatch.REWARDS: np.array([0], dtype=np.float32)}
    if self._obs_include_prev_action_reward:
        dummy_batch.update({SampleBatch.PREV_ACTIONS: fake_array(self._prev_action_input), SampleBatch.PREV_REWARDS: fake_array(self._prev_reward_input)})
    state_init = self.get_initial_state()
    state_batches = []
    for (i, h) in enumerate(state_init):
        dummy_batch['state_in_{}'.format(i)] = np.expand_dims(h, 0)
        dummy_batch['state_out_{}'.format(i)] = np.expand_dims(h, 0)
        state_batches.append(np.expand_dims(h, 0))
    if state_init:
        dummy_batch['seq_lens'] = np.array([1], dtype=np.int32)
    for (k, v) in self.extra_compute_action_fetches().items():
        dummy_batch[k] = fake_array(v)
    self._sess.run(tf.global_variables_initializer())
    postprocessed_batch = self.postprocess_trajectory(SampleBatch(dummy_batch))
    self.model(self._input_dict, self._state_in, self._seq_lens)
    if self._obs_include_prev_action_reward:
        train_batch = UsageTrackingDict({SampleBatch.PREV_ACTIONS: self._prev_action_input, SampleBatch.PREV_REWARDS: self._prev_reward_input, SampleBatch.CUR_OBS: self._obs_input})
        loss_inputs = [(SampleBatch.PREV_ACTIONS, self._prev_action_input), (SampleBatch.PREV_REWARDS, self._prev_reward_input), (SampleBatch.CUR_OBS, self._obs_input)]
    else:
        train_batch = UsageTrackingDict({SampleBatch.CUR_OBS: self._obs_input})
        loss_inputs = [(SampleBatch.CUR_OBS, self._obs_input)]
    for (k, v) in postprocessed_batch.items():
        if (k in train_batch):
            continue
        elif (v.dtype == np.object):
            continue
        elif ((k == 'seq_lens') or k.startswith('state_in_')):
            continue
        shape = ((None,) + v.shape[1:])
        dtype = (np.float32 if (v.dtype == np.float64) else v.dtype)
        placeholder = tf.placeholder(dtype, shape=shape, name=k)
        train_batch[k] = placeholder
    for (i, si) in enumerate(self._state_in):
        train_batch['state_in_{}'.format(i)] = si
    train_batch['seq_lens'] = self._seq_lens
    if log_once('loss_init'):
        logger.debug('Initializing loss function with dummy input:\n\n{}\n'.format(summarize(train_batch)))
    self._loss_input_dict = train_batch
    loss = self._do_loss_init(train_batch)
    for k in sorted(train_batch.accessed_keys):
        if ((k != 'seq_lens') and (not k.startswith('state_in_'))):
            loss_inputs.append((k, train_batch[k]))
    TFPolicy._initialize_loss(self, loss, loss_inputs)
    if self._grad_stats_fn:
        self._stats_fetches.update(self._grad_stats_fn(self, train_batch, self._grads))
    self._sess.run(tf.global_variables_initializer())