def read(self, index, name=None): """See TensorArray.""" del name # not meaningful when executing eagerly. if isinstance(index, ops.EagerTensor): index = index.numpy() if index < 0: raise errors_impl.OutOfRangeError( None, None, "Reading from negative indices (index %d) is not allowed." % index) if index >= len(self._tensor_array): raise errors_impl.OutOfRangeError( None, None, "Tried to read from index %d but array size is: %d" % (index, len(self._tensor_array))) tensor = self._tensor_array[index] if tensor is None: if index in self._previously_read_indices: raise errors_impl.InvalidArgumentError( None, None, "Could not read index %d twice because it was cleared after " "a previous read (perhaps try setting clear_after_read = false?)" % index) else: tensor = self._maybe_zero(index) if self._clear_after_read: self._tensor_array[index] = None self._previously_read_indices.append(index) return tensor
def _eager_write_no_copy(ta, index, value): """Writes value into an _EagerTensorArray without creating a new TensorArray. Args: ta: _EagerTensorArray into which to write value. index: 0-D. int32 scalar with the index to write to. value: N-D. Tensor of type `dtype`. The Tensor to write to this index. Raises: errors_impl.AlreadyExistsError: attempting to overwrite an entry. errors_impl.InvalidArgumentError: value dtype does not match `ta`'s dtype. errors_impl.OutOfRangeError: `index` is out of bounds. ValueError: shape of `value` is not consistent with inferred shape. """ if isinstance(index, ops.EagerTensor): index = index.numpy() if index < 0: raise errors_impl.OutOfRangeError( None, None, "Writing to negative indices (index %d) is not allowed." % index) tensor_array = ta._tensor_array size = len(tensor_array) if index >= size: if not ta._dynamic_size: raise errors_impl.OutOfRangeError( None, None, "Tried to write to index %d but array is not resizeable and size " "is: %d" % (index, size)) tensor_array.extend([None for _ in range(index - size + 1)]) if not isinstance(value, ops.EagerTensor): value = constant_op.constant(value) if ta._infer_shape: if ta._element_shape is None: ta._element_shape = value.shape elif ta._element_shape != value.shape: raise ValueError( "Incompatible shape for value (%s), expected (%s)" % (value.shape.as_list(), ta._element_shape.as_list())) if ta._dtype != value.dtype: raise errors_impl.InvalidArgumentError( None, None, "TensorArray dtype is %s but Op is trying to write dtype %s" % (ta._dtype.name, value.dtype.name)) if ta._tensor_array[index] is not None: raise errors_impl.AlreadyExistsError( None, None, "Could not write to TensorArray index %d because it has already been " "written to." % index) tensor_array[index] = value
def _write(self, index, value): """Writes `value` into index named by `index`. Args: index: 0-D. int32 scalar with the index to write to. value: N-D. Tensor of type `dtype`. The `Tensor` to write to `index`. Raises: errors_impl.InvalidArgumentError: `value` dtype does not match dtype. errors_impl.OutOfRangeError: `index` is out of bounds. ValueError: shape of `value` is not consistent with inferred shape. """ if isinstance(index, ops.EagerTensor): index = index.numpy() if index < 0: raise errors_impl.OutOfRangeError( None, None, "Writing to negative indices (index %d) is not allowed." % index) size = len(self._tensor_array) if index >= size: if not self._dynamic_size: raise errors_impl.OutOfRangeError( None, None, "Tried to write to index %d but array is not resizeable and size " "is: %d" % (index, size)) self._tensor_array.extend([None for _ in range(index - size + 1)]) if not isinstance(value, ops.EagerTensor): value = constant_op.constant(value) if self._infer_shape: if self._element_shape is None: self._element_shape = value.shape elif self._element_shape != value.shape: raise ValueError( "Incompatible shape for value (%s), expected (%s)" % (value.shape.as_list(), self._element_shape.as_list())) if self._dtype != value.dtype: raise errors_impl.InvalidArgumentError( None, None, "TensorArray dtype is %s but Op is trying to write dtype %s" % (self._dtype.name, value.dtype.name)) self._tensor_array[index] = value
def read(self, limit=1): if not self.memory: raise errors_impl.OutOfRangeError() memory = self.memory.pop(0) if len(memory[0]) > limit: partitions = memory.partition(limit, allow_overflow=True) memory = partitions.pop(0) self.memory = partitions + self.memory return self.serialize_replay(memory)
def testJoinIgnoresOutOfRange(self): coord = coordinator.Coordinator() threads = [ threading.Thread(target=RaiseInN, args=(coord, 0.01, errors_impl.OutOfRangeError( None, None, "First"), True)) ] for t in threads: t.start() coord.join(threads)
def serialize_map(replay_example_str): """Parse each example string to `tf.Tensor`.""" try: assert_op = control_flow_ops.Assert(replay_example_str != "", [replay_example_str]) with ops.control_dependencies([assert_op]): _, replay = parsing_ops.parse_single_sequence_example( replay_example_str, sequence_features=replay_features) except errors_impl.InvalidArgumentError: raise errors_impl.OutOfRangeError() return convert_and_fix_dtypes(replay)
def read(self, limit=1): if not self.memory: raise errors_impl.OutOfRangeError() index = np.random.randint(len(self.memory)) memory = self.memory.pop(index) if len(memory[0]) >= limit: partitions = memory.partition(limit, allow_overflow=True) index = np.random.randint(len(partitions)) memory = partitions.pop(index) self.memory = partitions + self.memory return self.serialize_replay(memory)
def testJoinIgnoresOutOfRange(self): coord = coordinator.Coordinator() ev_1 = threading.Event() threads = [ threading.Thread(target=RaiseOnEvent, args=(coord, ev_1, None, errors_impl.OutOfRangeError( None, None, "First"), True)) ] for t in threads: t.start() ev_1.set() coord.join(threads)
def testManagedSessionIgnoreOutOfRangeError(self): logdir = self._test_dir("managed_out_of_range") with ops.Graph().as_default(): my_op = constant_op.constant(1.0) sv = supervisor.Supervisor(logdir=logdir) last_step = None with sv.managed_session("") as sess: for step in xrange(10): last_step = step if step == 3: raise errors_impl.OutOfRangeError( my_op.op.node_def, my_op.op, "all done") else: self.evaluate(my_op) # Supervisor has been stopped. OutOfRangeError was not thrown. self.assertTrue(sv.should_stop()) self.assertEqual(3, last_step)
def read(self, limit=1): if not self.memory: raise errors_impl.OutOfRangeError() index = np.random.randint(len(self.memory)) return self.memory.pop(index)
def read(self, limit=1): if not self.memory: raise errors_impl.OutOfRangeError() return self.memory.pop(0)