Example #1
0
 def testFeedIndexedSlicesWithoutDenseShape(self):
     with session.Session() as s:
         values = np.array([1.0, 2.0]).astype(np.float32)
         indices = np.array([[3, 2, 0], [4, 5, 1]]).astype(np.int64)
         dense_shape = None
         ind = ops.IndexedSlices(
             array_ops.placeholder(dtype=np.float32, shape=(2, )),
             array_ops.placeholder(dtype=np.int64, shape=(2, 3)), None)
         ind_values = array_ops.identity(ind.values)
         ind_indices = array_ops.identity(ind.indices)
         ind2 = ops.IndexedSlices(ind_values, ind_indices)
         # Feed with tuple
         values_out, indices_out = s.run([ind_values, ind_indices],
                                         {ind: (values, indices)})
         self.assertAllEqual(values_out, values)
         self.assertAllEqual(indices_out, indices)
         # Feed with IndexedSlicesValue
         values_out, indices_out = s.run(
             [ind_values, ind_indices],
             {ind: ops.IndexedSlicesValue(values, indices, dense_shape)})
         self.assertAllEqual(values_out, values)
         self.assertAllEqual(indices_out, indices)
         # Feed with IndexedSlicesValue, fetch IndexedSlicesValue
         ind2_out = s.run(
             ind2,
             {ind: ops.IndexedSlicesValue(values, indices, dense_shape)})
         self.assertAllEqual(ind2_out.values, values)
         self.assertAllEqual(ind2_out.indices, indices)
         self.assertAllEqual(ind2_out.dense_shape, dense_shape)
Example #2
0
def _to_numpy(a):
    """Converts tensors to numpy arrays.

  Converts Tensors and EagerTensors to numpy arrays.
  When eager execution is enabled, converts IndexedSlices
  to IndexedSlicesValue with numpy indices/values

  Args:
    a: any value.

  Returns:
    If a is EagerTensor or Tensor, returns the evaluation of a by calling
    numpy() or run().
    If a is IndexedSlices and eager execution is enabled, calls numpy() on a's
    fields. Otherwise returns a unchanged.
  """
    if isinstance(a, ops.EagerTensor):
        return a.numpy()
    if isinstance(a, ops.Tensor):
        sess = ops.get_default_session()
        return sess.run(a)
    if isinstance(a, ops.IndexedSlices) and context.executing_eagerly():
        return ops.IndexedSlicesValue(indices=[x.numpy() for x in a.indices],
                                      values=[x.numpy() for x in a.values],
                                      dense_shape=a.dense_shape)
    return a
def _eval_indexed_slices(a):
    """Converts IndexedSlices to IndexedSlicesValue with numpy indices/values.

  When eager execution is enabled, converts IndexedSlices
  to IndexedSlicesValue with numpy indices/values.

  Args:
    a: any value.

  Returns:
    If a is IndexedSlices and eager execution is enabled, calls numpy() on a's
    fields. Otherwise returns a unchanged.
  """
    if isinstance(a, ops.IndexedSlices) and context.executing_eagerly():
        return ops.IndexedSlicesValue(indices=[x.numpy() for x in a.indices],
                                      values=[x.numpy() for x in a.values],
                                      dense_shape=a.dense_shape)
    return a
Example #4
0
 def _eval_tensor(self, tensor):
     if tensor is None:
         return None
     elif callable(tensor):
         return self._eval_helper(tensor())
     else:
         try:
             if sparse_tensor.is_sparse(tensor):
                 return sparse_tensor.SparseTensorValue(
                     tensor.indices.numpy(), tensor.values.numpy(),
                     tensor.dense_shape.numpy())
             elif ragged_tensor.is_ragged(tensor):
                 return ragged_tensor_value.RaggedTensorValue(
                     self._eval_tensor(tensor.values),
                     self._eval_tensor(tensor.row_splits))
             elif isinstance(tensor, ops.IndexedSlices):
                 return ops.IndexedSlicesValue(
                     values=tensor.values.numpy(),
                     indices=tensor.indices.numpy(),
                     dense_shape=tensor.dense_shape.numpy())
             return tensor.numpy()
         except AttributeError as e:
             ValueError("Unsupported type %s." % type(tensor)), e
Example #5
0
def _get_indexed_slices_value_from_fetches(fetched_vals):
  return ops.IndexedSlicesValue(fetched_vals[0], fetched_vals[1],
                                fetched_vals[2]
                                if len(fetched_vals) == 3 else None)