Python tensorflow.compat.v2.zeros() Examples

The following are 30 code examples of tensorflow.compat.v2.zeros(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.compat.v2 , or try the search function .
Example #1
Source File: array_ops.py    From trax with Apache License 2.0 6 votes vote down vote up
def zeros(shape, dtype=float):  # pylint: disable=redefined-outer-name
  """Returns an ndarray with the given shape and type filled with zeros.

  Args:
    shape: A fully defined shape. Could be - NumPy array or a python scalar,
      list or tuple of integers, - TensorFlow tensor/ndarray of integer type and
      rank <=1.
    dtype: Optional, defaults to float. The type of the resulting ndarray. Could
      be a python type, a NumPy type or a TensorFlow `DType`.

  Returns:
    An ndarray.
  """
  if dtype:
    dtype = utils.result_type(dtype)
  if isinstance(shape, arrays_lib.ndarray):
    shape = shape.data
  return arrays_lib.tensor_to_ndarray(tf.zeros(shape, dtype=dtype)) 
Example #2
Source File: base_agent_test.py    From valan with Apache License 2.0 6 votes vote down vote up
def _neck(self, torso_output, state):
    # Verify state. It could have been reset if done was true.
    expected_state = np.copy(self._current_state.numpy())
    done = self._done[self._timestep]
    for i, d in enumerate(done):
      if d:
        expected_state[i] = np.zeros(self._init_state_size)
    np.testing.assert_array_almost_equal(expected_state, state.numpy())
    # Verify torso_output
    expected_torso_output = np.concatenate([
        np.ones(shape=(self._batch_size, 50)),
        np.zeros(shape=(self._batch_size, 50))
    ],
                                           axis=1)
    np.testing.assert_array_almost_equal(expected_torso_output,
                                         torso_output.numpy())
    self._timestep += 1
    self._current_state = state + 1
    return (tf.ones([self._batch_size, 6]) * self._timestep,
            self._current_state) 
Example #3
Source File: utils_test.py    From valan with Apache License 2.0 6 votes vote down vote up
def testBatchApply(self):
    time_dim = 4
    batch_dim = 5
    inputs = {
        'a': tf.zeros(shape=(time_dim, batch_dim)),
        'b': {
            'b_1': tf.ones(shape=(time_dim, batch_dim, 9, 10)),
            'b_2': tf.ones(shape=(time_dim, batch_dim, 6)),
        }
    }

    def f(tensors):
      np.testing.assert_array_almost_equal(
          np.zeros(shape=(time_dim * batch_dim)), tensors['a'].numpy())
      np.testing.assert_array_almost_equal(
          np.ones(shape=(time_dim * batch_dim, 9, 10)),
          tensors['b']['b_1'].numpy())
      np.testing.assert_array_almost_equal(
          np.ones(shape=(time_dim * batch_dim, 6)), tensors['b']['b_2'].numpy())

      return tf.ones(shape=(time_dim * batch_dim, 2))

    result = utils.batch_apply(f, inputs)
    np.testing.assert_array_almost_equal(
        np.ones(shape=(time_dim, batch_dim, 2)), result.numpy()) 
Example #4
Source File: mt_agent.py    From valan with Apache License 2.0 6 votes vote down vote up
def _get_initial_state(self, observation, batch_size):
    text_token_ids = observation[constants.INS_TOKEN_IDS]
    text_enc_outputs, final_state = self._instruction_encoder(text_token_ids)
    if self._ndh_instruction_encoder is not None:
      ndh_text_enc_outputs, ndh_final_state = self._ndh_instruction_encoder(
          text_token_ids)
      mask = tf.equal(observation[constants.PROBLEM_TYPE],
                      constants.PROBLEM_VLN)
      text_enc_outputs = tf.nest.map_structure(
          lambda x, y: tf.compat.v1.where(mask, x, y), text_enc_outputs,
          ndh_text_enc_outputs)
      final_state = tf.nest.map_structure(
          lambda x, y: tf.compat.v1.where(mask, x, y), final_state,
          ndh_final_state)

    if self._ins_classifier is not None:
      # Concatenate all hidden layers' state vectors. Use state.h
      ins_classifier_logits = self._ins_classifier(
          tf.concat([s[0] for s in final_state], axis=1))
    else:
      ins_classifier_logits = tf.zeros(shape=(batch_size, 2))
    return (final_state, text_enc_outputs, ins_classifier_logits) 
Example #5
Source File: continuous_batched.py    From compression with Apache License 2.0 6 votes vote down vote up
def from_config(cls, config):
    """Instantiates an entropy model from a configuration dictionary.

    Arguments:
      config: A `dict`, typically the output of `get_config`.

    Returns:
      An entropy model.
    """
    self = super().from_config(config)
    with self.name_scope:
      # pylint:disable=protected-access
      if config["quantization_offset"]:
        zeros = tf.zeros(self.prior_shape, dtype=self.dtype)
        self._quantization_offset = tf.Variable(
            zeros, name="quantization_offset")
      else:
        self._quantization_offset = None
      # pylint:enable=protected-access
    return self 
Example #6
Source File: vector_hull_white.py    From tf-quant-finance with Apache License 2.0 6 votes vote down vote up
def _exact_discretization_setup(self, dim):
    """Initial setup for efficient computations."""
    self._zero_padding = tf.zeros((dim, 1), dtype=self._dtype)
    self._jump_locations = tf.concat(
        [self._volatility.jump_locations(),
         self._mean_reversion.jump_locations()], axis=-1)
    self._jump_values_vol = self._volatility(self._jump_locations)
    self._jump_values_mr = self._mean_reversion(self._jump_locations)
    if dim == 1:
      self._padded_knots = tf.concat([
          self._zero_padding,
          tf.expand_dims(self._jump_locations[:-1], axis=0)
      ], axis=1)
      self._jump_values_vol = tf.expand_dims(self._jump_values_vol, axis=0)
      self._jump_values_mr = tf.expand_dims(self._jump_values_mr, axis=0)
      self._jump_locations = tf.expand_dims(self._jump_locations, axis=0)

    else:
      self._padded_knots = tf.concat(
          [self._zero_padding, self._jump_locations[:, :-1]], axis=1) 
Example #7
Source File: utils_test.py    From tf-quant-finance with Apache License 2.0 6 votes vote down vote up
def test_maybe_update_along_axis(self, dtype):
    """Tests that the values are updated correctly."""
    tensor = tf.ones([5, 4, 3, 2], dtype=dtype)
    new_tensor = tf.zeros([5, 4, 1, 2], dtype=dtype)
    @tf.function
    def maybe_update_along_axis(do_update):
      return utils.maybe_update_along_axis(
          tensor=tensor, new_tensor=new_tensor, axis=1, ind=2,
          do_update=do_update)
    updated_tensor = maybe_update_along_axis(True)
    with self.subTest(name='Shape'):
      self.assertEqual(updated_tensor.shape, tensor.shape)
    with self.subTest(name='UpdatedVals'):
      self.assertAllEqual(updated_tensor[:, 2, :, :],
                          tf.zeros_like(updated_tensor[:, 2, :, :]))
    with self.subTest(name='NotUpdatedVals'):
      self.assertAllEqual(updated_tensor[:, 1, :, :],
                          tf.ones_like(updated_tensor[:, 2, :, :]))
    with self.subTest(name='DoNotUpdateVals'):
      not_updated_tensor = maybe_update_along_axis(False)
      self.assertAllEqual(not_updated_tensor, tensor) 
Example #8
Source File: lsm_v2.py    From tf-quant-finance with Apache License 2.0 6 votes vote down vote up
def _updated_cashflow(num_times, exercise_index, exercise_value,
                      expected_continuation, cashflow):
  """Revises the cashflow tensor where options will be exercised earlier."""
  do_exercise_bool = exercise_value > expected_continuation
  do_exercise = tf.cast(do_exercise_bool, exercise_value.dtype)
  # Shape [num_samples, payoff_dim]
  scaled_do_exercise = tf.where(do_exercise_bool, exercise_value,
                                tf.zeros_like(exercise_value))
  # This picks out the samples where we now wish to exercise.
  # Shape [num_samples, payoff_dim, 1]
  new_samp_masked = tf.expand_dims(scaled_do_exercise, axis=2)
  # This should be one on the current time step and zero otherwise.
  # This is an array with nonzero entries showing newly exercised payoffs.
  zeros = tf.zeros_like(cashflow)
  mask = tf.equal(tf.range(0, num_times), exercise_index - 1)
  new_cash = tf.where(mask, new_samp_masked, zeros)
  # Has shape [num_samples, payoff_dim, 1]
  old_mask = tf.expand_dims(1 - do_exercise, axis=2)
  mask = tf.range(0, num_times) >= exercise_index
  old_mask = tf.where(mask, old_mask, zeros)
  # Shape [num_samples, payoff_dim, num_times]
  old_cash = old_mask * cashflow
  return new_cash + old_cash 
Example #9
Source File: custom_loops.py    From tf-quant-finance with Apache License 2.0 6 votes vote down vote up
def _make_unit_jacobian(initial_state, params):
  """Creates a unit Jacobian matrix."""
  n = len(initial_state)
  d = [initial_state[i].shape.as_list()[-1] for i in range(n)]
  if None in d:
    raise ValueError("Last dimensions of initial_state Tensors must be known.")
  p = len(params)
  dtype = initial_state[0].dtype

  def make_js_block(i, j):
    shape = initial_state[i].shape.concatenate((d[j],))
    if i != j:
      return tf.zeros(shape, dtype=dtype)
    eye = tf.eye(d[i], dtype=dtype)
    return tf.broadcast_to(eye, shape)

  def make_jp_block(i, j):
    del j
    shape = initial_state[i].shape.concatenate((1,))
    return tf.zeros(shape, dtype=dtype)

  js = [[make_js_block(i, j) for j in range(n)] for i in range(n)]
  jp = [[make_jp_block(i, j) for j in range(p)] for i in range(n)]
  return js, jp 
Example #10
Source File: gradient_test.py    From tf-quant-finance with Apache License 2.0 6 votes vote down vote up
def test_backward_unconnected_gradient(self):
    t = tf.range(1, 3, dtype=tf.float32)  # Shape [2]
    zeros = tf.zeros([2], dtype=t.dtype)
    expected_result = [0.0, 0.0]
    func = lambda t: tf.stack([zeros, zeros, zeros], axis=0)  # Shape [3, 2]
    with self.subTest("EagerExecution"):
      backward_grad = self.evaluate(tff.math.gradients(
          func, t, unconnected_gradients=tf.UnconnectedGradients.ZERO))
      self.assertEqual(backward_grad.shape, (2,))
      np.testing.assert_allclose(backward_grad, expected_result)
    with self.subTest("GraphExecution"):
      @tf.function
      def grad_computation():
        y = func(t)
        return tff.math.gradients(
            y, t, unconnected_gradients=tf.UnconnectedGradients.ZERO)
      backward_grad = self.evaluate(grad_computation())
      self.assertEqual(backward_grad.shape, (2,))
      np.testing.assert_allclose(backward_grad, expected_result) 
Example #11
Source File: gradient_test.py    From tf-quant-finance with Apache License 2.0 6 votes vote down vote up
def test_forward_unconnected_gradient(self):
    t = tf.range(1, 3, dtype=tf.float32)  # Shape [2]
    zeros = tf.zeros([2], dtype=t.dtype)
    func = lambda t: tf.stack([zeros, zeros, zeros], axis=0)  # Shape [3, 2]
    expected_result = [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]
    with self.subTest("EagerExecution"):
      fwd_grad = self.evaluate(tff.math.fwd_gradient(
          func, t, unconnected_gradients=tf.UnconnectedGradients.ZERO))
      self.assertEqual(fwd_grad.shape, (3, 2))
      np.testing.assert_allclose(fwd_grad, expected_result)
    with self.subTest("GraphExecution"):
      @tf.function
      def grad_computation():
        y = func(t)
        return tff.math.fwd_gradient(
            y, t, unconnected_gradients=tf.UnconnectedGradients.ZERO)
      fwd_grad = self.evaluate(grad_computation())
      self.assertEqual(fwd_grad.shape, (3, 2))
      np.testing.assert_allclose(fwd_grad, expected_result) 
Example #12
Source File: array_ops.py    From trax with Apache License 2.0 6 votes vote down vote up
def tri(N, M=None, k=0, dtype=None):  # pylint: disable=invalid-name,missing-docstring
  M = M if M is not None else N
  if dtype is not None:
    dtype = utils.result_type(dtype)
  else:
    dtype = dtypes.default_float_type()

  if k < 0:
    lower = -k - 1
    if lower > N:
      r = tf.zeros([N, M], dtype)
    else:
      # Keep as tf bool, since we create an upper triangular matrix and invert
      # it.
      o = tf.ones([N, M], dtype=tf.bool)
      r = tf.cast(tf.math.logical_not(tf.linalg.band_part(o, lower, -1)), dtype)
  else:
    o = tf.ones([N, M], dtype)
    if k > M:
      r = o
    else:
      r = tf.linalg.band_part(o, -1, k)
  return utils.tensor_to_ndarray(r) 
Example #13
Source File: array_ops.py    From trax with Apache License 2.0 6 votes vote down vote up
def zeros_like(a, dtype=None):
  """Returns an array of zeros with the shape and type of the input array.

  Args:
    a: array_like. Could be an ndarray, a Tensor or any object that can be
      converted to a Tensor using `tf.convert_to_tensor`.
    dtype: Optional, defaults to dtype of the input array. The type of the
      resulting ndarray. Could be a python type, a NumPy type or a TensorFlow
      `DType`.

  Returns:
    An ndarray.
  """
  if isinstance(a, arrays_lib.ndarray):
    a = a.data
  if dtype is None:
    # We need to let utils.result_type decide the dtype, not tf.zeros_like
    dtype = utils.result_type(a)
  else:
    # TF and numpy has different interpretations of Python types such as
    # `float`, so we let `utils.result_type` decide.
    dtype = utils.result_type(dtype)
  dtype = tf.as_dtype(dtype)  # Work around b/149877262
  return arrays_lib.tensor_to_ndarray(tf.zeros_like(a, dtype)) 
Example #14
Source File: math_ops.py    From trax with Apache License 2.0 6 votes vote down vote up
def _tf_gcd(x1, x2):
  def _gcd_cond_fn(x1, x2):
    return tf.reduce_any(x2 != 0)
  def _gcd_body_fn(x1, x2):
    # tf.math.mod will raise an error when any element of x2 is 0. To avoid
    # that, we change those zeros to ones. Their values don't matter because
    # they won't be used.
    x2_safe = tf.where(x2 != 0, x2, tf.constant(1, x2.dtype))
    x1, x2 = (tf.where(x2 != 0, x2, x1),
              tf.where(x2 != 0, tf.math.mod(x1, x2_safe),
                       tf.constant(0, x2.dtype)))
    return (tf.where(x1 < x2, x2, x1), tf.where(x1 < x2, x1, x2))
  if (not np.issubdtype(x1.dtype.as_numpy_dtype, np.integer) or
      not np.issubdtype(x2.dtype.as_numpy_dtype, np.integer)):
    raise ValueError("Arguments to gcd must be integers.")
  shape = tf.broadcast_static_shape(x1.shape, x2.shape)
  x1 = tf.broadcast_to(x1, shape)
  x2 = tf.broadcast_to(x2, shape)
  gcd, _ = tf.while_loop(_gcd_cond_fn, _gcd_body_fn,
                         (tf.math.abs(x1), tf.math.abs(x2)))
  return gcd 
Example #15
Source File: generic_ito_process_test.py    From tf-quant-finance with Apache License 2.0 5 votes vote down vote up
def test_sample_paths_wiener(self, use_xla):
    """Tests paths properties for Wiener process (dX = dW)."""

    def drift_fn(_, x):
      return tf.zeros_like(x)

    def vol_fn(_, x):
      return tf.expand_dims(tf.ones_like(x), -1)

    process = GenericItoProcess(dim=1, drift_fn=drift_fn, volatility_fn=vol_fn)
    times = np.array([0.1, 0.2, 0.3])
    num_samples = 10000

    @tf.function
    def fn():
      return process.sample_paths(
          times=times, num_samples=num_samples, seed=42, time_step=0.01)

    if use_xla:
      paths = self.evaluate(tf.xla.experimental.compile(fn))[0]
    else:
      paths = self.evaluate(fn())

    means = np.mean(paths, axis=0).reshape([-1])
    covars = np.cov(paths.reshape([num_samples, -1]), rowvar=False)
    expected_means = np.zeros((3,))
    expected_covars = np.minimum(times.reshape([-1, 1]), times.reshape([1, -1]))
    with self.subTest(name="Means"):
      self.assertAllClose(means, expected_means, rtol=1e-2, atol=1e-2)
    with self.subTest(name="Covar"):
      self.assertAllClose(covars, expected_covars, rtol=1e-2, atol=1e-2) 
Example #16
Source File: base_agent_test.py    From valan with Apache License 2.0 5 votes vote down vote up
def _head(self, neck_output):
    # Verify neck_output
    np.testing.assert_equal((self._total_timesteps * self._batch_size, 6),
                            neck_output.shape)
    arrays = []
    for i in range(self._total_timesteps):
      arrays.append(np.ones((self._batch_size, 6)) * (i + 1))
    expected_neck_output = np.concatenate(arrays, axis=0)
    np.testing.assert_array_almost_equal(expected_neck_output,
                                         neck_output.numpy())
    return common.AgentOutput(
        policy_logits=tf.zeros(
            shape=[self._total_timesteps * self._batch_size, 4]),
        baseline=tf.ones(shape=[self._total_timesteps * self._batch_size])) 
Example #17
Source File: array_ops.py    From trax with Apache License 2.0 5 votes vote down vote up
def empty(shape, dtype=float):  # pylint: disable=redefined-outer-name
  """Returns an empty array with the specified shape and dtype.

  Args:
    shape: A fully defined shape. Could be - NumPy array or a python scalar,
      list or tuple of integers, - TensorFlow tensor/ndarray of integer type and
      rank <=1.
    dtype: Optional, defaults to float. The type of the resulting ndarray. Could
      be a python type, a NumPy type or a TensorFlow `DType`.

  Returns:
    An ndarray.
  """
  return zeros(shape, dtype) 
Example #18
Source File: base_agent_test.py    From valan with Apache License 2.0 5 votes vote down vote up
def _get_initial_state(self, observation, batch_size):
    return tf.zeros([batch_size, self._init_state_size]) 
Example #19
Source File: brownian_motion_utils.py    From tf-quant-finance with Apache License 2.0 5 votes vote down vote up
def _default_drift_data(dimension, dtype):
  """Constructs a function which returns a zero drift."""

  def zero_drift(time):
    shape = tf.concat([tf.shape(time), [dimension]], axis=0)
    time = tf.convert_to_tensor(time, dtype=dtype)
    return tf.zeros(shape, dtype=time.dtype)

  return zero_drift, (lambda t1, t2: zero_drift(t1)) 
Example #20
Source File: pixelcnn.py    From alibi-detect with Apache License 2.0 5 votes vote down vote up
def _make_kernel_constraint(kernel_size, valid_rows, valid_columns):
    """Make the masking function for layer kernels."""
    mask = np.zeros(kernel_size)
    lower, upper = valid_rows
    left, right = valid_columns
    mask[lower:upper, left:right] = 1.
    mask = mask[:, :, np.newaxis, np.newaxis]
    return lambda x: x * mask 
Example #21
Source File: euler_sampling.py    From tf-quant-finance with Apache License 2.0 5 votes vote down vote up
def _while_loop(*, dim, steps_num, current_state,
                drift_fn, volatility_fn, wiener_mean,
                num_samples, times, dt, sqrt_dt, time_step, num_requested_times,
                keep_mask, swap_memory, random_type, seed, normal_draws):
  """Smaple paths using tf.while_loop."""
  cond_fn = lambda i, *args: i < steps_num
  def step_fn(i, written_count, current_state, result):
    return _euler_step(
        i=i,
        written_count=written_count,
        current_state=current_state,
        result=result,
        drift_fn=drift_fn,
        volatility_fn=volatility_fn,
        wiener_mean=wiener_mean,
        num_samples=num_samples,
        times=times,
        dt=dt,
        sqrt_dt=sqrt_dt,
        keep_mask=keep_mask,
        random_type=random_type,
        seed=seed,
        normal_draws=normal_draws)
  maximum_iterations = (tf.cast(1. / time_step, dtype=tf.int32)
                        + tf.size(times))
  result = tf.zeros((num_samples, num_requested_times, dim),
                    dtype=current_state.dtype)
  _, _, _, result = tf.while_loop(
      cond_fn, step_fn, (0, 0, current_state, result),
      maximum_iterations=maximum_iterations,
      swap_memory=swap_memory)
  return result 
Example #22
Source File: mt_agent.py    From valan with Apache License 2.0 5 votes vote down vote up
def _head(self, neck_outputs):
    # The shape of hidden_state is [batch_size * time, 1, hidden_size]
    hidden_state = neck_outputs['hidden_state']
    if self._scan_classifier is not None:
      scan_classifier_logits = self._scan_classifier(hidden_state)
    else:
      scan_classifier_logits = tf.zeros(shape=(tf.shape(hidden_state)[0], 61))
    image_features = tf.cast(neck_outputs[constants.PANO_ENC], tf.float32)

    # c_visual has shape [batch_size * time, 1, self._c_visual_attention_size]
    c_visual = self._visual_attention([
        self._visual_attention_project_ctext(neck_outputs['c_text']),
        self._visual_attention_project_feature(image_features),
    ])

    # Concatenating the h_t, c_text and c_visual as described in RCM paper.
    input_feature = tf.concat([hidden_state, neck_outputs['c_text'], c_visual],
                              axis=2)
    connection_encoding = neck_outputs[constants.CONN_ENC]
    connection_encoding = tf.cast(connection_encoding, tf.float32)
    logits = self._dot_product([
        self._project_feature(input_feature),
        self._project_action(connection_encoding)
    ])
    # The final shape of logits is [batch_size * time, num_connections]
    logits = tf.squeeze(logits, axis=1)
    # mask out invalid connections.
    valid_conn_mask = tf.cast(neck_outputs[constants.VALID_CONN_MASK],
                              tf.float32)
    logits += (1. - valid_conn_mask) * -_INFINITY
    value = self._value_network(tf.squeeze(neck_outputs['c_text'], axis=1))
    value = tf.squeeze(value, axis=1)
    return common.AgentOutput(
        policy_logits=logits,
        baseline={
            'value': value,
            'ins_classifier_logits': neck_outputs['ins_classifier_logits'],
            'scan_classifier_logits': scan_classifier_logits,
        }) 
Example #23
Source File: array_ops.py    From trax with Apache License 2.0 5 votes vote down vote up
def diagonal(a, offset=0, axis1=0, axis2=1):  # pylint: disable=missing-docstring
  a = asarray(a).data

  maybe_rank = a.shape.rank
  if maybe_rank is not None and offset == 0 and (
      axis1 == maybe_rank - 2 or axis1 == -2) and (axis2 == maybe_rank - 1 or
                                                   axis2 == -1):
    return utils.tensor_to_ndarray(tf.linalg.diag_part(a))

  a = moveaxis(utils.tensor_to_ndarray(a), (axis1, axis2), (-2, -1)).data

  a_shape = tf.shape(a)

  def _zeros():  # pylint: disable=missing-docstring
    return (tf.zeros(tf.concat([a_shape[:-1], [0]], 0), dtype=a.dtype), 0)

  # All zeros since diag_part doesn't handle all possible k (aka offset).
  # Written this way since cond will run shape inference on both branches,
  # and diag_part shape inference will fail when offset is out of bounds.
  a, offset = utils.cond(
      utils.logical_or(
          utils.less_equal(offset, -1 * utils.getitem(a_shape, -2)),
          utils.greater_equal(offset, utils.getitem(a_shape, -1)),
      ), _zeros, lambda: (a, offset))

  a = utils.tensor_to_ndarray(tf.linalg.diag_part(a, k=offset))
  return a 
Example #24
Source File: vector_hull_white.py    From tf-quant-finance with Apache License 2.0 5 votes vote down vote up
def _prepare_grid(times, *params):
  """Prepares grid of times for path generation.

  Args:
    times:  Rank 1 `Tensor` of increasing positive real values. The times at
      which the path points are to be evaluated.
    *params: Parameters of the Heston model. Either scalar `Tensor`s of the
      same `dtype` or instances of `PiecewiseConstantFunc`.

  Returns:
    Tuple `(all_times, mask)`.
    `all_times` is a 1-D real `Tensor` containing all points from 'times`, the
    uniform grid of points between `[0, times[-1]]` with grid size equal to
    `time_step`, and jump locations of piecewise constant parameters The
    `Tensor` is sorted in ascending order and may contain duplicates.
    `mask` is a boolean 1-D `Tensor` of the same shape as 'all_times', showing
    which elements of 'all_times' correspond to THE values from `times`.
    Guarantees that times[0]=0 and mask[0]=False.
  """
  additional_times = []
  for param in params:
    if hasattr(param, 'is_piecewise_constant'):
      if param.is_piecewise_constant:
        # Flatten all jump locations
        additional_times.append(tf.reshape(param.jump_locations(), [-1]))
  zeros = tf.constant([0], dtype=times.dtype)
  all_times = tf.concat([zeros] + [times] + additional_times, axis=0)
  additional_times_mask = [
      tf.zeros_like(times, dtype=tf.bool) for times in additional_times]
  mask = tf.concat([
      tf.cast(zeros, dtype=tf.bool),
      tf.ones_like(times, dtype=tf.bool)
  ] + additional_times_mask, axis=0)
  perm = tf.argsort(all_times, stable=True)
  all_times = tf.gather(all_times, perm)
  mask = tf.gather(mask, perm)
  return all_times, mask 
Example #25
Source File: deep_factorized.py    From compression with Apache License 2.0 5 votes vote down vote up
def _make_variables(self):
    """Creates the variables representing the parameters of the distribution."""
    channels = self.batch_shape.num_elements()
    filters = (1,) + self.num_filters + (1,)
    scale = self.init_scale ** (1 / (len(self.num_filters) + 1))
    self._matrices = []
    self._biases = []
    self._factors = []

    for i in range(len(self.num_filters) + 1):
      init = tf.math.log(tf.math.expm1(1 / scale / filters[i + 1]))
      init = tf.cast(init, dtype=self.dtype)
      init = tf.broadcast_to(init, (channels, filters[i + 1], filters[i]))
      matrix = tf.Variable(init, name="matrix_{}".format(i))
      self._matrices.append(matrix)

      bias = tf.Variable(
          tf.random.uniform(
              (channels, filters[i + 1], 1), -.5, .5, dtype=self.dtype),
          name="bias_{}".format(i))
      self._biases.append(bias)

      if i < len(self.num_filters):
        factor = tf.Variable(
            tf.zeros((channels, filters[i + 1], 1), dtype=self.dtype),
            name="factor_{}".format(i))
        self._factors.append(factor) 
Example #26
Source File: array_ops.py    From trax with Apache License 2.0 5 votes vote down vote up
def identity(n, dtype=float):
  """Returns a square array with ones on the main diagonal and zeros elsewhere.

  Args:
    n: number of rows/cols.
    dtype: Optional, defaults to float. The type of the resulting ndarray. Could
      be a python type, a NumPy type or a TensorFlow `DType`.

  Returns:
    An ndarray of shape (n, n) and requested type.
  """
  return eye(N=n, M=n, dtype=dtype) 
Example #27
Source File: continuous_base.py    From compression with Apache License 2.0 5 votes vote down vote up
def from_config(cls, config):
    """Instantiates an entropy model from a configuration dictionary.

    Arguments:
      config: A `dict`, typically the output of `get_config`.

    Returns:
      An entropy model.
    """
    # Instantiate new object without calling initializers, and call superclass
    # (tf.Module) initializer manually. Note: `cls` is child class of this one.
    self = cls.__new__(cls)  # pylint:disable=no-value-for-parameter
    super().__init__(self)

    # What follows is the alternative initializer.
    with self.name_scope:
      # pylint:disable=protected-access
      self._dtype = tf.as_dtype(config["dtype"])
      self._prior_shape = tuple(int(s) for s in config["prior_shape"])
      self._coding_rank = int(config["coding_rank"])
      self._compression = True
      self._likelihood_bound = float(config["likelihood_bound"])
      self._tail_mass = float(config["tail_mass"])
      self._range_coder_precision = int(config["range_coder_precision"])

      prior_size = functools.reduce(lambda x, y: x * y, self.prior_shape, 1)
      cdf_width = int(config["cdf_width"])
      zeros = tf.zeros([prior_size, cdf_width], dtype=tf.int32)
      self._cdf = tf.Variable(zeros, trainable=False, name="cdf")
      self._cdf_offset = tf.Variable(
          zeros[:, 0], trainable=False, name="cdf_offset")
      self._cdf_length = tf.Variable(
          zeros[:, 0], trainable=False, name="cdf_length")
      # pylint:enable=protected-access

    return self 
Example #28
Source File: continuous_batched_test.py    From compression with Apache License 2.0 5 votes vote down vote up
def test_default_kwargs_throw_error_on_compression(self):
    noisy = uniform_noise.NoisyNormal(loc=.25, scale=10.)
    em = ContinuousBatchedEntropyModel(noisy, 1)
    x = tf.zeros(10)
    with self.assertRaises(RuntimeError):
      em.compress(x)
    s = tf.zeros(10, dtype=tf.string)
    with self.assertRaises(RuntimeError):
      em.decompress(s, [10]) 
Example #29
Source File: array_ops.py    From trax with Apache License 2.0 5 votes vote down vote up
def eye(N, M=None, k=0, dtype=float):  # pylint: disable=invalid-name,missing-docstring
  if dtype:
    dtype = utils.result_type(dtype)
  if not M:
    M = N
  # Making sure N, M and k are `int`
  N = int(N)
  M = int(M)
  k = int(k)
  if k >= M or -k >= N:
    # tf.linalg.diag will raise an error in this case
    return zeros([N, M], dtype=dtype)
  if k == 0:
    return arrays_lib.tensor_to_ndarray(tf.eye(N, M, dtype=dtype))
  # We need the precise length, otherwise tf.linalg.diag will raise an error
  diag_len = min(N, M)
  if k > 0:
    if N >= M:
      diag_len -= k
    elif N + k > M:
      diag_len = M - k
  elif k <= 0:
    if M >= N:
      diag_len += k
    elif M - k > N:
      diag_len = N + k
  diagonal_ = tf.ones([diag_len], dtype=dtype)
  return arrays_lib.tensor_to_ndarray(
      tf.linalg.diag(diagonal=diagonal_, num_rows=N, num_cols=M, k=k)) 
Example #30
Source File: lsm.py    From tf-quant-finance with Apache License 2.0 5 votes vote down vote up
def _updated_cashflow(num_times, exercise_index, exercise_value,
                      expected_continuation, cashflow):
  """Revises the cashflow tensor where options will be exercised earlier."""
  do_exercise_bool = exercise_value > expected_continuation
  do_exercise = tf.cast(do_exercise_bool, exercise_value.dtype)
  # Shape [num_samples, payoff_dim]
  scaled_do_exercise = tf.where(do_exercise_bool, exercise_value,
                                tf.zeros_like(exercise_value))
  # This picks out the samples where we now wish to exercise.
  # Shape [num_samples, payoff_dim, 1]
  new_samp_masked = tf.expand_dims(scaled_do_exercise, 2)
  # This should be one on the current time step and zero otherwise.
  # This is an array with nonzero entries showing newly exercised payoffs.
  pad_shape = scaled_do_exercise.shape.as_list()
  zeros_before = tf.zeros(pad_shape + [exercise_index - 1],
                          dtype=scaled_do_exercise.dtype)
  zeros_after = tf.zeros(pad_shape + [num_times - exercise_index],
                         dtype=scaled_do_exercise.dtype)
  new_cash = tf.concat([zeros_before, new_samp_masked, zeros_after], -1)

  # Has shape [num_samples, payoff_dim, 1]
  old_samp_masker = tf.expand_dims(1 - do_exercise, 2)
  # Broadcast to shape [num_samples, payoff_dim, num_times - exercise_index]
  old_samp_masker_after = tf.broadcast_to(
      old_samp_masker, pad_shape + [num_times - exercise_index])
  # Has shape `[num_samples, payoff_dim, exercise_index]`
  zeros_before = tf.zeros(pad_shape + [exercise_index],
                          dtype=scaled_do_exercise.dtype)
  # Shape [num_samples, payoff_dim, num_times]
  old_mask = tf.concat([zeros_before,
                        old_samp_masker_after], -1)
  # Shape [num_samples, payoff_dim, num_times]
  old_cash = old_mask * cashflow
  return new_cash + old_cash