repo stringlengths 7 55 | path stringlengths 4 223 | func_name stringlengths 1 134 | original_string stringlengths 75 104k | language stringclasses 1 value | code stringlengths 75 104k | code_tokens listlengths 19 28.4k | docstring stringlengths 1 46.9k | docstring_tokens listlengths 1 1.97k | sha stringlengths 40 40 | url stringlengths 87 315 | partition stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|
tensorflow/tensorboard | tensorboard/backend/event_processing/reservoir.py | Reservoir.AddItem | def AddItem(self, key, item, f=lambda x: x):
"""Add a new item to the Reservoir with the given tag.
If the reservoir has not yet reached full size, the new item is guaranteed
to be added. If the reservoir is full, then behavior depends on the
always_keep_last boolean.
If always_keep_last was set to true, the new item is guaranteed to be added
to the reservoir, and either the previous last item will be replaced, or
(with low probability) an older item will be replaced.
If always_keep_last was set to false, then the new item will replace an
old item with low probability.
If f is provided, it will be applied to transform item (lazily, iff item is
going to be included in the reservoir).
Args:
key: The key to store the item under.
item: The item to add to the reservoir.
f: An optional function to transform the item prior to addition.
"""
with self._mutex:
bucket = self._buckets[key]
bucket.AddItem(item, f) | python | def AddItem(self, key, item, f=lambda x: x):
"""Add a new item to the Reservoir with the given tag.
If the reservoir has not yet reached full size, the new item is guaranteed
to be added. If the reservoir is full, then behavior depends on the
always_keep_last boolean.
If always_keep_last was set to true, the new item is guaranteed to be added
to the reservoir, and either the previous last item will be replaced, or
(with low probability) an older item will be replaced.
If always_keep_last was set to false, then the new item will replace an
old item with low probability.
If f is provided, it will be applied to transform item (lazily, iff item is
going to be included in the reservoir).
Args:
key: The key to store the item under.
item: The item to add to the reservoir.
f: An optional function to transform the item prior to addition.
"""
with self._mutex:
bucket = self._buckets[key]
bucket.AddItem(item, f) | [
"def",
"AddItem",
"(",
"self",
",",
"key",
",",
"item",
",",
"f",
"=",
"lambda",
"x",
":",
"x",
")",
":",
"with",
"self",
".",
"_mutex",
":",
"bucket",
"=",
"self",
".",
"_buckets",
"[",
"key",
"]",
"bucket",
".",
"AddItem",
"(",
"item",
",",
"f",
")"
] | Add a new item to the Reservoir with the given tag.
If the reservoir has not yet reached full size, the new item is guaranteed
to be added. If the reservoir is full, then behavior depends on the
always_keep_last boolean.
If always_keep_last was set to true, the new item is guaranteed to be added
to the reservoir, and either the previous last item will be replaced, or
(with low probability) an older item will be replaced.
If always_keep_last was set to false, then the new item will replace an
old item with low probability.
If f is provided, it will be applied to transform item (lazily, iff item is
going to be included in the reservoir).
Args:
key: The key to store the item under.
item: The item to add to the reservoir.
f: An optional function to transform the item prior to addition. | [
"Add",
"a",
"new",
"item",
"to",
"the",
"Reservoir",
"with",
"the",
"given",
"tag",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/reservoir.py#L114-L138 | train |
tensorflow/tensorboard | tensorboard/backend/event_processing/reservoir.py | Reservoir.FilterItems | def FilterItems(self, filterFn, key=None):
"""Filter items within a Reservoir, using a filtering function.
Args:
filterFn: A function that returns True for the items to be kept.
key: An optional bucket key to filter. If not specified, will filter all
all buckets.
Returns:
The number of items removed.
"""
with self._mutex:
if key:
if key in self._buckets:
return self._buckets[key].FilterItems(filterFn)
else:
return 0
else:
return sum(bucket.FilterItems(filterFn)
for bucket in self._buckets.values()) | python | def FilterItems(self, filterFn, key=None):
"""Filter items within a Reservoir, using a filtering function.
Args:
filterFn: A function that returns True for the items to be kept.
key: An optional bucket key to filter. If not specified, will filter all
all buckets.
Returns:
The number of items removed.
"""
with self._mutex:
if key:
if key in self._buckets:
return self._buckets[key].FilterItems(filterFn)
else:
return 0
else:
return sum(bucket.FilterItems(filterFn)
for bucket in self._buckets.values()) | [
"def",
"FilterItems",
"(",
"self",
",",
"filterFn",
",",
"key",
"=",
"None",
")",
":",
"with",
"self",
".",
"_mutex",
":",
"if",
"key",
":",
"if",
"key",
"in",
"self",
".",
"_buckets",
":",
"return",
"self",
".",
"_buckets",
"[",
"key",
"]",
".",
"FilterItems",
"(",
"filterFn",
")",
"else",
":",
"return",
"0",
"else",
":",
"return",
"sum",
"(",
"bucket",
".",
"FilterItems",
"(",
"filterFn",
")",
"for",
"bucket",
"in",
"self",
".",
"_buckets",
".",
"values",
"(",
")",
")"
] | Filter items within a Reservoir, using a filtering function.
Args:
filterFn: A function that returns True for the items to be kept.
key: An optional bucket key to filter. If not specified, will filter all
all buckets.
Returns:
The number of items removed. | [
"Filter",
"items",
"within",
"a",
"Reservoir",
"using",
"a",
"filtering",
"function",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/reservoir.py#L140-L159 | train |
tensorflow/tensorboard | tensorboard/backend/event_processing/reservoir.py | _ReservoirBucket.AddItem | def AddItem(self, item, f=lambda x: x):
"""Add an item to the ReservoirBucket, replacing an old item if necessary.
The new item is guaranteed to be added to the bucket, and to be the last
element in the bucket. If the bucket has reached capacity, then an old item
will be replaced. With probability (_max_size/_num_items_seen) a random item
in the bucket will be popped out and the new item will be appended
to the end. With probability (1 - _max_size/_num_items_seen)
the last item in the bucket will be replaced.
Since the O(n) replacements occur with O(1/_num_items_seen) likelihood,
the amortized runtime is O(1).
Args:
item: The item to add to the bucket.
f: A function to transform item before addition, if it will be kept in
the reservoir.
"""
with self._mutex:
if len(self.items) < self._max_size or self._max_size == 0:
self.items.append(f(item))
else:
r = self._random.randint(0, self._num_items_seen)
if r < self._max_size:
self.items.pop(r)
self.items.append(f(item))
elif self.always_keep_last:
self.items[-1] = f(item)
self._num_items_seen += 1 | python | def AddItem(self, item, f=lambda x: x):
"""Add an item to the ReservoirBucket, replacing an old item if necessary.
The new item is guaranteed to be added to the bucket, and to be the last
element in the bucket. If the bucket has reached capacity, then an old item
will be replaced. With probability (_max_size/_num_items_seen) a random item
in the bucket will be popped out and the new item will be appended
to the end. With probability (1 - _max_size/_num_items_seen)
the last item in the bucket will be replaced.
Since the O(n) replacements occur with O(1/_num_items_seen) likelihood,
the amortized runtime is O(1).
Args:
item: The item to add to the bucket.
f: A function to transform item before addition, if it will be kept in
the reservoir.
"""
with self._mutex:
if len(self.items) < self._max_size or self._max_size == 0:
self.items.append(f(item))
else:
r = self._random.randint(0, self._num_items_seen)
if r < self._max_size:
self.items.pop(r)
self.items.append(f(item))
elif self.always_keep_last:
self.items[-1] = f(item)
self._num_items_seen += 1 | [
"def",
"AddItem",
"(",
"self",
",",
"item",
",",
"f",
"=",
"lambda",
"x",
":",
"x",
")",
":",
"with",
"self",
".",
"_mutex",
":",
"if",
"len",
"(",
"self",
".",
"items",
")",
"<",
"self",
".",
"_max_size",
"or",
"self",
".",
"_max_size",
"==",
"0",
":",
"self",
".",
"items",
".",
"append",
"(",
"f",
"(",
"item",
")",
")",
"else",
":",
"r",
"=",
"self",
".",
"_random",
".",
"randint",
"(",
"0",
",",
"self",
".",
"_num_items_seen",
")",
"if",
"r",
"<",
"self",
".",
"_max_size",
":",
"self",
".",
"items",
".",
"pop",
"(",
"r",
")",
"self",
".",
"items",
".",
"append",
"(",
"f",
"(",
"item",
")",
")",
"elif",
"self",
".",
"always_keep_last",
":",
"self",
".",
"items",
"[",
"-",
"1",
"]",
"=",
"f",
"(",
"item",
")",
"self",
".",
"_num_items_seen",
"+=",
"1"
] | Add an item to the ReservoirBucket, replacing an old item if necessary.
The new item is guaranteed to be added to the bucket, and to be the last
element in the bucket. If the bucket has reached capacity, then an old item
will be replaced. With probability (_max_size/_num_items_seen) a random item
in the bucket will be popped out and the new item will be appended
to the end. With probability (1 - _max_size/_num_items_seen)
the last item in the bucket will be replaced.
Since the O(n) replacements occur with O(1/_num_items_seen) likelihood,
the amortized runtime is O(1).
Args:
item: The item to add to the bucket.
f: A function to transform item before addition, if it will be kept in
the reservoir. | [
"Add",
"an",
"item",
"to",
"the",
"ReservoirBucket",
"replacing",
"an",
"old",
"item",
"if",
"necessary",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/reservoir.py#L196-L224 | train |
tensorflow/tensorboard | tensorboard/backend/event_processing/reservoir.py | _ReservoirBucket.FilterItems | def FilterItems(self, filterFn):
"""Filter items in a ReservoirBucket, using a filtering function.
Filtering items from the reservoir bucket must update the
internal state variable self._num_items_seen, which is used for determining
the rate of replacement in reservoir sampling. Ideally, self._num_items_seen
would contain the exact number of items that have ever seen by the
ReservoirBucket and satisfy filterFn. However, the ReservoirBucket does not
have access to all items seen -- it only has access to the subset of items
that have survived sampling (self.items). Therefore, we estimate
self._num_items_seen by scaling it by the same ratio as the ratio of items
not removed from self.items.
Args:
filterFn: A function that returns True for items to be kept.
Returns:
The number of items removed from the bucket.
"""
with self._mutex:
size_before = len(self.items)
self.items = list(filter(filterFn, self.items))
size_diff = size_before - len(self.items)
# Estimate a correction the number of items seen
prop_remaining = len(self.items) / float(
size_before) if size_before > 0 else 0
self._num_items_seen = int(round(self._num_items_seen * prop_remaining))
return size_diff | python | def FilterItems(self, filterFn):
"""Filter items in a ReservoirBucket, using a filtering function.
Filtering items from the reservoir bucket must update the
internal state variable self._num_items_seen, which is used for determining
the rate of replacement in reservoir sampling. Ideally, self._num_items_seen
would contain the exact number of items that have ever seen by the
ReservoirBucket and satisfy filterFn. However, the ReservoirBucket does not
have access to all items seen -- it only has access to the subset of items
that have survived sampling (self.items). Therefore, we estimate
self._num_items_seen by scaling it by the same ratio as the ratio of items
not removed from self.items.
Args:
filterFn: A function that returns True for items to be kept.
Returns:
The number of items removed from the bucket.
"""
with self._mutex:
size_before = len(self.items)
self.items = list(filter(filterFn, self.items))
size_diff = size_before - len(self.items)
# Estimate a correction the number of items seen
prop_remaining = len(self.items) / float(
size_before) if size_before > 0 else 0
self._num_items_seen = int(round(self._num_items_seen * prop_remaining))
return size_diff | [
"def",
"FilterItems",
"(",
"self",
",",
"filterFn",
")",
":",
"with",
"self",
".",
"_mutex",
":",
"size_before",
"=",
"len",
"(",
"self",
".",
"items",
")",
"self",
".",
"items",
"=",
"list",
"(",
"filter",
"(",
"filterFn",
",",
"self",
".",
"items",
")",
")",
"size_diff",
"=",
"size_before",
"-",
"len",
"(",
"self",
".",
"items",
")",
"# Estimate a correction the number of items seen",
"prop_remaining",
"=",
"len",
"(",
"self",
".",
"items",
")",
"/",
"float",
"(",
"size_before",
")",
"if",
"size_before",
">",
"0",
"else",
"0",
"self",
".",
"_num_items_seen",
"=",
"int",
"(",
"round",
"(",
"self",
".",
"_num_items_seen",
"*",
"prop_remaining",
")",
")",
"return",
"size_diff"
] | Filter items in a ReservoirBucket, using a filtering function.
Filtering items from the reservoir bucket must update the
internal state variable self._num_items_seen, which is used for determining
the rate of replacement in reservoir sampling. Ideally, self._num_items_seen
would contain the exact number of items that have ever seen by the
ReservoirBucket and satisfy filterFn. However, the ReservoirBucket does not
have access to all items seen -- it only has access to the subset of items
that have survived sampling (self.items). Therefore, we estimate
self._num_items_seen by scaling it by the same ratio as the ratio of items
not removed from self.items.
Args:
filterFn: A function that returns True for items to be kept.
Returns:
The number of items removed from the bucket. | [
"Filter",
"items",
"in",
"a",
"ReservoirBucket",
"using",
"a",
"filtering",
"function",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/reservoir.py#L226-L254 | train |
tensorflow/tensorboard | tensorboard/util/tensor_util.py | _GetDenseDimensions | def _GetDenseDimensions(list_of_lists):
"""Returns the inferred dense dimensions of a list of lists."""
if not isinstance(list_of_lists, (list, tuple)):
return []
elif not list_of_lists:
return [0]
else:
return [len(list_of_lists)] + _GetDenseDimensions(list_of_lists[0]) | python | def _GetDenseDimensions(list_of_lists):
"""Returns the inferred dense dimensions of a list of lists."""
if not isinstance(list_of_lists, (list, tuple)):
return []
elif not list_of_lists:
return [0]
else:
return [len(list_of_lists)] + _GetDenseDimensions(list_of_lists[0]) | [
"def",
"_GetDenseDimensions",
"(",
"list_of_lists",
")",
":",
"if",
"not",
"isinstance",
"(",
"list_of_lists",
",",
"(",
"list",
",",
"tuple",
")",
")",
":",
"return",
"[",
"]",
"elif",
"not",
"list_of_lists",
":",
"return",
"[",
"0",
"]",
"else",
":",
"return",
"[",
"len",
"(",
"list_of_lists",
")",
"]",
"+",
"_GetDenseDimensions",
"(",
"list_of_lists",
"[",
"0",
"]",
")"
] | Returns the inferred dense dimensions of a list of lists. | [
"Returns",
"the",
"inferred",
"dense",
"dimensions",
"of",
"a",
"list",
"of",
"lists",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/util/tensor_util.py#L134-L141 | train |
tensorflow/tensorboard | tensorboard/util/tensor_util.py | make_tensor_proto | def make_tensor_proto(values, dtype=None, shape=None, verify_shape=False):
"""Create a TensorProto.
Args:
values: Values to put in the TensorProto.
dtype: Optional tensor_pb2 DataType value.
shape: List of integers representing the dimensions of tensor.
verify_shape: Boolean that enables verification of a shape of values.
Returns:
A `TensorProto`. Depending on the type, it may contain data in the
"tensor_content" attribute, which is not directly useful to Python programs.
To access the values you should convert the proto back to a numpy ndarray
with `tensor_util.MakeNdarray(proto)`.
If `values` is a `TensorProto`, it is immediately returned; `dtype` and
`shape` are ignored.
Raises:
TypeError: if unsupported types are provided.
ValueError: if arguments have inappropriate values or if verify_shape is
True and shape of values is not equals to a shape from the argument.
make_tensor_proto accepts "values" of a python scalar, a python list, a
numpy ndarray, or a numpy scalar.
If "values" is a python scalar or a python list, make_tensor_proto
first convert it to numpy ndarray. If dtype is None, the
conversion tries its best to infer the right numpy data
type. Otherwise, the resulting numpy array has a convertible data
type with the given dtype.
In either case above, the numpy ndarray (either the caller provided
or the auto converted) must have the convertible type with dtype.
make_tensor_proto then converts the numpy array to a tensor proto.
If "shape" is None, the resulting tensor proto represents the numpy
array precisely.
Otherwise, "shape" specifies the tensor's shape and the numpy array
can not have more elements than what "shape" specifies.
"""
if isinstance(values, tensor_pb2.TensorProto):
return values
if dtype:
dtype = dtypes.as_dtype(dtype)
is_quantized = dtype in [
dtypes.qint8,
dtypes.quint8,
dtypes.qint16,
dtypes.quint16,
dtypes.qint32,
]
# We first convert value to a numpy array or scalar.
if isinstance(values, (np.ndarray, np.generic)):
if dtype:
nparray = values.astype(dtype.as_numpy_dtype)
else:
nparray = values
elif callable(getattr(values, "__array__", None)) or isinstance(
getattr(values, "__array_interface__", None), dict
):
# If a class has the __array__ method, or __array_interface__ dict, then it
# is possible to convert to numpy array.
nparray = np.asarray(values, dtype=dtype)
# This is the preferred way to create an array from the object, so replace
# the `values` with the array so that _FlattenToStrings is not run.
values = nparray
else:
if values is None:
raise ValueError("None values not supported.")
# if dtype is provided, forces numpy array to be the type
# provided if possible.
if dtype and dtype.is_numpy_compatible:
np_dt = dtype.as_numpy_dtype
else:
np_dt = None
# If shape is None, numpy.prod returns None when dtype is not set, but raises
# exception when dtype is set to np.int64
if shape is not None and np.prod(shape, dtype=np.int64) == 0:
nparray = np.empty(shape, dtype=np_dt)
else:
_Assertconvertible(values, dtype)
nparray = np.array(values, dtype=np_dt)
# check to them.
# We need to pass in quantized values as tuples, so don't apply the shape
if list(nparray.shape) != _GetDenseDimensions(values) and not is_quantized:
raise ValueError(
"""Argument must be a dense tensor: %s"""
""" - got shape %s, but wanted %s."""
% (values, list(nparray.shape), _GetDenseDimensions(values))
)
# python/numpy default float type is float64. We prefer float32 instead.
if (nparray.dtype == np.float64) and dtype is None:
nparray = nparray.astype(np.float32)
# python/numpy default int type is int64. We prefer int32 instead.
elif (nparray.dtype == np.int64) and dtype is None:
downcasted_array = nparray.astype(np.int32)
# Do not down cast if it leads to precision loss.
if np.array_equal(downcasted_array, nparray):
nparray = downcasted_array
# if dtype is provided, it must be convertible with what numpy
# conversion says.
numpy_dtype = dtypes.as_dtype(nparray.dtype)
if numpy_dtype is None:
raise TypeError("Unrecognized data type: %s" % nparray.dtype)
# If dtype was specified and is a quantized type, we convert
# numpy_dtype back into the quantized version.
if is_quantized:
numpy_dtype = dtype
if dtype is not None and (
not hasattr(dtype, "base_dtype") or dtype.base_dtype != numpy_dtype.base_dtype
):
raise TypeError(
"Inconvertible types: %s vs. %s. Value is %s"
% (dtype, nparray.dtype, values)
)
# If shape is not given, get the shape from the numpy array.
if shape is None:
shape = nparray.shape
is_same_size = True
shape_size = nparray.size
else:
shape = [int(dim) for dim in shape]
shape_size = np.prod(shape, dtype=np.int64)
is_same_size = shape_size == nparray.size
if verify_shape:
if not nparray.shape == tuple(shape):
raise TypeError(
"Expected Tensor's shape: %s, got %s."
% (tuple(shape), nparray.shape)
)
if nparray.size > shape_size:
raise ValueError(
"Too many elements provided. Needed at most %d, but received %d"
% (shape_size, nparray.size)
)
tensor_proto = tensor_pb2.TensorProto(
dtype=numpy_dtype.as_datatype_enum,
tensor_shape=tensor_shape.as_shape(shape).as_proto(),
)
if is_same_size and numpy_dtype in _TENSOR_CONTENT_TYPES and shape_size > 1:
if nparray.size * nparray.itemsize >= (1 << 31):
raise ValueError(
"Cannot create a tensor proto whose content is larger than 2GB."
)
tensor_proto.tensor_content = nparray.tostring()
return tensor_proto
# If we were not given values as a numpy array, compute the proto_values
# from the given values directly, to avoid numpy trimming nulls from the
# strings. Since values could be a list of strings, or a multi-dimensional
# list of lists that might or might not correspond to the given shape,
# we flatten it conservatively.
if numpy_dtype == dtypes.string and not isinstance(values, np.ndarray):
proto_values = _FlattenToStrings(values)
# At this point, values may be a list of objects that we could not
# identify a common type for (hence it was inferred as
# np.object/dtypes.string). If we are unable to convert it to a
# string, we raise a more helpful error message.
#
# Ideally, we'd be able to convert the elements of the list to a
# common type, but this type inference requires some thinking and
# so we defer it for now.
try:
str_values = [compat.as_bytes(x) for x in proto_values]
except TypeError:
raise TypeError(
"Failed to convert object of type %s to Tensor. "
"Contents: %s. Consider casting elements to a "
"supported type." % (type(values), values)
)
tensor_proto.string_val.extend(str_values)
return tensor_proto
# TensorFlow expects C order (a.k.a., eigen row major).
proto_values = nparray.ravel()
append_fn = GetNumpyAppendFn(proto_values.dtype)
if append_fn is None:
raise TypeError(
"Element type not supported in TensorProto: %s" % numpy_dtype.name
)
append_fn(tensor_proto, proto_values)
return tensor_proto | python | def make_tensor_proto(values, dtype=None, shape=None, verify_shape=False):
"""Create a TensorProto.
Args:
values: Values to put in the TensorProto.
dtype: Optional tensor_pb2 DataType value.
shape: List of integers representing the dimensions of tensor.
verify_shape: Boolean that enables verification of a shape of values.
Returns:
A `TensorProto`. Depending on the type, it may contain data in the
"tensor_content" attribute, which is not directly useful to Python programs.
To access the values you should convert the proto back to a numpy ndarray
with `tensor_util.MakeNdarray(proto)`.
If `values` is a `TensorProto`, it is immediately returned; `dtype` and
`shape` are ignored.
Raises:
TypeError: if unsupported types are provided.
ValueError: if arguments have inappropriate values or if verify_shape is
True and shape of values is not equals to a shape from the argument.
make_tensor_proto accepts "values" of a python scalar, a python list, a
numpy ndarray, or a numpy scalar.
If "values" is a python scalar or a python list, make_tensor_proto
first convert it to numpy ndarray. If dtype is None, the
conversion tries its best to infer the right numpy data
type. Otherwise, the resulting numpy array has a convertible data
type with the given dtype.
In either case above, the numpy ndarray (either the caller provided
or the auto converted) must have the convertible type with dtype.
make_tensor_proto then converts the numpy array to a tensor proto.
If "shape" is None, the resulting tensor proto represents the numpy
array precisely.
Otherwise, "shape" specifies the tensor's shape and the numpy array
can not have more elements than what "shape" specifies.
"""
if isinstance(values, tensor_pb2.TensorProto):
return values
if dtype:
dtype = dtypes.as_dtype(dtype)
is_quantized = dtype in [
dtypes.qint8,
dtypes.quint8,
dtypes.qint16,
dtypes.quint16,
dtypes.qint32,
]
# We first convert value to a numpy array or scalar.
if isinstance(values, (np.ndarray, np.generic)):
if dtype:
nparray = values.astype(dtype.as_numpy_dtype)
else:
nparray = values
elif callable(getattr(values, "__array__", None)) or isinstance(
getattr(values, "__array_interface__", None), dict
):
# If a class has the __array__ method, or __array_interface__ dict, then it
# is possible to convert to numpy array.
nparray = np.asarray(values, dtype=dtype)
# This is the preferred way to create an array from the object, so replace
# the `values` with the array so that _FlattenToStrings is not run.
values = nparray
else:
if values is None:
raise ValueError("None values not supported.")
# if dtype is provided, forces numpy array to be the type
# provided if possible.
if dtype and dtype.is_numpy_compatible:
np_dt = dtype.as_numpy_dtype
else:
np_dt = None
# If shape is None, numpy.prod returns None when dtype is not set, but raises
# exception when dtype is set to np.int64
if shape is not None and np.prod(shape, dtype=np.int64) == 0:
nparray = np.empty(shape, dtype=np_dt)
else:
_Assertconvertible(values, dtype)
nparray = np.array(values, dtype=np_dt)
# check to them.
# We need to pass in quantized values as tuples, so don't apply the shape
if list(nparray.shape) != _GetDenseDimensions(values) and not is_quantized:
raise ValueError(
"""Argument must be a dense tensor: %s"""
""" - got shape %s, but wanted %s."""
% (values, list(nparray.shape), _GetDenseDimensions(values))
)
# python/numpy default float type is float64. We prefer float32 instead.
if (nparray.dtype == np.float64) and dtype is None:
nparray = nparray.astype(np.float32)
# python/numpy default int type is int64. We prefer int32 instead.
elif (nparray.dtype == np.int64) and dtype is None:
downcasted_array = nparray.astype(np.int32)
# Do not down cast if it leads to precision loss.
if np.array_equal(downcasted_array, nparray):
nparray = downcasted_array
# if dtype is provided, it must be convertible with what numpy
# conversion says.
numpy_dtype = dtypes.as_dtype(nparray.dtype)
if numpy_dtype is None:
raise TypeError("Unrecognized data type: %s" % nparray.dtype)
# If dtype was specified and is a quantized type, we convert
# numpy_dtype back into the quantized version.
if is_quantized:
numpy_dtype = dtype
if dtype is not None and (
not hasattr(dtype, "base_dtype") or dtype.base_dtype != numpy_dtype.base_dtype
):
raise TypeError(
"Inconvertible types: %s vs. %s. Value is %s"
% (dtype, nparray.dtype, values)
)
# If shape is not given, get the shape from the numpy array.
if shape is None:
shape = nparray.shape
is_same_size = True
shape_size = nparray.size
else:
shape = [int(dim) for dim in shape]
shape_size = np.prod(shape, dtype=np.int64)
is_same_size = shape_size == nparray.size
if verify_shape:
if not nparray.shape == tuple(shape):
raise TypeError(
"Expected Tensor's shape: %s, got %s."
% (tuple(shape), nparray.shape)
)
if nparray.size > shape_size:
raise ValueError(
"Too many elements provided. Needed at most %d, but received %d"
% (shape_size, nparray.size)
)
tensor_proto = tensor_pb2.TensorProto(
dtype=numpy_dtype.as_datatype_enum,
tensor_shape=tensor_shape.as_shape(shape).as_proto(),
)
if is_same_size and numpy_dtype in _TENSOR_CONTENT_TYPES and shape_size > 1:
if nparray.size * nparray.itemsize >= (1 << 31):
raise ValueError(
"Cannot create a tensor proto whose content is larger than 2GB."
)
tensor_proto.tensor_content = nparray.tostring()
return tensor_proto
# If we were not given values as a numpy array, compute the proto_values
# from the given values directly, to avoid numpy trimming nulls from the
# strings. Since values could be a list of strings, or a multi-dimensional
# list of lists that might or might not correspond to the given shape,
# we flatten it conservatively.
if numpy_dtype == dtypes.string and not isinstance(values, np.ndarray):
proto_values = _FlattenToStrings(values)
# At this point, values may be a list of objects that we could not
# identify a common type for (hence it was inferred as
# np.object/dtypes.string). If we are unable to convert it to a
# string, we raise a more helpful error message.
#
# Ideally, we'd be able to convert the elements of the list to a
# common type, but this type inference requires some thinking and
# so we defer it for now.
try:
str_values = [compat.as_bytes(x) for x in proto_values]
except TypeError:
raise TypeError(
"Failed to convert object of type %s to Tensor. "
"Contents: %s. Consider casting elements to a "
"supported type." % (type(values), values)
)
tensor_proto.string_val.extend(str_values)
return tensor_proto
# TensorFlow expects C order (a.k.a., eigen row major).
proto_values = nparray.ravel()
append_fn = GetNumpyAppendFn(proto_values.dtype)
if append_fn is None:
raise TypeError(
"Element type not supported in TensorProto: %s" % numpy_dtype.name
)
append_fn(tensor_proto, proto_values)
return tensor_proto | [
"def",
"make_tensor_proto",
"(",
"values",
",",
"dtype",
"=",
"None",
",",
"shape",
"=",
"None",
",",
"verify_shape",
"=",
"False",
")",
":",
"if",
"isinstance",
"(",
"values",
",",
"tensor_pb2",
".",
"TensorProto",
")",
":",
"return",
"values",
"if",
"dtype",
":",
"dtype",
"=",
"dtypes",
".",
"as_dtype",
"(",
"dtype",
")",
"is_quantized",
"=",
"dtype",
"in",
"[",
"dtypes",
".",
"qint8",
",",
"dtypes",
".",
"quint8",
",",
"dtypes",
".",
"qint16",
",",
"dtypes",
".",
"quint16",
",",
"dtypes",
".",
"qint32",
",",
"]",
"# We first convert value to a numpy array or scalar.",
"if",
"isinstance",
"(",
"values",
",",
"(",
"np",
".",
"ndarray",
",",
"np",
".",
"generic",
")",
")",
":",
"if",
"dtype",
":",
"nparray",
"=",
"values",
".",
"astype",
"(",
"dtype",
".",
"as_numpy_dtype",
")",
"else",
":",
"nparray",
"=",
"values",
"elif",
"callable",
"(",
"getattr",
"(",
"values",
",",
"\"__array__\"",
",",
"None",
")",
")",
"or",
"isinstance",
"(",
"getattr",
"(",
"values",
",",
"\"__array_interface__\"",
",",
"None",
")",
",",
"dict",
")",
":",
"# If a class has the __array__ method, or __array_interface__ dict, then it",
"# is possible to convert to numpy array.",
"nparray",
"=",
"np",
".",
"asarray",
"(",
"values",
",",
"dtype",
"=",
"dtype",
")",
"# This is the preferred way to create an array from the object, so replace",
"# the `values` with the array so that _FlattenToStrings is not run.",
"values",
"=",
"nparray",
"else",
":",
"if",
"values",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"\"None values not supported.\"",
")",
"# if dtype is provided, forces numpy array to be the type",
"# provided if possible.",
"if",
"dtype",
"and",
"dtype",
".",
"is_numpy_compatible",
":",
"np_dt",
"=",
"dtype",
".",
"as_numpy_dtype",
"else",
":",
"np_dt",
"=",
"None",
"# If shape is None, numpy.prod returns None when dtype is not set, but raises",
"# exception when dtype is set to np.int64",
"if",
"shape",
"is",
"not",
"None",
"and",
"np",
".",
"prod",
"(",
"shape",
",",
"dtype",
"=",
"np",
".",
"int64",
")",
"==",
"0",
":",
"nparray",
"=",
"np",
".",
"empty",
"(",
"shape",
",",
"dtype",
"=",
"np_dt",
")",
"else",
":",
"_Assertconvertible",
"(",
"values",
",",
"dtype",
")",
"nparray",
"=",
"np",
".",
"array",
"(",
"values",
",",
"dtype",
"=",
"np_dt",
")",
"# check to them.",
"# We need to pass in quantized values as tuples, so don't apply the shape",
"if",
"list",
"(",
"nparray",
".",
"shape",
")",
"!=",
"_GetDenseDimensions",
"(",
"values",
")",
"and",
"not",
"is_quantized",
":",
"raise",
"ValueError",
"(",
"\"\"\"Argument must be a dense tensor: %s\"\"\"",
"\"\"\" - got shape %s, but wanted %s.\"\"\"",
"%",
"(",
"values",
",",
"list",
"(",
"nparray",
".",
"shape",
")",
",",
"_GetDenseDimensions",
"(",
"values",
")",
")",
")",
"# python/numpy default float type is float64. We prefer float32 instead.",
"if",
"(",
"nparray",
".",
"dtype",
"==",
"np",
".",
"float64",
")",
"and",
"dtype",
"is",
"None",
":",
"nparray",
"=",
"nparray",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"# python/numpy default int type is int64. We prefer int32 instead.",
"elif",
"(",
"nparray",
".",
"dtype",
"==",
"np",
".",
"int64",
")",
"and",
"dtype",
"is",
"None",
":",
"downcasted_array",
"=",
"nparray",
".",
"astype",
"(",
"np",
".",
"int32",
")",
"# Do not down cast if it leads to precision loss.",
"if",
"np",
".",
"array_equal",
"(",
"downcasted_array",
",",
"nparray",
")",
":",
"nparray",
"=",
"downcasted_array",
"# if dtype is provided, it must be convertible with what numpy",
"# conversion says.",
"numpy_dtype",
"=",
"dtypes",
".",
"as_dtype",
"(",
"nparray",
".",
"dtype",
")",
"if",
"numpy_dtype",
"is",
"None",
":",
"raise",
"TypeError",
"(",
"\"Unrecognized data type: %s\"",
"%",
"nparray",
".",
"dtype",
")",
"# If dtype was specified and is a quantized type, we convert",
"# numpy_dtype back into the quantized version.",
"if",
"is_quantized",
":",
"numpy_dtype",
"=",
"dtype",
"if",
"dtype",
"is",
"not",
"None",
"and",
"(",
"not",
"hasattr",
"(",
"dtype",
",",
"\"base_dtype\"",
")",
"or",
"dtype",
".",
"base_dtype",
"!=",
"numpy_dtype",
".",
"base_dtype",
")",
":",
"raise",
"TypeError",
"(",
"\"Inconvertible types: %s vs. %s. Value is %s\"",
"%",
"(",
"dtype",
",",
"nparray",
".",
"dtype",
",",
"values",
")",
")",
"# If shape is not given, get the shape from the numpy array.",
"if",
"shape",
"is",
"None",
":",
"shape",
"=",
"nparray",
".",
"shape",
"is_same_size",
"=",
"True",
"shape_size",
"=",
"nparray",
".",
"size",
"else",
":",
"shape",
"=",
"[",
"int",
"(",
"dim",
")",
"for",
"dim",
"in",
"shape",
"]",
"shape_size",
"=",
"np",
".",
"prod",
"(",
"shape",
",",
"dtype",
"=",
"np",
".",
"int64",
")",
"is_same_size",
"=",
"shape_size",
"==",
"nparray",
".",
"size",
"if",
"verify_shape",
":",
"if",
"not",
"nparray",
".",
"shape",
"==",
"tuple",
"(",
"shape",
")",
":",
"raise",
"TypeError",
"(",
"\"Expected Tensor's shape: %s, got %s.\"",
"%",
"(",
"tuple",
"(",
"shape",
")",
",",
"nparray",
".",
"shape",
")",
")",
"if",
"nparray",
".",
"size",
">",
"shape_size",
":",
"raise",
"ValueError",
"(",
"\"Too many elements provided. Needed at most %d, but received %d\"",
"%",
"(",
"shape_size",
",",
"nparray",
".",
"size",
")",
")",
"tensor_proto",
"=",
"tensor_pb2",
".",
"TensorProto",
"(",
"dtype",
"=",
"numpy_dtype",
".",
"as_datatype_enum",
",",
"tensor_shape",
"=",
"tensor_shape",
".",
"as_shape",
"(",
"shape",
")",
".",
"as_proto",
"(",
")",
",",
")",
"if",
"is_same_size",
"and",
"numpy_dtype",
"in",
"_TENSOR_CONTENT_TYPES",
"and",
"shape_size",
">",
"1",
":",
"if",
"nparray",
".",
"size",
"*",
"nparray",
".",
"itemsize",
">=",
"(",
"1",
"<<",
"31",
")",
":",
"raise",
"ValueError",
"(",
"\"Cannot create a tensor proto whose content is larger than 2GB.\"",
")",
"tensor_proto",
".",
"tensor_content",
"=",
"nparray",
".",
"tostring",
"(",
")",
"return",
"tensor_proto",
"# If we were not given values as a numpy array, compute the proto_values",
"# from the given values directly, to avoid numpy trimming nulls from the",
"# strings. Since values could be a list of strings, or a multi-dimensional",
"# list of lists that might or might not correspond to the given shape,",
"# we flatten it conservatively.",
"if",
"numpy_dtype",
"==",
"dtypes",
".",
"string",
"and",
"not",
"isinstance",
"(",
"values",
",",
"np",
".",
"ndarray",
")",
":",
"proto_values",
"=",
"_FlattenToStrings",
"(",
"values",
")",
"# At this point, values may be a list of objects that we could not",
"# identify a common type for (hence it was inferred as",
"# np.object/dtypes.string). If we are unable to convert it to a",
"# string, we raise a more helpful error message.",
"#",
"# Ideally, we'd be able to convert the elements of the list to a",
"# common type, but this type inference requires some thinking and",
"# so we defer it for now.",
"try",
":",
"str_values",
"=",
"[",
"compat",
".",
"as_bytes",
"(",
"x",
")",
"for",
"x",
"in",
"proto_values",
"]",
"except",
"TypeError",
":",
"raise",
"TypeError",
"(",
"\"Failed to convert object of type %s to Tensor. \"",
"\"Contents: %s. Consider casting elements to a \"",
"\"supported type.\"",
"%",
"(",
"type",
"(",
"values",
")",
",",
"values",
")",
")",
"tensor_proto",
".",
"string_val",
".",
"extend",
"(",
"str_values",
")",
"return",
"tensor_proto",
"# TensorFlow expects C order (a.k.a., eigen row major).",
"proto_values",
"=",
"nparray",
".",
"ravel",
"(",
")",
"append_fn",
"=",
"GetNumpyAppendFn",
"(",
"proto_values",
".",
"dtype",
")",
"if",
"append_fn",
"is",
"None",
":",
"raise",
"TypeError",
"(",
"\"Element type not supported in TensorProto: %s\"",
"%",
"numpy_dtype",
".",
"name",
")",
"append_fn",
"(",
"tensor_proto",
",",
"proto_values",
")",
"return",
"tensor_proto"
] | Create a TensorProto.
Args:
values: Values to put in the TensorProto.
dtype: Optional tensor_pb2 DataType value.
shape: List of integers representing the dimensions of tensor.
verify_shape: Boolean that enables verification of a shape of values.
Returns:
A `TensorProto`. Depending on the type, it may contain data in the
"tensor_content" attribute, which is not directly useful to Python programs.
To access the values you should convert the proto back to a numpy ndarray
with `tensor_util.MakeNdarray(proto)`.
If `values` is a `TensorProto`, it is immediately returned; `dtype` and
`shape` are ignored.
Raises:
TypeError: if unsupported types are provided.
ValueError: if arguments have inappropriate values or if verify_shape is
True and shape of values is not equals to a shape from the argument.
make_tensor_proto accepts "values" of a python scalar, a python list, a
numpy ndarray, or a numpy scalar.
If "values" is a python scalar or a python list, make_tensor_proto
first convert it to numpy ndarray. If dtype is None, the
conversion tries its best to infer the right numpy data
type. Otherwise, the resulting numpy array has a convertible data
type with the given dtype.
In either case above, the numpy ndarray (either the caller provided
or the auto converted) must have the convertible type with dtype.
make_tensor_proto then converts the numpy array to a tensor proto.
If "shape" is None, the resulting tensor proto represents the numpy
array precisely.
Otherwise, "shape" specifies the tensor's shape and the numpy array
can not have more elements than what "shape" specifies. | [
"Create",
"a",
"TensorProto",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/util/tensor_util.py#L280-L480 | train |
tensorflow/tensorboard | tensorboard/util/tensor_util.py | make_ndarray | def make_ndarray(tensor):
"""Create a numpy ndarray from a tensor.
Create a numpy ndarray with the same shape and data as the tensor.
Args:
tensor: A TensorProto.
Returns:
A numpy array with the tensor contents.
Raises:
TypeError: if tensor has unsupported type.
"""
shape = [d.size for d in tensor.tensor_shape.dim]
num_elements = np.prod(shape, dtype=np.int64)
tensor_dtype = dtypes.as_dtype(tensor.dtype)
dtype = tensor_dtype.as_numpy_dtype
if tensor.tensor_content:
return np.frombuffer(tensor.tensor_content, dtype=dtype).copy().reshape(shape)
elif tensor_dtype == dtypes.float16 or tensor_dtype == dtypes.bfloat16:
# the half_val field of the TensorProto stores the binary representation
# of the fp16: we need to reinterpret this as a proper float16
if len(tensor.half_val) == 1:
tmp = np.array(tensor.half_val[0], dtype=np.uint16)
tmp.dtype = tensor_dtype.as_numpy_dtype
return np.repeat(tmp, num_elements).reshape(shape)
else:
tmp = np.fromiter(tensor.half_val, dtype=np.uint16)
tmp.dtype = tensor_dtype.as_numpy_dtype
return tmp.reshape(shape)
elif tensor_dtype == dtypes.float32:
if len(tensor.float_val) == 1:
return np.repeat(
np.array(tensor.float_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.fromiter(tensor.float_val, dtype=dtype).reshape(shape)
elif tensor_dtype == dtypes.float64:
if len(tensor.double_val) == 1:
return np.repeat(
np.array(tensor.double_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.fromiter(tensor.double_val, dtype=dtype).reshape(shape)
elif tensor_dtype in [
dtypes.int32,
dtypes.uint8,
dtypes.uint16,
dtypes.int16,
dtypes.int8,
dtypes.qint32,
dtypes.quint8,
dtypes.qint8,
dtypes.qint16,
dtypes.quint16,
]:
if len(tensor.int_val) == 1:
return np.repeat(
np.array(tensor.int_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.fromiter(tensor.int_val, dtype=dtype).reshape(shape)
elif tensor_dtype == dtypes.int64:
if len(tensor.int64_val) == 1:
return np.repeat(
np.array(tensor.int64_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.fromiter(tensor.int64_val, dtype=dtype).reshape(shape)
elif tensor_dtype == dtypes.string:
if len(tensor.string_val) == 1:
return np.repeat(
np.array(tensor.string_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.array([x for x in tensor.string_val], dtype=dtype).reshape(shape)
elif tensor_dtype == dtypes.complex64:
it = iter(tensor.scomplex_val)
if len(tensor.scomplex_val) == 2:
return np.repeat(
np.array(
complex(tensor.scomplex_val[0], tensor.scomplex_val[1]), dtype=dtype
),
num_elements,
).reshape(shape)
else:
return np.array(
[complex(x[0], x[1]) for x in zip(it, it)], dtype=dtype
).reshape(shape)
elif tensor_dtype == dtypes.complex128:
it = iter(tensor.dcomplex_val)
if len(tensor.dcomplex_val) == 2:
return np.repeat(
np.array(
complex(tensor.dcomplex_val[0], tensor.dcomplex_val[1]), dtype=dtype
),
num_elements,
).reshape(shape)
else:
return np.array(
[complex(x[0], x[1]) for x in zip(it, it)], dtype=dtype
).reshape(shape)
elif tensor_dtype == dtypes.bool:
if len(tensor.bool_val) == 1:
return np.repeat(
np.array(tensor.bool_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.fromiter(tensor.bool_val, dtype=dtype).reshape(shape)
else:
raise TypeError("Unsupported tensor type: %s" % tensor.dtype) | python | def make_ndarray(tensor):
"""Create a numpy ndarray from a tensor.
Create a numpy ndarray with the same shape and data as the tensor.
Args:
tensor: A TensorProto.
Returns:
A numpy array with the tensor contents.
Raises:
TypeError: if tensor has unsupported type.
"""
shape = [d.size for d in tensor.tensor_shape.dim]
num_elements = np.prod(shape, dtype=np.int64)
tensor_dtype = dtypes.as_dtype(tensor.dtype)
dtype = tensor_dtype.as_numpy_dtype
if tensor.tensor_content:
return np.frombuffer(tensor.tensor_content, dtype=dtype).copy().reshape(shape)
elif tensor_dtype == dtypes.float16 or tensor_dtype == dtypes.bfloat16:
# the half_val field of the TensorProto stores the binary representation
# of the fp16: we need to reinterpret this as a proper float16
if len(tensor.half_val) == 1:
tmp = np.array(tensor.half_val[0], dtype=np.uint16)
tmp.dtype = tensor_dtype.as_numpy_dtype
return np.repeat(tmp, num_elements).reshape(shape)
else:
tmp = np.fromiter(tensor.half_val, dtype=np.uint16)
tmp.dtype = tensor_dtype.as_numpy_dtype
return tmp.reshape(shape)
elif tensor_dtype == dtypes.float32:
if len(tensor.float_val) == 1:
return np.repeat(
np.array(tensor.float_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.fromiter(tensor.float_val, dtype=dtype).reshape(shape)
elif tensor_dtype == dtypes.float64:
if len(tensor.double_val) == 1:
return np.repeat(
np.array(tensor.double_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.fromiter(tensor.double_val, dtype=dtype).reshape(shape)
elif tensor_dtype in [
dtypes.int32,
dtypes.uint8,
dtypes.uint16,
dtypes.int16,
dtypes.int8,
dtypes.qint32,
dtypes.quint8,
dtypes.qint8,
dtypes.qint16,
dtypes.quint16,
]:
if len(tensor.int_val) == 1:
return np.repeat(
np.array(tensor.int_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.fromiter(tensor.int_val, dtype=dtype).reshape(shape)
elif tensor_dtype == dtypes.int64:
if len(tensor.int64_val) == 1:
return np.repeat(
np.array(tensor.int64_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.fromiter(tensor.int64_val, dtype=dtype).reshape(shape)
elif tensor_dtype == dtypes.string:
if len(tensor.string_val) == 1:
return np.repeat(
np.array(tensor.string_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.array([x for x in tensor.string_val], dtype=dtype).reshape(shape)
elif tensor_dtype == dtypes.complex64:
it = iter(tensor.scomplex_val)
if len(tensor.scomplex_val) == 2:
return np.repeat(
np.array(
complex(tensor.scomplex_val[0], tensor.scomplex_val[1]), dtype=dtype
),
num_elements,
).reshape(shape)
else:
return np.array(
[complex(x[0], x[1]) for x in zip(it, it)], dtype=dtype
).reshape(shape)
elif tensor_dtype == dtypes.complex128:
it = iter(tensor.dcomplex_val)
if len(tensor.dcomplex_val) == 2:
return np.repeat(
np.array(
complex(tensor.dcomplex_val[0], tensor.dcomplex_val[1]), dtype=dtype
),
num_elements,
).reshape(shape)
else:
return np.array(
[complex(x[0], x[1]) for x in zip(it, it)], dtype=dtype
).reshape(shape)
elif tensor_dtype == dtypes.bool:
if len(tensor.bool_val) == 1:
return np.repeat(
np.array(tensor.bool_val[0], dtype=dtype), num_elements
).reshape(shape)
else:
return np.fromiter(tensor.bool_val, dtype=dtype).reshape(shape)
else:
raise TypeError("Unsupported tensor type: %s" % tensor.dtype) | [
"def",
"make_ndarray",
"(",
"tensor",
")",
":",
"shape",
"=",
"[",
"d",
".",
"size",
"for",
"d",
"in",
"tensor",
".",
"tensor_shape",
".",
"dim",
"]",
"num_elements",
"=",
"np",
".",
"prod",
"(",
"shape",
",",
"dtype",
"=",
"np",
".",
"int64",
")",
"tensor_dtype",
"=",
"dtypes",
".",
"as_dtype",
"(",
"tensor",
".",
"dtype",
")",
"dtype",
"=",
"tensor_dtype",
".",
"as_numpy_dtype",
"if",
"tensor",
".",
"tensor_content",
":",
"return",
"np",
".",
"frombuffer",
"(",
"tensor",
".",
"tensor_content",
",",
"dtype",
"=",
"dtype",
")",
".",
"copy",
"(",
")",
".",
"reshape",
"(",
"shape",
")",
"elif",
"tensor_dtype",
"==",
"dtypes",
".",
"float16",
"or",
"tensor_dtype",
"==",
"dtypes",
".",
"bfloat16",
":",
"# the half_val field of the TensorProto stores the binary representation",
"# of the fp16: we need to reinterpret this as a proper float16",
"if",
"len",
"(",
"tensor",
".",
"half_val",
")",
"==",
"1",
":",
"tmp",
"=",
"np",
".",
"array",
"(",
"tensor",
".",
"half_val",
"[",
"0",
"]",
",",
"dtype",
"=",
"np",
".",
"uint16",
")",
"tmp",
".",
"dtype",
"=",
"tensor_dtype",
".",
"as_numpy_dtype",
"return",
"np",
".",
"repeat",
"(",
"tmp",
",",
"num_elements",
")",
".",
"reshape",
"(",
"shape",
")",
"else",
":",
"tmp",
"=",
"np",
".",
"fromiter",
"(",
"tensor",
".",
"half_val",
",",
"dtype",
"=",
"np",
".",
"uint16",
")",
"tmp",
".",
"dtype",
"=",
"tensor_dtype",
".",
"as_numpy_dtype",
"return",
"tmp",
".",
"reshape",
"(",
"shape",
")",
"elif",
"tensor_dtype",
"==",
"dtypes",
".",
"float32",
":",
"if",
"len",
"(",
"tensor",
".",
"float_val",
")",
"==",
"1",
":",
"return",
"np",
".",
"repeat",
"(",
"np",
".",
"array",
"(",
"tensor",
".",
"float_val",
"[",
"0",
"]",
",",
"dtype",
"=",
"dtype",
")",
",",
"num_elements",
")",
".",
"reshape",
"(",
"shape",
")",
"else",
":",
"return",
"np",
".",
"fromiter",
"(",
"tensor",
".",
"float_val",
",",
"dtype",
"=",
"dtype",
")",
".",
"reshape",
"(",
"shape",
")",
"elif",
"tensor_dtype",
"==",
"dtypes",
".",
"float64",
":",
"if",
"len",
"(",
"tensor",
".",
"double_val",
")",
"==",
"1",
":",
"return",
"np",
".",
"repeat",
"(",
"np",
".",
"array",
"(",
"tensor",
".",
"double_val",
"[",
"0",
"]",
",",
"dtype",
"=",
"dtype",
")",
",",
"num_elements",
")",
".",
"reshape",
"(",
"shape",
")",
"else",
":",
"return",
"np",
".",
"fromiter",
"(",
"tensor",
".",
"double_val",
",",
"dtype",
"=",
"dtype",
")",
".",
"reshape",
"(",
"shape",
")",
"elif",
"tensor_dtype",
"in",
"[",
"dtypes",
".",
"int32",
",",
"dtypes",
".",
"uint8",
",",
"dtypes",
".",
"uint16",
",",
"dtypes",
".",
"int16",
",",
"dtypes",
".",
"int8",
",",
"dtypes",
".",
"qint32",
",",
"dtypes",
".",
"quint8",
",",
"dtypes",
".",
"qint8",
",",
"dtypes",
".",
"qint16",
",",
"dtypes",
".",
"quint16",
",",
"]",
":",
"if",
"len",
"(",
"tensor",
".",
"int_val",
")",
"==",
"1",
":",
"return",
"np",
".",
"repeat",
"(",
"np",
".",
"array",
"(",
"tensor",
".",
"int_val",
"[",
"0",
"]",
",",
"dtype",
"=",
"dtype",
")",
",",
"num_elements",
")",
".",
"reshape",
"(",
"shape",
")",
"else",
":",
"return",
"np",
".",
"fromiter",
"(",
"tensor",
".",
"int_val",
",",
"dtype",
"=",
"dtype",
")",
".",
"reshape",
"(",
"shape",
")",
"elif",
"tensor_dtype",
"==",
"dtypes",
".",
"int64",
":",
"if",
"len",
"(",
"tensor",
".",
"int64_val",
")",
"==",
"1",
":",
"return",
"np",
".",
"repeat",
"(",
"np",
".",
"array",
"(",
"tensor",
".",
"int64_val",
"[",
"0",
"]",
",",
"dtype",
"=",
"dtype",
")",
",",
"num_elements",
")",
".",
"reshape",
"(",
"shape",
")",
"else",
":",
"return",
"np",
".",
"fromiter",
"(",
"tensor",
".",
"int64_val",
",",
"dtype",
"=",
"dtype",
")",
".",
"reshape",
"(",
"shape",
")",
"elif",
"tensor_dtype",
"==",
"dtypes",
".",
"string",
":",
"if",
"len",
"(",
"tensor",
".",
"string_val",
")",
"==",
"1",
":",
"return",
"np",
".",
"repeat",
"(",
"np",
".",
"array",
"(",
"tensor",
".",
"string_val",
"[",
"0",
"]",
",",
"dtype",
"=",
"dtype",
")",
",",
"num_elements",
")",
".",
"reshape",
"(",
"shape",
")",
"else",
":",
"return",
"np",
".",
"array",
"(",
"[",
"x",
"for",
"x",
"in",
"tensor",
".",
"string_val",
"]",
",",
"dtype",
"=",
"dtype",
")",
".",
"reshape",
"(",
"shape",
")",
"elif",
"tensor_dtype",
"==",
"dtypes",
".",
"complex64",
":",
"it",
"=",
"iter",
"(",
"tensor",
".",
"scomplex_val",
")",
"if",
"len",
"(",
"tensor",
".",
"scomplex_val",
")",
"==",
"2",
":",
"return",
"np",
".",
"repeat",
"(",
"np",
".",
"array",
"(",
"complex",
"(",
"tensor",
".",
"scomplex_val",
"[",
"0",
"]",
",",
"tensor",
".",
"scomplex_val",
"[",
"1",
"]",
")",
",",
"dtype",
"=",
"dtype",
")",
",",
"num_elements",
",",
")",
".",
"reshape",
"(",
"shape",
")",
"else",
":",
"return",
"np",
".",
"array",
"(",
"[",
"complex",
"(",
"x",
"[",
"0",
"]",
",",
"x",
"[",
"1",
"]",
")",
"for",
"x",
"in",
"zip",
"(",
"it",
",",
"it",
")",
"]",
",",
"dtype",
"=",
"dtype",
")",
".",
"reshape",
"(",
"shape",
")",
"elif",
"tensor_dtype",
"==",
"dtypes",
".",
"complex128",
":",
"it",
"=",
"iter",
"(",
"tensor",
".",
"dcomplex_val",
")",
"if",
"len",
"(",
"tensor",
".",
"dcomplex_val",
")",
"==",
"2",
":",
"return",
"np",
".",
"repeat",
"(",
"np",
".",
"array",
"(",
"complex",
"(",
"tensor",
".",
"dcomplex_val",
"[",
"0",
"]",
",",
"tensor",
".",
"dcomplex_val",
"[",
"1",
"]",
")",
",",
"dtype",
"=",
"dtype",
")",
",",
"num_elements",
",",
")",
".",
"reshape",
"(",
"shape",
")",
"else",
":",
"return",
"np",
".",
"array",
"(",
"[",
"complex",
"(",
"x",
"[",
"0",
"]",
",",
"x",
"[",
"1",
"]",
")",
"for",
"x",
"in",
"zip",
"(",
"it",
",",
"it",
")",
"]",
",",
"dtype",
"=",
"dtype",
")",
".",
"reshape",
"(",
"shape",
")",
"elif",
"tensor_dtype",
"==",
"dtypes",
".",
"bool",
":",
"if",
"len",
"(",
"tensor",
".",
"bool_val",
")",
"==",
"1",
":",
"return",
"np",
".",
"repeat",
"(",
"np",
".",
"array",
"(",
"tensor",
".",
"bool_val",
"[",
"0",
"]",
",",
"dtype",
"=",
"dtype",
")",
",",
"num_elements",
")",
".",
"reshape",
"(",
"shape",
")",
"else",
":",
"return",
"np",
".",
"fromiter",
"(",
"tensor",
".",
"bool_val",
",",
"dtype",
"=",
"dtype",
")",
".",
"reshape",
"(",
"shape",
")",
"else",
":",
"raise",
"TypeError",
"(",
"\"Unsupported tensor type: %s\"",
"%",
"tensor",
".",
"dtype",
")"
] | Create a numpy ndarray from a tensor.
Create a numpy ndarray with the same shape and data as the tensor.
Args:
tensor: A TensorProto.
Returns:
A numpy array with the tensor contents.
Raises:
TypeError: if tensor has unsupported type. | [
"Create",
"a",
"numpy",
"ndarray",
"from",
"a",
"tensor",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/util/tensor_util.py#L483-L596 | train |
tensorflow/tensorboard | tensorboard/plugins/custom_scalar/summary.py | op | def op(scalars_layout, collections=None):
"""Creates a summary that contains a layout.
When users navigate to the custom scalars dashboard, they will see a layout
based on the proto provided to this function.
Args:
scalars_layout: The scalars_layout_pb2.Layout proto that specifies the
layout.
collections: Optional list of graph collections keys. The new
summary op is added to these collections. Defaults to
`[Graph Keys.SUMMARIES]`.
Returns:
A tensor summary op that writes the layout to disk.
"""
# TODO(nickfelt): remove on-demand imports once dep situation is fixed.
import tensorflow.compat.v1 as tf
assert isinstance(scalars_layout, layout_pb2.Layout)
summary_metadata = metadata.create_summary_metadata()
return tf.summary.tensor_summary(name=metadata.CONFIG_SUMMARY_TAG,
tensor=tf.constant(
scalars_layout.SerializeToString(),
dtype=tf.string),
collections=collections,
summary_metadata=summary_metadata) | python | def op(scalars_layout, collections=None):
"""Creates a summary that contains a layout.
When users navigate to the custom scalars dashboard, they will see a layout
based on the proto provided to this function.
Args:
scalars_layout: The scalars_layout_pb2.Layout proto that specifies the
layout.
collections: Optional list of graph collections keys. The new
summary op is added to these collections. Defaults to
`[Graph Keys.SUMMARIES]`.
Returns:
A tensor summary op that writes the layout to disk.
"""
# TODO(nickfelt): remove on-demand imports once dep situation is fixed.
import tensorflow.compat.v1 as tf
assert isinstance(scalars_layout, layout_pb2.Layout)
summary_metadata = metadata.create_summary_metadata()
return tf.summary.tensor_summary(name=metadata.CONFIG_SUMMARY_TAG,
tensor=tf.constant(
scalars_layout.SerializeToString(),
dtype=tf.string),
collections=collections,
summary_metadata=summary_metadata) | [
"def",
"op",
"(",
"scalars_layout",
",",
"collections",
"=",
"None",
")",
":",
"# TODO(nickfelt): remove on-demand imports once dep situation is fixed.",
"import",
"tensorflow",
".",
"compat",
".",
"v1",
"as",
"tf",
"assert",
"isinstance",
"(",
"scalars_layout",
",",
"layout_pb2",
".",
"Layout",
")",
"summary_metadata",
"=",
"metadata",
".",
"create_summary_metadata",
"(",
")",
"return",
"tf",
".",
"summary",
".",
"tensor_summary",
"(",
"name",
"=",
"metadata",
".",
"CONFIG_SUMMARY_TAG",
",",
"tensor",
"=",
"tf",
".",
"constant",
"(",
"scalars_layout",
".",
"SerializeToString",
"(",
")",
",",
"dtype",
"=",
"tf",
".",
"string",
")",
",",
"collections",
"=",
"collections",
",",
"summary_metadata",
"=",
"summary_metadata",
")"
] | Creates a summary that contains a layout.
When users navigate to the custom scalars dashboard, they will see a layout
based on the proto provided to this function.
Args:
scalars_layout: The scalars_layout_pb2.Layout proto that specifies the
layout.
collections: Optional list of graph collections keys. The new
summary op is added to these collections. Defaults to
`[Graph Keys.SUMMARIES]`.
Returns:
A tensor summary op that writes the layout to disk. | [
"Creates",
"a",
"summary",
"that",
"contains",
"a",
"layout",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/custom_scalar/summary.py#L27-L53 | train |
tensorflow/tensorboard | tensorboard/plugins/custom_scalar/summary.py | pb | def pb(scalars_layout):
"""Creates a summary that contains a layout.
When users navigate to the custom scalars dashboard, they will see a layout
based on the proto provided to this function.
Args:
scalars_layout: The scalars_layout_pb2.Layout proto that specifies the
layout.
Returns:
A summary proto containing the layout.
"""
# TODO(nickfelt): remove on-demand imports once dep situation is fixed.
import tensorflow.compat.v1 as tf
assert isinstance(scalars_layout, layout_pb2.Layout)
tensor = tf.make_tensor_proto(
scalars_layout.SerializeToString(), dtype=tf.string)
tf_summary_metadata = tf.SummaryMetadata.FromString(
metadata.create_summary_metadata().SerializeToString())
summary = tf.Summary()
summary.value.add(tag=metadata.CONFIG_SUMMARY_TAG,
metadata=tf_summary_metadata,
tensor=tensor)
return summary | python | def pb(scalars_layout):
"""Creates a summary that contains a layout.
When users navigate to the custom scalars dashboard, they will see a layout
based on the proto provided to this function.
Args:
scalars_layout: The scalars_layout_pb2.Layout proto that specifies the
layout.
Returns:
A summary proto containing the layout.
"""
# TODO(nickfelt): remove on-demand imports once dep situation is fixed.
import tensorflow.compat.v1 as tf
assert isinstance(scalars_layout, layout_pb2.Layout)
tensor = tf.make_tensor_proto(
scalars_layout.SerializeToString(), dtype=tf.string)
tf_summary_metadata = tf.SummaryMetadata.FromString(
metadata.create_summary_metadata().SerializeToString())
summary = tf.Summary()
summary.value.add(tag=metadata.CONFIG_SUMMARY_TAG,
metadata=tf_summary_metadata,
tensor=tensor)
return summary | [
"def",
"pb",
"(",
"scalars_layout",
")",
":",
"# TODO(nickfelt): remove on-demand imports once dep situation is fixed.",
"import",
"tensorflow",
".",
"compat",
".",
"v1",
"as",
"tf",
"assert",
"isinstance",
"(",
"scalars_layout",
",",
"layout_pb2",
".",
"Layout",
")",
"tensor",
"=",
"tf",
".",
"make_tensor_proto",
"(",
"scalars_layout",
".",
"SerializeToString",
"(",
")",
",",
"dtype",
"=",
"tf",
".",
"string",
")",
"tf_summary_metadata",
"=",
"tf",
".",
"SummaryMetadata",
".",
"FromString",
"(",
"metadata",
".",
"create_summary_metadata",
"(",
")",
".",
"SerializeToString",
"(",
")",
")",
"summary",
"=",
"tf",
".",
"Summary",
"(",
")",
"summary",
".",
"value",
".",
"add",
"(",
"tag",
"=",
"metadata",
".",
"CONFIG_SUMMARY_TAG",
",",
"metadata",
"=",
"tf_summary_metadata",
",",
"tensor",
"=",
"tensor",
")",
"return",
"summary"
] | Creates a summary that contains a layout.
When users navigate to the custom scalars dashboard, they will see a layout
based on the proto provided to this function.
Args:
scalars_layout: The scalars_layout_pb2.Layout proto that specifies the
layout.
Returns:
A summary proto containing the layout. | [
"Creates",
"a",
"summary",
"that",
"contains",
"a",
"layout",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/custom_scalar/summary.py#L56-L81 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | Dimension.is_convertible_with | def is_convertible_with(self, other):
"""Returns true if `other` is convertible with this Dimension.
Two known Dimensions are convertible if they have the same value.
An unknown Dimension is convertible with all other Dimensions.
Args:
other: Another Dimension.
Returns:
True if this Dimension and `other` are convertible.
"""
other = as_dimension(other)
return self._value is None or other.value is None or self._value == other.value | python | def is_convertible_with(self, other):
"""Returns true if `other` is convertible with this Dimension.
Two known Dimensions are convertible if they have the same value.
An unknown Dimension is convertible with all other Dimensions.
Args:
other: Another Dimension.
Returns:
True if this Dimension and `other` are convertible.
"""
other = as_dimension(other)
return self._value is None or other.value is None or self._value == other.value | [
"def",
"is_convertible_with",
"(",
"self",
",",
"other",
")",
":",
"other",
"=",
"as_dimension",
"(",
"other",
")",
"return",
"self",
".",
"_value",
"is",
"None",
"or",
"other",
".",
"value",
"is",
"None",
"or",
"self",
".",
"_value",
"==",
"other",
".",
"value"
] | Returns true if `other` is convertible with this Dimension.
Two known Dimensions are convertible if they have the same value.
An unknown Dimension is convertible with all other Dimensions.
Args:
other: Another Dimension.
Returns:
True if this Dimension and `other` are convertible. | [
"Returns",
"true",
"if",
"other",
"is",
"convertible",
"with",
"this",
"Dimension",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L88-L101 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | Dimension.merge_with | def merge_with(self, other):
"""Returns a Dimension that combines the information in `self` and `other`.
Dimensions are combined as follows:
```python
tf.Dimension(n) .merge_with(tf.Dimension(n)) == tf.Dimension(n)
tf.Dimension(n) .merge_with(tf.Dimension(None)) == tf.Dimension(n)
tf.Dimension(None).merge_with(tf.Dimension(n)) == tf.Dimension(n)
tf.Dimension(None).merge_with(tf.Dimension(None)) == tf.Dimension(None)
tf.Dimension(n) .merge_with(tf.Dimension(m)) # raises ValueError for n != m
```
Args:
other: Another Dimension.
Returns:
A Dimension containing the combined information of `self` and
`other`.
Raises:
ValueError: If `self` and `other` are not convertible (see
is_convertible_with).
"""
other = as_dimension(other)
self.assert_is_convertible_with(other)
if self._value is None:
return Dimension(other.value)
else:
return Dimension(self._value) | python | def merge_with(self, other):
"""Returns a Dimension that combines the information in `self` and `other`.
Dimensions are combined as follows:
```python
tf.Dimension(n) .merge_with(tf.Dimension(n)) == tf.Dimension(n)
tf.Dimension(n) .merge_with(tf.Dimension(None)) == tf.Dimension(n)
tf.Dimension(None).merge_with(tf.Dimension(n)) == tf.Dimension(n)
tf.Dimension(None).merge_with(tf.Dimension(None)) == tf.Dimension(None)
tf.Dimension(n) .merge_with(tf.Dimension(m)) # raises ValueError for n != m
```
Args:
other: Another Dimension.
Returns:
A Dimension containing the combined information of `self` and
`other`.
Raises:
ValueError: If `self` and `other` are not convertible (see
is_convertible_with).
"""
other = as_dimension(other)
self.assert_is_convertible_with(other)
if self._value is None:
return Dimension(other.value)
else:
return Dimension(self._value) | [
"def",
"merge_with",
"(",
"self",
",",
"other",
")",
":",
"other",
"=",
"as_dimension",
"(",
"other",
")",
"self",
".",
"assert_is_convertible_with",
"(",
"other",
")",
"if",
"self",
".",
"_value",
"is",
"None",
":",
"return",
"Dimension",
"(",
"other",
".",
"value",
")",
"else",
":",
"return",
"Dimension",
"(",
"self",
".",
"_value",
")"
] | Returns a Dimension that combines the information in `self` and `other`.
Dimensions are combined as follows:
```python
tf.Dimension(n) .merge_with(tf.Dimension(n)) == tf.Dimension(n)
tf.Dimension(n) .merge_with(tf.Dimension(None)) == tf.Dimension(n)
tf.Dimension(None).merge_with(tf.Dimension(n)) == tf.Dimension(n)
tf.Dimension(None).merge_with(tf.Dimension(None)) == tf.Dimension(None)
tf.Dimension(n) .merge_with(tf.Dimension(m)) # raises ValueError for n != m
```
Args:
other: Another Dimension.
Returns:
A Dimension containing the combined information of `self` and
`other`.
Raises:
ValueError: If `self` and `other` are not convertible (see
is_convertible_with). | [
"Returns",
"a",
"Dimension",
"that",
"combines",
"the",
"information",
"in",
"self",
"and",
"other",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L116-L145 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.ndims | def ndims(self):
"""Returns the rank of this shape, or None if it is unspecified."""
if self._dims is None:
return None
else:
if self._ndims is None:
self._ndims = len(self._dims)
return self._ndims | python | def ndims(self):
"""Returns the rank of this shape, or None if it is unspecified."""
if self._dims is None:
return None
else:
if self._ndims is None:
self._ndims = len(self._dims)
return self._ndims | [
"def",
"ndims",
"(",
"self",
")",
":",
"if",
"self",
".",
"_dims",
"is",
"None",
":",
"return",
"None",
"else",
":",
"if",
"self",
".",
"_ndims",
"is",
"None",
":",
"self",
".",
"_ndims",
"=",
"len",
"(",
"self",
".",
"_dims",
")",
"return",
"self",
".",
"_ndims"
] | Returns the rank of this shape, or None if it is unspecified. | [
"Returns",
"the",
"rank",
"of",
"this",
"shape",
"or",
"None",
"if",
"it",
"is",
"unspecified",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L566-L573 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.num_elements | def num_elements(self):
"""Returns the total number of elements, or none for incomplete shapes."""
if self.is_fully_defined():
size = 1
for dim in self._dims:
size *= dim.value
return size
else:
return None | python | def num_elements(self):
"""Returns the total number of elements, or none for incomplete shapes."""
if self.is_fully_defined():
size = 1
for dim in self._dims:
size *= dim.value
return size
else:
return None | [
"def",
"num_elements",
"(",
"self",
")",
":",
"if",
"self",
".",
"is_fully_defined",
"(",
")",
":",
"size",
"=",
"1",
"for",
"dim",
"in",
"self",
".",
"_dims",
":",
"size",
"*=",
"dim",
".",
"value",
"return",
"size",
"else",
":",
"return",
"None"
] | Returns the total number of elements, or none for incomplete shapes. | [
"Returns",
"the",
"total",
"number",
"of",
"elements",
"or",
"none",
"for",
"incomplete",
"shapes",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L639-L647 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.merge_with | def merge_with(self, other):
"""Returns a `TensorShape` combining the information in `self` and `other`.
The dimensions in `self` and `other` are merged elementwise,
according to the rules defined for `Dimension.merge_with()`.
Args:
other: Another `TensorShape`.
Returns:
A `TensorShape` containing the combined information of `self` and
`other`.
Raises:
ValueError: If `self` and `other` are not convertible.
"""
other = as_shape(other)
if self._dims is None:
return other
else:
try:
self.assert_same_rank(other)
new_dims = []
for i, dim in enumerate(self._dims):
new_dims.append(dim.merge_with(other[i]))
return TensorShape(new_dims)
except ValueError:
raise ValueError("Shapes %s and %s are not convertible" % (self, other)) | python | def merge_with(self, other):
"""Returns a `TensorShape` combining the information in `self` and `other`.
The dimensions in `self` and `other` are merged elementwise,
according to the rules defined for `Dimension.merge_with()`.
Args:
other: Another `TensorShape`.
Returns:
A `TensorShape` containing the combined information of `self` and
`other`.
Raises:
ValueError: If `self` and `other` are not convertible.
"""
other = as_shape(other)
if self._dims is None:
return other
else:
try:
self.assert_same_rank(other)
new_dims = []
for i, dim in enumerate(self._dims):
new_dims.append(dim.merge_with(other[i]))
return TensorShape(new_dims)
except ValueError:
raise ValueError("Shapes %s and %s are not convertible" % (self, other)) | [
"def",
"merge_with",
"(",
"self",
",",
"other",
")",
":",
"other",
"=",
"as_shape",
"(",
"other",
")",
"if",
"self",
".",
"_dims",
"is",
"None",
":",
"return",
"other",
"else",
":",
"try",
":",
"self",
".",
"assert_same_rank",
"(",
"other",
")",
"new_dims",
"=",
"[",
"]",
"for",
"i",
",",
"dim",
"in",
"enumerate",
"(",
"self",
".",
"_dims",
")",
":",
"new_dims",
".",
"append",
"(",
"dim",
".",
"merge_with",
"(",
"other",
"[",
"i",
"]",
")",
")",
"return",
"TensorShape",
"(",
"new_dims",
")",
"except",
"ValueError",
":",
"raise",
"ValueError",
"(",
"\"Shapes %s and %s are not convertible\"",
"%",
"(",
"self",
",",
"other",
")",
")"
] | Returns a `TensorShape` combining the information in `self` and `other`.
The dimensions in `self` and `other` are merged elementwise,
according to the rules defined for `Dimension.merge_with()`.
Args:
other: Another `TensorShape`.
Returns:
A `TensorShape` containing the combined information of `self` and
`other`.
Raises:
ValueError: If `self` and `other` are not convertible. | [
"Returns",
"a",
"TensorShape",
"combining",
"the",
"information",
"in",
"self",
"and",
"other",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L649-L676 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.concatenate | def concatenate(self, other):
"""Returns the concatenation of the dimension in `self` and `other`.
*N.B.* If either `self` or `other` is completely unknown,
concatenation will discard information about the other shape. In
future, we might support concatenation that preserves this
information for use with slicing.
Args:
other: Another `TensorShape`.
Returns:
A `TensorShape` whose dimensions are the concatenation of the
dimensions in `self` and `other`.
"""
# TODO(mrry): Handle the case where we concatenate a known shape with a
# completely unknown shape, so that we can use the partial information.
other = as_shape(other)
if self._dims is None or other.dims is None:
return unknown_shape()
else:
return TensorShape(self._dims + other.dims) | python | def concatenate(self, other):
"""Returns the concatenation of the dimension in `self` and `other`.
*N.B.* If either `self` or `other` is completely unknown,
concatenation will discard information about the other shape. In
future, we might support concatenation that preserves this
information for use with slicing.
Args:
other: Another `TensorShape`.
Returns:
A `TensorShape` whose dimensions are the concatenation of the
dimensions in `self` and `other`.
"""
# TODO(mrry): Handle the case where we concatenate a known shape with a
# completely unknown shape, so that we can use the partial information.
other = as_shape(other)
if self._dims is None or other.dims is None:
return unknown_shape()
else:
return TensorShape(self._dims + other.dims) | [
"def",
"concatenate",
"(",
"self",
",",
"other",
")",
":",
"# TODO(mrry): Handle the case where we concatenate a known shape with a",
"# completely unknown shape, so that we can use the partial information.",
"other",
"=",
"as_shape",
"(",
"other",
")",
"if",
"self",
".",
"_dims",
"is",
"None",
"or",
"other",
".",
"dims",
"is",
"None",
":",
"return",
"unknown_shape",
"(",
")",
"else",
":",
"return",
"TensorShape",
"(",
"self",
".",
"_dims",
"+",
"other",
".",
"dims",
")"
] | Returns the concatenation of the dimension in `self` and `other`.
*N.B.* If either `self` or `other` is completely unknown,
concatenation will discard information about the other shape. In
future, we might support concatenation that preserves this
information for use with slicing.
Args:
other: Another `TensorShape`.
Returns:
A `TensorShape` whose dimensions are the concatenation of the
dimensions in `self` and `other`. | [
"Returns",
"the",
"concatenation",
"of",
"the",
"dimension",
"in",
"self",
"and",
"other",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L678-L699 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.assert_same_rank | def assert_same_rank(self, other):
"""Raises an exception if `self` and `other` do not have convertible ranks.
Args:
other: Another `TensorShape`.
Raises:
ValueError: If `self` and `other` do not represent shapes with the
same rank.
"""
other = as_shape(other)
if self.ndims is not None and other.ndims is not None:
if self.ndims != other.ndims:
raise ValueError(
"Shapes %s and %s must have the same rank" % (self, other)
) | python | def assert_same_rank(self, other):
"""Raises an exception if `self` and `other` do not have convertible ranks.
Args:
other: Another `TensorShape`.
Raises:
ValueError: If `self` and `other` do not represent shapes with the
same rank.
"""
other = as_shape(other)
if self.ndims is not None and other.ndims is not None:
if self.ndims != other.ndims:
raise ValueError(
"Shapes %s and %s must have the same rank" % (self, other)
) | [
"def",
"assert_same_rank",
"(",
"self",
",",
"other",
")",
":",
"other",
"=",
"as_shape",
"(",
"other",
")",
"if",
"self",
".",
"ndims",
"is",
"not",
"None",
"and",
"other",
".",
"ndims",
"is",
"not",
"None",
":",
"if",
"self",
".",
"ndims",
"!=",
"other",
".",
"ndims",
":",
"raise",
"ValueError",
"(",
"\"Shapes %s and %s must have the same rank\"",
"%",
"(",
"self",
",",
"other",
")",
")"
] | Raises an exception if `self` and `other` do not have convertible ranks.
Args:
other: Another `TensorShape`.
Raises:
ValueError: If `self` and `other` do not represent shapes with the
same rank. | [
"Raises",
"an",
"exception",
"if",
"self",
"and",
"other",
"do",
"not",
"have",
"convertible",
"ranks",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L701-L716 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.with_rank | def with_rank(self, rank):
"""Returns a shape based on `self` with the given rank.
This method promotes a completely unknown shape to one with a
known rank.
Args:
rank: An integer.
Returns:
A shape that is at least as specific as `self` with the given rank.
Raises:
ValueError: If `self` does not represent a shape with the given `rank`.
"""
try:
return self.merge_with(unknown_shape(ndims=rank))
except ValueError:
raise ValueError("Shape %s must have rank %d" % (self, rank)) | python | def with_rank(self, rank):
"""Returns a shape based on `self` with the given rank.
This method promotes a completely unknown shape to one with a
known rank.
Args:
rank: An integer.
Returns:
A shape that is at least as specific as `self` with the given rank.
Raises:
ValueError: If `self` does not represent a shape with the given `rank`.
"""
try:
return self.merge_with(unknown_shape(ndims=rank))
except ValueError:
raise ValueError("Shape %s must have rank %d" % (self, rank)) | [
"def",
"with_rank",
"(",
"self",
",",
"rank",
")",
":",
"try",
":",
"return",
"self",
".",
"merge_with",
"(",
"unknown_shape",
"(",
"ndims",
"=",
"rank",
")",
")",
"except",
"ValueError",
":",
"raise",
"ValueError",
"(",
"\"Shape %s must have rank %d\"",
"%",
"(",
"self",
",",
"rank",
")",
")"
] | Returns a shape based on `self` with the given rank.
This method promotes a completely unknown shape to one with a
known rank.
Args:
rank: An integer.
Returns:
A shape that is at least as specific as `self` with the given rank.
Raises:
ValueError: If `self` does not represent a shape with the given `rank`. | [
"Returns",
"a",
"shape",
"based",
"on",
"self",
"with",
"the",
"given",
"rank",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L730-L748 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.with_rank_at_least | def with_rank_at_least(self, rank):
"""Returns a shape based on `self` with at least the given rank.
Args:
rank: An integer.
Returns:
A shape that is at least as specific as `self` with at least the given
rank.
Raises:
ValueError: If `self` does not represent a shape with at least the given
`rank`.
"""
if self.ndims is not None and self.ndims < rank:
raise ValueError("Shape %s must have rank at least %d" % (self, rank))
else:
return self | python | def with_rank_at_least(self, rank):
"""Returns a shape based on `self` with at least the given rank.
Args:
rank: An integer.
Returns:
A shape that is at least as specific as `self` with at least the given
rank.
Raises:
ValueError: If `self` does not represent a shape with at least the given
`rank`.
"""
if self.ndims is not None and self.ndims < rank:
raise ValueError("Shape %s must have rank at least %d" % (self, rank))
else:
return self | [
"def",
"with_rank_at_least",
"(",
"self",
",",
"rank",
")",
":",
"if",
"self",
".",
"ndims",
"is",
"not",
"None",
"and",
"self",
".",
"ndims",
"<",
"rank",
":",
"raise",
"ValueError",
"(",
"\"Shape %s must have rank at least %d\"",
"%",
"(",
"self",
",",
"rank",
")",
")",
"else",
":",
"return",
"self"
] | Returns a shape based on `self` with at least the given rank.
Args:
rank: An integer.
Returns:
A shape that is at least as specific as `self` with at least the given
rank.
Raises:
ValueError: If `self` does not represent a shape with at least the given
`rank`. | [
"Returns",
"a",
"shape",
"based",
"on",
"self",
"with",
"at",
"least",
"the",
"given",
"rank",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L750-L767 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.with_rank_at_most | def with_rank_at_most(self, rank):
"""Returns a shape based on `self` with at most the given rank.
Args:
rank: An integer.
Returns:
A shape that is at least as specific as `self` with at most the given
rank.
Raises:
ValueError: If `self` does not represent a shape with at most the given
`rank`.
"""
if self.ndims is not None and self.ndims > rank:
raise ValueError("Shape %s must have rank at most %d" % (self, rank))
else:
return self | python | def with_rank_at_most(self, rank):
"""Returns a shape based on `self` with at most the given rank.
Args:
rank: An integer.
Returns:
A shape that is at least as specific as `self` with at most the given
rank.
Raises:
ValueError: If `self` does not represent a shape with at most the given
`rank`.
"""
if self.ndims is not None and self.ndims > rank:
raise ValueError("Shape %s must have rank at most %d" % (self, rank))
else:
return self | [
"def",
"with_rank_at_most",
"(",
"self",
",",
"rank",
")",
":",
"if",
"self",
".",
"ndims",
"is",
"not",
"None",
"and",
"self",
".",
"ndims",
">",
"rank",
":",
"raise",
"ValueError",
"(",
"\"Shape %s must have rank at most %d\"",
"%",
"(",
"self",
",",
"rank",
")",
")",
"else",
":",
"return",
"self"
] | Returns a shape based on `self` with at most the given rank.
Args:
rank: An integer.
Returns:
A shape that is at least as specific as `self` with at most the given
rank.
Raises:
ValueError: If `self` does not represent a shape with at most the given
`rank`. | [
"Returns",
"a",
"shape",
"based",
"on",
"self",
"with",
"at",
"most",
"the",
"given",
"rank",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L769-L786 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.is_convertible_with | def is_convertible_with(self, other):
"""Returns True iff `self` is convertible with `other`.
Two possibly-partially-defined shapes are convertible if there
exists a fully-defined shape that both shapes can represent. Thus,
convertibility allows the shape inference code to reason about
partially-defined shapes. For example:
* TensorShape(None) is convertible with all shapes.
* TensorShape([None, None]) is convertible with all two-dimensional
shapes, such as TensorShape([32, 784]), and also TensorShape(None). It is
not convertible with, for example, TensorShape([None]) or
TensorShape([None, None, None]).
* TensorShape([32, None]) is convertible with all two-dimensional shapes
with size 32 in the 0th dimension, and also TensorShape([None, None])
and TensorShape(None). It is not convertible with, for example,
TensorShape([32]), TensorShape([32, None, 1]) or TensorShape([64, None]).
* TensorShape([32, 784]) is convertible with itself, and also
TensorShape([32, None]), TensorShape([None, 784]), TensorShape([None,
None]) and TensorShape(None). It is not convertible with, for example,
TensorShape([32, 1, 784]) or TensorShape([None]).
The convertibility relation is reflexive and symmetric, but not
transitive. For example, TensorShape([32, 784]) is convertible with
TensorShape(None), and TensorShape(None) is convertible with
TensorShape([4, 4]), but TensorShape([32, 784]) is not convertible with
TensorShape([4, 4]).
Args:
other: Another TensorShape.
Returns:
True iff `self` is convertible with `other`.
"""
other = as_shape(other)
if self._dims is not None and other.dims is not None:
if self.ndims != other.ndims:
return False
for x_dim, y_dim in zip(self._dims, other.dims):
if not x_dim.is_convertible_with(y_dim):
return False
return True | python | def is_convertible_with(self, other):
"""Returns True iff `self` is convertible with `other`.
Two possibly-partially-defined shapes are convertible if there
exists a fully-defined shape that both shapes can represent. Thus,
convertibility allows the shape inference code to reason about
partially-defined shapes. For example:
* TensorShape(None) is convertible with all shapes.
* TensorShape([None, None]) is convertible with all two-dimensional
shapes, such as TensorShape([32, 784]), and also TensorShape(None). It is
not convertible with, for example, TensorShape([None]) or
TensorShape([None, None, None]).
* TensorShape([32, None]) is convertible with all two-dimensional shapes
with size 32 in the 0th dimension, and also TensorShape([None, None])
and TensorShape(None). It is not convertible with, for example,
TensorShape([32]), TensorShape([32, None, 1]) or TensorShape([64, None]).
* TensorShape([32, 784]) is convertible with itself, and also
TensorShape([32, None]), TensorShape([None, 784]), TensorShape([None,
None]) and TensorShape(None). It is not convertible with, for example,
TensorShape([32, 1, 784]) or TensorShape([None]).
The convertibility relation is reflexive and symmetric, but not
transitive. For example, TensorShape([32, 784]) is convertible with
TensorShape(None), and TensorShape(None) is convertible with
TensorShape([4, 4]), but TensorShape([32, 784]) is not convertible with
TensorShape([4, 4]).
Args:
other: Another TensorShape.
Returns:
True iff `self` is convertible with `other`.
"""
other = as_shape(other)
if self._dims is not None and other.dims is not None:
if self.ndims != other.ndims:
return False
for x_dim, y_dim in zip(self._dims, other.dims):
if not x_dim.is_convertible_with(y_dim):
return False
return True | [
"def",
"is_convertible_with",
"(",
"self",
",",
"other",
")",
":",
"other",
"=",
"as_shape",
"(",
"other",
")",
"if",
"self",
".",
"_dims",
"is",
"not",
"None",
"and",
"other",
".",
"dims",
"is",
"not",
"None",
":",
"if",
"self",
".",
"ndims",
"!=",
"other",
".",
"ndims",
":",
"return",
"False",
"for",
"x_dim",
",",
"y_dim",
"in",
"zip",
"(",
"self",
".",
"_dims",
",",
"other",
".",
"dims",
")",
":",
"if",
"not",
"x_dim",
".",
"is_convertible_with",
"(",
"y_dim",
")",
":",
"return",
"False",
"return",
"True"
] | Returns True iff `self` is convertible with `other`.
Two possibly-partially-defined shapes are convertible if there
exists a fully-defined shape that both shapes can represent. Thus,
convertibility allows the shape inference code to reason about
partially-defined shapes. For example:
* TensorShape(None) is convertible with all shapes.
* TensorShape([None, None]) is convertible with all two-dimensional
shapes, such as TensorShape([32, 784]), and also TensorShape(None). It is
not convertible with, for example, TensorShape([None]) or
TensorShape([None, None, None]).
* TensorShape([32, None]) is convertible with all two-dimensional shapes
with size 32 in the 0th dimension, and also TensorShape([None, None])
and TensorShape(None). It is not convertible with, for example,
TensorShape([32]), TensorShape([32, None, 1]) or TensorShape([64, None]).
* TensorShape([32, 784]) is convertible with itself, and also
TensorShape([32, None]), TensorShape([None, 784]), TensorShape([None,
None]) and TensorShape(None). It is not convertible with, for example,
TensorShape([32, 1, 784]) or TensorShape([None]).
The convertibility relation is reflexive and symmetric, but not
transitive. For example, TensorShape([32, 784]) is convertible with
TensorShape(None), and TensorShape(None) is convertible with
TensorShape([4, 4]), but TensorShape([32, 784]) is not convertible with
TensorShape([4, 4]).
Args:
other: Another TensorShape.
Returns:
True iff `self` is convertible with `other`. | [
"Returns",
"True",
"iff",
"self",
"is",
"convertible",
"with",
"other",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L788-L833 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.most_specific_convertible_shape | def most_specific_convertible_shape(self, other):
"""Returns the most specific TensorShape convertible with `self` and `other`.
* TensorShape([None, 1]) is the most specific TensorShape convertible with
both TensorShape([2, 1]) and TensorShape([5, 1]). Note that
TensorShape(None) is also convertible with above mentioned TensorShapes.
* TensorShape([1, 2, 3]) is the most specific TensorShape convertible with
both TensorShape([1, 2, 3]) and TensorShape([1, 2, 3]). There are more
less specific TensorShapes convertible with above mentioned TensorShapes,
e.g. TensorShape([1, 2, None]), TensorShape(None).
Args:
other: Another `TensorShape`.
Returns:
A `TensorShape` which is the most specific convertible shape of `self`
and `other`.
"""
other = as_shape(other)
if self._dims is None or other.dims is None or self.ndims != other.ndims:
return unknown_shape()
dims = [(Dimension(None))] * self.ndims
for i, (d1, d2) in enumerate(zip(self._dims, other.dims)):
if d1 is not None and d2 is not None and d1 == d2:
dims[i] = d1
return TensorShape(dims) | python | def most_specific_convertible_shape(self, other):
"""Returns the most specific TensorShape convertible with `self` and `other`.
* TensorShape([None, 1]) is the most specific TensorShape convertible with
both TensorShape([2, 1]) and TensorShape([5, 1]). Note that
TensorShape(None) is also convertible with above mentioned TensorShapes.
* TensorShape([1, 2, 3]) is the most specific TensorShape convertible with
both TensorShape([1, 2, 3]) and TensorShape([1, 2, 3]). There are more
less specific TensorShapes convertible with above mentioned TensorShapes,
e.g. TensorShape([1, 2, None]), TensorShape(None).
Args:
other: Another `TensorShape`.
Returns:
A `TensorShape` which is the most specific convertible shape of `self`
and `other`.
"""
other = as_shape(other)
if self._dims is None or other.dims is None or self.ndims != other.ndims:
return unknown_shape()
dims = [(Dimension(None))] * self.ndims
for i, (d1, d2) in enumerate(zip(self._dims, other.dims)):
if d1 is not None and d2 is not None and d1 == d2:
dims[i] = d1
return TensorShape(dims) | [
"def",
"most_specific_convertible_shape",
"(",
"self",
",",
"other",
")",
":",
"other",
"=",
"as_shape",
"(",
"other",
")",
"if",
"self",
".",
"_dims",
"is",
"None",
"or",
"other",
".",
"dims",
"is",
"None",
"or",
"self",
".",
"ndims",
"!=",
"other",
".",
"ndims",
":",
"return",
"unknown_shape",
"(",
")",
"dims",
"=",
"[",
"(",
"Dimension",
"(",
"None",
")",
")",
"]",
"*",
"self",
".",
"ndims",
"for",
"i",
",",
"(",
"d1",
",",
"d2",
")",
"in",
"enumerate",
"(",
"zip",
"(",
"self",
".",
"_dims",
",",
"other",
".",
"dims",
")",
")",
":",
"if",
"d1",
"is",
"not",
"None",
"and",
"d2",
"is",
"not",
"None",
"and",
"d1",
"==",
"d2",
":",
"dims",
"[",
"i",
"]",
"=",
"d1",
"return",
"TensorShape",
"(",
"dims",
")"
] | Returns the most specific TensorShape convertible with `self` and `other`.
* TensorShape([None, 1]) is the most specific TensorShape convertible with
both TensorShape([2, 1]) and TensorShape([5, 1]). Note that
TensorShape(None) is also convertible with above mentioned TensorShapes.
* TensorShape([1, 2, 3]) is the most specific TensorShape convertible with
both TensorShape([1, 2, 3]) and TensorShape([1, 2, 3]). There are more
less specific TensorShapes convertible with above mentioned TensorShapes,
e.g. TensorShape([1, 2, None]), TensorShape(None).
Args:
other: Another `TensorShape`.
Returns:
A `TensorShape` which is the most specific convertible shape of `self`
and `other`. | [
"Returns",
"the",
"most",
"specific",
"TensorShape",
"convertible",
"with",
"self",
"and",
"other",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L850-L878 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.is_fully_defined | def is_fully_defined(self):
"""Returns True iff `self` is fully defined in every dimension."""
return self._dims is not None and all(
dim.value is not None for dim in self._dims
) | python | def is_fully_defined(self):
"""Returns True iff `self` is fully defined in every dimension."""
return self._dims is not None and all(
dim.value is not None for dim in self._dims
) | [
"def",
"is_fully_defined",
"(",
"self",
")",
":",
"return",
"self",
".",
"_dims",
"is",
"not",
"None",
"and",
"all",
"(",
"dim",
".",
"value",
"is",
"not",
"None",
"for",
"dim",
"in",
"self",
".",
"_dims",
")"
] | Returns True iff `self` is fully defined in every dimension. | [
"Returns",
"True",
"iff",
"self",
"is",
"fully",
"defined",
"in",
"every",
"dimension",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L880-L884 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.as_list | def as_list(self):
"""Returns a list of integers or `None` for each dimension.
Returns:
A list of integers or `None` for each dimension.
Raises:
ValueError: If `self` is an unknown shape with an unknown rank.
"""
if self._dims is None:
raise ValueError("as_list() is not defined on an unknown TensorShape.")
return [dim.value for dim in self._dims] | python | def as_list(self):
"""Returns a list of integers or `None` for each dimension.
Returns:
A list of integers or `None` for each dimension.
Raises:
ValueError: If `self` is an unknown shape with an unknown rank.
"""
if self._dims is None:
raise ValueError("as_list() is not defined on an unknown TensorShape.")
return [dim.value for dim in self._dims] | [
"def",
"as_list",
"(",
"self",
")",
":",
"if",
"self",
".",
"_dims",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"\"as_list() is not defined on an unknown TensorShape.\"",
")",
"return",
"[",
"dim",
".",
"value",
"for",
"dim",
"in",
"self",
".",
"_dims",
"]"
] | Returns a list of integers or `None` for each dimension.
Returns:
A list of integers or `None` for each dimension.
Raises:
ValueError: If `self` is an unknown shape with an unknown rank. | [
"Returns",
"a",
"list",
"of",
"integers",
"or",
"None",
"for",
"each",
"dimension",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L895-L906 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/tensor_shape.py | TensorShape.as_proto | def as_proto(self):
"""Returns this shape as a `TensorShapeProto`."""
if self._dims is None:
return tensor_shape_pb2.TensorShapeProto(unknown_rank=True)
else:
return tensor_shape_pb2.TensorShapeProto(
dim=[
tensor_shape_pb2.TensorShapeProto.Dim(
size=-1 if d.value is None else d.value
)
for d in self._dims
]
) | python | def as_proto(self):
"""Returns this shape as a `TensorShapeProto`."""
if self._dims is None:
return tensor_shape_pb2.TensorShapeProto(unknown_rank=True)
else:
return tensor_shape_pb2.TensorShapeProto(
dim=[
tensor_shape_pb2.TensorShapeProto.Dim(
size=-1 if d.value is None else d.value
)
for d in self._dims
]
) | [
"def",
"as_proto",
"(",
"self",
")",
":",
"if",
"self",
".",
"_dims",
"is",
"None",
":",
"return",
"tensor_shape_pb2",
".",
"TensorShapeProto",
"(",
"unknown_rank",
"=",
"True",
")",
"else",
":",
"return",
"tensor_shape_pb2",
".",
"TensorShapeProto",
"(",
"dim",
"=",
"[",
"tensor_shape_pb2",
".",
"TensorShapeProto",
".",
"Dim",
"(",
"size",
"=",
"-",
"1",
"if",
"d",
".",
"value",
"is",
"None",
"else",
"d",
".",
"value",
")",
"for",
"d",
"in",
"self",
".",
"_dims",
"]",
")"
] | Returns this shape as a `TensorShapeProto`. | [
"Returns",
"this",
"shape",
"as",
"a",
"TensorShapeProto",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/tensor_shape.py#L908-L920 | train |
tensorflow/tensorboard | tensorboard/plugins/interactive_inference/utils/common_utils.py | convert_predict_response | def convert_predict_response(pred, serving_bundle):
"""Converts a PredictResponse to ClassificationResponse or RegressionResponse.
Args:
pred: PredictResponse to convert.
serving_bundle: A `ServingBundle` object that contains the information about
the serving request that the response was generated by.
Returns:
A ClassificationResponse or RegressionResponse.
"""
output = pred.outputs[serving_bundle.predict_output_tensor]
raw_output = output.float_val
if serving_bundle.model_type == 'classification':
values = []
for example_index in range(output.tensor_shape.dim[0].size):
start = example_index * output.tensor_shape.dim[1].size
values.append(raw_output[start:start + output.tensor_shape.dim[1].size])
else:
values = raw_output
return convert_prediction_values(values, serving_bundle, pred.model_spec) | python | def convert_predict_response(pred, serving_bundle):
"""Converts a PredictResponse to ClassificationResponse or RegressionResponse.
Args:
pred: PredictResponse to convert.
serving_bundle: A `ServingBundle` object that contains the information about
the serving request that the response was generated by.
Returns:
A ClassificationResponse or RegressionResponse.
"""
output = pred.outputs[serving_bundle.predict_output_tensor]
raw_output = output.float_val
if serving_bundle.model_type == 'classification':
values = []
for example_index in range(output.tensor_shape.dim[0].size):
start = example_index * output.tensor_shape.dim[1].size
values.append(raw_output[start:start + output.tensor_shape.dim[1].size])
else:
values = raw_output
return convert_prediction_values(values, serving_bundle, pred.model_spec) | [
"def",
"convert_predict_response",
"(",
"pred",
",",
"serving_bundle",
")",
":",
"output",
"=",
"pred",
".",
"outputs",
"[",
"serving_bundle",
".",
"predict_output_tensor",
"]",
"raw_output",
"=",
"output",
".",
"float_val",
"if",
"serving_bundle",
".",
"model_type",
"==",
"'classification'",
":",
"values",
"=",
"[",
"]",
"for",
"example_index",
"in",
"range",
"(",
"output",
".",
"tensor_shape",
".",
"dim",
"[",
"0",
"]",
".",
"size",
")",
":",
"start",
"=",
"example_index",
"*",
"output",
".",
"tensor_shape",
".",
"dim",
"[",
"1",
"]",
".",
"size",
"values",
".",
"append",
"(",
"raw_output",
"[",
"start",
":",
"start",
"+",
"output",
".",
"tensor_shape",
".",
"dim",
"[",
"1",
"]",
".",
"size",
"]",
")",
"else",
":",
"values",
"=",
"raw_output",
"return",
"convert_prediction_values",
"(",
"values",
",",
"serving_bundle",
",",
"pred",
".",
"model_spec",
")"
] | Converts a PredictResponse to ClassificationResponse or RegressionResponse.
Args:
pred: PredictResponse to convert.
serving_bundle: A `ServingBundle` object that contains the information about
the serving request that the response was generated by.
Returns:
A ClassificationResponse or RegressionResponse. | [
"Converts",
"a",
"PredictResponse",
"to",
"ClassificationResponse",
"or",
"RegressionResponse",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/interactive_inference/utils/common_utils.py#L39-L59 | train |
tensorflow/tensorboard | tensorboard/plugins/interactive_inference/utils/common_utils.py | convert_prediction_values | def convert_prediction_values(values, serving_bundle, model_spec=None):
"""Converts tensor values into ClassificationResponse or RegressionResponse.
Args:
values: For classification, a 2D list of numbers. The first dimension is for
each example being predicted. The second dimension are the probabilities
for each class ID in the prediction. For regression, a 1D list of numbers,
with a regression score for each example being predicted.
serving_bundle: A `ServingBundle` object that contains the information about
the serving request that the response was generated by.
model_spec: Optional model spec to put into the response.
Returns:
A ClassificationResponse or RegressionResponse.
"""
if serving_bundle.model_type == 'classification':
response = classification_pb2.ClassificationResponse()
for example_index in range(len(values)):
classification = response.result.classifications.add()
for class_index in range(len(values[example_index])):
class_score = classification.classes.add()
class_score.score = values[example_index][class_index]
class_score.label = str(class_index)
else:
response = regression_pb2.RegressionResponse()
for example_index in range(len(values)):
regression = response.result.regressions.add()
regression.value = values[example_index]
if model_spec:
response.model_spec.CopyFrom(model_spec)
return response | python | def convert_prediction_values(values, serving_bundle, model_spec=None):
"""Converts tensor values into ClassificationResponse or RegressionResponse.
Args:
values: For classification, a 2D list of numbers. The first dimension is for
each example being predicted. The second dimension are the probabilities
for each class ID in the prediction. For regression, a 1D list of numbers,
with a regression score for each example being predicted.
serving_bundle: A `ServingBundle` object that contains the information about
the serving request that the response was generated by.
model_spec: Optional model spec to put into the response.
Returns:
A ClassificationResponse or RegressionResponse.
"""
if serving_bundle.model_type == 'classification':
response = classification_pb2.ClassificationResponse()
for example_index in range(len(values)):
classification = response.result.classifications.add()
for class_index in range(len(values[example_index])):
class_score = classification.classes.add()
class_score.score = values[example_index][class_index]
class_score.label = str(class_index)
else:
response = regression_pb2.RegressionResponse()
for example_index in range(len(values)):
regression = response.result.regressions.add()
regression.value = values[example_index]
if model_spec:
response.model_spec.CopyFrom(model_spec)
return response | [
"def",
"convert_prediction_values",
"(",
"values",
",",
"serving_bundle",
",",
"model_spec",
"=",
"None",
")",
":",
"if",
"serving_bundle",
".",
"model_type",
"==",
"'classification'",
":",
"response",
"=",
"classification_pb2",
".",
"ClassificationResponse",
"(",
")",
"for",
"example_index",
"in",
"range",
"(",
"len",
"(",
"values",
")",
")",
":",
"classification",
"=",
"response",
".",
"result",
".",
"classifications",
".",
"add",
"(",
")",
"for",
"class_index",
"in",
"range",
"(",
"len",
"(",
"values",
"[",
"example_index",
"]",
")",
")",
":",
"class_score",
"=",
"classification",
".",
"classes",
".",
"add",
"(",
")",
"class_score",
".",
"score",
"=",
"values",
"[",
"example_index",
"]",
"[",
"class_index",
"]",
"class_score",
".",
"label",
"=",
"str",
"(",
"class_index",
")",
"else",
":",
"response",
"=",
"regression_pb2",
".",
"RegressionResponse",
"(",
")",
"for",
"example_index",
"in",
"range",
"(",
"len",
"(",
"values",
")",
")",
":",
"regression",
"=",
"response",
".",
"result",
".",
"regressions",
".",
"add",
"(",
")",
"regression",
".",
"value",
"=",
"values",
"[",
"example_index",
"]",
"if",
"model_spec",
":",
"response",
".",
"model_spec",
".",
"CopyFrom",
"(",
"model_spec",
")",
"return",
"response"
] | Converts tensor values into ClassificationResponse or RegressionResponse.
Args:
values: For classification, a 2D list of numbers. The first dimension is for
each example being predicted. The second dimension are the probabilities
for each class ID in the prediction. For regression, a 1D list of numbers,
with a regression score for each example being predicted.
serving_bundle: A `ServingBundle` object that contains the information about
the serving request that the response was generated by.
model_spec: Optional model spec to put into the response.
Returns:
A ClassificationResponse or RegressionResponse. | [
"Converts",
"tensor",
"values",
"into",
"ClassificationResponse",
"or",
"RegressionResponse",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/interactive_inference/utils/common_utils.py#L61-L91 | train |
tensorflow/tensorboard | tensorboard/backend/event_processing/plugin_event_accumulator.py | _GetPurgeMessage | def _GetPurgeMessage(most_recent_step, most_recent_wall_time, event_step,
event_wall_time, num_expired):
"""Return the string message associated with TensorBoard purges."""
return ('Detected out of order event.step likely caused by a TensorFlow '
'restart. Purging {} expired tensor events from Tensorboard display '
'between the previous step: {} (timestamp: {}) and current step: {} '
'(timestamp: {}).'
).format(num_expired, most_recent_step, most_recent_wall_time,
event_step, event_wall_time) | python | def _GetPurgeMessage(most_recent_step, most_recent_wall_time, event_step,
event_wall_time, num_expired):
"""Return the string message associated with TensorBoard purges."""
return ('Detected out of order event.step likely caused by a TensorFlow '
'restart. Purging {} expired tensor events from Tensorboard display '
'between the previous step: {} (timestamp: {}) and current step: {} '
'(timestamp: {}).'
).format(num_expired, most_recent_step, most_recent_wall_time,
event_step, event_wall_time) | [
"def",
"_GetPurgeMessage",
"(",
"most_recent_step",
",",
"most_recent_wall_time",
",",
"event_step",
",",
"event_wall_time",
",",
"num_expired",
")",
":",
"return",
"(",
"'Detected out of order event.step likely caused by a TensorFlow '",
"'restart. Purging {} expired tensor events from Tensorboard display '",
"'between the previous step: {} (timestamp: {}) and current step: {} '",
"'(timestamp: {}).'",
")",
".",
"format",
"(",
"num_expired",
",",
"most_recent_step",
",",
"most_recent_wall_time",
",",
"event_step",
",",
"event_wall_time",
")"
] | Return the string message associated with TensorBoard purges. | [
"Return",
"the",
"string",
"message",
"associated",
"with",
"TensorBoard",
"purges",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/plugin_event_accumulator.py#L560-L568 | train |
tensorflow/tensorboard | tensorboard/backend/event_processing/plugin_event_accumulator.py | EventAccumulator.PluginTagToContent | def PluginTagToContent(self, plugin_name):
"""Returns a dict mapping tags to content specific to that plugin.
Args:
plugin_name: The name of the plugin for which to fetch plugin-specific
content.
Raises:
KeyError: if the plugin name is not found.
Returns:
A dict mapping tags to plugin-specific content (which are always strings).
Those strings are often serialized protos.
"""
if plugin_name not in self._plugin_to_tag_to_content:
raise KeyError('Plugin %r could not be found.' % plugin_name)
with self._plugin_tag_locks[plugin_name]:
# Return a snapshot to avoid concurrent mutation and iteration issues.
return dict(self._plugin_to_tag_to_content[plugin_name]) | python | def PluginTagToContent(self, plugin_name):
"""Returns a dict mapping tags to content specific to that plugin.
Args:
plugin_name: The name of the plugin for which to fetch plugin-specific
content.
Raises:
KeyError: if the plugin name is not found.
Returns:
A dict mapping tags to plugin-specific content (which are always strings).
Those strings are often serialized protos.
"""
if plugin_name not in self._plugin_to_tag_to_content:
raise KeyError('Plugin %r could not be found.' % plugin_name)
with self._plugin_tag_locks[plugin_name]:
# Return a snapshot to avoid concurrent mutation and iteration issues.
return dict(self._plugin_to_tag_to_content[plugin_name]) | [
"def",
"PluginTagToContent",
"(",
"self",
",",
"plugin_name",
")",
":",
"if",
"plugin_name",
"not",
"in",
"self",
".",
"_plugin_to_tag_to_content",
":",
"raise",
"KeyError",
"(",
"'Plugin %r could not be found.'",
"%",
"plugin_name",
")",
"with",
"self",
".",
"_plugin_tag_locks",
"[",
"plugin_name",
"]",
":",
"# Return a snapshot to avoid concurrent mutation and iteration issues.",
"return",
"dict",
"(",
"self",
".",
"_plugin_to_tag_to_content",
"[",
"plugin_name",
"]",
")"
] | Returns a dict mapping tags to content specific to that plugin.
Args:
plugin_name: The name of the plugin for which to fetch plugin-specific
content.
Raises:
KeyError: if the plugin name is not found.
Returns:
A dict mapping tags to plugin-specific content (which are always strings).
Those strings are often serialized protos. | [
"Returns",
"a",
"dict",
"mapping",
"tags",
"to",
"content",
"specific",
"to",
"that",
"plugin",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/plugin_event_accumulator.py#L234-L252 | train |
tensorflow/tensorboard | tensorboard/backend/event_processing/plugin_event_accumulator.py | EventAccumulator._ProcessEvent | def _ProcessEvent(self, event):
"""Called whenever an event is loaded."""
if self._first_event_timestamp is None:
self._first_event_timestamp = event.wall_time
if event.HasField('file_version'):
new_file_version = _ParseFileVersion(event.file_version)
if self.file_version and self.file_version != new_file_version:
## This should not happen.
logger.warn(('Found new file_version for event.proto. This will '
'affect purging logic for TensorFlow restarts. '
'Old: {0} New: {1}').format(self.file_version,
new_file_version))
self.file_version = new_file_version
self._MaybePurgeOrphanedData(event)
## Process the event.
# GraphDef and MetaGraphDef are handled in a special way:
# If no graph_def Event is available, but a meta_graph_def is, and it
# contains a graph_def, then use the meta_graph_def.graph_def as our graph.
# If a graph_def Event is available, always prefer it to the graph_def
# inside the meta_graph_def.
if event.HasField('graph_def'):
if self._graph is not None:
logger.warn(
('Found more than one graph event per run, or there was '
'a metagraph containing a graph_def, as well as one or '
'more graph events. Overwriting the graph with the '
'newest event.'))
self._graph = event.graph_def
self._graph_from_metagraph = False
elif event.HasField('meta_graph_def'):
if self._meta_graph is not None:
logger.warn(('Found more than one metagraph event per run. '
'Overwriting the metagraph with the newest event.'))
self._meta_graph = event.meta_graph_def
if self._graph is None or self._graph_from_metagraph:
# We may have a graph_def in the metagraph. If so, and no
# graph_def is directly available, use this one instead.
meta_graph = meta_graph_pb2.MetaGraphDef()
meta_graph.ParseFromString(self._meta_graph)
if meta_graph.graph_def:
if self._graph is not None:
logger.warn(
('Found multiple metagraphs containing graph_defs,'
'but did not find any graph events. Overwriting the '
'graph with the newest metagraph version.'))
self._graph_from_metagraph = True
self._graph = meta_graph.graph_def.SerializeToString()
elif event.HasField('tagged_run_metadata'):
tag = event.tagged_run_metadata.tag
if tag in self._tagged_metadata:
logger.warn('Found more than one "run metadata" event with tag ' +
tag + '. Overwriting it with the newest event.')
self._tagged_metadata[tag] = event.tagged_run_metadata.run_metadata
elif event.HasField('summary'):
for value in event.summary.value:
value = data_compat.migrate_value(value)
if value.HasField('metadata'):
tag = value.tag
# We only store the first instance of the metadata. This check
# is important: the `FileWriter` does strip metadata from all
# values except the first one per each tag, but a new
# `FileWriter` is created every time a training job stops and
# restarts. Hence, we must also ignore non-initial metadata in
# this logic.
if tag not in self.summary_metadata:
self.summary_metadata[tag] = value.metadata
plugin_data = value.metadata.plugin_data
if plugin_data.plugin_name:
with self._plugin_tag_locks[plugin_data.plugin_name]:
self._plugin_to_tag_to_content[plugin_data.plugin_name][tag] = (
plugin_data.content)
else:
logger.warn(
('This summary with tag %r is oddly not associated with a '
'plugin.'), tag)
for summary_type, summary_func in SUMMARY_TYPES.items():
if value.HasField(summary_type):
datum = getattr(value, summary_type)
tag = value.tag
if summary_type == 'tensor' and not tag:
# This tensor summary was created using the old method that used
# plugin assets. We must still continue to support it.
tag = value.node_name
getattr(self, summary_func)(tag, event.wall_time, event.step, datum) | python | def _ProcessEvent(self, event):
"""Called whenever an event is loaded."""
if self._first_event_timestamp is None:
self._first_event_timestamp = event.wall_time
if event.HasField('file_version'):
new_file_version = _ParseFileVersion(event.file_version)
if self.file_version and self.file_version != new_file_version:
## This should not happen.
logger.warn(('Found new file_version for event.proto. This will '
'affect purging logic for TensorFlow restarts. '
'Old: {0} New: {1}').format(self.file_version,
new_file_version))
self.file_version = new_file_version
self._MaybePurgeOrphanedData(event)
## Process the event.
# GraphDef and MetaGraphDef are handled in a special way:
# If no graph_def Event is available, but a meta_graph_def is, and it
# contains a graph_def, then use the meta_graph_def.graph_def as our graph.
# If a graph_def Event is available, always prefer it to the graph_def
# inside the meta_graph_def.
if event.HasField('graph_def'):
if self._graph is not None:
logger.warn(
('Found more than one graph event per run, or there was '
'a metagraph containing a graph_def, as well as one or '
'more graph events. Overwriting the graph with the '
'newest event.'))
self._graph = event.graph_def
self._graph_from_metagraph = False
elif event.HasField('meta_graph_def'):
if self._meta_graph is not None:
logger.warn(('Found more than one metagraph event per run. '
'Overwriting the metagraph with the newest event.'))
self._meta_graph = event.meta_graph_def
if self._graph is None or self._graph_from_metagraph:
# We may have a graph_def in the metagraph. If so, and no
# graph_def is directly available, use this one instead.
meta_graph = meta_graph_pb2.MetaGraphDef()
meta_graph.ParseFromString(self._meta_graph)
if meta_graph.graph_def:
if self._graph is not None:
logger.warn(
('Found multiple metagraphs containing graph_defs,'
'but did not find any graph events. Overwriting the '
'graph with the newest metagraph version.'))
self._graph_from_metagraph = True
self._graph = meta_graph.graph_def.SerializeToString()
elif event.HasField('tagged_run_metadata'):
tag = event.tagged_run_metadata.tag
if tag in self._tagged_metadata:
logger.warn('Found more than one "run metadata" event with tag ' +
tag + '. Overwriting it with the newest event.')
self._tagged_metadata[tag] = event.tagged_run_metadata.run_metadata
elif event.HasField('summary'):
for value in event.summary.value:
value = data_compat.migrate_value(value)
if value.HasField('metadata'):
tag = value.tag
# We only store the first instance of the metadata. This check
# is important: the `FileWriter` does strip metadata from all
# values except the first one per each tag, but a new
# `FileWriter` is created every time a training job stops and
# restarts. Hence, we must also ignore non-initial metadata in
# this logic.
if tag not in self.summary_metadata:
self.summary_metadata[tag] = value.metadata
plugin_data = value.metadata.plugin_data
if plugin_data.plugin_name:
with self._plugin_tag_locks[plugin_data.plugin_name]:
self._plugin_to_tag_to_content[plugin_data.plugin_name][tag] = (
plugin_data.content)
else:
logger.warn(
('This summary with tag %r is oddly not associated with a '
'plugin.'), tag)
for summary_type, summary_func in SUMMARY_TYPES.items():
if value.HasField(summary_type):
datum = getattr(value, summary_type)
tag = value.tag
if summary_type == 'tensor' and not tag:
# This tensor summary was created using the old method that used
# plugin assets. We must still continue to support it.
tag = value.node_name
getattr(self, summary_func)(tag, event.wall_time, event.step, datum) | [
"def",
"_ProcessEvent",
"(",
"self",
",",
"event",
")",
":",
"if",
"self",
".",
"_first_event_timestamp",
"is",
"None",
":",
"self",
".",
"_first_event_timestamp",
"=",
"event",
".",
"wall_time",
"if",
"event",
".",
"HasField",
"(",
"'file_version'",
")",
":",
"new_file_version",
"=",
"_ParseFileVersion",
"(",
"event",
".",
"file_version",
")",
"if",
"self",
".",
"file_version",
"and",
"self",
".",
"file_version",
"!=",
"new_file_version",
":",
"## This should not happen.",
"logger",
".",
"warn",
"(",
"(",
"'Found new file_version for event.proto. This will '",
"'affect purging logic for TensorFlow restarts. '",
"'Old: {0} New: {1}'",
")",
".",
"format",
"(",
"self",
".",
"file_version",
",",
"new_file_version",
")",
")",
"self",
".",
"file_version",
"=",
"new_file_version",
"self",
".",
"_MaybePurgeOrphanedData",
"(",
"event",
")",
"## Process the event.",
"# GraphDef and MetaGraphDef are handled in a special way:",
"# If no graph_def Event is available, but a meta_graph_def is, and it",
"# contains a graph_def, then use the meta_graph_def.graph_def as our graph.",
"# If a graph_def Event is available, always prefer it to the graph_def",
"# inside the meta_graph_def.",
"if",
"event",
".",
"HasField",
"(",
"'graph_def'",
")",
":",
"if",
"self",
".",
"_graph",
"is",
"not",
"None",
":",
"logger",
".",
"warn",
"(",
"(",
"'Found more than one graph event per run, or there was '",
"'a metagraph containing a graph_def, as well as one or '",
"'more graph events. Overwriting the graph with the '",
"'newest event.'",
")",
")",
"self",
".",
"_graph",
"=",
"event",
".",
"graph_def",
"self",
".",
"_graph_from_metagraph",
"=",
"False",
"elif",
"event",
".",
"HasField",
"(",
"'meta_graph_def'",
")",
":",
"if",
"self",
".",
"_meta_graph",
"is",
"not",
"None",
":",
"logger",
".",
"warn",
"(",
"(",
"'Found more than one metagraph event per run. '",
"'Overwriting the metagraph with the newest event.'",
")",
")",
"self",
".",
"_meta_graph",
"=",
"event",
".",
"meta_graph_def",
"if",
"self",
".",
"_graph",
"is",
"None",
"or",
"self",
".",
"_graph_from_metagraph",
":",
"# We may have a graph_def in the metagraph. If so, and no",
"# graph_def is directly available, use this one instead.",
"meta_graph",
"=",
"meta_graph_pb2",
".",
"MetaGraphDef",
"(",
")",
"meta_graph",
".",
"ParseFromString",
"(",
"self",
".",
"_meta_graph",
")",
"if",
"meta_graph",
".",
"graph_def",
":",
"if",
"self",
".",
"_graph",
"is",
"not",
"None",
":",
"logger",
".",
"warn",
"(",
"(",
"'Found multiple metagraphs containing graph_defs,'",
"'but did not find any graph events. Overwriting the '",
"'graph with the newest metagraph version.'",
")",
")",
"self",
".",
"_graph_from_metagraph",
"=",
"True",
"self",
".",
"_graph",
"=",
"meta_graph",
".",
"graph_def",
".",
"SerializeToString",
"(",
")",
"elif",
"event",
".",
"HasField",
"(",
"'tagged_run_metadata'",
")",
":",
"tag",
"=",
"event",
".",
"tagged_run_metadata",
".",
"tag",
"if",
"tag",
"in",
"self",
".",
"_tagged_metadata",
":",
"logger",
".",
"warn",
"(",
"'Found more than one \"run metadata\" event with tag '",
"+",
"tag",
"+",
"'. Overwriting it with the newest event.'",
")",
"self",
".",
"_tagged_metadata",
"[",
"tag",
"]",
"=",
"event",
".",
"tagged_run_metadata",
".",
"run_metadata",
"elif",
"event",
".",
"HasField",
"(",
"'summary'",
")",
":",
"for",
"value",
"in",
"event",
".",
"summary",
".",
"value",
":",
"value",
"=",
"data_compat",
".",
"migrate_value",
"(",
"value",
")",
"if",
"value",
".",
"HasField",
"(",
"'metadata'",
")",
":",
"tag",
"=",
"value",
".",
"tag",
"# We only store the first instance of the metadata. This check",
"# is important: the `FileWriter` does strip metadata from all",
"# values except the first one per each tag, but a new",
"# `FileWriter` is created every time a training job stops and",
"# restarts. Hence, we must also ignore non-initial metadata in",
"# this logic.",
"if",
"tag",
"not",
"in",
"self",
".",
"summary_metadata",
":",
"self",
".",
"summary_metadata",
"[",
"tag",
"]",
"=",
"value",
".",
"metadata",
"plugin_data",
"=",
"value",
".",
"metadata",
".",
"plugin_data",
"if",
"plugin_data",
".",
"plugin_name",
":",
"with",
"self",
".",
"_plugin_tag_locks",
"[",
"plugin_data",
".",
"plugin_name",
"]",
":",
"self",
".",
"_plugin_to_tag_to_content",
"[",
"plugin_data",
".",
"plugin_name",
"]",
"[",
"tag",
"]",
"=",
"(",
"plugin_data",
".",
"content",
")",
"else",
":",
"logger",
".",
"warn",
"(",
"(",
"'This summary with tag %r is oddly not associated with a '",
"'plugin.'",
")",
",",
"tag",
")",
"for",
"summary_type",
",",
"summary_func",
"in",
"SUMMARY_TYPES",
".",
"items",
"(",
")",
":",
"if",
"value",
".",
"HasField",
"(",
"summary_type",
")",
":",
"datum",
"=",
"getattr",
"(",
"value",
",",
"summary_type",
")",
"tag",
"=",
"value",
".",
"tag",
"if",
"summary_type",
"==",
"'tensor'",
"and",
"not",
"tag",
":",
"# This tensor summary was created using the old method that used",
"# plugin assets. We must still continue to support it.",
"tag",
"=",
"value",
".",
"node_name",
"getattr",
"(",
"self",
",",
"summary_func",
")",
"(",
"tag",
",",
"event",
".",
"wall_time",
",",
"event",
".",
"step",
",",
"datum",
")"
] | Called whenever an event is loaded. | [
"Called",
"whenever",
"an",
"event",
"is",
"loaded",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/plugin_event_accumulator.py#L268-L356 | train |
tensorflow/tensorboard | tensorboard/backend/event_processing/plugin_event_accumulator.py | EventAccumulator.Tags | def Tags(self):
"""Return all tags found in the value stream.
Returns:
A `{tagType: ['list', 'of', 'tags']}` dictionary.
"""
return {
TENSORS: list(self.tensors_by_tag.keys()),
# Use a heuristic: if the metagraph is available, but
# graph is not, then we assume the metagraph contains the graph.
GRAPH: self._graph is not None,
META_GRAPH: self._meta_graph is not None,
RUN_METADATA: list(self._tagged_metadata.keys())
} | python | def Tags(self):
"""Return all tags found in the value stream.
Returns:
A `{tagType: ['list', 'of', 'tags']}` dictionary.
"""
return {
TENSORS: list(self.tensors_by_tag.keys()),
# Use a heuristic: if the metagraph is available, but
# graph is not, then we assume the metagraph contains the graph.
GRAPH: self._graph is not None,
META_GRAPH: self._meta_graph is not None,
RUN_METADATA: list(self._tagged_metadata.keys())
} | [
"def",
"Tags",
"(",
"self",
")",
":",
"return",
"{",
"TENSORS",
":",
"list",
"(",
"self",
".",
"tensors_by_tag",
".",
"keys",
"(",
")",
")",
",",
"# Use a heuristic: if the metagraph is available, but",
"# graph is not, then we assume the metagraph contains the graph.",
"GRAPH",
":",
"self",
".",
"_graph",
"is",
"not",
"None",
",",
"META_GRAPH",
":",
"self",
".",
"_meta_graph",
"is",
"not",
"None",
",",
"RUN_METADATA",
":",
"list",
"(",
"self",
".",
"_tagged_metadata",
".",
"keys",
"(",
")",
")",
"}"
] | Return all tags found in the value stream.
Returns:
A `{tagType: ['list', 'of', 'tags']}` dictionary. | [
"Return",
"all",
"tags",
"found",
"in",
"the",
"value",
"stream",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/plugin_event_accumulator.py#L358-L371 | train |
tensorflow/tensorboard | tensorboard/backend/event_processing/plugin_event_accumulator.py | EventAccumulator._MaybePurgeOrphanedData | def _MaybePurgeOrphanedData(self, event):
"""Maybe purge orphaned data due to a TensorFlow crash.
When TensorFlow crashes at step T+O and restarts at step T, any events
written after step T are now "orphaned" and will be at best misleading if
they are included in TensorBoard.
This logic attempts to determine if there is orphaned data, and purge it
if it is found.
Args:
event: The event to use as a reference, to determine if a purge is needed.
"""
if not self.purge_orphaned_data:
return
## Check if the event happened after a crash, and purge expired tags.
if self.file_version and self.file_version >= 2:
## If the file_version is recent enough, use the SessionLog enum
## to check for restarts.
self._CheckForRestartAndMaybePurge(event)
else:
## If there is no file version, default to old logic of checking for
## out of order steps.
self._CheckForOutOfOrderStepAndMaybePurge(event)
# After checking, update the most recent summary step and wall time.
if event.HasField('summary'):
self.most_recent_step = event.step
self.most_recent_wall_time = event.wall_time | python | def _MaybePurgeOrphanedData(self, event):
"""Maybe purge orphaned data due to a TensorFlow crash.
When TensorFlow crashes at step T+O and restarts at step T, any events
written after step T are now "orphaned" and will be at best misleading if
they are included in TensorBoard.
This logic attempts to determine if there is orphaned data, and purge it
if it is found.
Args:
event: The event to use as a reference, to determine if a purge is needed.
"""
if not self.purge_orphaned_data:
return
## Check if the event happened after a crash, and purge expired tags.
if self.file_version and self.file_version >= 2:
## If the file_version is recent enough, use the SessionLog enum
## to check for restarts.
self._CheckForRestartAndMaybePurge(event)
else:
## If there is no file version, default to old logic of checking for
## out of order steps.
self._CheckForOutOfOrderStepAndMaybePurge(event)
# After checking, update the most recent summary step and wall time.
if event.HasField('summary'):
self.most_recent_step = event.step
self.most_recent_wall_time = event.wall_time | [
"def",
"_MaybePurgeOrphanedData",
"(",
"self",
",",
"event",
")",
":",
"if",
"not",
"self",
".",
"purge_orphaned_data",
":",
"return",
"## Check if the event happened after a crash, and purge expired tags.",
"if",
"self",
".",
"file_version",
"and",
"self",
".",
"file_version",
">=",
"2",
":",
"## If the file_version is recent enough, use the SessionLog enum",
"## to check for restarts.",
"self",
".",
"_CheckForRestartAndMaybePurge",
"(",
"event",
")",
"else",
":",
"## If there is no file version, default to old logic of checking for",
"## out of order steps.",
"self",
".",
"_CheckForOutOfOrderStepAndMaybePurge",
"(",
"event",
")",
"# After checking, update the most recent summary step and wall time.",
"if",
"event",
".",
"HasField",
"(",
"'summary'",
")",
":",
"self",
".",
"most_recent_step",
"=",
"event",
".",
"step",
"self",
".",
"most_recent_wall_time",
"=",
"event",
".",
"wall_time"
] | Maybe purge orphaned data due to a TensorFlow crash.
When TensorFlow crashes at step T+O and restarts at step T, any events
written after step T are now "orphaned" and will be at best misleading if
they are included in TensorBoard.
This logic attempts to determine if there is orphaned data, and purge it
if it is found.
Args:
event: The event to use as a reference, to determine if a purge is needed. | [
"Maybe",
"purge",
"orphaned",
"data",
"due",
"to",
"a",
"TensorFlow",
"crash",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/plugin_event_accumulator.py#L439-L466 | train |
tensorflow/tensorboard | tensorboard/backend/event_processing/plugin_event_accumulator.py | EventAccumulator._CheckForOutOfOrderStepAndMaybePurge | def _CheckForOutOfOrderStepAndMaybePurge(self, event):
"""Check for out-of-order event.step and discard expired events for tags.
Check if the event is out of order relative to the global most recent step.
If it is, purge outdated summaries for tags that the event contains.
Args:
event: The event to use as reference. If the event is out-of-order, all
events with the same tags, but with a greater event.step will be purged.
"""
if event.step < self.most_recent_step and event.HasField('summary'):
self._Purge(event, by_tags=True) | python | def _CheckForOutOfOrderStepAndMaybePurge(self, event):
"""Check for out-of-order event.step and discard expired events for tags.
Check if the event is out of order relative to the global most recent step.
If it is, purge outdated summaries for tags that the event contains.
Args:
event: The event to use as reference. If the event is out-of-order, all
events with the same tags, but with a greater event.step will be purged.
"""
if event.step < self.most_recent_step and event.HasField('summary'):
self._Purge(event, by_tags=True) | [
"def",
"_CheckForOutOfOrderStepAndMaybePurge",
"(",
"self",
",",
"event",
")",
":",
"if",
"event",
".",
"step",
"<",
"self",
".",
"most_recent_step",
"and",
"event",
".",
"HasField",
"(",
"'summary'",
")",
":",
"self",
".",
"_Purge",
"(",
"event",
",",
"by_tags",
"=",
"True",
")"
] | Check for out-of-order event.step and discard expired events for tags.
Check if the event is out of order relative to the global most recent step.
If it is, purge outdated summaries for tags that the event contains.
Args:
event: The event to use as reference. If the event is out-of-order, all
events with the same tags, but with a greater event.step will be purged. | [
"Check",
"for",
"out",
"-",
"of",
"-",
"order",
"event",
".",
"step",
"and",
"discard",
"expired",
"events",
"for",
"tags",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/plugin_event_accumulator.py#L488-L499 | train |
tensorflow/tensorboard | tensorboard/backend/event_processing/plugin_event_accumulator.py | EventAccumulator._Purge | def _Purge(self, event, by_tags):
"""Purge all events that have occurred after the given event.step.
If by_tags is True, purge all events that occurred after the given
event.step, but only for the tags that the event has. Non-sequential
event.steps suggest that a TensorFlow restart occurred, and we discard
the out-of-order events to display a consistent view in TensorBoard.
Discarding by tags is the safer method, when we are unsure whether a restart
has occurred, given that threading in supervisor can cause events of
different tags to arrive with unsynchronized step values.
If by_tags is False, then purge all events with event.step greater than the
given event.step. This can be used when we are certain that a TensorFlow
restart has occurred and these events can be discarded.
Args:
event: The event to use as reference for the purge. All events with
the same tags, but with a greater event.step will be purged.
by_tags: Bool to dictate whether to discard all out-of-order events or
only those that are associated with the given reference event.
"""
## Keep data in reservoirs that has a step less than event.step
_NotExpired = lambda x: x.step < event.step
num_expired = 0
if by_tags:
for value in event.summary.value:
if value.tag in self.tensors_by_tag:
tag_reservoir = self.tensors_by_tag[value.tag]
num_expired += tag_reservoir.FilterItems(
_NotExpired, _TENSOR_RESERVOIR_KEY)
else:
for tag_reservoir in six.itervalues(self.tensors_by_tag):
num_expired += tag_reservoir.FilterItems(
_NotExpired, _TENSOR_RESERVOIR_KEY)
if num_expired > 0:
purge_msg = _GetPurgeMessage(self.most_recent_step,
self.most_recent_wall_time, event.step,
event.wall_time, num_expired)
logger.warn(purge_msg) | python | def _Purge(self, event, by_tags):
"""Purge all events that have occurred after the given event.step.
If by_tags is True, purge all events that occurred after the given
event.step, but only for the tags that the event has. Non-sequential
event.steps suggest that a TensorFlow restart occurred, and we discard
the out-of-order events to display a consistent view in TensorBoard.
Discarding by tags is the safer method, when we are unsure whether a restart
has occurred, given that threading in supervisor can cause events of
different tags to arrive with unsynchronized step values.
If by_tags is False, then purge all events with event.step greater than the
given event.step. This can be used when we are certain that a TensorFlow
restart has occurred and these events can be discarded.
Args:
event: The event to use as reference for the purge. All events with
the same tags, but with a greater event.step will be purged.
by_tags: Bool to dictate whether to discard all out-of-order events or
only those that are associated with the given reference event.
"""
## Keep data in reservoirs that has a step less than event.step
_NotExpired = lambda x: x.step < event.step
num_expired = 0
if by_tags:
for value in event.summary.value:
if value.tag in self.tensors_by_tag:
tag_reservoir = self.tensors_by_tag[value.tag]
num_expired += tag_reservoir.FilterItems(
_NotExpired, _TENSOR_RESERVOIR_KEY)
else:
for tag_reservoir in six.itervalues(self.tensors_by_tag):
num_expired += tag_reservoir.FilterItems(
_NotExpired, _TENSOR_RESERVOIR_KEY)
if num_expired > 0:
purge_msg = _GetPurgeMessage(self.most_recent_step,
self.most_recent_wall_time, event.step,
event.wall_time, num_expired)
logger.warn(purge_msg) | [
"def",
"_Purge",
"(",
"self",
",",
"event",
",",
"by_tags",
")",
":",
"## Keep data in reservoirs that has a step less than event.step",
"_NotExpired",
"=",
"lambda",
"x",
":",
"x",
".",
"step",
"<",
"event",
".",
"step",
"num_expired",
"=",
"0",
"if",
"by_tags",
":",
"for",
"value",
"in",
"event",
".",
"summary",
".",
"value",
":",
"if",
"value",
".",
"tag",
"in",
"self",
".",
"tensors_by_tag",
":",
"tag_reservoir",
"=",
"self",
".",
"tensors_by_tag",
"[",
"value",
".",
"tag",
"]",
"num_expired",
"+=",
"tag_reservoir",
".",
"FilterItems",
"(",
"_NotExpired",
",",
"_TENSOR_RESERVOIR_KEY",
")",
"else",
":",
"for",
"tag_reservoir",
"in",
"six",
".",
"itervalues",
"(",
"self",
".",
"tensors_by_tag",
")",
":",
"num_expired",
"+=",
"tag_reservoir",
".",
"FilterItems",
"(",
"_NotExpired",
",",
"_TENSOR_RESERVOIR_KEY",
")",
"if",
"num_expired",
">",
"0",
":",
"purge_msg",
"=",
"_GetPurgeMessage",
"(",
"self",
".",
"most_recent_step",
",",
"self",
".",
"most_recent_wall_time",
",",
"event",
".",
"step",
",",
"event",
".",
"wall_time",
",",
"num_expired",
")",
"logger",
".",
"warn",
"(",
"purge_msg",
")"
] | Purge all events that have occurred after the given event.step.
If by_tags is True, purge all events that occurred after the given
event.step, but only for the tags that the event has. Non-sequential
event.steps suggest that a TensorFlow restart occurred, and we discard
the out-of-order events to display a consistent view in TensorBoard.
Discarding by tags is the safer method, when we are unsure whether a restart
has occurred, given that threading in supervisor can cause events of
different tags to arrive with unsynchronized step values.
If by_tags is False, then purge all events with event.step greater than the
given event.step. This can be used when we are certain that a TensorFlow
restart has occurred and these events can be discarded.
Args:
event: The event to use as reference for the purge. All events with
the same tags, but with a greater event.step will be purged.
by_tags: Bool to dictate whether to discard all out-of-order events or
only those that are associated with the given reference event. | [
"Purge",
"all",
"events",
"that",
"have",
"occurred",
"after",
"the",
"given",
"event",
".",
"step",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/event_processing/plugin_event_accumulator.py#L517-L557 | train |
tensorflow/tensorboard | tensorboard/plugins/beholder/beholder_plugin_loader.py | BeholderPluginLoader.load | def load(self, context):
"""Returns the plugin, if possible.
Args:
context: The TBContext flags.
Returns:
A BeholderPlugin instance or None if it couldn't be loaded.
"""
try:
# pylint: disable=g-import-not-at-top,unused-import
import tensorflow
except ImportError:
return
# pylint: disable=g-import-not-at-top
from tensorboard.plugins.beholder.beholder_plugin import BeholderPlugin
return BeholderPlugin(context) | python | def load(self, context):
"""Returns the plugin, if possible.
Args:
context: The TBContext flags.
Returns:
A BeholderPlugin instance or None if it couldn't be loaded.
"""
try:
# pylint: disable=g-import-not-at-top,unused-import
import tensorflow
except ImportError:
return
# pylint: disable=g-import-not-at-top
from tensorboard.plugins.beholder.beholder_plugin import BeholderPlugin
return BeholderPlugin(context) | [
"def",
"load",
"(",
"self",
",",
"context",
")",
":",
"try",
":",
"# pylint: disable=g-import-not-at-top,unused-import",
"import",
"tensorflow",
"except",
"ImportError",
":",
"return",
"# pylint: disable=g-import-not-at-top",
"from",
"tensorboard",
".",
"plugins",
".",
"beholder",
".",
"beholder_plugin",
"import",
"BeholderPlugin",
"return",
"BeholderPlugin",
"(",
"context",
")"
] | Returns the plugin, if possible.
Args:
context: The TBContext flags.
Returns:
A BeholderPlugin instance or None if it couldn't be loaded. | [
"Returns",
"the",
"plugin",
"if",
"possible",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/beholder/beholder_plugin_loader.py#L30-L46 | train |
tensorflow/tensorboard | tensorboard/plugins/graph/keras_util.py | _walk_layers | def _walk_layers(keras_layer):
"""Walks the nested keras layer configuration in preorder.
Args:
keras_layer: Keras configuration from model.to_json.
Yields:
A tuple of (name_scope, layer_config).
name_scope: a string representing a scope name, similar to that of tf.name_scope.
layer_config: a dict representing a Keras layer configuration.
"""
yield ('', keras_layer)
if keras_layer.get('config').get('layers'):
name_scope = keras_layer.get('config').get('name')
for layer in keras_layer.get('config').get('layers'):
for (sub_name_scope, sublayer) in _walk_layers(layer):
sub_name_scope = '%s/%s' % (
name_scope, sub_name_scope) if sub_name_scope else name_scope
yield (sub_name_scope, sublayer) | python | def _walk_layers(keras_layer):
"""Walks the nested keras layer configuration in preorder.
Args:
keras_layer: Keras configuration from model.to_json.
Yields:
A tuple of (name_scope, layer_config).
name_scope: a string representing a scope name, similar to that of tf.name_scope.
layer_config: a dict representing a Keras layer configuration.
"""
yield ('', keras_layer)
if keras_layer.get('config').get('layers'):
name_scope = keras_layer.get('config').get('name')
for layer in keras_layer.get('config').get('layers'):
for (sub_name_scope, sublayer) in _walk_layers(layer):
sub_name_scope = '%s/%s' % (
name_scope, sub_name_scope) if sub_name_scope else name_scope
yield (sub_name_scope, sublayer) | [
"def",
"_walk_layers",
"(",
"keras_layer",
")",
":",
"yield",
"(",
"''",
",",
"keras_layer",
")",
"if",
"keras_layer",
".",
"get",
"(",
"'config'",
")",
".",
"get",
"(",
"'layers'",
")",
":",
"name_scope",
"=",
"keras_layer",
".",
"get",
"(",
"'config'",
")",
".",
"get",
"(",
"'name'",
")",
"for",
"layer",
"in",
"keras_layer",
".",
"get",
"(",
"'config'",
")",
".",
"get",
"(",
"'layers'",
")",
":",
"for",
"(",
"sub_name_scope",
",",
"sublayer",
")",
"in",
"_walk_layers",
"(",
"layer",
")",
":",
"sub_name_scope",
"=",
"'%s/%s'",
"%",
"(",
"name_scope",
",",
"sub_name_scope",
")",
"if",
"sub_name_scope",
"else",
"name_scope",
"yield",
"(",
"sub_name_scope",
",",
"sublayer",
")"
] | Walks the nested keras layer configuration in preorder.
Args:
keras_layer: Keras configuration from model.to_json.
Yields:
A tuple of (name_scope, layer_config).
name_scope: a string representing a scope name, similar to that of tf.name_scope.
layer_config: a dict representing a Keras layer configuration. | [
"Walks",
"the",
"nested",
"keras",
"layer",
"configuration",
"in",
"preorder",
".",
"Args",
":",
"keras_layer",
":",
"Keras",
"configuration",
"from",
"model",
".",
"to_json",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/graph/keras_util.py#L48-L65 | train |
tensorflow/tensorboard | tensorboard/plugins/graph/keras_util.py | _update_dicts | def _update_dicts(name_scope,
model_layer,
input_to_in_layer,
model_name_to_output,
prev_node_name):
"""Updates input_to_in_layer, model_name_to_output, and prev_node_name
based on the model_layer.
Args:
name_scope: a string representing a scope name, similar to that of tf.name_scope.
model_layer: a dict representing a Keras model configuration.
input_to_in_layer: a dict mapping Keras.layers.Input to inbound layer.
model_name_to_output: a dict mapping Keras Model name to output layer of the model.
prev_node_name: a string representing a previous, in sequential model layout,
node name.
Returns:
A tuple of (input_to_in_layer, model_name_to_output, prev_node_name).
input_to_in_layer: a dict mapping Keras.layers.Input to inbound layer.
model_name_to_output: a dict mapping Keras Model name to output layer of the model.
prev_node_name: a string representing a previous, in sequential model layout,
node name.
"""
layer_config = model_layer.get('config')
if not layer_config.get('layers'):
raise ValueError('layer is not a model.')
node_name = _scoped_name(name_scope, layer_config.get('name'))
input_layers = layer_config.get('input_layers')
output_layers = layer_config.get('output_layers')
inbound_nodes = model_layer.get('inbound_nodes')
is_functional_model = bool(input_layers and output_layers)
# In case of [1] and the parent model is functional, current layer
# will have the 'inbound_nodes' property.
is_parent_functional_model = bool(inbound_nodes)
if is_parent_functional_model and is_functional_model:
for (input_layer, inbound_node) in zip(input_layers, inbound_nodes):
input_layer_name = _scoped_name(node_name, input_layer)
inbound_node_name = _scoped_name(name_scope, inbound_node[0])
input_to_in_layer[input_layer_name] = inbound_node_name
elif is_parent_functional_model and not is_functional_model:
# Sequential model can take only one input. Make sure inbound to the
# model is linked to the first layer in the Sequential model.
prev_node_name = _scoped_name(name_scope, inbound_nodes[0][0][0])
elif not is_parent_functional_model and prev_node_name and is_functional_model:
assert len(input_layers) == 1, (
'Cannot have multi-input Functional model when parent model '
'is not Functional. Number of input layers: %d' % len(input_layer))
input_layer = input_layers[0]
input_layer_name = _scoped_name(node_name, input_layer)
input_to_in_layer[input_layer_name] = prev_node_name
if is_functional_model and output_layers:
layers = _norm_to_list_of_layers(output_layers)
layer_names = [_scoped_name(node_name, layer[0]) for layer in layers]
model_name_to_output[node_name] = layer_names
else:
last_layer = layer_config.get('layers')[-1]
last_layer_name = last_layer.get('config').get('name')
output_node = _scoped_name(node_name, last_layer_name)
model_name_to_output[node_name] = [output_node]
return (input_to_in_layer, model_name_to_output, prev_node_name) | python | def _update_dicts(name_scope,
model_layer,
input_to_in_layer,
model_name_to_output,
prev_node_name):
"""Updates input_to_in_layer, model_name_to_output, and prev_node_name
based on the model_layer.
Args:
name_scope: a string representing a scope name, similar to that of tf.name_scope.
model_layer: a dict representing a Keras model configuration.
input_to_in_layer: a dict mapping Keras.layers.Input to inbound layer.
model_name_to_output: a dict mapping Keras Model name to output layer of the model.
prev_node_name: a string representing a previous, in sequential model layout,
node name.
Returns:
A tuple of (input_to_in_layer, model_name_to_output, prev_node_name).
input_to_in_layer: a dict mapping Keras.layers.Input to inbound layer.
model_name_to_output: a dict mapping Keras Model name to output layer of the model.
prev_node_name: a string representing a previous, in sequential model layout,
node name.
"""
layer_config = model_layer.get('config')
if not layer_config.get('layers'):
raise ValueError('layer is not a model.')
node_name = _scoped_name(name_scope, layer_config.get('name'))
input_layers = layer_config.get('input_layers')
output_layers = layer_config.get('output_layers')
inbound_nodes = model_layer.get('inbound_nodes')
is_functional_model = bool(input_layers and output_layers)
# In case of [1] and the parent model is functional, current layer
# will have the 'inbound_nodes' property.
is_parent_functional_model = bool(inbound_nodes)
if is_parent_functional_model and is_functional_model:
for (input_layer, inbound_node) in zip(input_layers, inbound_nodes):
input_layer_name = _scoped_name(node_name, input_layer)
inbound_node_name = _scoped_name(name_scope, inbound_node[0])
input_to_in_layer[input_layer_name] = inbound_node_name
elif is_parent_functional_model and not is_functional_model:
# Sequential model can take only one input. Make sure inbound to the
# model is linked to the first layer in the Sequential model.
prev_node_name = _scoped_name(name_scope, inbound_nodes[0][0][0])
elif not is_parent_functional_model and prev_node_name and is_functional_model:
assert len(input_layers) == 1, (
'Cannot have multi-input Functional model when parent model '
'is not Functional. Number of input layers: %d' % len(input_layer))
input_layer = input_layers[0]
input_layer_name = _scoped_name(node_name, input_layer)
input_to_in_layer[input_layer_name] = prev_node_name
if is_functional_model and output_layers:
layers = _norm_to_list_of_layers(output_layers)
layer_names = [_scoped_name(node_name, layer[0]) for layer in layers]
model_name_to_output[node_name] = layer_names
else:
last_layer = layer_config.get('layers')[-1]
last_layer_name = last_layer.get('config').get('name')
output_node = _scoped_name(node_name, last_layer_name)
model_name_to_output[node_name] = [output_node]
return (input_to_in_layer, model_name_to_output, prev_node_name) | [
"def",
"_update_dicts",
"(",
"name_scope",
",",
"model_layer",
",",
"input_to_in_layer",
",",
"model_name_to_output",
",",
"prev_node_name",
")",
":",
"layer_config",
"=",
"model_layer",
".",
"get",
"(",
"'config'",
")",
"if",
"not",
"layer_config",
".",
"get",
"(",
"'layers'",
")",
":",
"raise",
"ValueError",
"(",
"'layer is not a model.'",
")",
"node_name",
"=",
"_scoped_name",
"(",
"name_scope",
",",
"layer_config",
".",
"get",
"(",
"'name'",
")",
")",
"input_layers",
"=",
"layer_config",
".",
"get",
"(",
"'input_layers'",
")",
"output_layers",
"=",
"layer_config",
".",
"get",
"(",
"'output_layers'",
")",
"inbound_nodes",
"=",
"model_layer",
".",
"get",
"(",
"'inbound_nodes'",
")",
"is_functional_model",
"=",
"bool",
"(",
"input_layers",
"and",
"output_layers",
")",
"# In case of [1] and the parent model is functional, current layer",
"# will have the 'inbound_nodes' property.",
"is_parent_functional_model",
"=",
"bool",
"(",
"inbound_nodes",
")",
"if",
"is_parent_functional_model",
"and",
"is_functional_model",
":",
"for",
"(",
"input_layer",
",",
"inbound_node",
")",
"in",
"zip",
"(",
"input_layers",
",",
"inbound_nodes",
")",
":",
"input_layer_name",
"=",
"_scoped_name",
"(",
"node_name",
",",
"input_layer",
")",
"inbound_node_name",
"=",
"_scoped_name",
"(",
"name_scope",
",",
"inbound_node",
"[",
"0",
"]",
")",
"input_to_in_layer",
"[",
"input_layer_name",
"]",
"=",
"inbound_node_name",
"elif",
"is_parent_functional_model",
"and",
"not",
"is_functional_model",
":",
"# Sequential model can take only one input. Make sure inbound to the",
"# model is linked to the first layer in the Sequential model.",
"prev_node_name",
"=",
"_scoped_name",
"(",
"name_scope",
",",
"inbound_nodes",
"[",
"0",
"]",
"[",
"0",
"]",
"[",
"0",
"]",
")",
"elif",
"not",
"is_parent_functional_model",
"and",
"prev_node_name",
"and",
"is_functional_model",
":",
"assert",
"len",
"(",
"input_layers",
")",
"==",
"1",
",",
"(",
"'Cannot have multi-input Functional model when parent model '",
"'is not Functional. Number of input layers: %d'",
"%",
"len",
"(",
"input_layer",
")",
")",
"input_layer",
"=",
"input_layers",
"[",
"0",
"]",
"input_layer_name",
"=",
"_scoped_name",
"(",
"node_name",
",",
"input_layer",
")",
"input_to_in_layer",
"[",
"input_layer_name",
"]",
"=",
"prev_node_name",
"if",
"is_functional_model",
"and",
"output_layers",
":",
"layers",
"=",
"_norm_to_list_of_layers",
"(",
"output_layers",
")",
"layer_names",
"=",
"[",
"_scoped_name",
"(",
"node_name",
",",
"layer",
"[",
"0",
"]",
")",
"for",
"layer",
"in",
"layers",
"]",
"model_name_to_output",
"[",
"node_name",
"]",
"=",
"layer_names",
"else",
":",
"last_layer",
"=",
"layer_config",
".",
"get",
"(",
"'layers'",
")",
"[",
"-",
"1",
"]",
"last_layer_name",
"=",
"last_layer",
".",
"get",
"(",
"'config'",
")",
".",
"get",
"(",
"'name'",
")",
"output_node",
"=",
"_scoped_name",
"(",
"node_name",
",",
"last_layer_name",
")",
"model_name_to_output",
"[",
"node_name",
"]",
"=",
"[",
"output_node",
"]",
"return",
"(",
"input_to_in_layer",
",",
"model_name_to_output",
",",
"prev_node_name",
")"
] | Updates input_to_in_layer, model_name_to_output, and prev_node_name
based on the model_layer.
Args:
name_scope: a string representing a scope name, similar to that of tf.name_scope.
model_layer: a dict representing a Keras model configuration.
input_to_in_layer: a dict mapping Keras.layers.Input to inbound layer.
model_name_to_output: a dict mapping Keras Model name to output layer of the model.
prev_node_name: a string representing a previous, in sequential model layout,
node name.
Returns:
A tuple of (input_to_in_layer, model_name_to_output, prev_node_name).
input_to_in_layer: a dict mapping Keras.layers.Input to inbound layer.
model_name_to_output: a dict mapping Keras Model name to output layer of the model.
prev_node_name: a string representing a previous, in sequential model layout,
node name. | [
"Updates",
"input_to_in_layer",
"model_name_to_output",
"and",
"prev_node_name",
"based",
"on",
"the",
"model_layer",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/graph/keras_util.py#L114-L177 | train |
tensorflow/tensorboard | tensorboard/plugins/graph/keras_util.py | keras_model_to_graph_def | def keras_model_to_graph_def(keras_layer):
"""Returns a GraphDef representation of the Keras model in a dict form.
Note that it only supports models that implemented to_json().
Args:
keras_layer: A dict from Keras model.to_json().
Returns:
A GraphDef representation of the layers in the model.
"""
input_to_layer = {}
model_name_to_output = {}
g = GraphDef()
# Sequential model layers do not have a field "inbound_nodes" but
# instead are defined implicitly via order of layers.
prev_node_name = None
for (name_scope, layer) in _walk_layers(keras_layer):
if _is_model(layer):
(input_to_layer, model_name_to_output, prev_node_name) = _update_dicts(
name_scope, layer, input_to_layer, model_name_to_output, prev_node_name)
continue
layer_config = layer.get('config')
node_name = _scoped_name(name_scope, layer_config.get('name'))
node_def = g.node.add()
node_def.name = node_name
if layer.get('class_name') is not None:
keras_cls_name = layer.get('class_name').encode('ascii')
node_def.attr['keras_class'].s = keras_cls_name
if layer_config.get('dtype') is not None:
tf_dtype = dtypes.as_dtype(layer_config.get('dtype'))
node_def.attr['dtype'].type = tf_dtype.as_datatype_enum
if layer.get('inbound_nodes') is not None:
for maybe_inbound_node in layer.get('inbound_nodes'):
inbound_nodes = _norm_to_list_of_layers(maybe_inbound_node)
for [name, size, index, _] in inbound_nodes:
inbound_name = _scoped_name(name_scope, name)
# An input to a layer can be output from a model. In that case, the name
# of inbound_nodes to a layer is a name of a model. Remap the name of the
# model to output layer of the model. Also, since there can be multiple
# outputs in a model, make sure we pick the right output_layer from the model.
inbound_node_names = model_name_to_output.get(
inbound_name, [inbound_name])
node_def.input.append(inbound_node_names[index])
elif prev_node_name is not None:
node_def.input.append(prev_node_name)
if node_name in input_to_layer:
node_def.input.append(input_to_layer.get(node_name))
prev_node_name = node_def.name
return g | python | def keras_model_to_graph_def(keras_layer):
"""Returns a GraphDef representation of the Keras model in a dict form.
Note that it only supports models that implemented to_json().
Args:
keras_layer: A dict from Keras model.to_json().
Returns:
A GraphDef representation of the layers in the model.
"""
input_to_layer = {}
model_name_to_output = {}
g = GraphDef()
# Sequential model layers do not have a field "inbound_nodes" but
# instead are defined implicitly via order of layers.
prev_node_name = None
for (name_scope, layer) in _walk_layers(keras_layer):
if _is_model(layer):
(input_to_layer, model_name_to_output, prev_node_name) = _update_dicts(
name_scope, layer, input_to_layer, model_name_to_output, prev_node_name)
continue
layer_config = layer.get('config')
node_name = _scoped_name(name_scope, layer_config.get('name'))
node_def = g.node.add()
node_def.name = node_name
if layer.get('class_name') is not None:
keras_cls_name = layer.get('class_name').encode('ascii')
node_def.attr['keras_class'].s = keras_cls_name
if layer_config.get('dtype') is not None:
tf_dtype = dtypes.as_dtype(layer_config.get('dtype'))
node_def.attr['dtype'].type = tf_dtype.as_datatype_enum
if layer.get('inbound_nodes') is not None:
for maybe_inbound_node in layer.get('inbound_nodes'):
inbound_nodes = _norm_to_list_of_layers(maybe_inbound_node)
for [name, size, index, _] in inbound_nodes:
inbound_name = _scoped_name(name_scope, name)
# An input to a layer can be output from a model. In that case, the name
# of inbound_nodes to a layer is a name of a model. Remap the name of the
# model to output layer of the model. Also, since there can be multiple
# outputs in a model, make sure we pick the right output_layer from the model.
inbound_node_names = model_name_to_output.get(
inbound_name, [inbound_name])
node_def.input.append(inbound_node_names[index])
elif prev_node_name is not None:
node_def.input.append(prev_node_name)
if node_name in input_to_layer:
node_def.input.append(input_to_layer.get(node_name))
prev_node_name = node_def.name
return g | [
"def",
"keras_model_to_graph_def",
"(",
"keras_layer",
")",
":",
"input_to_layer",
"=",
"{",
"}",
"model_name_to_output",
"=",
"{",
"}",
"g",
"=",
"GraphDef",
"(",
")",
"# Sequential model layers do not have a field \"inbound_nodes\" but",
"# instead are defined implicitly via order of layers.",
"prev_node_name",
"=",
"None",
"for",
"(",
"name_scope",
",",
"layer",
")",
"in",
"_walk_layers",
"(",
"keras_layer",
")",
":",
"if",
"_is_model",
"(",
"layer",
")",
":",
"(",
"input_to_layer",
",",
"model_name_to_output",
",",
"prev_node_name",
")",
"=",
"_update_dicts",
"(",
"name_scope",
",",
"layer",
",",
"input_to_layer",
",",
"model_name_to_output",
",",
"prev_node_name",
")",
"continue",
"layer_config",
"=",
"layer",
".",
"get",
"(",
"'config'",
")",
"node_name",
"=",
"_scoped_name",
"(",
"name_scope",
",",
"layer_config",
".",
"get",
"(",
"'name'",
")",
")",
"node_def",
"=",
"g",
".",
"node",
".",
"add",
"(",
")",
"node_def",
".",
"name",
"=",
"node_name",
"if",
"layer",
".",
"get",
"(",
"'class_name'",
")",
"is",
"not",
"None",
":",
"keras_cls_name",
"=",
"layer",
".",
"get",
"(",
"'class_name'",
")",
".",
"encode",
"(",
"'ascii'",
")",
"node_def",
".",
"attr",
"[",
"'keras_class'",
"]",
".",
"s",
"=",
"keras_cls_name",
"if",
"layer_config",
".",
"get",
"(",
"'dtype'",
")",
"is",
"not",
"None",
":",
"tf_dtype",
"=",
"dtypes",
".",
"as_dtype",
"(",
"layer_config",
".",
"get",
"(",
"'dtype'",
")",
")",
"node_def",
".",
"attr",
"[",
"'dtype'",
"]",
".",
"type",
"=",
"tf_dtype",
".",
"as_datatype_enum",
"if",
"layer",
".",
"get",
"(",
"'inbound_nodes'",
")",
"is",
"not",
"None",
":",
"for",
"maybe_inbound_node",
"in",
"layer",
".",
"get",
"(",
"'inbound_nodes'",
")",
":",
"inbound_nodes",
"=",
"_norm_to_list_of_layers",
"(",
"maybe_inbound_node",
")",
"for",
"[",
"name",
",",
"size",
",",
"index",
",",
"_",
"]",
"in",
"inbound_nodes",
":",
"inbound_name",
"=",
"_scoped_name",
"(",
"name_scope",
",",
"name",
")",
"# An input to a layer can be output from a model. In that case, the name",
"# of inbound_nodes to a layer is a name of a model. Remap the name of the",
"# model to output layer of the model. Also, since there can be multiple",
"# outputs in a model, make sure we pick the right output_layer from the model.",
"inbound_node_names",
"=",
"model_name_to_output",
".",
"get",
"(",
"inbound_name",
",",
"[",
"inbound_name",
"]",
")",
"node_def",
".",
"input",
".",
"append",
"(",
"inbound_node_names",
"[",
"index",
"]",
")",
"elif",
"prev_node_name",
"is",
"not",
"None",
":",
"node_def",
".",
"input",
".",
"append",
"(",
"prev_node_name",
")",
"if",
"node_name",
"in",
"input_to_layer",
":",
"node_def",
".",
"input",
".",
"append",
"(",
"input_to_layer",
".",
"get",
"(",
"node_name",
")",
")",
"prev_node_name",
"=",
"node_def",
".",
"name",
"return",
"g"
] | Returns a GraphDef representation of the Keras model in a dict form.
Note that it only supports models that implemented to_json().
Args:
keras_layer: A dict from Keras model.to_json().
Returns:
A GraphDef representation of the layers in the model. | [
"Returns",
"a",
"GraphDef",
"representation",
"of",
"the",
"Keras",
"model",
"in",
"a",
"dict",
"form",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/graph/keras_util.py#L180-L239 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/hparams_plugin_loader.py | HParamsPluginLoader.load | def load(self, context):
"""Returns the plugin, if possible.
Args:
context: The TBContext flags.
Returns:
A HParamsPlugin instance or None if it couldn't be loaded.
"""
try:
# pylint: disable=g-import-not-at-top,unused-import
import tensorflow
except ImportError:
return
# pylint: disable=g-import-not-at-top
from tensorboard.plugins.hparams.hparams_plugin import HParamsPlugin
return HParamsPlugin(context) | python | def load(self, context):
"""Returns the plugin, if possible.
Args:
context: The TBContext flags.
Returns:
A HParamsPlugin instance or None if it couldn't be loaded.
"""
try:
# pylint: disable=g-import-not-at-top,unused-import
import tensorflow
except ImportError:
return
# pylint: disable=g-import-not-at-top
from tensorboard.plugins.hparams.hparams_plugin import HParamsPlugin
return HParamsPlugin(context) | [
"def",
"load",
"(",
"self",
",",
"context",
")",
":",
"try",
":",
"# pylint: disable=g-import-not-at-top,unused-import",
"import",
"tensorflow",
"except",
"ImportError",
":",
"return",
"# pylint: disable=g-import-not-at-top",
"from",
"tensorboard",
".",
"plugins",
".",
"hparams",
".",
"hparams_plugin",
"import",
"HParamsPlugin",
"return",
"HParamsPlugin",
"(",
"context",
")"
] | Returns the plugin, if possible.
Args:
context: The TBContext flags.
Returns:
A HParamsPlugin instance or None if it couldn't be loaded. | [
"Returns",
"the",
"plugin",
"if",
"possible",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/hparams_plugin_loader.py#L30-L46 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/hparams_plugin.py | HParamsPlugin.is_active | def is_active(self):
"""Returns True if the hparams plugin is active.
The hparams plugin is active iff there is a tag with
the hparams plugin name as its plugin name and the scalars plugin is
registered and active.
"""
if not self._context.multiplexer:
return False
scalars_plugin = self._get_scalars_plugin()
if not scalars_plugin or not scalars_plugin.is_active():
return False
return bool(self._context.multiplexer.PluginRunToTagToContent(
metadata.PLUGIN_NAME)) | python | def is_active(self):
"""Returns True if the hparams plugin is active.
The hparams plugin is active iff there is a tag with
the hparams plugin name as its plugin name and the scalars plugin is
registered and active.
"""
if not self._context.multiplexer:
return False
scalars_plugin = self._get_scalars_plugin()
if not scalars_plugin or not scalars_plugin.is_active():
return False
return bool(self._context.multiplexer.PluginRunToTagToContent(
metadata.PLUGIN_NAME)) | [
"def",
"is_active",
"(",
"self",
")",
":",
"if",
"not",
"self",
".",
"_context",
".",
"multiplexer",
":",
"return",
"False",
"scalars_plugin",
"=",
"self",
".",
"_get_scalars_plugin",
"(",
")",
"if",
"not",
"scalars_plugin",
"or",
"not",
"scalars_plugin",
".",
"is_active",
"(",
")",
":",
"return",
"False",
"return",
"bool",
"(",
"self",
".",
"_context",
".",
"multiplexer",
".",
"PluginRunToTagToContent",
"(",
"metadata",
".",
"PLUGIN_NAME",
")",
")"
] | Returns True if the hparams plugin is active.
The hparams plugin is active iff there is a tag with
the hparams plugin name as its plugin name and the scalars plugin is
registered and active. | [
"Returns",
"True",
"if",
"the",
"hparams",
"plugin",
"is",
"active",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/hparams_plugin.py#L71-L84 | train |
tensorflow/tensorboard | tensorboard/plugin_util.py | markdown_to_safe_html | def markdown_to_safe_html(markdown_string):
"""Convert Markdown to HTML that's safe to splice into the DOM.
Arguments:
markdown_string: A Unicode string or UTF-8--encoded bytestring
containing Markdown source. Markdown tables are supported.
Returns:
A string containing safe HTML.
"""
warning = ''
# Convert to utf-8 whenever we have a binary input.
if isinstance(markdown_string, six.binary_type):
markdown_string_decoded = markdown_string.decode('utf-8')
# Remove null bytes and warn if there were any, since it probably means
# we were given a bad encoding.
markdown_string = markdown_string_decoded.replace(u'\x00', u'')
num_null_bytes = len(markdown_string_decoded) - len(markdown_string)
if num_null_bytes:
warning = ('<!-- WARNING: discarded %d null bytes in markdown string '
'after UTF-8 decoding -->\n') % num_null_bytes
string_html = markdown.markdown(
markdown_string, extensions=['markdown.extensions.tables'])
string_sanitized = bleach.clean(
string_html, tags=_ALLOWED_TAGS, attributes=_ALLOWED_ATTRIBUTES)
return warning + string_sanitized | python | def markdown_to_safe_html(markdown_string):
"""Convert Markdown to HTML that's safe to splice into the DOM.
Arguments:
markdown_string: A Unicode string or UTF-8--encoded bytestring
containing Markdown source. Markdown tables are supported.
Returns:
A string containing safe HTML.
"""
warning = ''
# Convert to utf-8 whenever we have a binary input.
if isinstance(markdown_string, six.binary_type):
markdown_string_decoded = markdown_string.decode('utf-8')
# Remove null bytes and warn if there were any, since it probably means
# we were given a bad encoding.
markdown_string = markdown_string_decoded.replace(u'\x00', u'')
num_null_bytes = len(markdown_string_decoded) - len(markdown_string)
if num_null_bytes:
warning = ('<!-- WARNING: discarded %d null bytes in markdown string '
'after UTF-8 decoding -->\n') % num_null_bytes
string_html = markdown.markdown(
markdown_string, extensions=['markdown.extensions.tables'])
string_sanitized = bleach.clean(
string_html, tags=_ALLOWED_TAGS, attributes=_ALLOWED_ATTRIBUTES)
return warning + string_sanitized | [
"def",
"markdown_to_safe_html",
"(",
"markdown_string",
")",
":",
"warning",
"=",
"''",
"# Convert to utf-8 whenever we have a binary input.",
"if",
"isinstance",
"(",
"markdown_string",
",",
"six",
".",
"binary_type",
")",
":",
"markdown_string_decoded",
"=",
"markdown_string",
".",
"decode",
"(",
"'utf-8'",
")",
"# Remove null bytes and warn if there were any, since it probably means",
"# we were given a bad encoding.",
"markdown_string",
"=",
"markdown_string_decoded",
".",
"replace",
"(",
"u'\\x00'",
",",
"u''",
")",
"num_null_bytes",
"=",
"len",
"(",
"markdown_string_decoded",
")",
"-",
"len",
"(",
"markdown_string",
")",
"if",
"num_null_bytes",
":",
"warning",
"=",
"(",
"'<!-- WARNING: discarded %d null bytes in markdown string '",
"'after UTF-8 decoding -->\\n'",
")",
"%",
"num_null_bytes",
"string_html",
"=",
"markdown",
".",
"markdown",
"(",
"markdown_string",
",",
"extensions",
"=",
"[",
"'markdown.extensions.tables'",
"]",
")",
"string_sanitized",
"=",
"bleach",
".",
"clean",
"(",
"string_html",
",",
"tags",
"=",
"_ALLOWED_TAGS",
",",
"attributes",
"=",
"_ALLOWED_ATTRIBUTES",
")",
"return",
"warning",
"+",
"string_sanitized"
] | Convert Markdown to HTML that's safe to splice into the DOM.
Arguments:
markdown_string: A Unicode string or UTF-8--encoded bytestring
containing Markdown source. Markdown tables are supported.
Returns:
A string containing safe HTML. | [
"Convert",
"Markdown",
"to",
"HTML",
"that",
"s",
"safe",
"to",
"splice",
"into",
"the",
"DOM",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugin_util.py#L61-L87 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/dtypes.py | as_dtype | def as_dtype(type_value):
"""Converts the given `type_value` to a `DType`.
Args:
type_value: A value that can be converted to a `tf.DType` object. This may
currently be a `tf.DType` object, a [`DataType`
enum](https://www.tensorflow.org/code/tensorflow/core/framework/types.proto),
a string type name, or a `numpy.dtype`.
Returns:
A `DType` corresponding to `type_value`.
Raises:
TypeError: If `type_value` cannot be converted to a `DType`.
"""
if isinstance(type_value, DType):
return type_value
try:
return _INTERN_TABLE[type_value]
except KeyError:
pass
try:
return _STRING_TO_TF[type_value]
except KeyError:
pass
try:
return _PYTHON_TO_TF[type_value]
except KeyError:
pass
if isinstance(type_value, np.dtype):
# The numpy dtype for strings is variable length. We can not compare
# dtype with a single constant (np.string does not exist) to decide
# dtype is a "string" type. We need to compare the dtype.type to be
# sure it's a string type.
if type_value.type == np.string_ or type_value.type == np.unicode_:
return string
if isinstance(type_value, (type, np.dtype)):
for key, val in _NP_TO_TF:
try:
if key == type_value:
return val
except TypeError as e:
raise TypeError(
"Cannot convert {} to a dtype. {}".format(type_value, e)
)
raise TypeError("Cannot convert value %r to a TensorFlow DType." % type_value) | python | def as_dtype(type_value):
"""Converts the given `type_value` to a `DType`.
Args:
type_value: A value that can be converted to a `tf.DType` object. This may
currently be a `tf.DType` object, a [`DataType`
enum](https://www.tensorflow.org/code/tensorflow/core/framework/types.proto),
a string type name, or a `numpy.dtype`.
Returns:
A `DType` corresponding to `type_value`.
Raises:
TypeError: If `type_value` cannot be converted to a `DType`.
"""
if isinstance(type_value, DType):
return type_value
try:
return _INTERN_TABLE[type_value]
except KeyError:
pass
try:
return _STRING_TO_TF[type_value]
except KeyError:
pass
try:
return _PYTHON_TO_TF[type_value]
except KeyError:
pass
if isinstance(type_value, np.dtype):
# The numpy dtype for strings is variable length. We can not compare
# dtype with a single constant (np.string does not exist) to decide
# dtype is a "string" type. We need to compare the dtype.type to be
# sure it's a string type.
if type_value.type == np.string_ or type_value.type == np.unicode_:
return string
if isinstance(type_value, (type, np.dtype)):
for key, val in _NP_TO_TF:
try:
if key == type_value:
return val
except TypeError as e:
raise TypeError(
"Cannot convert {} to a dtype. {}".format(type_value, e)
)
raise TypeError("Cannot convert value %r to a TensorFlow DType." % type_value) | [
"def",
"as_dtype",
"(",
"type_value",
")",
":",
"if",
"isinstance",
"(",
"type_value",
",",
"DType",
")",
":",
"return",
"type_value",
"try",
":",
"return",
"_INTERN_TABLE",
"[",
"type_value",
"]",
"except",
"KeyError",
":",
"pass",
"try",
":",
"return",
"_STRING_TO_TF",
"[",
"type_value",
"]",
"except",
"KeyError",
":",
"pass",
"try",
":",
"return",
"_PYTHON_TO_TF",
"[",
"type_value",
"]",
"except",
"KeyError",
":",
"pass",
"if",
"isinstance",
"(",
"type_value",
",",
"np",
".",
"dtype",
")",
":",
"# The numpy dtype for strings is variable length. We can not compare",
"# dtype with a single constant (np.string does not exist) to decide",
"# dtype is a \"string\" type. We need to compare the dtype.type to be",
"# sure it's a string type.",
"if",
"type_value",
".",
"type",
"==",
"np",
".",
"string_",
"or",
"type_value",
".",
"type",
"==",
"np",
".",
"unicode_",
":",
"return",
"string",
"if",
"isinstance",
"(",
"type_value",
",",
"(",
"type",
",",
"np",
".",
"dtype",
")",
")",
":",
"for",
"key",
",",
"val",
"in",
"_NP_TO_TF",
":",
"try",
":",
"if",
"key",
"==",
"type_value",
":",
"return",
"val",
"except",
"TypeError",
"as",
"e",
":",
"raise",
"TypeError",
"(",
"\"Cannot convert {} to a dtype. {}\"",
".",
"format",
"(",
"type_value",
",",
"e",
")",
")",
"raise",
"TypeError",
"(",
"\"Cannot convert value %r to a TensorFlow DType.\"",
"%",
"type_value",
")"
] | Converts the given `type_value` to a `DType`.
Args:
type_value: A value that can be converted to a `tf.DType` object. This may
currently be a `tf.DType` object, a [`DataType`
enum](https://www.tensorflow.org/code/tensorflow/core/framework/types.proto),
a string type name, or a `numpy.dtype`.
Returns:
A `DType` corresponding to `type_value`.
Raises:
TypeError: If `type_value` cannot be converted to a `DType`. | [
"Converts",
"the",
"given",
"type_value",
"to",
"a",
"DType",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/dtypes.py#L639-L690 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/dtypes.py | DType.real_dtype | def real_dtype(self):
"""Returns the dtype correspond to this dtype's real part."""
base = self.base_dtype
if base == complex64:
return float32
elif base == complex128:
return float64
else:
return self | python | def real_dtype(self):
"""Returns the dtype correspond to this dtype's real part."""
base = self.base_dtype
if base == complex64:
return float32
elif base == complex128:
return float64
else:
return self | [
"def",
"real_dtype",
"(",
"self",
")",
":",
"base",
"=",
"self",
".",
"base_dtype",
"if",
"base",
"==",
"complex64",
":",
"return",
"float32",
"elif",
"base",
"==",
"complex128",
":",
"return",
"float64",
"else",
":",
"return",
"self"
] | Returns the dtype correspond to this dtype's real part. | [
"Returns",
"the",
"dtype",
"correspond",
"to",
"this",
"dtype",
"s",
"real",
"part",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/dtypes.py#L113-L121 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/dtypes.py | DType.is_integer | def is_integer(self):
"""Returns whether this is a (non-quantized) integer type."""
return (
self.is_numpy_compatible
and not self.is_quantized
and np.issubdtype(self.as_numpy_dtype, np.integer)
) | python | def is_integer(self):
"""Returns whether this is a (non-quantized) integer type."""
return (
self.is_numpy_compatible
and not self.is_quantized
and np.issubdtype(self.as_numpy_dtype, np.integer)
) | [
"def",
"is_integer",
"(",
"self",
")",
":",
"return",
"(",
"self",
".",
"is_numpy_compatible",
"and",
"not",
"self",
".",
"is_quantized",
"and",
"np",
".",
"issubdtype",
"(",
"self",
".",
"as_numpy_dtype",
",",
"np",
".",
"integer",
")",
")"
] | Returns whether this is a (non-quantized) integer type. | [
"Returns",
"whether",
"this",
"is",
"a",
"(",
"non",
"-",
"quantized",
")",
"integer",
"type",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/dtypes.py#L143-L149 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/dtypes.py | DType.is_floating | def is_floating(self):
"""Returns whether this is a (non-quantized, real) floating point type."""
return (
self.is_numpy_compatible and np.issubdtype(self.as_numpy_dtype, np.floating)
) or self.base_dtype == bfloat16 | python | def is_floating(self):
"""Returns whether this is a (non-quantized, real) floating point type."""
return (
self.is_numpy_compatible and np.issubdtype(self.as_numpy_dtype, np.floating)
) or self.base_dtype == bfloat16 | [
"def",
"is_floating",
"(",
"self",
")",
":",
"return",
"(",
"self",
".",
"is_numpy_compatible",
"and",
"np",
".",
"issubdtype",
"(",
"self",
".",
"as_numpy_dtype",
",",
"np",
".",
"floating",
")",
")",
"or",
"self",
".",
"base_dtype",
"==",
"bfloat16"
] | Returns whether this is a (non-quantized, real) floating point type. | [
"Returns",
"whether",
"this",
"is",
"a",
"(",
"non",
"-",
"quantized",
"real",
")",
"floating",
"point",
"type",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/dtypes.py#L152-L156 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/dtypes.py | DType.min | def min(self):
"""Returns the minimum representable value in this data type.
Raises:
TypeError: if this is a non-numeric, unordered, or quantized type.
"""
if self.is_quantized or self.base_dtype in (
bool,
string,
complex64,
complex128,
):
raise TypeError("Cannot find minimum value of %s." % self)
# there is no simple way to get the min value of a dtype, we have to check
# float and int types separately
try:
return np.finfo(self.as_numpy_dtype()).min
except: # bare except as possible raises by finfo not documented
try:
return np.iinfo(self.as_numpy_dtype()).min
except:
if self.base_dtype == bfloat16:
return _np_bfloat16(float.fromhex("-0x1.FEp127"))
raise TypeError("Cannot find minimum value of %s." % self) | python | def min(self):
"""Returns the minimum representable value in this data type.
Raises:
TypeError: if this is a non-numeric, unordered, or quantized type.
"""
if self.is_quantized or self.base_dtype in (
bool,
string,
complex64,
complex128,
):
raise TypeError("Cannot find minimum value of %s." % self)
# there is no simple way to get the min value of a dtype, we have to check
# float and int types separately
try:
return np.finfo(self.as_numpy_dtype()).min
except: # bare except as possible raises by finfo not documented
try:
return np.iinfo(self.as_numpy_dtype()).min
except:
if self.base_dtype == bfloat16:
return _np_bfloat16(float.fromhex("-0x1.FEp127"))
raise TypeError("Cannot find minimum value of %s." % self) | [
"def",
"min",
"(",
"self",
")",
":",
"if",
"self",
".",
"is_quantized",
"or",
"self",
".",
"base_dtype",
"in",
"(",
"bool",
",",
"string",
",",
"complex64",
",",
"complex128",
",",
")",
":",
"raise",
"TypeError",
"(",
"\"Cannot find minimum value of %s.\"",
"%",
"self",
")",
"# there is no simple way to get the min value of a dtype, we have to check",
"# float and int types separately",
"try",
":",
"return",
"np",
".",
"finfo",
"(",
"self",
".",
"as_numpy_dtype",
"(",
")",
")",
".",
"min",
"except",
":",
"# bare except as possible raises by finfo not documented",
"try",
":",
"return",
"np",
".",
"iinfo",
"(",
"self",
".",
"as_numpy_dtype",
"(",
")",
")",
".",
"min",
"except",
":",
"if",
"self",
".",
"base_dtype",
"==",
"bfloat16",
":",
"return",
"_np_bfloat16",
"(",
"float",
".",
"fromhex",
"(",
"\"-0x1.FEp127\"",
")",
")",
"raise",
"TypeError",
"(",
"\"Cannot find minimum value of %s.\"",
"%",
"self",
")"
] | Returns the minimum representable value in this data type.
Raises:
TypeError: if this is a non-numeric, unordered, or quantized type. | [
"Returns",
"the",
"minimum",
"representable",
"value",
"in",
"this",
"data",
"type",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/dtypes.py#L184-L209 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/dtypes.py | DType.limits | def limits(self, clip_negative=True):
"""Return intensity limits, i.e. (min, max) tuple, of the dtype.
Args:
clip_negative : bool, optional
If True, clip the negative range (i.e. return 0 for min intensity)
even if the image dtype allows negative values.
Returns
min, max : tuple
Lower and upper intensity limits.
"""
min, max = dtype_range[self.as_numpy_dtype] # pylint: disable=redefined-builtin
if clip_negative:
min = 0 # pylint: disable=redefined-builtin
return min, max | python | def limits(self, clip_negative=True):
"""Return intensity limits, i.e. (min, max) tuple, of the dtype.
Args:
clip_negative : bool, optional
If True, clip the negative range (i.e. return 0 for min intensity)
even if the image dtype allows negative values.
Returns
min, max : tuple
Lower and upper intensity limits.
"""
min, max = dtype_range[self.as_numpy_dtype] # pylint: disable=redefined-builtin
if clip_negative:
min = 0 # pylint: disable=redefined-builtin
return min, max | [
"def",
"limits",
"(",
"self",
",",
"clip_negative",
"=",
"True",
")",
":",
"min",
",",
"max",
"=",
"dtype_range",
"[",
"self",
".",
"as_numpy_dtype",
"]",
"# pylint: disable=redefined-builtin",
"if",
"clip_negative",
":",
"min",
"=",
"0",
"# pylint: disable=redefined-builtin",
"return",
"min",
",",
"max"
] | Return intensity limits, i.e. (min, max) tuple, of the dtype.
Args:
clip_negative : bool, optional
If True, clip the negative range (i.e. return 0 for min intensity)
even if the image dtype allows negative values.
Returns
min, max : tuple
Lower and upper intensity limits. | [
"Return",
"intensity",
"limits",
"i",
".",
"e",
".",
"(",
"min",
"max",
")",
"tuple",
"of",
"the",
"dtype",
".",
"Args",
":",
"clip_negative",
":",
"bool",
"optional",
"If",
"True",
"clip",
"the",
"negative",
"range",
"(",
"i",
".",
"e",
".",
"return",
"0",
"for",
"min",
"intensity",
")",
"even",
"if",
"the",
"image",
"dtype",
"allows",
"negative",
"values",
".",
"Returns",
"min",
"max",
":",
"tuple",
"Lower",
"and",
"upper",
"intensity",
"limits",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/dtypes.py#L240-L253 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/dtypes.py | DType.is_compatible_with | def is_compatible_with(self, other):
"""Returns True if the `other` DType will be converted to this DType.
The conversion rules are as follows:
```python
DType(T) .is_compatible_with(DType(T)) == True
DType(T) .is_compatible_with(DType(T).as_ref) == True
DType(T).as_ref.is_compatible_with(DType(T)) == False
DType(T).as_ref.is_compatible_with(DType(T).as_ref) == True
```
Args:
other: A `DType` (or object that may be converted to a `DType`).
Returns:
True if a Tensor of the `other` `DType` will be implicitly converted to
this `DType`.
"""
other = as_dtype(other)
return self._type_enum in (
other.as_datatype_enum,
other.base_dtype.as_datatype_enum,
) | python | def is_compatible_with(self, other):
"""Returns True if the `other` DType will be converted to this DType.
The conversion rules are as follows:
```python
DType(T) .is_compatible_with(DType(T)) == True
DType(T) .is_compatible_with(DType(T).as_ref) == True
DType(T).as_ref.is_compatible_with(DType(T)) == False
DType(T).as_ref.is_compatible_with(DType(T).as_ref) == True
```
Args:
other: A `DType` (or object that may be converted to a `DType`).
Returns:
True if a Tensor of the `other` `DType` will be implicitly converted to
this `DType`.
"""
other = as_dtype(other)
return self._type_enum in (
other.as_datatype_enum,
other.base_dtype.as_datatype_enum,
) | [
"def",
"is_compatible_with",
"(",
"self",
",",
"other",
")",
":",
"other",
"=",
"as_dtype",
"(",
"other",
")",
"return",
"self",
".",
"_type_enum",
"in",
"(",
"other",
".",
"as_datatype_enum",
",",
"other",
".",
"base_dtype",
".",
"as_datatype_enum",
",",
")"
] | Returns True if the `other` DType will be converted to this DType.
The conversion rules are as follows:
```python
DType(T) .is_compatible_with(DType(T)) == True
DType(T) .is_compatible_with(DType(T).as_ref) == True
DType(T).as_ref.is_compatible_with(DType(T)) == False
DType(T).as_ref.is_compatible_with(DType(T).as_ref) == True
```
Args:
other: A `DType` (or object that may be converted to a `DType`).
Returns:
True if a Tensor of the `other` `DType` will be implicitly converted to
this `DType`. | [
"Returns",
"True",
"if",
"the",
"other",
"DType",
"will",
"be",
"converted",
"to",
"this",
"DType",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/dtypes.py#L255-L278 | train |
tensorflow/tensorboard | tensorboard/plugins/debugger/interactive_debugger_plugin.py | InteractiveDebuggerPlugin.listen | def listen(self, grpc_port):
"""Start listening on the given gRPC port.
This method of an instance of InteractiveDebuggerPlugin can be invoked at
most once. This method is not thread safe.
Args:
grpc_port: port number to listen at.
Raises:
ValueError: If this instance is already listening at a gRPC port.
"""
if self._grpc_port:
raise ValueError(
'This InteractiveDebuggerPlugin instance is already listening at '
'gRPC port %d' % self._grpc_port)
self._grpc_port = grpc_port
sys.stderr.write('Creating InteractiveDebuggerPlugin at port %d\n' %
self._grpc_port)
sys.stderr.flush()
self._debugger_data_server = (
interactive_debugger_server_lib.InteractiveDebuggerDataServer(
self._grpc_port))
self._server_thread = threading.Thread(
target=self._debugger_data_server.run_server)
self._server_thread.start()
signal.signal(signal.SIGINT, self.signal_handler) | python | def listen(self, grpc_port):
"""Start listening on the given gRPC port.
This method of an instance of InteractiveDebuggerPlugin can be invoked at
most once. This method is not thread safe.
Args:
grpc_port: port number to listen at.
Raises:
ValueError: If this instance is already listening at a gRPC port.
"""
if self._grpc_port:
raise ValueError(
'This InteractiveDebuggerPlugin instance is already listening at '
'gRPC port %d' % self._grpc_port)
self._grpc_port = grpc_port
sys.stderr.write('Creating InteractiveDebuggerPlugin at port %d\n' %
self._grpc_port)
sys.stderr.flush()
self._debugger_data_server = (
interactive_debugger_server_lib.InteractiveDebuggerDataServer(
self._grpc_port))
self._server_thread = threading.Thread(
target=self._debugger_data_server.run_server)
self._server_thread.start()
signal.signal(signal.SIGINT, self.signal_handler) | [
"def",
"listen",
"(",
"self",
",",
"grpc_port",
")",
":",
"if",
"self",
".",
"_grpc_port",
":",
"raise",
"ValueError",
"(",
"'This InteractiveDebuggerPlugin instance is already listening at '",
"'gRPC port %d'",
"%",
"self",
".",
"_grpc_port",
")",
"self",
".",
"_grpc_port",
"=",
"grpc_port",
"sys",
".",
"stderr",
".",
"write",
"(",
"'Creating InteractiveDebuggerPlugin at port %d\\n'",
"%",
"self",
".",
"_grpc_port",
")",
"sys",
".",
"stderr",
".",
"flush",
"(",
")",
"self",
".",
"_debugger_data_server",
"=",
"(",
"interactive_debugger_server_lib",
".",
"InteractiveDebuggerDataServer",
"(",
"self",
".",
"_grpc_port",
")",
")",
"self",
".",
"_server_thread",
"=",
"threading",
".",
"Thread",
"(",
"target",
"=",
"self",
".",
"_debugger_data_server",
".",
"run_server",
")",
"self",
".",
"_server_thread",
".",
"start",
"(",
")",
"signal",
".",
"signal",
"(",
"signal",
".",
"SIGINT",
",",
"self",
".",
"signal_handler",
")"
] | Start listening on the given gRPC port.
This method of an instance of InteractiveDebuggerPlugin can be invoked at
most once. This method is not thread safe.
Args:
grpc_port: port number to listen at.
Raises:
ValueError: If this instance is already listening at a gRPC port. | [
"Start",
"listening",
"on",
"the",
"given",
"gRPC",
"port",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/debugger/interactive_debugger_plugin.py#L80-L109 | train |
tensorflow/tensorboard | tensorboard/plugins/debugger/interactive_debugger_plugin.py | InteractiveDebuggerPlugin.get_plugin_apps | def get_plugin_apps(self):
"""Obtains a mapping between routes and handlers.
This function also starts a debugger data server on separate thread if the
plugin has not started one yet.
Returns:
A mapping between routes and handlers (functions that respond to
requests).
"""
return {
_ACK_ROUTE: self._serve_ack,
_COMM_ROUTE: self._serve_comm,
_DEBUGGER_GRPC_HOST_PORT_ROUTE: self._serve_debugger_grpc_host_port,
_DEBUGGER_GRAPH_ROUTE: self._serve_debugger_graph,
_GATED_GRPC_ROUTE: self._serve_gated_grpc,
_TENSOR_DATA_ROUTE: self._serve_tensor_data,
_SOURCE_CODE_ROUTE: self._serve_source_code,
} | python | def get_plugin_apps(self):
"""Obtains a mapping between routes and handlers.
This function also starts a debugger data server on separate thread if the
plugin has not started one yet.
Returns:
A mapping between routes and handlers (functions that respond to
requests).
"""
return {
_ACK_ROUTE: self._serve_ack,
_COMM_ROUTE: self._serve_comm,
_DEBUGGER_GRPC_HOST_PORT_ROUTE: self._serve_debugger_grpc_host_port,
_DEBUGGER_GRAPH_ROUTE: self._serve_debugger_graph,
_GATED_GRPC_ROUTE: self._serve_gated_grpc,
_TENSOR_DATA_ROUTE: self._serve_tensor_data,
_SOURCE_CODE_ROUTE: self._serve_source_code,
} | [
"def",
"get_plugin_apps",
"(",
"self",
")",
":",
"return",
"{",
"_ACK_ROUTE",
":",
"self",
".",
"_serve_ack",
",",
"_COMM_ROUTE",
":",
"self",
".",
"_serve_comm",
",",
"_DEBUGGER_GRPC_HOST_PORT_ROUTE",
":",
"self",
".",
"_serve_debugger_grpc_host_port",
",",
"_DEBUGGER_GRAPH_ROUTE",
":",
"self",
".",
"_serve_debugger_graph",
",",
"_GATED_GRPC_ROUTE",
":",
"self",
".",
"_serve_gated_grpc",
",",
"_TENSOR_DATA_ROUTE",
":",
"self",
".",
"_serve_tensor_data",
",",
"_SOURCE_CODE_ROUTE",
":",
"self",
".",
"_serve_source_code",
",",
"}"
] | Obtains a mapping between routes and handlers.
This function also starts a debugger data server on separate thread if the
plugin has not started one yet.
Returns:
A mapping between routes and handlers (functions that respond to
requests). | [
"Obtains",
"a",
"mapping",
"between",
"routes",
"and",
"handlers",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/debugger/interactive_debugger_plugin.py#L131-L149 | train |
tensorflow/tensorboard | tensorboard/plugins/audio/audio_plugin.py | AudioPlugin.is_active | def is_active(self):
"""The audio plugin is active iff any run has at least one relevant tag."""
if not self._multiplexer:
return False
return bool(self._multiplexer.PluginRunToTagToContent(metadata.PLUGIN_NAME)) | python | def is_active(self):
"""The audio plugin is active iff any run has at least one relevant tag."""
if not self._multiplexer:
return False
return bool(self._multiplexer.PluginRunToTagToContent(metadata.PLUGIN_NAME)) | [
"def",
"is_active",
"(",
"self",
")",
":",
"if",
"not",
"self",
".",
"_multiplexer",
":",
"return",
"False",
"return",
"bool",
"(",
"self",
".",
"_multiplexer",
".",
"PluginRunToTagToContent",
"(",
"metadata",
".",
"PLUGIN_NAME",
")",
")"
] | The audio plugin is active iff any run has at least one relevant tag. | [
"The",
"audio",
"plugin",
"is",
"active",
"iff",
"any",
"run",
"has",
"at",
"least",
"one",
"relevant",
"tag",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/audio/audio_plugin.py#L59-L63 | train |
tensorflow/tensorboard | tensorboard/plugins/audio/audio_plugin.py | AudioPlugin._index_impl | def _index_impl(self):
"""Return information about the tags in each run.
Result is a dictionary of the form
{
"runName1": {
"tagName1": {
"displayName": "The first tag",
"description": "<p>Long ago there was just one tag...</p>",
"samples": 3
},
"tagName2": ...,
...
},
"runName2": ...,
...
}
For each tag, `samples` is the greatest number of audio clips that
appear at any particular step. (It's not related to "samples of a
waveform.") For example, if for tag `minibatch_input` there are
five audio clips at step 0 and ten audio clips at step 1, then the
dictionary for `"minibatch_input"` will contain `"samples": 10`.
"""
runs = self._multiplexer.Runs()
result = {run: {} for run in runs}
mapping = self._multiplexer.PluginRunToTagToContent(metadata.PLUGIN_NAME)
for (run, tag_to_content) in six.iteritems(mapping):
for tag in tag_to_content:
summary_metadata = self._multiplexer.SummaryMetadata(run, tag)
tensor_events = self._multiplexer.Tensors(run, tag)
samples = max([self._number_of_samples(event.tensor_proto)
for event in tensor_events] + [0])
result[run][tag] = {'displayName': summary_metadata.display_name,
'description': plugin_util.markdown_to_safe_html(
summary_metadata.summary_description),
'samples': samples}
return result | python | def _index_impl(self):
"""Return information about the tags in each run.
Result is a dictionary of the form
{
"runName1": {
"tagName1": {
"displayName": "The first tag",
"description": "<p>Long ago there was just one tag...</p>",
"samples": 3
},
"tagName2": ...,
...
},
"runName2": ...,
...
}
For each tag, `samples` is the greatest number of audio clips that
appear at any particular step. (It's not related to "samples of a
waveform.") For example, if for tag `minibatch_input` there are
five audio clips at step 0 and ten audio clips at step 1, then the
dictionary for `"minibatch_input"` will contain `"samples": 10`.
"""
runs = self._multiplexer.Runs()
result = {run: {} for run in runs}
mapping = self._multiplexer.PluginRunToTagToContent(metadata.PLUGIN_NAME)
for (run, tag_to_content) in six.iteritems(mapping):
for tag in tag_to_content:
summary_metadata = self._multiplexer.SummaryMetadata(run, tag)
tensor_events = self._multiplexer.Tensors(run, tag)
samples = max([self._number_of_samples(event.tensor_proto)
for event in tensor_events] + [0])
result[run][tag] = {'displayName': summary_metadata.display_name,
'description': plugin_util.markdown_to_safe_html(
summary_metadata.summary_description),
'samples': samples}
return result | [
"def",
"_index_impl",
"(",
"self",
")",
":",
"runs",
"=",
"self",
".",
"_multiplexer",
".",
"Runs",
"(",
")",
"result",
"=",
"{",
"run",
":",
"{",
"}",
"for",
"run",
"in",
"runs",
"}",
"mapping",
"=",
"self",
".",
"_multiplexer",
".",
"PluginRunToTagToContent",
"(",
"metadata",
".",
"PLUGIN_NAME",
")",
"for",
"(",
"run",
",",
"tag_to_content",
")",
"in",
"six",
".",
"iteritems",
"(",
"mapping",
")",
":",
"for",
"tag",
"in",
"tag_to_content",
":",
"summary_metadata",
"=",
"self",
".",
"_multiplexer",
".",
"SummaryMetadata",
"(",
"run",
",",
"tag",
")",
"tensor_events",
"=",
"self",
".",
"_multiplexer",
".",
"Tensors",
"(",
"run",
",",
"tag",
")",
"samples",
"=",
"max",
"(",
"[",
"self",
".",
"_number_of_samples",
"(",
"event",
".",
"tensor_proto",
")",
"for",
"event",
"in",
"tensor_events",
"]",
"+",
"[",
"0",
"]",
")",
"result",
"[",
"run",
"]",
"[",
"tag",
"]",
"=",
"{",
"'displayName'",
":",
"summary_metadata",
".",
"display_name",
",",
"'description'",
":",
"plugin_util",
".",
"markdown_to_safe_html",
"(",
"summary_metadata",
".",
"summary_description",
")",
",",
"'samples'",
":",
"samples",
"}",
"return",
"result"
] | Return information about the tags in each run.
Result is a dictionary of the form
{
"runName1": {
"tagName1": {
"displayName": "The first tag",
"description": "<p>Long ago there was just one tag...</p>",
"samples": 3
},
"tagName2": ...,
...
},
"runName2": ...,
...
}
For each tag, `samples` is the greatest number of audio clips that
appear at any particular step. (It's not related to "samples of a
waveform.") For example, if for tag `minibatch_input` there are
five audio clips at step 0 and ten audio clips at step 1, then the
dictionary for `"minibatch_input"` will contain `"samples": 10`. | [
"Return",
"information",
"about",
"the",
"tags",
"in",
"each",
"run",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/audio/audio_plugin.py#L65-L105 | train |
tensorflow/tensorboard | tensorboard/plugins/audio/audio_plugin.py | AudioPlugin._serve_audio_metadata | def _serve_audio_metadata(self, request):
"""Given a tag and list of runs, serve a list of metadata for audio.
Note that the actual audio data are not sent; instead, we respond
with URLs to the audio. The frontend should treat these URLs as
opaque and should not try to parse information about them or
generate them itself, as the format may change.
Args:
request: A werkzeug.wrappers.Request object.
Returns:
A werkzeug.Response application.
"""
tag = request.args.get('tag')
run = request.args.get('run')
sample = int(request.args.get('sample', 0))
events = self._multiplexer.Tensors(run, tag)
response = self._audio_response_for_run(events, run, tag, sample)
return http_util.Respond(request, response, 'application/json') | python | def _serve_audio_metadata(self, request):
"""Given a tag and list of runs, serve a list of metadata for audio.
Note that the actual audio data are not sent; instead, we respond
with URLs to the audio. The frontend should treat these URLs as
opaque and should not try to parse information about them or
generate them itself, as the format may change.
Args:
request: A werkzeug.wrappers.Request object.
Returns:
A werkzeug.Response application.
"""
tag = request.args.get('tag')
run = request.args.get('run')
sample = int(request.args.get('sample', 0))
events = self._multiplexer.Tensors(run, tag)
response = self._audio_response_for_run(events, run, tag, sample)
return http_util.Respond(request, response, 'application/json') | [
"def",
"_serve_audio_metadata",
"(",
"self",
",",
"request",
")",
":",
"tag",
"=",
"request",
".",
"args",
".",
"get",
"(",
"'tag'",
")",
"run",
"=",
"request",
".",
"args",
".",
"get",
"(",
"'run'",
")",
"sample",
"=",
"int",
"(",
"request",
".",
"args",
".",
"get",
"(",
"'sample'",
",",
"0",
")",
")",
"events",
"=",
"self",
".",
"_multiplexer",
".",
"Tensors",
"(",
"run",
",",
"tag",
")",
"response",
"=",
"self",
".",
"_audio_response_for_run",
"(",
"events",
",",
"run",
",",
"tag",
",",
"sample",
")",
"return",
"http_util",
".",
"Respond",
"(",
"request",
",",
"response",
",",
"'application/json'",
")"
] | Given a tag and list of runs, serve a list of metadata for audio.
Note that the actual audio data are not sent; instead, we respond
with URLs to the audio. The frontend should treat these URLs as
opaque and should not try to parse information about them or
generate them itself, as the format may change.
Args:
request: A werkzeug.wrappers.Request object.
Returns:
A werkzeug.Response application. | [
"Given",
"a",
"tag",
"and",
"list",
"of",
"runs",
"serve",
"a",
"list",
"of",
"metadata",
"for",
"audio",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/audio/audio_plugin.py#L121-L141 | train |
tensorflow/tensorboard | tensorboard/plugins/audio/audio_plugin.py | AudioPlugin._audio_response_for_run | def _audio_response_for_run(self, tensor_events, run, tag, sample):
"""Builds a JSON-serializable object with information about audio.
Args:
tensor_events: A list of image event_accumulator.TensorEvent objects.
run: The name of the run.
tag: The name of the tag the audio entries all belong to.
sample: The zero-indexed sample of the audio sample for which to
retrieve information. For instance, setting `sample` to `2` will
fetch information about only the third audio clip of each batch,
and steps with fewer than three audio clips will be omitted from
the results.
Returns:
A list of dictionaries containing the wall time, step, URL, width, and
height for each audio entry.
"""
response = []
index = 0
filtered_events = self._filter_by_sample(tensor_events, sample)
content_type = self._get_mime_type(run, tag)
for (index, tensor_event) in enumerate(filtered_events):
data = tensor_util.make_ndarray(tensor_event.tensor_proto)
label = data[sample, 1]
response.append({
'wall_time': tensor_event.wall_time,
'step': tensor_event.step,
'label': plugin_util.markdown_to_safe_html(label),
'contentType': content_type,
'query': self._query_for_individual_audio(run, tag, sample, index)
})
return response | python | def _audio_response_for_run(self, tensor_events, run, tag, sample):
"""Builds a JSON-serializable object with information about audio.
Args:
tensor_events: A list of image event_accumulator.TensorEvent objects.
run: The name of the run.
tag: The name of the tag the audio entries all belong to.
sample: The zero-indexed sample of the audio sample for which to
retrieve information. For instance, setting `sample` to `2` will
fetch information about only the third audio clip of each batch,
and steps with fewer than three audio clips will be omitted from
the results.
Returns:
A list of dictionaries containing the wall time, step, URL, width, and
height for each audio entry.
"""
response = []
index = 0
filtered_events = self._filter_by_sample(tensor_events, sample)
content_type = self._get_mime_type(run, tag)
for (index, tensor_event) in enumerate(filtered_events):
data = tensor_util.make_ndarray(tensor_event.tensor_proto)
label = data[sample, 1]
response.append({
'wall_time': tensor_event.wall_time,
'step': tensor_event.step,
'label': plugin_util.markdown_to_safe_html(label),
'contentType': content_type,
'query': self._query_for_individual_audio(run, tag, sample, index)
})
return response | [
"def",
"_audio_response_for_run",
"(",
"self",
",",
"tensor_events",
",",
"run",
",",
"tag",
",",
"sample",
")",
":",
"response",
"=",
"[",
"]",
"index",
"=",
"0",
"filtered_events",
"=",
"self",
".",
"_filter_by_sample",
"(",
"tensor_events",
",",
"sample",
")",
"content_type",
"=",
"self",
".",
"_get_mime_type",
"(",
"run",
",",
"tag",
")",
"for",
"(",
"index",
",",
"tensor_event",
")",
"in",
"enumerate",
"(",
"filtered_events",
")",
":",
"data",
"=",
"tensor_util",
".",
"make_ndarray",
"(",
"tensor_event",
".",
"tensor_proto",
")",
"label",
"=",
"data",
"[",
"sample",
",",
"1",
"]",
"response",
".",
"append",
"(",
"{",
"'wall_time'",
":",
"tensor_event",
".",
"wall_time",
",",
"'step'",
":",
"tensor_event",
".",
"step",
",",
"'label'",
":",
"plugin_util",
".",
"markdown_to_safe_html",
"(",
"label",
")",
",",
"'contentType'",
":",
"content_type",
",",
"'query'",
":",
"self",
".",
"_query_for_individual_audio",
"(",
"run",
",",
"tag",
",",
"sample",
",",
"index",
")",
"}",
")",
"return",
"response"
] | Builds a JSON-serializable object with information about audio.
Args:
tensor_events: A list of image event_accumulator.TensorEvent objects.
run: The name of the run.
tag: The name of the tag the audio entries all belong to.
sample: The zero-indexed sample of the audio sample for which to
retrieve information. For instance, setting `sample` to `2` will
fetch information about only the third audio clip of each batch,
and steps with fewer than three audio clips will be omitted from
the results.
Returns:
A list of dictionaries containing the wall time, step, URL, width, and
height for each audio entry. | [
"Builds",
"a",
"JSON",
"-",
"serializable",
"object",
"with",
"information",
"about",
"audio",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/audio/audio_plugin.py#L143-L174 | train |
tensorflow/tensorboard | tensorboard/plugins/audio/audio_plugin.py | AudioPlugin._query_for_individual_audio | def _query_for_individual_audio(self, run, tag, sample, index):
"""Builds a URL for accessing the specified audio.
This should be kept in sync with _serve_audio_metadata. Note that the URL is
*not* guaranteed to always return the same audio, since audio may be
unloaded from the reservoir as new audio entries come in.
Args:
run: The name of the run.
tag: The tag.
index: The index of the audio entry. Negative values are OK.
Returns:
A string representation of a URL that will load the index-th sampled audio
in the given run with the given tag.
"""
query_string = urllib.parse.urlencode({
'run': run,
'tag': tag,
'sample': sample,
'index': index,
})
return query_string | python | def _query_for_individual_audio(self, run, tag, sample, index):
"""Builds a URL for accessing the specified audio.
This should be kept in sync with _serve_audio_metadata. Note that the URL is
*not* guaranteed to always return the same audio, since audio may be
unloaded from the reservoir as new audio entries come in.
Args:
run: The name of the run.
tag: The tag.
index: The index of the audio entry. Negative values are OK.
Returns:
A string representation of a URL that will load the index-th sampled audio
in the given run with the given tag.
"""
query_string = urllib.parse.urlencode({
'run': run,
'tag': tag,
'sample': sample,
'index': index,
})
return query_string | [
"def",
"_query_for_individual_audio",
"(",
"self",
",",
"run",
",",
"tag",
",",
"sample",
",",
"index",
")",
":",
"query_string",
"=",
"urllib",
".",
"parse",
".",
"urlencode",
"(",
"{",
"'run'",
":",
"run",
",",
"'tag'",
":",
"tag",
",",
"'sample'",
":",
"sample",
",",
"'index'",
":",
"index",
",",
"}",
")",
"return",
"query_string"
] | Builds a URL for accessing the specified audio.
This should be kept in sync with _serve_audio_metadata. Note that the URL is
*not* guaranteed to always return the same audio, since audio may be
unloaded from the reservoir as new audio entries come in.
Args:
run: The name of the run.
tag: The tag.
index: The index of the audio entry. Negative values are OK.
Returns:
A string representation of a URL that will load the index-th sampled audio
in the given run with the given tag. | [
"Builds",
"a",
"URL",
"for",
"accessing",
"the",
"specified",
"audio",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/audio/audio_plugin.py#L176-L198 | train |
tensorflow/tensorboard | tensorboard/plugins/audio/audio_plugin.py | AudioPlugin._serve_individual_audio | def _serve_individual_audio(self, request):
"""Serve encoded audio data."""
tag = request.args.get('tag')
run = request.args.get('run')
index = int(request.args.get('index'))
sample = int(request.args.get('sample', 0))
events = self._filter_by_sample(self._multiplexer.Tensors(run, tag), sample)
data = tensor_util.make_ndarray(events[index].tensor_proto)[sample, 0]
mime_type = self._get_mime_type(run, tag)
return http_util.Respond(request, data, mime_type) | python | def _serve_individual_audio(self, request):
"""Serve encoded audio data."""
tag = request.args.get('tag')
run = request.args.get('run')
index = int(request.args.get('index'))
sample = int(request.args.get('sample', 0))
events = self._filter_by_sample(self._multiplexer.Tensors(run, tag), sample)
data = tensor_util.make_ndarray(events[index].tensor_proto)[sample, 0]
mime_type = self._get_mime_type(run, tag)
return http_util.Respond(request, data, mime_type) | [
"def",
"_serve_individual_audio",
"(",
"self",
",",
"request",
")",
":",
"tag",
"=",
"request",
".",
"args",
".",
"get",
"(",
"'tag'",
")",
"run",
"=",
"request",
".",
"args",
".",
"get",
"(",
"'run'",
")",
"index",
"=",
"int",
"(",
"request",
".",
"args",
".",
"get",
"(",
"'index'",
")",
")",
"sample",
"=",
"int",
"(",
"request",
".",
"args",
".",
"get",
"(",
"'sample'",
",",
"0",
")",
")",
"events",
"=",
"self",
".",
"_filter_by_sample",
"(",
"self",
".",
"_multiplexer",
".",
"Tensors",
"(",
"run",
",",
"tag",
")",
",",
"sample",
")",
"data",
"=",
"tensor_util",
".",
"make_ndarray",
"(",
"events",
"[",
"index",
"]",
".",
"tensor_proto",
")",
"[",
"sample",
",",
"0",
"]",
"mime_type",
"=",
"self",
".",
"_get_mime_type",
"(",
"run",
",",
"tag",
")",
"return",
"http_util",
".",
"Respond",
"(",
"request",
",",
"data",
",",
"mime_type",
")"
] | Serve encoded audio data. | [
"Serve",
"encoded",
"audio",
"data",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/audio/audio_plugin.py#L206-L215 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/app.py | _usage | def _usage(shorthelp):
"""Writes __main__'s docstring to stdout with some help text.
Args:
shorthelp: bool, if True, prints only flags from the main module,
rather than all flags.
"""
doc = _sys.modules['__main__'].__doc__
if not doc:
doc = '\nUSAGE: %s [flags]\n' % _sys.argv[0]
doc = flags.text_wrap(doc, indent=' ', firstline_indent='')
else:
# Replace all '%s' with sys.argv[0], and all '%%' with '%'.
num_specifiers = doc.count('%') - 2 * doc.count('%%')
try:
doc %= (_sys.argv[0],) * num_specifiers
except (OverflowError, TypeError, ValueError):
# Just display the docstring as-is.
pass
if shorthelp:
flag_str = flags.FLAGS.main_module_help()
else:
flag_str = str(flags.FLAGS)
try:
_sys.stdout.write(doc)
if flag_str:
_sys.stdout.write('\nflags:\n')
_sys.stdout.write(flag_str)
_sys.stdout.write('\n')
except IOError as e:
# We avoid printing a huge backtrace if we get EPIPE, because
# "foo.par --help | less" is a frequent use case.
if e.errno != _errno.EPIPE:
raise | python | def _usage(shorthelp):
"""Writes __main__'s docstring to stdout with some help text.
Args:
shorthelp: bool, if True, prints only flags from the main module,
rather than all flags.
"""
doc = _sys.modules['__main__'].__doc__
if not doc:
doc = '\nUSAGE: %s [flags]\n' % _sys.argv[0]
doc = flags.text_wrap(doc, indent=' ', firstline_indent='')
else:
# Replace all '%s' with sys.argv[0], and all '%%' with '%'.
num_specifiers = doc.count('%') - 2 * doc.count('%%')
try:
doc %= (_sys.argv[0],) * num_specifiers
except (OverflowError, TypeError, ValueError):
# Just display the docstring as-is.
pass
if shorthelp:
flag_str = flags.FLAGS.main_module_help()
else:
flag_str = str(flags.FLAGS)
try:
_sys.stdout.write(doc)
if flag_str:
_sys.stdout.write('\nflags:\n')
_sys.stdout.write(flag_str)
_sys.stdout.write('\n')
except IOError as e:
# We avoid printing a huge backtrace if we get EPIPE, because
# "foo.par --help | less" is a frequent use case.
if e.errno != _errno.EPIPE:
raise | [
"def",
"_usage",
"(",
"shorthelp",
")",
":",
"doc",
"=",
"_sys",
".",
"modules",
"[",
"'__main__'",
"]",
".",
"__doc__",
"if",
"not",
"doc",
":",
"doc",
"=",
"'\\nUSAGE: %s [flags]\\n'",
"%",
"_sys",
".",
"argv",
"[",
"0",
"]",
"doc",
"=",
"flags",
".",
"text_wrap",
"(",
"doc",
",",
"indent",
"=",
"' '",
",",
"firstline_indent",
"=",
"''",
")",
"else",
":",
"# Replace all '%s' with sys.argv[0], and all '%%' with '%'.",
"num_specifiers",
"=",
"doc",
".",
"count",
"(",
"'%'",
")",
"-",
"2",
"*",
"doc",
".",
"count",
"(",
"'%%'",
")",
"try",
":",
"doc",
"%=",
"(",
"_sys",
".",
"argv",
"[",
"0",
"]",
",",
")",
"*",
"num_specifiers",
"except",
"(",
"OverflowError",
",",
"TypeError",
",",
"ValueError",
")",
":",
"# Just display the docstring as-is.",
"pass",
"if",
"shorthelp",
":",
"flag_str",
"=",
"flags",
".",
"FLAGS",
".",
"main_module_help",
"(",
")",
"else",
":",
"flag_str",
"=",
"str",
"(",
"flags",
".",
"FLAGS",
")",
"try",
":",
"_sys",
".",
"stdout",
".",
"write",
"(",
"doc",
")",
"if",
"flag_str",
":",
"_sys",
".",
"stdout",
".",
"write",
"(",
"'\\nflags:\\n'",
")",
"_sys",
".",
"stdout",
".",
"write",
"(",
"flag_str",
")",
"_sys",
".",
"stdout",
".",
"write",
"(",
"'\\n'",
")",
"except",
"IOError",
"as",
"e",
":",
"# We avoid printing a huge backtrace if we get EPIPE, because",
"# \"foo.par --help | less\" is a frequent use case.",
"if",
"e",
".",
"errno",
"!=",
"_errno",
".",
"EPIPE",
":",
"raise"
] | Writes __main__'s docstring to stdout with some help text.
Args:
shorthelp: bool, if True, prints only flags from the main module,
rather than all flags. | [
"Writes",
"__main__",
"s",
"docstring",
"to",
"stdout",
"with",
"some",
"help",
"text",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/app.py#L27-L60 | train |
tensorflow/tensorboard | tensorboard/compat/tensorflow_stub/app.py | run | def run(main=None, argv=None):
"""Runs the program with an optional 'main' function and 'argv' list."""
# Define help flags.
_define_help_flags()
# Parse known flags.
argv = flags.FLAGS(_sys.argv if argv is None else argv, known_only=True)
main = main or _sys.modules['__main__'].main
# Call the main function, passing through any arguments
# to the final program.
_sys.exit(main(argv)) | python | def run(main=None, argv=None):
"""Runs the program with an optional 'main' function and 'argv' list."""
# Define help flags.
_define_help_flags()
# Parse known flags.
argv = flags.FLAGS(_sys.argv if argv is None else argv, known_only=True)
main = main or _sys.modules['__main__'].main
# Call the main function, passing through any arguments
# to the final program.
_sys.exit(main(argv)) | [
"def",
"run",
"(",
"main",
"=",
"None",
",",
"argv",
"=",
"None",
")",
":",
"# Define help flags.",
"_define_help_flags",
"(",
")",
"# Parse known flags.",
"argv",
"=",
"flags",
".",
"FLAGS",
"(",
"_sys",
".",
"argv",
"if",
"argv",
"is",
"None",
"else",
"argv",
",",
"known_only",
"=",
"True",
")",
"main",
"=",
"main",
"or",
"_sys",
".",
"modules",
"[",
"'__main__'",
"]",
".",
"main",
"# Call the main function, passing through any arguments",
"# to the final program.",
"_sys",
".",
"exit",
"(",
"main",
"(",
"argv",
")",
")"
] | Runs the program with an optional 'main' function and 'argv' list. | [
"Runs",
"the",
"program",
"with",
"an",
"optional",
"main",
"function",
"and",
"argv",
"list",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/compat/tensorflow_stub/app.py#L116-L129 | train |
tensorflow/tensorboard | tensorboard/plugins/image/summary.py | op | def op(name,
images,
max_outputs=3,
display_name=None,
description=None,
collections=None):
"""Create a legacy image summary op for use in a TensorFlow graph.
Arguments:
name: A unique name for the generated summary node.
images: A `Tensor` representing pixel data with shape `[k, h, w, c]`,
where `k` is the number of images, `h` and `w` are the height and
width of the images, and `c` is the number of channels, which
should be 1, 3, or 4. Any of the dimensions may be statically
unknown (i.e., `None`).
max_outputs: Optional `int` or rank-0 integer `Tensor`. At most this
many images will be emitted at each step. When more than
`max_outputs` many images are provided, the first `max_outputs` many
images will be used and the rest silently discarded.
display_name: Optional name for this summary in TensorBoard, as a
constant `str`. Defaults to `name`.
description: Optional long-form description for this summary, as a
constant `str`. Markdown is supported. Defaults to empty.
collections: Optional list of graph collections keys. The new
summary op is added to these collections. Defaults to
`[Graph Keys.SUMMARIES]`.
Returns:
A TensorFlow summary op.
"""
# TODO(nickfelt): remove on-demand imports once dep situation is fixed.
import tensorflow.compat.v1 as tf
if display_name is None:
display_name = name
summary_metadata = metadata.create_summary_metadata(
display_name=display_name, description=description)
with tf.name_scope(name), \
tf.control_dependencies([tf.assert_rank(images, 4),
tf.assert_type(images, tf.uint8),
tf.assert_non_negative(max_outputs)]):
limited_images = images[:max_outputs]
encoded_images = tf.map_fn(tf.image.encode_png, limited_images,
dtype=tf.string,
name='encode_each_image')
image_shape = tf.shape(input=images)
dimensions = tf.stack([tf.as_string(image_shape[2], name='width'),
tf.as_string(image_shape[1], name='height')],
name='dimensions')
tensor = tf.concat([dimensions, encoded_images], axis=0)
return tf.summary.tensor_summary(name='image_summary',
tensor=tensor,
collections=collections,
summary_metadata=summary_metadata) | python | def op(name,
images,
max_outputs=3,
display_name=None,
description=None,
collections=None):
"""Create a legacy image summary op for use in a TensorFlow graph.
Arguments:
name: A unique name for the generated summary node.
images: A `Tensor` representing pixel data with shape `[k, h, w, c]`,
where `k` is the number of images, `h` and `w` are the height and
width of the images, and `c` is the number of channels, which
should be 1, 3, or 4. Any of the dimensions may be statically
unknown (i.e., `None`).
max_outputs: Optional `int` or rank-0 integer `Tensor`. At most this
many images will be emitted at each step. When more than
`max_outputs` many images are provided, the first `max_outputs` many
images will be used and the rest silently discarded.
display_name: Optional name for this summary in TensorBoard, as a
constant `str`. Defaults to `name`.
description: Optional long-form description for this summary, as a
constant `str`. Markdown is supported. Defaults to empty.
collections: Optional list of graph collections keys. The new
summary op is added to these collections. Defaults to
`[Graph Keys.SUMMARIES]`.
Returns:
A TensorFlow summary op.
"""
# TODO(nickfelt): remove on-demand imports once dep situation is fixed.
import tensorflow.compat.v1 as tf
if display_name is None:
display_name = name
summary_metadata = metadata.create_summary_metadata(
display_name=display_name, description=description)
with tf.name_scope(name), \
tf.control_dependencies([tf.assert_rank(images, 4),
tf.assert_type(images, tf.uint8),
tf.assert_non_negative(max_outputs)]):
limited_images = images[:max_outputs]
encoded_images = tf.map_fn(tf.image.encode_png, limited_images,
dtype=tf.string,
name='encode_each_image')
image_shape = tf.shape(input=images)
dimensions = tf.stack([tf.as_string(image_shape[2], name='width'),
tf.as_string(image_shape[1], name='height')],
name='dimensions')
tensor = tf.concat([dimensions, encoded_images], axis=0)
return tf.summary.tensor_summary(name='image_summary',
tensor=tensor,
collections=collections,
summary_metadata=summary_metadata) | [
"def",
"op",
"(",
"name",
",",
"images",
",",
"max_outputs",
"=",
"3",
",",
"display_name",
"=",
"None",
",",
"description",
"=",
"None",
",",
"collections",
"=",
"None",
")",
":",
"# TODO(nickfelt): remove on-demand imports once dep situation is fixed.",
"import",
"tensorflow",
".",
"compat",
".",
"v1",
"as",
"tf",
"if",
"display_name",
"is",
"None",
":",
"display_name",
"=",
"name",
"summary_metadata",
"=",
"metadata",
".",
"create_summary_metadata",
"(",
"display_name",
"=",
"display_name",
",",
"description",
"=",
"description",
")",
"with",
"tf",
".",
"name_scope",
"(",
"name",
")",
",",
"tf",
".",
"control_dependencies",
"(",
"[",
"tf",
".",
"assert_rank",
"(",
"images",
",",
"4",
")",
",",
"tf",
".",
"assert_type",
"(",
"images",
",",
"tf",
".",
"uint8",
")",
",",
"tf",
".",
"assert_non_negative",
"(",
"max_outputs",
")",
"]",
")",
":",
"limited_images",
"=",
"images",
"[",
":",
"max_outputs",
"]",
"encoded_images",
"=",
"tf",
".",
"map_fn",
"(",
"tf",
".",
"image",
".",
"encode_png",
",",
"limited_images",
",",
"dtype",
"=",
"tf",
".",
"string",
",",
"name",
"=",
"'encode_each_image'",
")",
"image_shape",
"=",
"tf",
".",
"shape",
"(",
"input",
"=",
"images",
")",
"dimensions",
"=",
"tf",
".",
"stack",
"(",
"[",
"tf",
".",
"as_string",
"(",
"image_shape",
"[",
"2",
"]",
",",
"name",
"=",
"'width'",
")",
",",
"tf",
".",
"as_string",
"(",
"image_shape",
"[",
"1",
"]",
",",
"name",
"=",
"'height'",
")",
"]",
",",
"name",
"=",
"'dimensions'",
")",
"tensor",
"=",
"tf",
".",
"concat",
"(",
"[",
"dimensions",
",",
"encoded_images",
"]",
",",
"axis",
"=",
"0",
")",
"return",
"tf",
".",
"summary",
".",
"tensor_summary",
"(",
"name",
"=",
"'image_summary'",
",",
"tensor",
"=",
"tensor",
",",
"collections",
"=",
"collections",
",",
"summary_metadata",
"=",
"summary_metadata",
")"
] | Create a legacy image summary op for use in a TensorFlow graph.
Arguments:
name: A unique name for the generated summary node.
images: A `Tensor` representing pixel data with shape `[k, h, w, c]`,
where `k` is the number of images, `h` and `w` are the height and
width of the images, and `c` is the number of channels, which
should be 1, 3, or 4. Any of the dimensions may be statically
unknown (i.e., `None`).
max_outputs: Optional `int` or rank-0 integer `Tensor`. At most this
many images will be emitted at each step. When more than
`max_outputs` many images are provided, the first `max_outputs` many
images will be used and the rest silently discarded.
display_name: Optional name for this summary in TensorBoard, as a
constant `str`. Defaults to `name`.
description: Optional long-form description for this summary, as a
constant `str`. Markdown is supported. Defaults to empty.
collections: Optional list of graph collections keys. The new
summary op is added to these collections. Defaults to
`[Graph Keys.SUMMARIES]`.
Returns:
A TensorFlow summary op. | [
"Create",
"a",
"legacy",
"image",
"summary",
"op",
"for",
"use",
"in",
"a",
"TensorFlow",
"graph",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/image/summary.py#L39-L92 | train |
tensorflow/tensorboard | tensorboard/plugins/image/summary.py | pb | def pb(name, images, max_outputs=3, display_name=None, description=None):
"""Create a legacy image summary protobuf.
This behaves as if you were to create an `op` with the same arguments
(wrapped with constant tensors where appropriate) and then execute
that summary op in a TensorFlow session.
Arguments:
name: A unique name for the generated summary, including any desired
name scopes.
images: An `np.array` representing pixel data with shape
`[k, h, w, c]`, where `k` is the number of images, `w` and `h` are
the width and height of the images, and `c` is the number of
channels, which should be 1, 3, or 4.
max_outputs: Optional `int`. At most this many images will be
emitted. If more than this many images are provided, the first
`max_outputs` many images will be used and the rest silently
discarded.
display_name: Optional name for this summary in TensorBoard, as a
`str`. Defaults to `name`.
description: Optional long-form description for this summary, as a
`str`. Markdown is supported. Defaults to empty.
Returns:
A `tf.Summary` protobuf object.
"""
# TODO(nickfelt): remove on-demand imports once dep situation is fixed.
import tensorflow.compat.v1 as tf
images = np.array(images).astype(np.uint8)
if images.ndim != 4:
raise ValueError('Shape %r must have rank 4' % (images.shape, ))
limited_images = images[:max_outputs]
encoded_images = [encoder.encode_png(image) for image in limited_images]
(width, height) = (images.shape[2], images.shape[1])
content = [str(width), str(height)] + encoded_images
tensor = tf.make_tensor_proto(content, dtype=tf.string)
if display_name is None:
display_name = name
summary_metadata = metadata.create_summary_metadata(
display_name=display_name, description=description)
tf_summary_metadata = tf.SummaryMetadata.FromString(
summary_metadata.SerializeToString())
summary = tf.Summary()
summary.value.add(tag='%s/image_summary' % name,
metadata=tf_summary_metadata,
tensor=tensor)
return summary | python | def pb(name, images, max_outputs=3, display_name=None, description=None):
"""Create a legacy image summary protobuf.
This behaves as if you were to create an `op` with the same arguments
(wrapped with constant tensors where appropriate) and then execute
that summary op in a TensorFlow session.
Arguments:
name: A unique name for the generated summary, including any desired
name scopes.
images: An `np.array` representing pixel data with shape
`[k, h, w, c]`, where `k` is the number of images, `w` and `h` are
the width and height of the images, and `c` is the number of
channels, which should be 1, 3, or 4.
max_outputs: Optional `int`. At most this many images will be
emitted. If more than this many images are provided, the first
`max_outputs` many images will be used and the rest silently
discarded.
display_name: Optional name for this summary in TensorBoard, as a
`str`. Defaults to `name`.
description: Optional long-form description for this summary, as a
`str`. Markdown is supported. Defaults to empty.
Returns:
A `tf.Summary` protobuf object.
"""
# TODO(nickfelt): remove on-demand imports once dep situation is fixed.
import tensorflow.compat.v1 as tf
images = np.array(images).astype(np.uint8)
if images.ndim != 4:
raise ValueError('Shape %r must have rank 4' % (images.shape, ))
limited_images = images[:max_outputs]
encoded_images = [encoder.encode_png(image) for image in limited_images]
(width, height) = (images.shape[2], images.shape[1])
content = [str(width), str(height)] + encoded_images
tensor = tf.make_tensor_proto(content, dtype=tf.string)
if display_name is None:
display_name = name
summary_metadata = metadata.create_summary_metadata(
display_name=display_name, description=description)
tf_summary_metadata = tf.SummaryMetadata.FromString(
summary_metadata.SerializeToString())
summary = tf.Summary()
summary.value.add(tag='%s/image_summary' % name,
metadata=tf_summary_metadata,
tensor=tensor)
return summary | [
"def",
"pb",
"(",
"name",
",",
"images",
",",
"max_outputs",
"=",
"3",
",",
"display_name",
"=",
"None",
",",
"description",
"=",
"None",
")",
":",
"# TODO(nickfelt): remove on-demand imports once dep situation is fixed.",
"import",
"tensorflow",
".",
"compat",
".",
"v1",
"as",
"tf",
"images",
"=",
"np",
".",
"array",
"(",
"images",
")",
".",
"astype",
"(",
"np",
".",
"uint8",
")",
"if",
"images",
".",
"ndim",
"!=",
"4",
":",
"raise",
"ValueError",
"(",
"'Shape %r must have rank 4'",
"%",
"(",
"images",
".",
"shape",
",",
")",
")",
"limited_images",
"=",
"images",
"[",
":",
"max_outputs",
"]",
"encoded_images",
"=",
"[",
"encoder",
".",
"encode_png",
"(",
"image",
")",
"for",
"image",
"in",
"limited_images",
"]",
"(",
"width",
",",
"height",
")",
"=",
"(",
"images",
".",
"shape",
"[",
"2",
"]",
",",
"images",
".",
"shape",
"[",
"1",
"]",
")",
"content",
"=",
"[",
"str",
"(",
"width",
")",
",",
"str",
"(",
"height",
")",
"]",
"+",
"encoded_images",
"tensor",
"=",
"tf",
".",
"make_tensor_proto",
"(",
"content",
",",
"dtype",
"=",
"tf",
".",
"string",
")",
"if",
"display_name",
"is",
"None",
":",
"display_name",
"=",
"name",
"summary_metadata",
"=",
"metadata",
".",
"create_summary_metadata",
"(",
"display_name",
"=",
"display_name",
",",
"description",
"=",
"description",
")",
"tf_summary_metadata",
"=",
"tf",
".",
"SummaryMetadata",
".",
"FromString",
"(",
"summary_metadata",
".",
"SerializeToString",
"(",
")",
")",
"summary",
"=",
"tf",
".",
"Summary",
"(",
")",
"summary",
".",
"value",
".",
"add",
"(",
"tag",
"=",
"'%s/image_summary'",
"%",
"name",
",",
"metadata",
"=",
"tf_summary_metadata",
",",
"tensor",
"=",
"tensor",
")",
"return",
"summary"
] | Create a legacy image summary protobuf.
This behaves as if you were to create an `op` with the same arguments
(wrapped with constant tensors where appropriate) and then execute
that summary op in a TensorFlow session.
Arguments:
name: A unique name for the generated summary, including any desired
name scopes.
images: An `np.array` representing pixel data with shape
`[k, h, w, c]`, where `k` is the number of images, `w` and `h` are
the width and height of the images, and `c` is the number of
channels, which should be 1, 3, or 4.
max_outputs: Optional `int`. At most this many images will be
emitted. If more than this many images are provided, the first
`max_outputs` many images will be used and the rest silently
discarded.
display_name: Optional name for this summary in TensorBoard, as a
`str`. Defaults to `name`.
description: Optional long-form description for this summary, as a
`str`. Markdown is supported. Defaults to empty.
Returns:
A `tf.Summary` protobuf object. | [
"Create",
"a",
"legacy",
"image",
"summary",
"protobuf",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/image/summary.py#L95-L145 | train |
tensorflow/tensorboard | tensorboard/backend/application.py | tensor_size_guidance_from_flags | def tensor_size_guidance_from_flags(flags):
"""Apply user per-summary size guidance overrides."""
tensor_size_guidance = dict(DEFAULT_TENSOR_SIZE_GUIDANCE)
if not flags or not flags.samples_per_plugin:
return tensor_size_guidance
for token in flags.samples_per_plugin.split(','):
k, v = token.strip().split('=')
tensor_size_guidance[k] = int(v)
return tensor_size_guidance | python | def tensor_size_guidance_from_flags(flags):
"""Apply user per-summary size guidance overrides."""
tensor_size_guidance = dict(DEFAULT_TENSOR_SIZE_GUIDANCE)
if not flags or not flags.samples_per_plugin:
return tensor_size_guidance
for token in flags.samples_per_plugin.split(','):
k, v = token.strip().split('=')
tensor_size_guidance[k] = int(v)
return tensor_size_guidance | [
"def",
"tensor_size_guidance_from_flags",
"(",
"flags",
")",
":",
"tensor_size_guidance",
"=",
"dict",
"(",
"DEFAULT_TENSOR_SIZE_GUIDANCE",
")",
"if",
"not",
"flags",
"or",
"not",
"flags",
".",
"samples_per_plugin",
":",
"return",
"tensor_size_guidance",
"for",
"token",
"in",
"flags",
".",
"samples_per_plugin",
".",
"split",
"(",
"','",
")",
":",
"k",
",",
"v",
"=",
"token",
".",
"strip",
"(",
")",
".",
"split",
"(",
"'='",
")",
"tensor_size_guidance",
"[",
"k",
"]",
"=",
"int",
"(",
"v",
")",
"return",
"tensor_size_guidance"
] | Apply user per-summary size guidance overrides. | [
"Apply",
"user",
"per",
"-",
"summary",
"size",
"guidance",
"overrides",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/application.py#L80-L91 | train |
tensorflow/tensorboard | tensorboard/backend/application.py | standard_tensorboard_wsgi | def standard_tensorboard_wsgi(flags, plugin_loaders, assets_zip_provider):
"""Construct a TensorBoardWSGIApp with standard plugins and multiplexer.
Args:
flags: An argparse.Namespace containing TensorBoard CLI flags.
plugin_loaders: A list of TBLoader instances.
assets_zip_provider: See TBContext documentation for more information.
Returns:
The new TensorBoard WSGI application.
:type plugin_loaders: list[base_plugin.TBLoader]
:rtype: TensorBoardWSGI
"""
multiplexer = event_multiplexer.EventMultiplexer(
size_guidance=DEFAULT_SIZE_GUIDANCE,
tensor_size_guidance=tensor_size_guidance_from_flags(flags),
purge_orphaned_data=flags.purge_orphaned_data,
max_reload_threads=flags.max_reload_threads)
loading_multiplexer = multiplexer
reload_interval = flags.reload_interval
# For db import op mode, prefer reloading in a child process. See
# https://github.com/tensorflow/tensorboard/issues/1467
reload_task = flags.reload_task
if reload_task == 'auto' and flags.db_import and flags.db_import_use_op:
reload_task == 'process'
db_uri = flags.db
# For DB import mode, create a DB file if we weren't given one.
if flags.db_import and not flags.db:
tmpdir = tempfile.mkdtemp(prefix='tbimport')
atexit.register(shutil.rmtree, tmpdir)
db_uri = 'sqlite:%s/tmp.sqlite' % tmpdir
db_module, db_connection_provider = get_database_info(db_uri)
if flags.db_import:
# DB import mode.
if db_module != sqlite3:
raise base_plugin.FlagsError('--db_import is only compatible with sqlite DBs')
logger.info('Importing logdir into DB at %s', db_uri)
loading_multiplexer = db_import_multiplexer.DbImportMultiplexer(
db_connection_provider=db_connection_provider,
purge_orphaned_data=flags.purge_orphaned_data,
max_reload_threads=flags.max_reload_threads,
use_import_op=flags.db_import_use_op)
elif flags.db:
# DB read-only mode, never load event logs.
reload_interval = -1
plugin_name_to_instance = {}
context = base_plugin.TBContext(
db_module=db_module,
db_connection_provider=db_connection_provider,
db_uri=db_uri,
flags=flags,
logdir=flags.logdir,
multiplexer=multiplexer,
assets_zip_provider=assets_zip_provider,
plugin_name_to_instance=plugin_name_to_instance,
window_title=flags.window_title)
plugins = []
for loader in plugin_loaders:
plugin = loader.load(context)
if plugin is None:
continue
plugins.append(plugin)
plugin_name_to_instance[plugin.plugin_name] = plugin
return TensorBoardWSGIApp(flags.logdir, plugins, loading_multiplexer,
reload_interval, flags.path_prefix,
reload_task) | python | def standard_tensorboard_wsgi(flags, plugin_loaders, assets_zip_provider):
"""Construct a TensorBoardWSGIApp with standard plugins and multiplexer.
Args:
flags: An argparse.Namespace containing TensorBoard CLI flags.
plugin_loaders: A list of TBLoader instances.
assets_zip_provider: See TBContext documentation for more information.
Returns:
The new TensorBoard WSGI application.
:type plugin_loaders: list[base_plugin.TBLoader]
:rtype: TensorBoardWSGI
"""
multiplexer = event_multiplexer.EventMultiplexer(
size_guidance=DEFAULT_SIZE_GUIDANCE,
tensor_size_guidance=tensor_size_guidance_from_flags(flags),
purge_orphaned_data=flags.purge_orphaned_data,
max_reload_threads=flags.max_reload_threads)
loading_multiplexer = multiplexer
reload_interval = flags.reload_interval
# For db import op mode, prefer reloading in a child process. See
# https://github.com/tensorflow/tensorboard/issues/1467
reload_task = flags.reload_task
if reload_task == 'auto' and flags.db_import and flags.db_import_use_op:
reload_task == 'process'
db_uri = flags.db
# For DB import mode, create a DB file if we weren't given one.
if flags.db_import and not flags.db:
tmpdir = tempfile.mkdtemp(prefix='tbimport')
atexit.register(shutil.rmtree, tmpdir)
db_uri = 'sqlite:%s/tmp.sqlite' % tmpdir
db_module, db_connection_provider = get_database_info(db_uri)
if flags.db_import:
# DB import mode.
if db_module != sqlite3:
raise base_plugin.FlagsError('--db_import is only compatible with sqlite DBs')
logger.info('Importing logdir into DB at %s', db_uri)
loading_multiplexer = db_import_multiplexer.DbImportMultiplexer(
db_connection_provider=db_connection_provider,
purge_orphaned_data=flags.purge_orphaned_data,
max_reload_threads=flags.max_reload_threads,
use_import_op=flags.db_import_use_op)
elif flags.db:
# DB read-only mode, never load event logs.
reload_interval = -1
plugin_name_to_instance = {}
context = base_plugin.TBContext(
db_module=db_module,
db_connection_provider=db_connection_provider,
db_uri=db_uri,
flags=flags,
logdir=flags.logdir,
multiplexer=multiplexer,
assets_zip_provider=assets_zip_provider,
plugin_name_to_instance=plugin_name_to_instance,
window_title=flags.window_title)
plugins = []
for loader in plugin_loaders:
plugin = loader.load(context)
if plugin is None:
continue
plugins.append(plugin)
plugin_name_to_instance[plugin.plugin_name] = plugin
return TensorBoardWSGIApp(flags.logdir, plugins, loading_multiplexer,
reload_interval, flags.path_prefix,
reload_task) | [
"def",
"standard_tensorboard_wsgi",
"(",
"flags",
",",
"plugin_loaders",
",",
"assets_zip_provider",
")",
":",
"multiplexer",
"=",
"event_multiplexer",
".",
"EventMultiplexer",
"(",
"size_guidance",
"=",
"DEFAULT_SIZE_GUIDANCE",
",",
"tensor_size_guidance",
"=",
"tensor_size_guidance_from_flags",
"(",
"flags",
")",
",",
"purge_orphaned_data",
"=",
"flags",
".",
"purge_orphaned_data",
",",
"max_reload_threads",
"=",
"flags",
".",
"max_reload_threads",
")",
"loading_multiplexer",
"=",
"multiplexer",
"reload_interval",
"=",
"flags",
".",
"reload_interval",
"# For db import op mode, prefer reloading in a child process. See",
"# https://github.com/tensorflow/tensorboard/issues/1467",
"reload_task",
"=",
"flags",
".",
"reload_task",
"if",
"reload_task",
"==",
"'auto'",
"and",
"flags",
".",
"db_import",
"and",
"flags",
".",
"db_import_use_op",
":",
"reload_task",
"==",
"'process'",
"db_uri",
"=",
"flags",
".",
"db",
"# For DB import mode, create a DB file if we weren't given one.",
"if",
"flags",
".",
"db_import",
"and",
"not",
"flags",
".",
"db",
":",
"tmpdir",
"=",
"tempfile",
".",
"mkdtemp",
"(",
"prefix",
"=",
"'tbimport'",
")",
"atexit",
".",
"register",
"(",
"shutil",
".",
"rmtree",
",",
"tmpdir",
")",
"db_uri",
"=",
"'sqlite:%s/tmp.sqlite'",
"%",
"tmpdir",
"db_module",
",",
"db_connection_provider",
"=",
"get_database_info",
"(",
"db_uri",
")",
"if",
"flags",
".",
"db_import",
":",
"# DB import mode.",
"if",
"db_module",
"!=",
"sqlite3",
":",
"raise",
"base_plugin",
".",
"FlagsError",
"(",
"'--db_import is only compatible with sqlite DBs'",
")",
"logger",
".",
"info",
"(",
"'Importing logdir into DB at %s'",
",",
"db_uri",
")",
"loading_multiplexer",
"=",
"db_import_multiplexer",
".",
"DbImportMultiplexer",
"(",
"db_connection_provider",
"=",
"db_connection_provider",
",",
"purge_orphaned_data",
"=",
"flags",
".",
"purge_orphaned_data",
",",
"max_reload_threads",
"=",
"flags",
".",
"max_reload_threads",
",",
"use_import_op",
"=",
"flags",
".",
"db_import_use_op",
")",
"elif",
"flags",
".",
"db",
":",
"# DB read-only mode, never load event logs.",
"reload_interval",
"=",
"-",
"1",
"plugin_name_to_instance",
"=",
"{",
"}",
"context",
"=",
"base_plugin",
".",
"TBContext",
"(",
"db_module",
"=",
"db_module",
",",
"db_connection_provider",
"=",
"db_connection_provider",
",",
"db_uri",
"=",
"db_uri",
",",
"flags",
"=",
"flags",
",",
"logdir",
"=",
"flags",
".",
"logdir",
",",
"multiplexer",
"=",
"multiplexer",
",",
"assets_zip_provider",
"=",
"assets_zip_provider",
",",
"plugin_name_to_instance",
"=",
"plugin_name_to_instance",
",",
"window_title",
"=",
"flags",
".",
"window_title",
")",
"plugins",
"=",
"[",
"]",
"for",
"loader",
"in",
"plugin_loaders",
":",
"plugin",
"=",
"loader",
".",
"load",
"(",
"context",
")",
"if",
"plugin",
"is",
"None",
":",
"continue",
"plugins",
".",
"append",
"(",
"plugin",
")",
"plugin_name_to_instance",
"[",
"plugin",
".",
"plugin_name",
"]",
"=",
"plugin",
"return",
"TensorBoardWSGIApp",
"(",
"flags",
".",
"logdir",
",",
"plugins",
",",
"loading_multiplexer",
",",
"reload_interval",
",",
"flags",
".",
"path_prefix",
",",
"reload_task",
")"
] | Construct a TensorBoardWSGIApp with standard plugins and multiplexer.
Args:
flags: An argparse.Namespace containing TensorBoard CLI flags.
plugin_loaders: A list of TBLoader instances.
assets_zip_provider: See TBContext documentation for more information.
Returns:
The new TensorBoard WSGI application.
:type plugin_loaders: list[base_plugin.TBLoader]
:rtype: TensorBoardWSGI | [
"Construct",
"a",
"TensorBoardWSGIApp",
"with",
"standard",
"plugins",
"and",
"multiplexer",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/application.py#L94-L160 | train |
tensorflow/tensorboard | tensorboard/backend/application.py | TensorBoardWSGIApp | def TensorBoardWSGIApp(logdir, plugins, multiplexer, reload_interval,
path_prefix='', reload_task='auto'):
"""Constructs the TensorBoard application.
Args:
logdir: the logdir spec that describes where data will be loaded.
may be a directory, or comma,separated list of directories, or colons
can be used to provide named directories
plugins: A list of base_plugin.TBPlugin subclass instances.
multiplexer: The EventMultiplexer with TensorBoard data to serve
reload_interval: How often (in seconds) to reload the Multiplexer.
Zero means reload just once at startup; negative means never load.
path_prefix: A prefix of the path when app isn't served from root.
reload_task: Indicates the type of background task to reload with.
Returns:
A WSGI application that implements the TensorBoard backend.
Raises:
ValueError: If something is wrong with the plugin configuration.
:type plugins: list[base_plugin.TBPlugin]
:rtype: TensorBoardWSGI
"""
path_to_run = parse_event_files_spec(logdir)
if reload_interval >= 0:
# We either reload the multiplexer once when TensorBoard starts up, or we
# continuously reload the multiplexer.
start_reloading_multiplexer(multiplexer, path_to_run, reload_interval,
reload_task)
return TensorBoardWSGI(plugins, path_prefix) | python | def TensorBoardWSGIApp(logdir, plugins, multiplexer, reload_interval,
path_prefix='', reload_task='auto'):
"""Constructs the TensorBoard application.
Args:
logdir: the logdir spec that describes where data will be loaded.
may be a directory, or comma,separated list of directories, or colons
can be used to provide named directories
plugins: A list of base_plugin.TBPlugin subclass instances.
multiplexer: The EventMultiplexer with TensorBoard data to serve
reload_interval: How often (in seconds) to reload the Multiplexer.
Zero means reload just once at startup; negative means never load.
path_prefix: A prefix of the path when app isn't served from root.
reload_task: Indicates the type of background task to reload with.
Returns:
A WSGI application that implements the TensorBoard backend.
Raises:
ValueError: If something is wrong with the plugin configuration.
:type plugins: list[base_plugin.TBPlugin]
:rtype: TensorBoardWSGI
"""
path_to_run = parse_event_files_spec(logdir)
if reload_interval >= 0:
# We either reload the multiplexer once when TensorBoard starts up, or we
# continuously reload the multiplexer.
start_reloading_multiplexer(multiplexer, path_to_run, reload_interval,
reload_task)
return TensorBoardWSGI(plugins, path_prefix) | [
"def",
"TensorBoardWSGIApp",
"(",
"logdir",
",",
"plugins",
",",
"multiplexer",
",",
"reload_interval",
",",
"path_prefix",
"=",
"''",
",",
"reload_task",
"=",
"'auto'",
")",
":",
"path_to_run",
"=",
"parse_event_files_spec",
"(",
"logdir",
")",
"if",
"reload_interval",
">=",
"0",
":",
"# We either reload the multiplexer once when TensorBoard starts up, or we",
"# continuously reload the multiplexer.",
"start_reloading_multiplexer",
"(",
"multiplexer",
",",
"path_to_run",
",",
"reload_interval",
",",
"reload_task",
")",
"return",
"TensorBoardWSGI",
"(",
"plugins",
",",
"path_prefix",
")"
] | Constructs the TensorBoard application.
Args:
logdir: the logdir spec that describes where data will be loaded.
may be a directory, or comma,separated list of directories, or colons
can be used to provide named directories
plugins: A list of base_plugin.TBPlugin subclass instances.
multiplexer: The EventMultiplexer with TensorBoard data to serve
reload_interval: How often (in seconds) to reload the Multiplexer.
Zero means reload just once at startup; negative means never load.
path_prefix: A prefix of the path when app isn't served from root.
reload_task: Indicates the type of background task to reload with.
Returns:
A WSGI application that implements the TensorBoard backend.
Raises:
ValueError: If something is wrong with the plugin configuration.
:type plugins: list[base_plugin.TBPlugin]
:rtype: TensorBoardWSGI | [
"Constructs",
"the",
"TensorBoard",
"application",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/application.py#L163-L193 | train |
tensorflow/tensorboard | tensorboard/backend/application.py | parse_event_files_spec | def parse_event_files_spec(logdir):
"""Parses `logdir` into a map from paths to run group names.
The events files flag format is a comma-separated list of path specifications.
A path specification either looks like 'group_name:/path/to/directory' or
'/path/to/directory'; in the latter case, the group is unnamed. Group names
cannot start with a forward slash: /foo:bar/baz will be interpreted as a
spec with no name and path '/foo:bar/baz'.
Globs are not supported.
Args:
logdir: A comma-separated list of run specifications.
Returns:
A dict mapping directory paths to names like {'/path/to/directory': 'name'}.
Groups without an explicit name are named after their path. If logdir is
None, returns an empty dict, which is helpful for testing things that don't
require any valid runs.
"""
files = {}
if logdir is None:
return files
# Make sure keeping consistent with ParseURI in core/lib/io/path.cc
uri_pattern = re.compile('[a-zA-Z][0-9a-zA-Z.]*://.*')
for specification in logdir.split(','):
# Check if the spec contains group. A spec start with xyz:// is regarded as
# URI path spec instead of group spec. If the spec looks like /foo:bar/baz,
# then we assume it's a path with a colon. If the spec looks like
# [a-zA-z]:\foo then we assume its a Windows path and not a single letter
# group
if (uri_pattern.match(specification) is None and ':' in specification and
specification[0] != '/' and not os.path.splitdrive(specification)[0]):
# We split at most once so run_name:/path:with/a/colon will work.
run_name, _, path = specification.partition(':')
else:
run_name = None
path = specification
if uri_pattern.match(path) is None:
path = os.path.realpath(os.path.expanduser(path))
files[path] = run_name
return files | python | def parse_event_files_spec(logdir):
"""Parses `logdir` into a map from paths to run group names.
The events files flag format is a comma-separated list of path specifications.
A path specification either looks like 'group_name:/path/to/directory' or
'/path/to/directory'; in the latter case, the group is unnamed. Group names
cannot start with a forward slash: /foo:bar/baz will be interpreted as a
spec with no name and path '/foo:bar/baz'.
Globs are not supported.
Args:
logdir: A comma-separated list of run specifications.
Returns:
A dict mapping directory paths to names like {'/path/to/directory': 'name'}.
Groups without an explicit name are named after their path. If logdir is
None, returns an empty dict, which is helpful for testing things that don't
require any valid runs.
"""
files = {}
if logdir is None:
return files
# Make sure keeping consistent with ParseURI in core/lib/io/path.cc
uri_pattern = re.compile('[a-zA-Z][0-9a-zA-Z.]*://.*')
for specification in logdir.split(','):
# Check if the spec contains group. A spec start with xyz:// is regarded as
# URI path spec instead of group spec. If the spec looks like /foo:bar/baz,
# then we assume it's a path with a colon. If the spec looks like
# [a-zA-z]:\foo then we assume its a Windows path and not a single letter
# group
if (uri_pattern.match(specification) is None and ':' in specification and
specification[0] != '/' and not os.path.splitdrive(specification)[0]):
# We split at most once so run_name:/path:with/a/colon will work.
run_name, _, path = specification.partition(':')
else:
run_name = None
path = specification
if uri_pattern.match(path) is None:
path = os.path.realpath(os.path.expanduser(path))
files[path] = run_name
return files | [
"def",
"parse_event_files_spec",
"(",
"logdir",
")",
":",
"files",
"=",
"{",
"}",
"if",
"logdir",
"is",
"None",
":",
"return",
"files",
"# Make sure keeping consistent with ParseURI in core/lib/io/path.cc",
"uri_pattern",
"=",
"re",
".",
"compile",
"(",
"'[a-zA-Z][0-9a-zA-Z.]*://.*'",
")",
"for",
"specification",
"in",
"logdir",
".",
"split",
"(",
"','",
")",
":",
"# Check if the spec contains group. A spec start with xyz:// is regarded as",
"# URI path spec instead of group spec. If the spec looks like /foo:bar/baz,",
"# then we assume it's a path with a colon. If the spec looks like",
"# [a-zA-z]:\\foo then we assume its a Windows path and not a single letter",
"# group",
"if",
"(",
"uri_pattern",
".",
"match",
"(",
"specification",
")",
"is",
"None",
"and",
"':'",
"in",
"specification",
"and",
"specification",
"[",
"0",
"]",
"!=",
"'/'",
"and",
"not",
"os",
".",
"path",
".",
"splitdrive",
"(",
"specification",
")",
"[",
"0",
"]",
")",
":",
"# We split at most once so run_name:/path:with/a/colon will work.",
"run_name",
",",
"_",
",",
"path",
"=",
"specification",
".",
"partition",
"(",
"':'",
")",
"else",
":",
"run_name",
"=",
"None",
"path",
"=",
"specification",
"if",
"uri_pattern",
".",
"match",
"(",
"path",
")",
"is",
"None",
":",
"path",
"=",
"os",
".",
"path",
".",
"realpath",
"(",
"os",
".",
"path",
".",
"expanduser",
"(",
"path",
")",
")",
"files",
"[",
"path",
"]",
"=",
"run_name",
"return",
"files"
] | Parses `logdir` into a map from paths to run group names.
The events files flag format is a comma-separated list of path specifications.
A path specification either looks like 'group_name:/path/to/directory' or
'/path/to/directory'; in the latter case, the group is unnamed. Group names
cannot start with a forward slash: /foo:bar/baz will be interpreted as a
spec with no name and path '/foo:bar/baz'.
Globs are not supported.
Args:
logdir: A comma-separated list of run specifications.
Returns:
A dict mapping directory paths to names like {'/path/to/directory': 'name'}.
Groups without an explicit name are named after their path. If logdir is
None, returns an empty dict, which is helpful for testing things that don't
require any valid runs. | [
"Parses",
"logdir",
"into",
"a",
"map",
"from",
"paths",
"to",
"run",
"group",
"names",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/application.py#L317-L357 | train |
tensorflow/tensorboard | tensorboard/backend/application.py | start_reloading_multiplexer | def start_reloading_multiplexer(multiplexer, path_to_run, load_interval,
reload_task):
"""Starts automatically reloading the given multiplexer.
If `load_interval` is positive, the thread will reload the multiplexer
by calling `ReloadMultiplexer` every `load_interval` seconds, starting
immediately. Otherwise, reloads the multiplexer once and never again.
Args:
multiplexer: The `EventMultiplexer` to add runs to and reload.
path_to_run: A dict mapping from paths to run names, where `None` as the run
name is interpreted as a run name equal to the path.
load_interval: An integer greater than or equal to 0. If positive, how many
seconds to wait after one load before starting the next load. Otherwise,
reloads the multiplexer once and never again (no continuous reloading).
reload_task: Indicates the type of background task to reload with.
Raises:
ValueError: If `load_interval` is negative.
"""
if load_interval < 0:
raise ValueError('load_interval is negative: %d' % load_interval)
def _reload():
while True:
start = time.time()
logger.info('TensorBoard reload process beginning')
for path, name in six.iteritems(path_to_run):
multiplexer.AddRunsFromDirectory(path, name)
logger.info('TensorBoard reload process: Reload the whole Multiplexer')
multiplexer.Reload()
duration = time.time() - start
logger.info('TensorBoard done reloading. Load took %0.3f secs', duration)
if load_interval == 0:
# Only load the multiplexer once. Do not continuously reload.
break
time.sleep(load_interval)
if reload_task == 'process':
logger.info('Launching reload in a child process')
import multiprocessing
process = multiprocessing.Process(target=_reload, name='Reloader')
# Best-effort cleanup; on exit, the main TB parent process will attempt to
# kill all its daemonic children.
process.daemon = True
process.start()
elif reload_task in ('thread', 'auto'):
logger.info('Launching reload in a daemon thread')
thread = threading.Thread(target=_reload, name='Reloader')
# Make this a daemon thread, which won't block TB from exiting.
thread.daemon = True
thread.start()
elif reload_task == 'blocking':
if load_interval != 0:
raise ValueError('blocking reload only allowed with load_interval=0')
_reload()
else:
raise ValueError('unrecognized reload_task: %s' % reload_task) | python | def start_reloading_multiplexer(multiplexer, path_to_run, load_interval,
reload_task):
"""Starts automatically reloading the given multiplexer.
If `load_interval` is positive, the thread will reload the multiplexer
by calling `ReloadMultiplexer` every `load_interval` seconds, starting
immediately. Otherwise, reloads the multiplexer once and never again.
Args:
multiplexer: The `EventMultiplexer` to add runs to and reload.
path_to_run: A dict mapping from paths to run names, where `None` as the run
name is interpreted as a run name equal to the path.
load_interval: An integer greater than or equal to 0. If positive, how many
seconds to wait after one load before starting the next load. Otherwise,
reloads the multiplexer once and never again (no continuous reloading).
reload_task: Indicates the type of background task to reload with.
Raises:
ValueError: If `load_interval` is negative.
"""
if load_interval < 0:
raise ValueError('load_interval is negative: %d' % load_interval)
def _reload():
while True:
start = time.time()
logger.info('TensorBoard reload process beginning')
for path, name in six.iteritems(path_to_run):
multiplexer.AddRunsFromDirectory(path, name)
logger.info('TensorBoard reload process: Reload the whole Multiplexer')
multiplexer.Reload()
duration = time.time() - start
logger.info('TensorBoard done reloading. Load took %0.3f secs', duration)
if load_interval == 0:
# Only load the multiplexer once. Do not continuously reload.
break
time.sleep(load_interval)
if reload_task == 'process':
logger.info('Launching reload in a child process')
import multiprocessing
process = multiprocessing.Process(target=_reload, name='Reloader')
# Best-effort cleanup; on exit, the main TB parent process will attempt to
# kill all its daemonic children.
process.daemon = True
process.start()
elif reload_task in ('thread', 'auto'):
logger.info('Launching reload in a daemon thread')
thread = threading.Thread(target=_reload, name='Reloader')
# Make this a daemon thread, which won't block TB from exiting.
thread.daemon = True
thread.start()
elif reload_task == 'blocking':
if load_interval != 0:
raise ValueError('blocking reload only allowed with load_interval=0')
_reload()
else:
raise ValueError('unrecognized reload_task: %s' % reload_task) | [
"def",
"start_reloading_multiplexer",
"(",
"multiplexer",
",",
"path_to_run",
",",
"load_interval",
",",
"reload_task",
")",
":",
"if",
"load_interval",
"<",
"0",
":",
"raise",
"ValueError",
"(",
"'load_interval is negative: %d'",
"%",
"load_interval",
")",
"def",
"_reload",
"(",
")",
":",
"while",
"True",
":",
"start",
"=",
"time",
".",
"time",
"(",
")",
"logger",
".",
"info",
"(",
"'TensorBoard reload process beginning'",
")",
"for",
"path",
",",
"name",
"in",
"six",
".",
"iteritems",
"(",
"path_to_run",
")",
":",
"multiplexer",
".",
"AddRunsFromDirectory",
"(",
"path",
",",
"name",
")",
"logger",
".",
"info",
"(",
"'TensorBoard reload process: Reload the whole Multiplexer'",
")",
"multiplexer",
".",
"Reload",
"(",
")",
"duration",
"=",
"time",
".",
"time",
"(",
")",
"-",
"start",
"logger",
".",
"info",
"(",
"'TensorBoard done reloading. Load took %0.3f secs'",
",",
"duration",
")",
"if",
"load_interval",
"==",
"0",
":",
"# Only load the multiplexer once. Do not continuously reload.",
"break",
"time",
".",
"sleep",
"(",
"load_interval",
")",
"if",
"reload_task",
"==",
"'process'",
":",
"logger",
".",
"info",
"(",
"'Launching reload in a child process'",
")",
"import",
"multiprocessing",
"process",
"=",
"multiprocessing",
".",
"Process",
"(",
"target",
"=",
"_reload",
",",
"name",
"=",
"'Reloader'",
")",
"# Best-effort cleanup; on exit, the main TB parent process will attempt to",
"# kill all its daemonic children.",
"process",
".",
"daemon",
"=",
"True",
"process",
".",
"start",
"(",
")",
"elif",
"reload_task",
"in",
"(",
"'thread'",
",",
"'auto'",
")",
":",
"logger",
".",
"info",
"(",
"'Launching reload in a daemon thread'",
")",
"thread",
"=",
"threading",
".",
"Thread",
"(",
"target",
"=",
"_reload",
",",
"name",
"=",
"'Reloader'",
")",
"# Make this a daemon thread, which won't block TB from exiting.",
"thread",
".",
"daemon",
"=",
"True",
"thread",
".",
"start",
"(",
")",
"elif",
"reload_task",
"==",
"'blocking'",
":",
"if",
"load_interval",
"!=",
"0",
":",
"raise",
"ValueError",
"(",
"'blocking reload only allowed with load_interval=0'",
")",
"_reload",
"(",
")",
"else",
":",
"raise",
"ValueError",
"(",
"'unrecognized reload_task: %s'",
"%",
"reload_task",
")"
] | Starts automatically reloading the given multiplexer.
If `load_interval` is positive, the thread will reload the multiplexer
by calling `ReloadMultiplexer` every `load_interval` seconds, starting
immediately. Otherwise, reloads the multiplexer once and never again.
Args:
multiplexer: The `EventMultiplexer` to add runs to and reload.
path_to_run: A dict mapping from paths to run names, where `None` as the run
name is interpreted as a run name equal to the path.
load_interval: An integer greater than or equal to 0. If positive, how many
seconds to wait after one load before starting the next load. Otherwise,
reloads the multiplexer once and never again (no continuous reloading).
reload_task: Indicates the type of background task to reload with.
Raises:
ValueError: If `load_interval` is negative. | [
"Starts",
"automatically",
"reloading",
"the",
"given",
"multiplexer",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/application.py#L360-L417 | train |
tensorflow/tensorboard | tensorboard/backend/application.py | get_database_info | def get_database_info(db_uri):
"""Returns TBContext fields relating to SQL database.
Args:
db_uri: A string URI expressing the DB file, e.g. "sqlite:~/tb.db".
Returns:
A tuple with the db_module and db_connection_provider TBContext fields. If
db_uri was empty, then (None, None) is returned.
Raises:
ValueError: If db_uri scheme is not supported.
"""
if not db_uri:
return None, None
scheme = urlparse.urlparse(db_uri).scheme
if scheme == 'sqlite':
return sqlite3, create_sqlite_connection_provider(db_uri)
else:
raise ValueError('Only sqlite DB URIs are supported now: ' + db_uri) | python | def get_database_info(db_uri):
"""Returns TBContext fields relating to SQL database.
Args:
db_uri: A string URI expressing the DB file, e.g. "sqlite:~/tb.db".
Returns:
A tuple with the db_module and db_connection_provider TBContext fields. If
db_uri was empty, then (None, None) is returned.
Raises:
ValueError: If db_uri scheme is not supported.
"""
if not db_uri:
return None, None
scheme = urlparse.urlparse(db_uri).scheme
if scheme == 'sqlite':
return sqlite3, create_sqlite_connection_provider(db_uri)
else:
raise ValueError('Only sqlite DB URIs are supported now: ' + db_uri) | [
"def",
"get_database_info",
"(",
"db_uri",
")",
":",
"if",
"not",
"db_uri",
":",
"return",
"None",
",",
"None",
"scheme",
"=",
"urlparse",
".",
"urlparse",
"(",
"db_uri",
")",
".",
"scheme",
"if",
"scheme",
"==",
"'sqlite'",
":",
"return",
"sqlite3",
",",
"create_sqlite_connection_provider",
"(",
"db_uri",
")",
"else",
":",
"raise",
"ValueError",
"(",
"'Only sqlite DB URIs are supported now: '",
"+",
"db_uri",
")"
] | Returns TBContext fields relating to SQL database.
Args:
db_uri: A string URI expressing the DB file, e.g. "sqlite:~/tb.db".
Returns:
A tuple with the db_module and db_connection_provider TBContext fields. If
db_uri was empty, then (None, None) is returned.
Raises:
ValueError: If db_uri scheme is not supported. | [
"Returns",
"TBContext",
"fields",
"relating",
"to",
"SQL",
"database",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/application.py#L420-L439 | train |
tensorflow/tensorboard | tensorboard/backend/application.py | create_sqlite_connection_provider | def create_sqlite_connection_provider(db_uri):
"""Returns function that returns SQLite Connection objects.
Args:
db_uri: A string URI expressing the DB file, e.g. "sqlite:~/tb.db".
Returns:
A function that returns a new PEP-249 DB Connection, which must be closed,
each time it is called.
Raises:
ValueError: If db_uri is not a valid sqlite file URI.
"""
uri = urlparse.urlparse(db_uri)
if uri.scheme != 'sqlite':
raise ValueError('Scheme is not sqlite: ' + db_uri)
if uri.netloc:
raise ValueError('Can not connect to SQLite over network: ' + db_uri)
if uri.path == ':memory:':
raise ValueError('Memory mode SQLite not supported: ' + db_uri)
path = os.path.expanduser(uri.path)
params = _get_connect_params(uri.query)
# TODO(@jart): Add thread-local pooling.
return lambda: sqlite3.connect(path, **params) | python | def create_sqlite_connection_provider(db_uri):
"""Returns function that returns SQLite Connection objects.
Args:
db_uri: A string URI expressing the DB file, e.g. "sqlite:~/tb.db".
Returns:
A function that returns a new PEP-249 DB Connection, which must be closed,
each time it is called.
Raises:
ValueError: If db_uri is not a valid sqlite file URI.
"""
uri = urlparse.urlparse(db_uri)
if uri.scheme != 'sqlite':
raise ValueError('Scheme is not sqlite: ' + db_uri)
if uri.netloc:
raise ValueError('Can not connect to SQLite over network: ' + db_uri)
if uri.path == ':memory:':
raise ValueError('Memory mode SQLite not supported: ' + db_uri)
path = os.path.expanduser(uri.path)
params = _get_connect_params(uri.query)
# TODO(@jart): Add thread-local pooling.
return lambda: sqlite3.connect(path, **params) | [
"def",
"create_sqlite_connection_provider",
"(",
"db_uri",
")",
":",
"uri",
"=",
"urlparse",
".",
"urlparse",
"(",
"db_uri",
")",
"if",
"uri",
".",
"scheme",
"!=",
"'sqlite'",
":",
"raise",
"ValueError",
"(",
"'Scheme is not sqlite: '",
"+",
"db_uri",
")",
"if",
"uri",
".",
"netloc",
":",
"raise",
"ValueError",
"(",
"'Can not connect to SQLite over network: '",
"+",
"db_uri",
")",
"if",
"uri",
".",
"path",
"==",
"':memory:'",
":",
"raise",
"ValueError",
"(",
"'Memory mode SQLite not supported: '",
"+",
"db_uri",
")",
"path",
"=",
"os",
".",
"path",
".",
"expanduser",
"(",
"uri",
".",
"path",
")",
"params",
"=",
"_get_connect_params",
"(",
"uri",
".",
"query",
")",
"# TODO(@jart): Add thread-local pooling.",
"return",
"lambda",
":",
"sqlite3",
".",
"connect",
"(",
"path",
",",
"*",
"*",
"params",
")"
] | Returns function that returns SQLite Connection objects.
Args:
db_uri: A string URI expressing the DB file, e.g. "sqlite:~/tb.db".
Returns:
A function that returns a new PEP-249 DB Connection, which must be closed,
each time it is called.
Raises:
ValueError: If db_uri is not a valid sqlite file URI. | [
"Returns",
"function",
"that",
"returns",
"SQLite",
"Connection",
"objects",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/application.py#L442-L465 | train |
tensorflow/tensorboard | tensorboard/backend/application.py | TensorBoardWSGI._serve_plugins_listing | def _serve_plugins_listing(self, request):
"""Serves an object mapping plugin name to whether it is enabled.
Args:
request: The werkzeug.Request object.
Returns:
A werkzeug.Response object.
"""
response = {}
for plugin in self._plugins:
start = time.time()
response[plugin.plugin_name] = plugin.is_active()
elapsed = time.time() - start
logger.info(
'Plugin listing: is_active() for %s took %0.3f seconds',
plugin.plugin_name, elapsed)
return http_util.Respond(request, response, 'application/json') | python | def _serve_plugins_listing(self, request):
"""Serves an object mapping plugin name to whether it is enabled.
Args:
request: The werkzeug.Request object.
Returns:
A werkzeug.Response object.
"""
response = {}
for plugin in self._plugins:
start = time.time()
response[plugin.plugin_name] = plugin.is_active()
elapsed = time.time() - start
logger.info(
'Plugin listing: is_active() for %s took %0.3f seconds',
plugin.plugin_name, elapsed)
return http_util.Respond(request, response, 'application/json') | [
"def",
"_serve_plugins_listing",
"(",
"self",
",",
"request",
")",
":",
"response",
"=",
"{",
"}",
"for",
"plugin",
"in",
"self",
".",
"_plugins",
":",
"start",
"=",
"time",
".",
"time",
"(",
")",
"response",
"[",
"plugin",
".",
"plugin_name",
"]",
"=",
"plugin",
".",
"is_active",
"(",
")",
"elapsed",
"=",
"time",
".",
"time",
"(",
")",
"-",
"start",
"logger",
".",
"info",
"(",
"'Plugin listing: is_active() for %s took %0.3f seconds'",
",",
"plugin",
".",
"plugin_name",
",",
"elapsed",
")",
"return",
"http_util",
".",
"Respond",
"(",
"request",
",",
"response",
",",
"'application/json'",
")"
] | Serves an object mapping plugin name to whether it is enabled.
Args:
request: The werkzeug.Request object.
Returns:
A werkzeug.Response object. | [
"Serves",
"an",
"object",
"mapping",
"plugin",
"name",
"to",
"whether",
"it",
"is",
"enabled",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/backend/application.py#L268-L285 | train |
tensorflow/tensorboard | tensorboard/plugins/debugger/tensor_helper.py | parse_time_indices | def parse_time_indices(s):
"""Parse a string as time indices.
Args:
s: A valid slicing string for time indices. E.g., '-1', '[:]', ':', '2:10'
Returns:
A slice object.
Raises:
ValueError: If `s` does not represent valid time indices.
"""
if not s.startswith('['):
s = '[' + s + ']'
parsed = command_parser._parse_slices(s)
if len(parsed) != 1:
raise ValueError(
'Invalid number of slicing objects in time indices (%d)' % len(parsed))
else:
return parsed[0] | python | def parse_time_indices(s):
"""Parse a string as time indices.
Args:
s: A valid slicing string for time indices. E.g., '-1', '[:]', ':', '2:10'
Returns:
A slice object.
Raises:
ValueError: If `s` does not represent valid time indices.
"""
if not s.startswith('['):
s = '[' + s + ']'
parsed = command_parser._parse_slices(s)
if len(parsed) != 1:
raise ValueError(
'Invalid number of slicing objects in time indices (%d)' % len(parsed))
else:
return parsed[0] | [
"def",
"parse_time_indices",
"(",
"s",
")",
":",
"if",
"not",
"s",
".",
"startswith",
"(",
"'['",
")",
":",
"s",
"=",
"'['",
"+",
"s",
"+",
"']'",
"parsed",
"=",
"command_parser",
".",
"_parse_slices",
"(",
"s",
")",
"if",
"len",
"(",
"parsed",
")",
"!=",
"1",
":",
"raise",
"ValueError",
"(",
"'Invalid number of slicing objects in time indices (%d)'",
"%",
"len",
"(",
"parsed",
")",
")",
"else",
":",
"return",
"parsed",
"[",
"0",
"]"
] | Parse a string as time indices.
Args:
s: A valid slicing string for time indices. E.g., '-1', '[:]', ':', '2:10'
Returns:
A slice object.
Raises:
ValueError: If `s` does not represent valid time indices. | [
"Parse",
"a",
"string",
"as",
"time",
"indices",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/debugger/tensor_helper.py#L43-L62 | train |
tensorflow/tensorboard | tensorboard/plugins/debugger/tensor_helper.py | process_buffers_for_display | def process_buffers_for_display(s, limit=40):
"""Process a buffer for human-readable display.
This function performs the following operation on each of the buffers in `s`.
1. Truncate input buffer if the length of the buffer is greater than
`limit`, to prevent large strings from overloading the frontend.
2. Apply `binascii.b2a_qp` on the truncated buffer to make the buffer
printable and convertible to JSON.
3. If truncation happened (in step 1), append a string at the end
describing the original length and the truncation.
Args:
s: The buffer to be processed, either a single buffer or a nested array of
them.
limit: Length limit for each buffer, beyond which truncation will occur.
Return:
A single processed buffer or a nested array of processed buffers.
"""
if isinstance(s, (list, tuple)):
return [process_buffers_for_display(elem, limit=limit) for elem in s]
else:
length = len(s)
if length > limit:
return (binascii.b2a_qp(s[:limit]) +
b' (length-%d truncated at %d bytes)' % (length, limit))
else:
return binascii.b2a_qp(s) | python | def process_buffers_for_display(s, limit=40):
"""Process a buffer for human-readable display.
This function performs the following operation on each of the buffers in `s`.
1. Truncate input buffer if the length of the buffer is greater than
`limit`, to prevent large strings from overloading the frontend.
2. Apply `binascii.b2a_qp` on the truncated buffer to make the buffer
printable and convertible to JSON.
3. If truncation happened (in step 1), append a string at the end
describing the original length and the truncation.
Args:
s: The buffer to be processed, either a single buffer or a nested array of
them.
limit: Length limit for each buffer, beyond which truncation will occur.
Return:
A single processed buffer or a nested array of processed buffers.
"""
if isinstance(s, (list, tuple)):
return [process_buffers_for_display(elem, limit=limit) for elem in s]
else:
length = len(s)
if length > limit:
return (binascii.b2a_qp(s[:limit]) +
b' (length-%d truncated at %d bytes)' % (length, limit))
else:
return binascii.b2a_qp(s) | [
"def",
"process_buffers_for_display",
"(",
"s",
",",
"limit",
"=",
"40",
")",
":",
"if",
"isinstance",
"(",
"s",
",",
"(",
"list",
",",
"tuple",
")",
")",
":",
"return",
"[",
"process_buffers_for_display",
"(",
"elem",
",",
"limit",
"=",
"limit",
")",
"for",
"elem",
"in",
"s",
"]",
"else",
":",
"length",
"=",
"len",
"(",
"s",
")",
"if",
"length",
">",
"limit",
":",
"return",
"(",
"binascii",
".",
"b2a_qp",
"(",
"s",
"[",
":",
"limit",
"]",
")",
"+",
"b' (length-%d truncated at %d bytes)'",
"%",
"(",
"length",
",",
"limit",
")",
")",
"else",
":",
"return",
"binascii",
".",
"b2a_qp",
"(",
"s",
")"
] | Process a buffer for human-readable display.
This function performs the following operation on each of the buffers in `s`.
1. Truncate input buffer if the length of the buffer is greater than
`limit`, to prevent large strings from overloading the frontend.
2. Apply `binascii.b2a_qp` on the truncated buffer to make the buffer
printable and convertible to JSON.
3. If truncation happened (in step 1), append a string at the end
describing the original length and the truncation.
Args:
s: The buffer to be processed, either a single buffer or a nested array of
them.
limit: Length limit for each buffer, beyond which truncation will occur.
Return:
A single processed buffer or a nested array of processed buffers. | [
"Process",
"a",
"buffer",
"for",
"human",
"-",
"readable",
"display",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/debugger/tensor_helper.py#L83-L110 | train |
tensorflow/tensorboard | tensorboard/plugins/debugger/tensor_helper.py | array_view | def array_view(array, slicing=None, mapping=None):
"""View a slice or the entirety of an ndarray.
Args:
array: The input array, as an numpy.ndarray.
slicing: Optional slicing string, e.g., "[:, 1:3, :]".
mapping: Optional mapping string. Supported mappings:
`None` or case-insensitive `'None'`: Unmapped nested list.
`'image/png'`: Image encoding of a 2D sliced array or 3D sliced array
with 3 as the last dimension. If the sliced array is not 2D or 3D with
3 as the last dimension, a `ValueError` will be thrown.
`health-pill`: A succinct summary of the numeric values of a tensor.
See documentation in [`health_pill_calc.py`] for more details.
Returns:
1. dtype as a `str`.
2. shape of the sliced array, as a tuple of `int`s.
3. the potentially sliced values, as a nested `list`.
"""
dtype = translate_dtype(array.dtype)
sliced_array = (array[command_parser._parse_slices(slicing)] if slicing
else array)
if np.isscalar(sliced_array) and str(dtype) == 'string':
# When a string Tensor (for which dtype is 'object') is sliced down to only
# one element, it becomes a string, instead of an numpy array.
# We preserve the dimensionality of original array in the returned shape
# and slice.
ndims = len(array.shape)
slice_shape = []
for _ in range(ndims):
sliced_array = [sliced_array]
slice_shape.append(1)
return dtype, tuple(slice_shape), sliced_array
else:
shape = sliced_array.shape
if mapping == "image/png":
if len(sliced_array.shape) == 2:
return dtype, shape, array_to_base64_png(sliced_array)
elif len(sliced_array.shape) == 3:
raise NotImplementedError(
"image/png mapping for 3D array has not been implemented")
else:
raise ValueError("Invalid rank for image/png mapping: %d" %
len(sliced_array.shape))
elif mapping == 'health-pill':
health_pill = health_pill_calc.calc_health_pill(array)
return dtype, shape, health_pill
elif mapping is None or mapping == '' or mapping.lower() == 'none':
return dtype, shape, sliced_array.tolist()
else:
raise ValueError("Invalid mapping: %s" % mapping) | python | def array_view(array, slicing=None, mapping=None):
"""View a slice or the entirety of an ndarray.
Args:
array: The input array, as an numpy.ndarray.
slicing: Optional slicing string, e.g., "[:, 1:3, :]".
mapping: Optional mapping string. Supported mappings:
`None` or case-insensitive `'None'`: Unmapped nested list.
`'image/png'`: Image encoding of a 2D sliced array or 3D sliced array
with 3 as the last dimension. If the sliced array is not 2D or 3D with
3 as the last dimension, a `ValueError` will be thrown.
`health-pill`: A succinct summary of the numeric values of a tensor.
See documentation in [`health_pill_calc.py`] for more details.
Returns:
1. dtype as a `str`.
2. shape of the sliced array, as a tuple of `int`s.
3. the potentially sliced values, as a nested `list`.
"""
dtype = translate_dtype(array.dtype)
sliced_array = (array[command_parser._parse_slices(slicing)] if slicing
else array)
if np.isscalar(sliced_array) and str(dtype) == 'string':
# When a string Tensor (for which dtype is 'object') is sliced down to only
# one element, it becomes a string, instead of an numpy array.
# We preserve the dimensionality of original array in the returned shape
# and slice.
ndims = len(array.shape)
slice_shape = []
for _ in range(ndims):
sliced_array = [sliced_array]
slice_shape.append(1)
return dtype, tuple(slice_shape), sliced_array
else:
shape = sliced_array.shape
if mapping == "image/png":
if len(sliced_array.shape) == 2:
return dtype, shape, array_to_base64_png(sliced_array)
elif len(sliced_array.shape) == 3:
raise NotImplementedError(
"image/png mapping for 3D array has not been implemented")
else:
raise ValueError("Invalid rank for image/png mapping: %d" %
len(sliced_array.shape))
elif mapping == 'health-pill':
health_pill = health_pill_calc.calc_health_pill(array)
return dtype, shape, health_pill
elif mapping is None or mapping == '' or mapping.lower() == 'none':
return dtype, shape, sliced_array.tolist()
else:
raise ValueError("Invalid mapping: %s" % mapping) | [
"def",
"array_view",
"(",
"array",
",",
"slicing",
"=",
"None",
",",
"mapping",
"=",
"None",
")",
":",
"dtype",
"=",
"translate_dtype",
"(",
"array",
".",
"dtype",
")",
"sliced_array",
"=",
"(",
"array",
"[",
"command_parser",
".",
"_parse_slices",
"(",
"slicing",
")",
"]",
"if",
"slicing",
"else",
"array",
")",
"if",
"np",
".",
"isscalar",
"(",
"sliced_array",
")",
"and",
"str",
"(",
"dtype",
")",
"==",
"'string'",
":",
"# When a string Tensor (for which dtype is 'object') is sliced down to only",
"# one element, it becomes a string, instead of an numpy array.",
"# We preserve the dimensionality of original array in the returned shape",
"# and slice.",
"ndims",
"=",
"len",
"(",
"array",
".",
"shape",
")",
"slice_shape",
"=",
"[",
"]",
"for",
"_",
"in",
"range",
"(",
"ndims",
")",
":",
"sliced_array",
"=",
"[",
"sliced_array",
"]",
"slice_shape",
".",
"append",
"(",
"1",
")",
"return",
"dtype",
",",
"tuple",
"(",
"slice_shape",
")",
",",
"sliced_array",
"else",
":",
"shape",
"=",
"sliced_array",
".",
"shape",
"if",
"mapping",
"==",
"\"image/png\"",
":",
"if",
"len",
"(",
"sliced_array",
".",
"shape",
")",
"==",
"2",
":",
"return",
"dtype",
",",
"shape",
",",
"array_to_base64_png",
"(",
"sliced_array",
")",
"elif",
"len",
"(",
"sliced_array",
".",
"shape",
")",
"==",
"3",
":",
"raise",
"NotImplementedError",
"(",
"\"image/png mapping for 3D array has not been implemented\"",
")",
"else",
":",
"raise",
"ValueError",
"(",
"\"Invalid rank for image/png mapping: %d\"",
"%",
"len",
"(",
"sliced_array",
".",
"shape",
")",
")",
"elif",
"mapping",
"==",
"'health-pill'",
":",
"health_pill",
"=",
"health_pill_calc",
".",
"calc_health_pill",
"(",
"array",
")",
"return",
"dtype",
",",
"shape",
",",
"health_pill",
"elif",
"mapping",
"is",
"None",
"or",
"mapping",
"==",
"''",
"or",
"mapping",
".",
"lower",
"(",
")",
"==",
"'none'",
":",
"return",
"dtype",
",",
"shape",
",",
"sliced_array",
".",
"tolist",
"(",
")",
"else",
":",
"raise",
"ValueError",
"(",
"\"Invalid mapping: %s\"",
"%",
"mapping",
")"
] | View a slice or the entirety of an ndarray.
Args:
array: The input array, as an numpy.ndarray.
slicing: Optional slicing string, e.g., "[:, 1:3, :]".
mapping: Optional mapping string. Supported mappings:
`None` or case-insensitive `'None'`: Unmapped nested list.
`'image/png'`: Image encoding of a 2D sliced array or 3D sliced array
with 3 as the last dimension. If the sliced array is not 2D or 3D with
3 as the last dimension, a `ValueError` will be thrown.
`health-pill`: A succinct summary of the numeric values of a tensor.
See documentation in [`health_pill_calc.py`] for more details.
Returns:
1. dtype as a `str`.
2. shape of the sliced array, as a tuple of `int`s.
3. the potentially sliced values, as a nested `list`. | [
"View",
"a",
"slice",
"or",
"the",
"entirety",
"of",
"an",
"ndarray",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/debugger/tensor_helper.py#L113-L165 | train |
tensorflow/tensorboard | tensorboard/plugins/debugger/tensor_helper.py | array_to_base64_png | def array_to_base64_png(array):
"""Convert an array into base64-enoded PNG image.
Args:
array: A 2D np.ndarray or nested list of items.
Returns:
A base64-encoded string the image. The image is grayscale if the array is
2D. The image is RGB color if the image is 3D with lsat dimension equal to
3.
Raises:
ValueError: If the input `array` is not rank-2, or if the rank-2 `array` is
empty.
"""
# TODO(cais): Deal with 3D case.
# TODO(cais): If there are None values in here, replace them with all NaNs.
array = np.array(array, dtype=np.float32)
if len(array.shape) != 2:
raise ValueError(
"Expected rank-2 array; received rank-%d array." % len(array.shape))
if not np.size(array):
raise ValueError(
"Cannot encode an empty array (size: %s) as image." % (array.shape,))
is_infinity = np.isinf(array)
is_positive = array > 0.0
is_positive_infinity = np.logical_and(is_infinity, is_positive)
is_negative_infinity = np.logical_and(is_infinity,
np.logical_not(is_positive))
is_nan = np.isnan(array)
finite_indices = np.where(np.logical_and(np.logical_not(is_infinity),
np.logical_not(is_nan)))
if np.size(finite_indices):
# Finite subset is not empty.
minval = np.min(array[finite_indices])
maxval = np.max(array[finite_indices])
scaled = np.array((array - minval) / (maxval - minval) * 255,
dtype=np.uint8)
rgb = np.repeat(np.expand_dims(scaled, -1), IMAGE_COLOR_CHANNELS, axis=-1)
else:
rgb = np.zeros(array.shape + (IMAGE_COLOR_CHANNELS,), dtype=np.uint8)
# Color-code pixels that correspond to infinities and nans.
rgb[is_positive_infinity] = POSITIVE_INFINITY_RGB
rgb[is_negative_infinity] = NEGATIVE_INFINITY_RGB
rgb[is_nan] = NAN_RGB
image_encoded = base64.b64encode(encoder.encode_png(rgb))
return image_encoded | python | def array_to_base64_png(array):
"""Convert an array into base64-enoded PNG image.
Args:
array: A 2D np.ndarray or nested list of items.
Returns:
A base64-encoded string the image. The image is grayscale if the array is
2D. The image is RGB color if the image is 3D with lsat dimension equal to
3.
Raises:
ValueError: If the input `array` is not rank-2, or if the rank-2 `array` is
empty.
"""
# TODO(cais): Deal with 3D case.
# TODO(cais): If there are None values in here, replace them with all NaNs.
array = np.array(array, dtype=np.float32)
if len(array.shape) != 2:
raise ValueError(
"Expected rank-2 array; received rank-%d array." % len(array.shape))
if not np.size(array):
raise ValueError(
"Cannot encode an empty array (size: %s) as image." % (array.shape,))
is_infinity = np.isinf(array)
is_positive = array > 0.0
is_positive_infinity = np.logical_and(is_infinity, is_positive)
is_negative_infinity = np.logical_and(is_infinity,
np.logical_not(is_positive))
is_nan = np.isnan(array)
finite_indices = np.where(np.logical_and(np.logical_not(is_infinity),
np.logical_not(is_nan)))
if np.size(finite_indices):
# Finite subset is not empty.
minval = np.min(array[finite_indices])
maxval = np.max(array[finite_indices])
scaled = np.array((array - minval) / (maxval - minval) * 255,
dtype=np.uint8)
rgb = np.repeat(np.expand_dims(scaled, -1), IMAGE_COLOR_CHANNELS, axis=-1)
else:
rgb = np.zeros(array.shape + (IMAGE_COLOR_CHANNELS,), dtype=np.uint8)
# Color-code pixels that correspond to infinities and nans.
rgb[is_positive_infinity] = POSITIVE_INFINITY_RGB
rgb[is_negative_infinity] = NEGATIVE_INFINITY_RGB
rgb[is_nan] = NAN_RGB
image_encoded = base64.b64encode(encoder.encode_png(rgb))
return image_encoded | [
"def",
"array_to_base64_png",
"(",
"array",
")",
":",
"# TODO(cais): Deal with 3D case.",
"# TODO(cais): If there are None values in here, replace them with all NaNs.",
"array",
"=",
"np",
".",
"array",
"(",
"array",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"if",
"len",
"(",
"array",
".",
"shape",
")",
"!=",
"2",
":",
"raise",
"ValueError",
"(",
"\"Expected rank-2 array; received rank-%d array.\"",
"%",
"len",
"(",
"array",
".",
"shape",
")",
")",
"if",
"not",
"np",
".",
"size",
"(",
"array",
")",
":",
"raise",
"ValueError",
"(",
"\"Cannot encode an empty array (size: %s) as image.\"",
"%",
"(",
"array",
".",
"shape",
",",
")",
")",
"is_infinity",
"=",
"np",
".",
"isinf",
"(",
"array",
")",
"is_positive",
"=",
"array",
">",
"0.0",
"is_positive_infinity",
"=",
"np",
".",
"logical_and",
"(",
"is_infinity",
",",
"is_positive",
")",
"is_negative_infinity",
"=",
"np",
".",
"logical_and",
"(",
"is_infinity",
",",
"np",
".",
"logical_not",
"(",
"is_positive",
")",
")",
"is_nan",
"=",
"np",
".",
"isnan",
"(",
"array",
")",
"finite_indices",
"=",
"np",
".",
"where",
"(",
"np",
".",
"logical_and",
"(",
"np",
".",
"logical_not",
"(",
"is_infinity",
")",
",",
"np",
".",
"logical_not",
"(",
"is_nan",
")",
")",
")",
"if",
"np",
".",
"size",
"(",
"finite_indices",
")",
":",
"# Finite subset is not empty.",
"minval",
"=",
"np",
".",
"min",
"(",
"array",
"[",
"finite_indices",
"]",
")",
"maxval",
"=",
"np",
".",
"max",
"(",
"array",
"[",
"finite_indices",
"]",
")",
"scaled",
"=",
"np",
".",
"array",
"(",
"(",
"array",
"-",
"minval",
")",
"/",
"(",
"maxval",
"-",
"minval",
")",
"*",
"255",
",",
"dtype",
"=",
"np",
".",
"uint8",
")",
"rgb",
"=",
"np",
".",
"repeat",
"(",
"np",
".",
"expand_dims",
"(",
"scaled",
",",
"-",
"1",
")",
",",
"IMAGE_COLOR_CHANNELS",
",",
"axis",
"=",
"-",
"1",
")",
"else",
":",
"rgb",
"=",
"np",
".",
"zeros",
"(",
"array",
".",
"shape",
"+",
"(",
"IMAGE_COLOR_CHANNELS",
",",
")",
",",
"dtype",
"=",
"np",
".",
"uint8",
")",
"# Color-code pixels that correspond to infinities and nans.",
"rgb",
"[",
"is_positive_infinity",
"]",
"=",
"POSITIVE_INFINITY_RGB",
"rgb",
"[",
"is_negative_infinity",
"]",
"=",
"NEGATIVE_INFINITY_RGB",
"rgb",
"[",
"is_nan",
"]",
"=",
"NAN_RGB",
"image_encoded",
"=",
"base64",
".",
"b64encode",
"(",
"encoder",
".",
"encode_png",
"(",
"rgb",
")",
")",
"return",
"image_encoded"
] | Convert an array into base64-enoded PNG image.
Args:
array: A 2D np.ndarray or nested list of items.
Returns:
A base64-encoded string the image. The image is grayscale if the array is
2D. The image is RGB color if the image is 3D with lsat dimension equal to
3.
Raises:
ValueError: If the input `array` is not rank-2, or if the rank-2 `array` is
empty. | [
"Convert",
"an",
"array",
"into",
"base64",
"-",
"enoded",
"PNG",
"image",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/debugger/tensor_helper.py#L174-L223 | train |
tensorflow/tensorboard | tensorboard/plugins/graph/graph_util.py | _safe_copy_proto_list_values | def _safe_copy_proto_list_values(dst_proto_list, src_proto_list, get_key):
"""Safely merge values from `src_proto_list` into `dst_proto_list`.
Each element in `dst_proto_list` must be mapped by `get_key` to a key
value that is unique within that list; likewise for `src_proto_list`.
If an element of `src_proto_list` has the same key as an existing
element in `dst_proto_list`, then the elements must also be equal.
Args:
dst_proto_list: A `RepeatedCompositeContainer` or
`RepeatedScalarContainer` into which values should be copied.
src_proto_list: A container holding the same kind of values as in
`dst_proto_list` from which values should be copied.
get_key: A function that takes an element of `dst_proto_list` or
`src_proto_list` and returns a key, such that if two elements have
the same key then it is required that they be deep-equal. For
instance, if `dst_proto_list` is a list of nodes, then `get_key`
might be `lambda node: node.name` to indicate that if two nodes
have the same name then they must be the same node. All keys must
be hashable.
Raises:
_ProtoListDuplicateKeyError: A proto_list contains items with duplicate
keys.
_SameKeyDiffContentError: An item with the same key has different contents.
"""
def _assert_proto_container_unique_keys(proto_list, get_key):
"""Asserts proto_list to only contains unique keys.
Args:
proto_list: A `RepeatedCompositeContainer` or `RepeatedScalarContainer`.
get_key: A function that takes an element of `proto_list` and returns a
hashable key.
Raises:
_ProtoListDuplicateKeyError: A proto_list contains items with duplicate
keys.
"""
keys = set()
for item in proto_list:
key = get_key(item)
if key in keys:
raise _ProtoListDuplicateKeyError(key)
keys.add(key)
_assert_proto_container_unique_keys(dst_proto_list, get_key)
_assert_proto_container_unique_keys(src_proto_list, get_key)
key_to_proto = {}
for proto in dst_proto_list:
key = get_key(proto)
key_to_proto[key] = proto
for proto in src_proto_list:
key = get_key(proto)
if key in key_to_proto:
if proto != key_to_proto.get(key):
raise _SameKeyDiffContentError(key)
else:
dst_proto_list.add().CopyFrom(proto) | python | def _safe_copy_proto_list_values(dst_proto_list, src_proto_list, get_key):
"""Safely merge values from `src_proto_list` into `dst_proto_list`.
Each element in `dst_proto_list` must be mapped by `get_key` to a key
value that is unique within that list; likewise for `src_proto_list`.
If an element of `src_proto_list` has the same key as an existing
element in `dst_proto_list`, then the elements must also be equal.
Args:
dst_proto_list: A `RepeatedCompositeContainer` or
`RepeatedScalarContainer` into which values should be copied.
src_proto_list: A container holding the same kind of values as in
`dst_proto_list` from which values should be copied.
get_key: A function that takes an element of `dst_proto_list` or
`src_proto_list` and returns a key, such that if two elements have
the same key then it is required that they be deep-equal. For
instance, if `dst_proto_list` is a list of nodes, then `get_key`
might be `lambda node: node.name` to indicate that if two nodes
have the same name then they must be the same node. All keys must
be hashable.
Raises:
_ProtoListDuplicateKeyError: A proto_list contains items with duplicate
keys.
_SameKeyDiffContentError: An item with the same key has different contents.
"""
def _assert_proto_container_unique_keys(proto_list, get_key):
"""Asserts proto_list to only contains unique keys.
Args:
proto_list: A `RepeatedCompositeContainer` or `RepeatedScalarContainer`.
get_key: A function that takes an element of `proto_list` and returns a
hashable key.
Raises:
_ProtoListDuplicateKeyError: A proto_list contains items with duplicate
keys.
"""
keys = set()
for item in proto_list:
key = get_key(item)
if key in keys:
raise _ProtoListDuplicateKeyError(key)
keys.add(key)
_assert_proto_container_unique_keys(dst_proto_list, get_key)
_assert_proto_container_unique_keys(src_proto_list, get_key)
key_to_proto = {}
for proto in dst_proto_list:
key = get_key(proto)
key_to_proto[key] = proto
for proto in src_proto_list:
key = get_key(proto)
if key in key_to_proto:
if proto != key_to_proto.get(key):
raise _SameKeyDiffContentError(key)
else:
dst_proto_list.add().CopyFrom(proto) | [
"def",
"_safe_copy_proto_list_values",
"(",
"dst_proto_list",
",",
"src_proto_list",
",",
"get_key",
")",
":",
"def",
"_assert_proto_container_unique_keys",
"(",
"proto_list",
",",
"get_key",
")",
":",
"\"\"\"Asserts proto_list to only contains unique keys.\n\n Args:\n proto_list: A `RepeatedCompositeContainer` or `RepeatedScalarContainer`.\n get_key: A function that takes an element of `proto_list` and returns a\n hashable key.\n\n Raises:\n _ProtoListDuplicateKeyError: A proto_list contains items with duplicate\n keys.\n \"\"\"",
"keys",
"=",
"set",
"(",
")",
"for",
"item",
"in",
"proto_list",
":",
"key",
"=",
"get_key",
"(",
"item",
")",
"if",
"key",
"in",
"keys",
":",
"raise",
"_ProtoListDuplicateKeyError",
"(",
"key",
")",
"keys",
".",
"add",
"(",
"key",
")",
"_assert_proto_container_unique_keys",
"(",
"dst_proto_list",
",",
"get_key",
")",
"_assert_proto_container_unique_keys",
"(",
"src_proto_list",
",",
"get_key",
")",
"key_to_proto",
"=",
"{",
"}",
"for",
"proto",
"in",
"dst_proto_list",
":",
"key",
"=",
"get_key",
"(",
"proto",
")",
"key_to_proto",
"[",
"key",
"]",
"=",
"proto",
"for",
"proto",
"in",
"src_proto_list",
":",
"key",
"=",
"get_key",
"(",
"proto",
")",
"if",
"key",
"in",
"key_to_proto",
":",
"if",
"proto",
"!=",
"key_to_proto",
".",
"get",
"(",
"key",
")",
":",
"raise",
"_SameKeyDiffContentError",
"(",
"key",
")",
"else",
":",
"dst_proto_list",
".",
"add",
"(",
")",
".",
"CopyFrom",
"(",
"proto",
")"
] | Safely merge values from `src_proto_list` into `dst_proto_list`.
Each element in `dst_proto_list` must be mapped by `get_key` to a key
value that is unique within that list; likewise for `src_proto_list`.
If an element of `src_proto_list` has the same key as an existing
element in `dst_proto_list`, then the elements must also be equal.
Args:
dst_proto_list: A `RepeatedCompositeContainer` or
`RepeatedScalarContainer` into which values should be copied.
src_proto_list: A container holding the same kind of values as in
`dst_proto_list` from which values should be copied.
get_key: A function that takes an element of `dst_proto_list` or
`src_proto_list` and returns a key, such that if two elements have
the same key then it is required that they be deep-equal. For
instance, if `dst_proto_list` is a list of nodes, then `get_key`
might be `lambda node: node.name` to indicate that if two nodes
have the same name then they must be the same node. All keys must
be hashable.
Raises:
_ProtoListDuplicateKeyError: A proto_list contains items with duplicate
keys.
_SameKeyDiffContentError: An item with the same key has different contents. | [
"Safely",
"merge",
"values",
"from",
"src_proto_list",
"into",
"dst_proto_list",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/graph/graph_util.py#L27-L87 | train |
tensorflow/tensorboard | tensorboard/plugins/graph/graph_util.py | combine_graph_defs | def combine_graph_defs(to_proto, from_proto):
"""Combines two GraphDefs by adding nodes from from_proto into to_proto.
All GraphDefs are expected to be of TensorBoard's.
It assumes node names are unique across GraphDefs if contents differ. The
names can be the same if the NodeDef content are exactly the same.
Args:
to_proto: A destination TensorBoard GraphDef.
from_proto: A TensorBoard GraphDef to copy contents from.
Returns:
to_proto
Raises:
ValueError in case any assumption about GraphDef is violated: A
GraphDef should have unique node, function, and gradient function
names. Also, when merging GraphDefs, they should have not have nodes,
functions, or gradient function mappings that share the name but details
do not match.
"""
if from_proto.version != to_proto.version:
raise ValueError('Cannot combine GraphDefs of different versions.')
try:
_safe_copy_proto_list_values(
to_proto.node,
from_proto.node,
lambda n: n.name)
except _ProtoListDuplicateKeyError as exc:
raise ValueError('A GraphDef contains non-unique node names: %s' % exc)
except _SameKeyDiffContentError as exc:
raise ValueError(
('Cannot combine GraphDefs because nodes share a name '
'but contents are different: %s') % exc)
try:
_safe_copy_proto_list_values(
to_proto.library.function,
from_proto.library.function,
lambda n: n.signature.name)
except _ProtoListDuplicateKeyError as exc:
raise ValueError('A GraphDef contains non-unique function names: %s' % exc)
except _SameKeyDiffContentError as exc:
raise ValueError(
('Cannot combine GraphDefs because functions share a name '
'but are different: %s') % exc)
try:
_safe_copy_proto_list_values(
to_proto.library.gradient,
from_proto.library.gradient,
lambda g: g.gradient_func)
except _ProtoListDuplicateKeyError as exc:
raise ValueError(
'A GraphDef contains non-unique gradient function names: %s' % exc)
except _SameKeyDiffContentError as exc:
raise ValueError(
('Cannot combine GraphDefs because gradients share a gradient_func name '
'but map to different functions: %s') % exc)
return to_proto | python | def combine_graph_defs(to_proto, from_proto):
"""Combines two GraphDefs by adding nodes from from_proto into to_proto.
All GraphDefs are expected to be of TensorBoard's.
It assumes node names are unique across GraphDefs if contents differ. The
names can be the same if the NodeDef content are exactly the same.
Args:
to_proto: A destination TensorBoard GraphDef.
from_proto: A TensorBoard GraphDef to copy contents from.
Returns:
to_proto
Raises:
ValueError in case any assumption about GraphDef is violated: A
GraphDef should have unique node, function, and gradient function
names. Also, when merging GraphDefs, they should have not have nodes,
functions, or gradient function mappings that share the name but details
do not match.
"""
if from_proto.version != to_proto.version:
raise ValueError('Cannot combine GraphDefs of different versions.')
try:
_safe_copy_proto_list_values(
to_proto.node,
from_proto.node,
lambda n: n.name)
except _ProtoListDuplicateKeyError as exc:
raise ValueError('A GraphDef contains non-unique node names: %s' % exc)
except _SameKeyDiffContentError as exc:
raise ValueError(
('Cannot combine GraphDefs because nodes share a name '
'but contents are different: %s') % exc)
try:
_safe_copy_proto_list_values(
to_proto.library.function,
from_proto.library.function,
lambda n: n.signature.name)
except _ProtoListDuplicateKeyError as exc:
raise ValueError('A GraphDef contains non-unique function names: %s' % exc)
except _SameKeyDiffContentError as exc:
raise ValueError(
('Cannot combine GraphDefs because functions share a name '
'but are different: %s') % exc)
try:
_safe_copy_proto_list_values(
to_proto.library.gradient,
from_proto.library.gradient,
lambda g: g.gradient_func)
except _ProtoListDuplicateKeyError as exc:
raise ValueError(
'A GraphDef contains non-unique gradient function names: %s' % exc)
except _SameKeyDiffContentError as exc:
raise ValueError(
('Cannot combine GraphDefs because gradients share a gradient_func name '
'but map to different functions: %s') % exc)
return to_proto | [
"def",
"combine_graph_defs",
"(",
"to_proto",
",",
"from_proto",
")",
":",
"if",
"from_proto",
".",
"version",
"!=",
"to_proto",
".",
"version",
":",
"raise",
"ValueError",
"(",
"'Cannot combine GraphDefs of different versions.'",
")",
"try",
":",
"_safe_copy_proto_list_values",
"(",
"to_proto",
".",
"node",
",",
"from_proto",
".",
"node",
",",
"lambda",
"n",
":",
"n",
".",
"name",
")",
"except",
"_ProtoListDuplicateKeyError",
"as",
"exc",
":",
"raise",
"ValueError",
"(",
"'A GraphDef contains non-unique node names: %s'",
"%",
"exc",
")",
"except",
"_SameKeyDiffContentError",
"as",
"exc",
":",
"raise",
"ValueError",
"(",
"(",
"'Cannot combine GraphDefs because nodes share a name '",
"'but contents are different: %s'",
")",
"%",
"exc",
")",
"try",
":",
"_safe_copy_proto_list_values",
"(",
"to_proto",
".",
"library",
".",
"function",
",",
"from_proto",
".",
"library",
".",
"function",
",",
"lambda",
"n",
":",
"n",
".",
"signature",
".",
"name",
")",
"except",
"_ProtoListDuplicateKeyError",
"as",
"exc",
":",
"raise",
"ValueError",
"(",
"'A GraphDef contains non-unique function names: %s'",
"%",
"exc",
")",
"except",
"_SameKeyDiffContentError",
"as",
"exc",
":",
"raise",
"ValueError",
"(",
"(",
"'Cannot combine GraphDefs because functions share a name '",
"'but are different: %s'",
")",
"%",
"exc",
")",
"try",
":",
"_safe_copy_proto_list_values",
"(",
"to_proto",
".",
"library",
".",
"gradient",
",",
"from_proto",
".",
"library",
".",
"gradient",
",",
"lambda",
"g",
":",
"g",
".",
"gradient_func",
")",
"except",
"_ProtoListDuplicateKeyError",
"as",
"exc",
":",
"raise",
"ValueError",
"(",
"'A GraphDef contains non-unique gradient function names: %s'",
"%",
"exc",
")",
"except",
"_SameKeyDiffContentError",
"as",
"exc",
":",
"raise",
"ValueError",
"(",
"(",
"'Cannot combine GraphDefs because gradients share a gradient_func name '",
"'but map to different functions: %s'",
")",
"%",
"exc",
")",
"return",
"to_proto"
] | Combines two GraphDefs by adding nodes from from_proto into to_proto.
All GraphDefs are expected to be of TensorBoard's.
It assumes node names are unique across GraphDefs if contents differ. The
names can be the same if the NodeDef content are exactly the same.
Args:
to_proto: A destination TensorBoard GraphDef.
from_proto: A TensorBoard GraphDef to copy contents from.
Returns:
to_proto
Raises:
ValueError in case any assumption about GraphDef is violated: A
GraphDef should have unique node, function, and gradient function
names. Also, when merging GraphDefs, they should have not have nodes,
functions, or gradient function mappings that share the name but details
do not match. | [
"Combines",
"two",
"GraphDefs",
"by",
"adding",
"nodes",
"from",
"from_proto",
"into",
"to_proto",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/graph/graph_util.py#L90-L150 | train |
tensorflow/tensorboard | tensorboard/plugins/scalar/summary_v2.py | scalar | def scalar(name, data, step=None, description=None):
"""Write a scalar summary.
Arguments:
name: A name for this summary. The summary tag used for TensorBoard will
be this name prefixed by any active name scopes.
data: A real numeric scalar value, convertible to a `float32` Tensor.
step: Explicit `int64`-castable monotonic step value for this summary. If
omitted, this defaults to `tf.summary.experimental.get_step()`, which must
not be None.
description: Optional long-form description for this summary, as a
constant `str`. Markdown is supported. Defaults to empty.
Returns:
True on success, or false if no summary was written because no default
summary writer was available.
Raises:
ValueError: if a default writer exists, but no step was provided and
`tf.summary.experimental.get_step()` is None.
"""
summary_metadata = metadata.create_summary_metadata(
display_name=None, description=description)
# TODO(https://github.com/tensorflow/tensorboard/issues/2109): remove fallback
summary_scope = (
getattr(tf.summary.experimental, 'summary_scope', None) or
tf.summary.summary_scope)
with summary_scope(
name, 'scalar_summary', values=[data, step]) as (tag, _):
tf.debugging.assert_scalar(data)
return tf.summary.write(tag=tag,
tensor=tf.cast(data, tf.float32),
step=step,
metadata=summary_metadata) | python | def scalar(name, data, step=None, description=None):
"""Write a scalar summary.
Arguments:
name: A name for this summary. The summary tag used for TensorBoard will
be this name prefixed by any active name scopes.
data: A real numeric scalar value, convertible to a `float32` Tensor.
step: Explicit `int64`-castable monotonic step value for this summary. If
omitted, this defaults to `tf.summary.experimental.get_step()`, which must
not be None.
description: Optional long-form description for this summary, as a
constant `str`. Markdown is supported. Defaults to empty.
Returns:
True on success, or false if no summary was written because no default
summary writer was available.
Raises:
ValueError: if a default writer exists, but no step was provided and
`tf.summary.experimental.get_step()` is None.
"""
summary_metadata = metadata.create_summary_metadata(
display_name=None, description=description)
# TODO(https://github.com/tensorflow/tensorboard/issues/2109): remove fallback
summary_scope = (
getattr(tf.summary.experimental, 'summary_scope', None) or
tf.summary.summary_scope)
with summary_scope(
name, 'scalar_summary', values=[data, step]) as (tag, _):
tf.debugging.assert_scalar(data)
return tf.summary.write(tag=tag,
tensor=tf.cast(data, tf.float32),
step=step,
metadata=summary_metadata) | [
"def",
"scalar",
"(",
"name",
",",
"data",
",",
"step",
"=",
"None",
",",
"description",
"=",
"None",
")",
":",
"summary_metadata",
"=",
"metadata",
".",
"create_summary_metadata",
"(",
"display_name",
"=",
"None",
",",
"description",
"=",
"description",
")",
"# TODO(https://github.com/tensorflow/tensorboard/issues/2109): remove fallback",
"summary_scope",
"=",
"(",
"getattr",
"(",
"tf",
".",
"summary",
".",
"experimental",
",",
"'summary_scope'",
",",
"None",
")",
"or",
"tf",
".",
"summary",
".",
"summary_scope",
")",
"with",
"summary_scope",
"(",
"name",
",",
"'scalar_summary'",
",",
"values",
"=",
"[",
"data",
",",
"step",
"]",
")",
"as",
"(",
"tag",
",",
"_",
")",
":",
"tf",
".",
"debugging",
".",
"assert_scalar",
"(",
"data",
")",
"return",
"tf",
".",
"summary",
".",
"write",
"(",
"tag",
"=",
"tag",
",",
"tensor",
"=",
"tf",
".",
"cast",
"(",
"data",
",",
"tf",
".",
"float32",
")",
",",
"step",
"=",
"step",
",",
"metadata",
"=",
"summary_metadata",
")"
] | Write a scalar summary.
Arguments:
name: A name for this summary. The summary tag used for TensorBoard will
be this name prefixed by any active name scopes.
data: A real numeric scalar value, convertible to a `float32` Tensor.
step: Explicit `int64`-castable monotonic step value for this summary. If
omitted, this defaults to `tf.summary.experimental.get_step()`, which must
not be None.
description: Optional long-form description for this summary, as a
constant `str`. Markdown is supported. Defaults to empty.
Returns:
True on success, or false if no summary was written because no default
summary writer was available.
Raises:
ValueError: if a default writer exists, but no step was provided and
`tf.summary.experimental.get_step()` is None. | [
"Write",
"a",
"scalar",
"summary",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/scalar/summary_v2.py#L32-L65 | train |
tensorflow/tensorboard | tensorboard/plugins/scalar/summary_v2.py | scalar_pb | def scalar_pb(tag, data, description=None):
"""Create a scalar summary_pb2.Summary protobuf.
Arguments:
tag: String tag for the summary.
data: A 0-dimensional `np.array` or a compatible python number type.
description: Optional long-form description for this summary, as a
`str`. Markdown is supported. Defaults to empty.
Raises:
ValueError: If the type or shape of the data is unsupported.
Returns:
A `summary_pb2.Summary` protobuf object.
"""
arr = np.array(data)
if arr.shape != ():
raise ValueError('Expected scalar shape for tensor, got shape: %s.'
% arr.shape)
if arr.dtype.kind not in ('b', 'i', 'u', 'f'): # bool, int, uint, float
raise ValueError('Cast %s to float is not supported' % arr.dtype.name)
tensor_proto = tensor_util.make_tensor_proto(arr.astype(np.float32))
summary_metadata = metadata.create_summary_metadata(
display_name=None, description=description)
summary = summary_pb2.Summary()
summary.value.add(tag=tag,
metadata=summary_metadata,
tensor=tensor_proto)
return summary | python | def scalar_pb(tag, data, description=None):
"""Create a scalar summary_pb2.Summary protobuf.
Arguments:
tag: String tag for the summary.
data: A 0-dimensional `np.array` or a compatible python number type.
description: Optional long-form description for this summary, as a
`str`. Markdown is supported. Defaults to empty.
Raises:
ValueError: If the type or shape of the data is unsupported.
Returns:
A `summary_pb2.Summary` protobuf object.
"""
arr = np.array(data)
if arr.shape != ():
raise ValueError('Expected scalar shape for tensor, got shape: %s.'
% arr.shape)
if arr.dtype.kind not in ('b', 'i', 'u', 'f'): # bool, int, uint, float
raise ValueError('Cast %s to float is not supported' % arr.dtype.name)
tensor_proto = tensor_util.make_tensor_proto(arr.astype(np.float32))
summary_metadata = metadata.create_summary_metadata(
display_name=None, description=description)
summary = summary_pb2.Summary()
summary.value.add(tag=tag,
metadata=summary_metadata,
tensor=tensor_proto)
return summary | [
"def",
"scalar_pb",
"(",
"tag",
",",
"data",
",",
"description",
"=",
"None",
")",
":",
"arr",
"=",
"np",
".",
"array",
"(",
"data",
")",
"if",
"arr",
".",
"shape",
"!=",
"(",
")",
":",
"raise",
"ValueError",
"(",
"'Expected scalar shape for tensor, got shape: %s.'",
"%",
"arr",
".",
"shape",
")",
"if",
"arr",
".",
"dtype",
".",
"kind",
"not",
"in",
"(",
"'b'",
",",
"'i'",
",",
"'u'",
",",
"'f'",
")",
":",
"# bool, int, uint, float",
"raise",
"ValueError",
"(",
"'Cast %s to float is not supported'",
"%",
"arr",
".",
"dtype",
".",
"name",
")",
"tensor_proto",
"=",
"tensor_util",
".",
"make_tensor_proto",
"(",
"arr",
".",
"astype",
"(",
"np",
".",
"float32",
")",
")",
"summary_metadata",
"=",
"metadata",
".",
"create_summary_metadata",
"(",
"display_name",
"=",
"None",
",",
"description",
"=",
"description",
")",
"summary",
"=",
"summary_pb2",
".",
"Summary",
"(",
")",
"summary",
".",
"value",
".",
"add",
"(",
"tag",
"=",
"tag",
",",
"metadata",
"=",
"summary_metadata",
",",
"tensor",
"=",
"tensor_proto",
")",
"return",
"summary"
] | Create a scalar summary_pb2.Summary protobuf.
Arguments:
tag: String tag for the summary.
data: A 0-dimensional `np.array` or a compatible python number type.
description: Optional long-form description for this summary, as a
`str`. Markdown is supported. Defaults to empty.
Raises:
ValueError: If the type or shape of the data is unsupported.
Returns:
A `summary_pb2.Summary` protobuf object. | [
"Create",
"a",
"scalar",
"summary_pb2",
".",
"Summary",
"protobuf",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/scalar/summary_v2.py#L68-L96 | train |
tensorflow/tensorboard | tensorboard/plugins/profile/profile_demo.py | dump_data | def dump_data(logdir):
"""Dumps plugin data to the log directory."""
# Create a tfevents file in the logdir so it is detected as a run.
write_empty_event_file(logdir)
plugin_logdir = plugin_asset_util.PluginDirectory(
logdir, profile_plugin.ProfilePlugin.plugin_name)
_maybe_create_directory(plugin_logdir)
for run in profile_demo_data.RUNS:
run_dir = os.path.join(plugin_logdir, run)
_maybe_create_directory(run_dir)
if run in profile_demo_data.TRACES:
with open(os.path.join(run_dir, 'trace'), 'w') as f:
proto = trace_events_pb2.Trace()
text_format.Merge(profile_demo_data.TRACES[run], proto)
f.write(proto.SerializeToString())
if run not in profile_demo_data.TRACE_ONLY:
shutil.copyfile('tensorboard/plugins/profile/profile_demo.op_profile.json',
os.path.join(run_dir, 'op_profile.json'))
shutil.copyfile(
'tensorboard/plugins/profile/profile_demo.memory_viewer.json',
os.path.join(run_dir, 'memory_viewer.json'))
shutil.copyfile(
'tensorboard/plugins/profile/profile_demo.pod_viewer.json',
os.path.join(run_dir, 'pod_viewer.json'))
shutil.copyfile(
'tensorboard/plugins/profile/profile_demo.google_chart_demo.json',
os.path.join(run_dir, 'google_chart_demo.json'))
# Unsupported tool data should not be displayed.
run_dir = os.path.join(plugin_logdir, 'empty')
_maybe_create_directory(run_dir)
with open(os.path.join(run_dir, 'unsupported'), 'w') as f:
f.write('unsupported data') | python | def dump_data(logdir):
"""Dumps plugin data to the log directory."""
# Create a tfevents file in the logdir so it is detected as a run.
write_empty_event_file(logdir)
plugin_logdir = plugin_asset_util.PluginDirectory(
logdir, profile_plugin.ProfilePlugin.plugin_name)
_maybe_create_directory(plugin_logdir)
for run in profile_demo_data.RUNS:
run_dir = os.path.join(plugin_logdir, run)
_maybe_create_directory(run_dir)
if run in profile_demo_data.TRACES:
with open(os.path.join(run_dir, 'trace'), 'w') as f:
proto = trace_events_pb2.Trace()
text_format.Merge(profile_demo_data.TRACES[run], proto)
f.write(proto.SerializeToString())
if run not in profile_demo_data.TRACE_ONLY:
shutil.copyfile('tensorboard/plugins/profile/profile_demo.op_profile.json',
os.path.join(run_dir, 'op_profile.json'))
shutil.copyfile(
'tensorboard/plugins/profile/profile_demo.memory_viewer.json',
os.path.join(run_dir, 'memory_viewer.json'))
shutil.copyfile(
'tensorboard/plugins/profile/profile_demo.pod_viewer.json',
os.path.join(run_dir, 'pod_viewer.json'))
shutil.copyfile(
'tensorboard/plugins/profile/profile_demo.google_chart_demo.json',
os.path.join(run_dir, 'google_chart_demo.json'))
# Unsupported tool data should not be displayed.
run_dir = os.path.join(plugin_logdir, 'empty')
_maybe_create_directory(run_dir)
with open(os.path.join(run_dir, 'unsupported'), 'w') as f:
f.write('unsupported data') | [
"def",
"dump_data",
"(",
"logdir",
")",
":",
"# Create a tfevents file in the logdir so it is detected as a run.",
"write_empty_event_file",
"(",
"logdir",
")",
"plugin_logdir",
"=",
"plugin_asset_util",
".",
"PluginDirectory",
"(",
"logdir",
",",
"profile_plugin",
".",
"ProfilePlugin",
".",
"plugin_name",
")",
"_maybe_create_directory",
"(",
"plugin_logdir",
")",
"for",
"run",
"in",
"profile_demo_data",
".",
"RUNS",
":",
"run_dir",
"=",
"os",
".",
"path",
".",
"join",
"(",
"plugin_logdir",
",",
"run",
")",
"_maybe_create_directory",
"(",
"run_dir",
")",
"if",
"run",
"in",
"profile_demo_data",
".",
"TRACES",
":",
"with",
"open",
"(",
"os",
".",
"path",
".",
"join",
"(",
"run_dir",
",",
"'trace'",
")",
",",
"'w'",
")",
"as",
"f",
":",
"proto",
"=",
"trace_events_pb2",
".",
"Trace",
"(",
")",
"text_format",
".",
"Merge",
"(",
"profile_demo_data",
".",
"TRACES",
"[",
"run",
"]",
",",
"proto",
")",
"f",
".",
"write",
"(",
"proto",
".",
"SerializeToString",
"(",
")",
")",
"if",
"run",
"not",
"in",
"profile_demo_data",
".",
"TRACE_ONLY",
":",
"shutil",
".",
"copyfile",
"(",
"'tensorboard/plugins/profile/profile_demo.op_profile.json'",
",",
"os",
".",
"path",
".",
"join",
"(",
"run_dir",
",",
"'op_profile.json'",
")",
")",
"shutil",
".",
"copyfile",
"(",
"'tensorboard/plugins/profile/profile_demo.memory_viewer.json'",
",",
"os",
".",
"path",
".",
"join",
"(",
"run_dir",
",",
"'memory_viewer.json'",
")",
")",
"shutil",
".",
"copyfile",
"(",
"'tensorboard/plugins/profile/profile_demo.pod_viewer.json'",
",",
"os",
".",
"path",
".",
"join",
"(",
"run_dir",
",",
"'pod_viewer.json'",
")",
")",
"shutil",
".",
"copyfile",
"(",
"'tensorboard/plugins/profile/profile_demo.google_chart_demo.json'",
",",
"os",
".",
"path",
".",
"join",
"(",
"run_dir",
",",
"'google_chart_demo.json'",
")",
")",
"# Unsupported tool data should not be displayed.",
"run_dir",
"=",
"os",
".",
"path",
".",
"join",
"(",
"plugin_logdir",
",",
"'empty'",
")",
"_maybe_create_directory",
"(",
"run_dir",
")",
"with",
"open",
"(",
"os",
".",
"path",
".",
"join",
"(",
"run_dir",
",",
"'unsupported'",
")",
",",
"'w'",
")",
"as",
"f",
":",
"f",
".",
"write",
"(",
"'unsupported data'",
")"
] | Dumps plugin data to the log directory. | [
"Dumps",
"plugin",
"data",
"to",
"the",
"log",
"directory",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/profile/profile_demo.py#L67-L102 | train |
tensorflow/tensorboard | tensorboard/plugins/debugger/health_pill_calc.py | calc_health_pill | def calc_health_pill(tensor):
"""Calculate health pill of a tensor.
Args:
tensor: An instance of `np.array` (for initialized tensors) or
`tensorflow.python.debug.lib.debug_data.InconvertibleTensorProto`
(for unininitialized tensors).
Returns:
If `tensor` is an initialized tensor of numeric or boolean types:
the calculated health pill, as a `list` of `float`s.
Else if `tensor` is an initialized tensor with `string`, `resource` or any
other non-numeric types:
`None`.
Else (i.e., if `tensor` is uninitialized): An all-zero `list`, with the
first element signifying that the tensor is uninitialized.
"""
health_pill = [0.0] * 14
# TODO(cais): Add unit test for this method that compares results with
# DebugNumericSummary output.
# Is tensor initialized.
if not isinstance(tensor, np.ndarray):
return health_pill
health_pill[0] = 1.0
if not (np.issubdtype(tensor.dtype, np.float) or
np.issubdtype(tensor.dtype, np.complex) or
np.issubdtype(tensor.dtype, np.integer) or
tensor.dtype == np.bool):
return None
# Total number of elements.
health_pill[1] = float(np.size(tensor))
# TODO(cais): Further performance optimization?
nan_mask = np.isnan(tensor)
inf_mask = np.isinf(tensor)
# Number of NaN elements.
health_pill[2] = float(np.sum(nan_mask))
# Number of -Inf elements.
health_pill[3] = float(np.sum(tensor == -np.inf))
# Number of finite negative elements.
health_pill[4] = float(np.sum(
np.logical_and(np.logical_not(inf_mask), tensor < 0.0)))
# Number of zero elements.
health_pill[5] = float(np.sum(tensor == 0.0))
# Number finite positive elements.
health_pill[6] = float(np.sum(
np.logical_and(np.logical_not(inf_mask), tensor > 0.0)))
# Number of +Inf elements.
health_pill[7] = float(np.sum(tensor == np.inf))
finite_subset = tensor[
np.logical_and(np.logical_not(nan_mask), np.logical_not(inf_mask))]
if np.size(finite_subset):
# Finite subset is not empty.
# Minimum of the non-NaN non-Inf elements.
health_pill[8] = float(np.min(finite_subset))
# Maximum of the non-NaN non-Inf elements.
health_pill[9] = float(np.max(finite_subset))
# Mean of the non-NaN non-Inf elements.
health_pill[10] = float(np.mean(finite_subset))
# Variance of the non-NaN non-Inf elements.
health_pill[11] = float(np.var(finite_subset))
else:
# If no finite element exists:
# Set minimum to +inf.
health_pill[8] = np.inf
# Set maximum to -inf.
health_pill[9] = -np.inf
# Set mean to NaN.
health_pill[10] = np.nan
# Set variance to NaN.
health_pill[11] = np.nan
# DType encoded as a number.
# TODO(cais): Convert numpy dtype to corresponding tensorflow dtype enum.
health_pill[12] = -1.0
# ndims.
health_pill[13] = float(len(tensor.shape))
# Size of the dimensions.
health_pill.extend([float(x) for x in tensor.shape])
return health_pill | python | def calc_health_pill(tensor):
"""Calculate health pill of a tensor.
Args:
tensor: An instance of `np.array` (for initialized tensors) or
`tensorflow.python.debug.lib.debug_data.InconvertibleTensorProto`
(for unininitialized tensors).
Returns:
If `tensor` is an initialized tensor of numeric or boolean types:
the calculated health pill, as a `list` of `float`s.
Else if `tensor` is an initialized tensor with `string`, `resource` or any
other non-numeric types:
`None`.
Else (i.e., if `tensor` is uninitialized): An all-zero `list`, with the
first element signifying that the tensor is uninitialized.
"""
health_pill = [0.0] * 14
# TODO(cais): Add unit test for this method that compares results with
# DebugNumericSummary output.
# Is tensor initialized.
if not isinstance(tensor, np.ndarray):
return health_pill
health_pill[0] = 1.0
if not (np.issubdtype(tensor.dtype, np.float) or
np.issubdtype(tensor.dtype, np.complex) or
np.issubdtype(tensor.dtype, np.integer) or
tensor.dtype == np.bool):
return None
# Total number of elements.
health_pill[1] = float(np.size(tensor))
# TODO(cais): Further performance optimization?
nan_mask = np.isnan(tensor)
inf_mask = np.isinf(tensor)
# Number of NaN elements.
health_pill[2] = float(np.sum(nan_mask))
# Number of -Inf elements.
health_pill[3] = float(np.sum(tensor == -np.inf))
# Number of finite negative elements.
health_pill[4] = float(np.sum(
np.logical_and(np.logical_not(inf_mask), tensor < 0.0)))
# Number of zero elements.
health_pill[5] = float(np.sum(tensor == 0.0))
# Number finite positive elements.
health_pill[6] = float(np.sum(
np.logical_and(np.logical_not(inf_mask), tensor > 0.0)))
# Number of +Inf elements.
health_pill[7] = float(np.sum(tensor == np.inf))
finite_subset = tensor[
np.logical_and(np.logical_not(nan_mask), np.logical_not(inf_mask))]
if np.size(finite_subset):
# Finite subset is not empty.
# Minimum of the non-NaN non-Inf elements.
health_pill[8] = float(np.min(finite_subset))
# Maximum of the non-NaN non-Inf elements.
health_pill[9] = float(np.max(finite_subset))
# Mean of the non-NaN non-Inf elements.
health_pill[10] = float(np.mean(finite_subset))
# Variance of the non-NaN non-Inf elements.
health_pill[11] = float(np.var(finite_subset))
else:
# If no finite element exists:
# Set minimum to +inf.
health_pill[8] = np.inf
# Set maximum to -inf.
health_pill[9] = -np.inf
# Set mean to NaN.
health_pill[10] = np.nan
# Set variance to NaN.
health_pill[11] = np.nan
# DType encoded as a number.
# TODO(cais): Convert numpy dtype to corresponding tensorflow dtype enum.
health_pill[12] = -1.0
# ndims.
health_pill[13] = float(len(tensor.shape))
# Size of the dimensions.
health_pill.extend([float(x) for x in tensor.shape])
return health_pill | [
"def",
"calc_health_pill",
"(",
"tensor",
")",
":",
"health_pill",
"=",
"[",
"0.0",
"]",
"*",
"14",
"# TODO(cais): Add unit test for this method that compares results with",
"# DebugNumericSummary output.",
"# Is tensor initialized.",
"if",
"not",
"isinstance",
"(",
"tensor",
",",
"np",
".",
"ndarray",
")",
":",
"return",
"health_pill",
"health_pill",
"[",
"0",
"]",
"=",
"1.0",
"if",
"not",
"(",
"np",
".",
"issubdtype",
"(",
"tensor",
".",
"dtype",
",",
"np",
".",
"float",
")",
"or",
"np",
".",
"issubdtype",
"(",
"tensor",
".",
"dtype",
",",
"np",
".",
"complex",
")",
"or",
"np",
".",
"issubdtype",
"(",
"tensor",
".",
"dtype",
",",
"np",
".",
"integer",
")",
"or",
"tensor",
".",
"dtype",
"==",
"np",
".",
"bool",
")",
":",
"return",
"None",
"# Total number of elements.",
"health_pill",
"[",
"1",
"]",
"=",
"float",
"(",
"np",
".",
"size",
"(",
"tensor",
")",
")",
"# TODO(cais): Further performance optimization?",
"nan_mask",
"=",
"np",
".",
"isnan",
"(",
"tensor",
")",
"inf_mask",
"=",
"np",
".",
"isinf",
"(",
"tensor",
")",
"# Number of NaN elements.",
"health_pill",
"[",
"2",
"]",
"=",
"float",
"(",
"np",
".",
"sum",
"(",
"nan_mask",
")",
")",
"# Number of -Inf elements.",
"health_pill",
"[",
"3",
"]",
"=",
"float",
"(",
"np",
".",
"sum",
"(",
"tensor",
"==",
"-",
"np",
".",
"inf",
")",
")",
"# Number of finite negative elements.",
"health_pill",
"[",
"4",
"]",
"=",
"float",
"(",
"np",
".",
"sum",
"(",
"np",
".",
"logical_and",
"(",
"np",
".",
"logical_not",
"(",
"inf_mask",
")",
",",
"tensor",
"<",
"0.0",
")",
")",
")",
"# Number of zero elements.",
"health_pill",
"[",
"5",
"]",
"=",
"float",
"(",
"np",
".",
"sum",
"(",
"tensor",
"==",
"0.0",
")",
")",
"# Number finite positive elements.",
"health_pill",
"[",
"6",
"]",
"=",
"float",
"(",
"np",
".",
"sum",
"(",
"np",
".",
"logical_and",
"(",
"np",
".",
"logical_not",
"(",
"inf_mask",
")",
",",
"tensor",
">",
"0.0",
")",
")",
")",
"# Number of +Inf elements.",
"health_pill",
"[",
"7",
"]",
"=",
"float",
"(",
"np",
".",
"sum",
"(",
"tensor",
"==",
"np",
".",
"inf",
")",
")",
"finite_subset",
"=",
"tensor",
"[",
"np",
".",
"logical_and",
"(",
"np",
".",
"logical_not",
"(",
"nan_mask",
")",
",",
"np",
".",
"logical_not",
"(",
"inf_mask",
")",
")",
"]",
"if",
"np",
".",
"size",
"(",
"finite_subset",
")",
":",
"# Finite subset is not empty.",
"# Minimum of the non-NaN non-Inf elements.",
"health_pill",
"[",
"8",
"]",
"=",
"float",
"(",
"np",
".",
"min",
"(",
"finite_subset",
")",
")",
"# Maximum of the non-NaN non-Inf elements.",
"health_pill",
"[",
"9",
"]",
"=",
"float",
"(",
"np",
".",
"max",
"(",
"finite_subset",
")",
")",
"# Mean of the non-NaN non-Inf elements.",
"health_pill",
"[",
"10",
"]",
"=",
"float",
"(",
"np",
".",
"mean",
"(",
"finite_subset",
")",
")",
"# Variance of the non-NaN non-Inf elements.",
"health_pill",
"[",
"11",
"]",
"=",
"float",
"(",
"np",
".",
"var",
"(",
"finite_subset",
")",
")",
"else",
":",
"# If no finite element exists:",
"# Set minimum to +inf.",
"health_pill",
"[",
"8",
"]",
"=",
"np",
".",
"inf",
"# Set maximum to -inf.",
"health_pill",
"[",
"9",
"]",
"=",
"-",
"np",
".",
"inf",
"# Set mean to NaN.",
"health_pill",
"[",
"10",
"]",
"=",
"np",
".",
"nan",
"# Set variance to NaN.",
"health_pill",
"[",
"11",
"]",
"=",
"np",
".",
"nan",
"# DType encoded as a number.",
"# TODO(cais): Convert numpy dtype to corresponding tensorflow dtype enum.",
"health_pill",
"[",
"12",
"]",
"=",
"-",
"1.0",
"# ndims.",
"health_pill",
"[",
"13",
"]",
"=",
"float",
"(",
"len",
"(",
"tensor",
".",
"shape",
")",
")",
"# Size of the dimensions.",
"health_pill",
".",
"extend",
"(",
"[",
"float",
"(",
"x",
")",
"for",
"x",
"in",
"tensor",
".",
"shape",
"]",
")",
"return",
"health_pill"
] | Calculate health pill of a tensor.
Args:
tensor: An instance of `np.array` (for initialized tensors) or
`tensorflow.python.debug.lib.debug_data.InconvertibleTensorProto`
(for unininitialized tensors).
Returns:
If `tensor` is an initialized tensor of numeric or boolean types:
the calculated health pill, as a `list` of `float`s.
Else if `tensor` is an initialized tensor with `string`, `resource` or any
other non-numeric types:
`None`.
Else (i.e., if `tensor` is uninitialized): An all-zero `list`, with the
first element signifying that the tensor is uninitialized. | [
"Calculate",
"health",
"pill",
"of",
"a",
"tensor",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/debugger/health_pill_calc.py#L34-L118 | train |
tensorflow/tensorboard | tensorboard/plugins/beholder/beholder.py | Beholder._get_config | def _get_config(self):
'''Reads the config file from disk or creates a new one.'''
filename = '{}/{}'.format(self.PLUGIN_LOGDIR, CONFIG_FILENAME)
modified_time = os.path.getmtime(filename)
if modified_time != self.config_last_modified_time:
config = read_pickle(filename, default=self.previous_config)
self.previous_config = config
else:
config = self.previous_config
self.config_last_modified_time = modified_time
return config | python | def _get_config(self):
'''Reads the config file from disk or creates a new one.'''
filename = '{}/{}'.format(self.PLUGIN_LOGDIR, CONFIG_FILENAME)
modified_time = os.path.getmtime(filename)
if modified_time != self.config_last_modified_time:
config = read_pickle(filename, default=self.previous_config)
self.previous_config = config
else:
config = self.previous_config
self.config_last_modified_time = modified_time
return config | [
"def",
"_get_config",
"(",
"self",
")",
":",
"filename",
"=",
"'{}/{}'",
".",
"format",
"(",
"self",
".",
"PLUGIN_LOGDIR",
",",
"CONFIG_FILENAME",
")",
"modified_time",
"=",
"os",
".",
"path",
".",
"getmtime",
"(",
"filename",
")",
"if",
"modified_time",
"!=",
"self",
".",
"config_last_modified_time",
":",
"config",
"=",
"read_pickle",
"(",
"filename",
",",
"default",
"=",
"self",
".",
"previous_config",
")",
"self",
".",
"previous_config",
"=",
"config",
"else",
":",
"config",
"=",
"self",
".",
"previous_config",
"self",
".",
"config_last_modified_time",
"=",
"modified_time",
"return",
"config"
] | Reads the config file from disk or creates a new one. | [
"Reads",
"the",
"config",
"file",
"from",
"disk",
"or",
"creates",
"a",
"new",
"one",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/beholder/beholder.py#L70-L82 | train |
tensorflow/tensorboard | tensorboard/plugins/beholder/beholder.py | Beholder._write_summary | def _write_summary(self, session, frame):
'''Writes the frame to disk as a tensor summary.'''
summary = session.run(self.summary_op, feed_dict={
self.frame_placeholder: frame
})
path = '{}/{}'.format(self.PLUGIN_LOGDIR, SUMMARY_FILENAME)
write_file(summary, path) | python | def _write_summary(self, session, frame):
'''Writes the frame to disk as a tensor summary.'''
summary = session.run(self.summary_op, feed_dict={
self.frame_placeholder: frame
})
path = '{}/{}'.format(self.PLUGIN_LOGDIR, SUMMARY_FILENAME)
write_file(summary, path) | [
"def",
"_write_summary",
"(",
"self",
",",
"session",
",",
"frame",
")",
":",
"summary",
"=",
"session",
".",
"run",
"(",
"self",
".",
"summary_op",
",",
"feed_dict",
"=",
"{",
"self",
".",
"frame_placeholder",
":",
"frame",
"}",
")",
"path",
"=",
"'{}/{}'",
".",
"format",
"(",
"self",
".",
"PLUGIN_LOGDIR",
",",
"SUMMARY_FILENAME",
")",
"write_file",
"(",
"summary",
",",
"path",
")"
] | Writes the frame to disk as a tensor summary. | [
"Writes",
"the",
"frame",
"to",
"disk",
"as",
"a",
"tensor",
"summary",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/beholder/beholder.py#L85-L91 | train |
tensorflow/tensorboard | tensorboard/plugins/beholder/beholder.py | Beholder._enough_time_has_passed | def _enough_time_has_passed(self, FPS):
'''For limiting how often frames are computed.'''
if FPS == 0:
return False
else:
earliest_time = self.last_update_time + (1.0 / FPS)
return time.time() >= earliest_time | python | def _enough_time_has_passed(self, FPS):
'''For limiting how often frames are computed.'''
if FPS == 0:
return False
else:
earliest_time = self.last_update_time + (1.0 / FPS)
return time.time() >= earliest_time | [
"def",
"_enough_time_has_passed",
"(",
"self",
",",
"FPS",
")",
":",
"if",
"FPS",
"==",
"0",
":",
"return",
"False",
"else",
":",
"earliest_time",
"=",
"self",
".",
"last_update_time",
"+",
"(",
"1.0",
"/",
"FPS",
")",
"return",
"time",
".",
"time",
"(",
")",
">=",
"earliest_time"
] | For limiting how often frames are computed. | [
"For",
"limiting",
"how",
"often",
"frames",
"are",
"computed",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/beholder/beholder.py#L121-L127 | train |
tensorflow/tensorboard | tensorboard/plugins/beholder/beholder.py | Beholder._update_recording | def _update_recording(self, frame, config):
'''Adds a frame to the current video output.'''
# pylint: disable=redefined-variable-type
should_record = config['is_recording']
if should_record:
if not self.is_recording:
self.is_recording = True
logger.info(
'Starting recording using %s',
self.video_writer.current_output().name())
self.video_writer.write_frame(frame)
elif self.is_recording:
self.is_recording = False
self.video_writer.finish()
logger.info('Finished recording') | python | def _update_recording(self, frame, config):
'''Adds a frame to the current video output.'''
# pylint: disable=redefined-variable-type
should_record = config['is_recording']
if should_record:
if not self.is_recording:
self.is_recording = True
logger.info(
'Starting recording using %s',
self.video_writer.current_output().name())
self.video_writer.write_frame(frame)
elif self.is_recording:
self.is_recording = False
self.video_writer.finish()
logger.info('Finished recording') | [
"def",
"_update_recording",
"(",
"self",
",",
"frame",
",",
"config",
")",
":",
"# pylint: disable=redefined-variable-type",
"should_record",
"=",
"config",
"[",
"'is_recording'",
"]",
"if",
"should_record",
":",
"if",
"not",
"self",
".",
"is_recording",
":",
"self",
".",
"is_recording",
"=",
"True",
"logger",
".",
"info",
"(",
"'Starting recording using %s'",
",",
"self",
".",
"video_writer",
".",
"current_output",
"(",
")",
".",
"name",
"(",
")",
")",
"self",
".",
"video_writer",
".",
"write_frame",
"(",
"frame",
")",
"elif",
"self",
".",
"is_recording",
":",
"self",
".",
"is_recording",
"=",
"False",
"self",
".",
"video_writer",
".",
"finish",
"(",
")",
"logger",
".",
"info",
"(",
"'Finished recording'",
")"
] | Adds a frame to the current video output. | [
"Adds",
"a",
"frame",
"to",
"the",
"current",
"video",
"output",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/beholder/beholder.py#L138-L153 | train |
tensorflow/tensorboard | tensorboard/plugins/beholder/beholder.py | Beholder.update | def update(self, session, arrays=None, frame=None):
'''Creates a frame and writes it to disk.
Args:
arrays: a list of np arrays. Use the "custom" option in the client.
frame: a 2D np array. This way the plugin can be used for video of any
kind, not just the visualization that comes with the plugin.
frame can also be a function, which only is evaluated when the
"frame" option is selected by the client.
'''
new_config = self._get_config()
if self._enough_time_has_passed(self.previous_config['FPS']):
self.visualizer.update(new_config)
self.last_update_time = time.time()
final_image = self._update_frame(session, arrays, frame, new_config)
self._update_recording(final_image, new_config) | python | def update(self, session, arrays=None, frame=None):
'''Creates a frame and writes it to disk.
Args:
arrays: a list of np arrays. Use the "custom" option in the client.
frame: a 2D np array. This way the plugin can be used for video of any
kind, not just the visualization that comes with the plugin.
frame can also be a function, which only is evaluated when the
"frame" option is selected by the client.
'''
new_config = self._get_config()
if self._enough_time_has_passed(self.previous_config['FPS']):
self.visualizer.update(new_config)
self.last_update_time = time.time()
final_image = self._update_frame(session, arrays, frame, new_config)
self._update_recording(final_image, new_config) | [
"def",
"update",
"(",
"self",
",",
"session",
",",
"arrays",
"=",
"None",
",",
"frame",
"=",
"None",
")",
":",
"new_config",
"=",
"self",
".",
"_get_config",
"(",
")",
"if",
"self",
".",
"_enough_time_has_passed",
"(",
"self",
".",
"previous_config",
"[",
"'FPS'",
"]",
")",
":",
"self",
".",
"visualizer",
".",
"update",
"(",
"new_config",
")",
"self",
".",
"last_update_time",
"=",
"time",
".",
"time",
"(",
")",
"final_image",
"=",
"self",
".",
"_update_frame",
"(",
"session",
",",
"arrays",
",",
"frame",
",",
"new_config",
")",
"self",
".",
"_update_recording",
"(",
"final_image",
",",
"new_config",
")"
] | Creates a frame and writes it to disk.
Args:
arrays: a list of np arrays. Use the "custom" option in the client.
frame: a 2D np array. This way the plugin can be used for video of any
kind, not just the visualization that comes with the plugin.
frame can also be a function, which only is evaluated when the
"frame" option is selected by the client. | [
"Creates",
"a",
"frame",
"and",
"writes",
"it",
"to",
"disk",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/beholder/beholder.py#L158-L175 | train |
tensorflow/tensorboard | tensorboard/plugins/beholder/beholder.py | Beholder.gradient_helper | def gradient_helper(optimizer, loss, var_list=None):
'''A helper to get the gradients out at each step.
Args:
optimizer: the optimizer op.
loss: the op that computes your loss value.
Returns: the gradient tensors and the train_step op.
'''
if var_list is None:
var_list = tf.compat.v1.trainable_variables()
grads_and_vars = optimizer.compute_gradients(loss, var_list=var_list)
grads = [pair[0] for pair in grads_and_vars]
return grads, optimizer.apply_gradients(grads_and_vars) | python | def gradient_helper(optimizer, loss, var_list=None):
'''A helper to get the gradients out at each step.
Args:
optimizer: the optimizer op.
loss: the op that computes your loss value.
Returns: the gradient tensors and the train_step op.
'''
if var_list is None:
var_list = tf.compat.v1.trainable_variables()
grads_and_vars = optimizer.compute_gradients(loss, var_list=var_list)
grads = [pair[0] for pair in grads_and_vars]
return grads, optimizer.apply_gradients(grads_and_vars) | [
"def",
"gradient_helper",
"(",
"optimizer",
",",
"loss",
",",
"var_list",
"=",
"None",
")",
":",
"if",
"var_list",
"is",
"None",
":",
"var_list",
"=",
"tf",
".",
"compat",
".",
"v1",
".",
"trainable_variables",
"(",
")",
"grads_and_vars",
"=",
"optimizer",
".",
"compute_gradients",
"(",
"loss",
",",
"var_list",
"=",
"var_list",
")",
"grads",
"=",
"[",
"pair",
"[",
"0",
"]",
"for",
"pair",
"in",
"grads_and_vars",
"]",
"return",
"grads",
",",
"optimizer",
".",
"apply_gradients",
"(",
"grads_and_vars",
")"
] | A helper to get the gradients out at each step.
Args:
optimizer: the optimizer op.
loss: the op that computes your loss value.
Returns: the gradient tensors and the train_step op. | [
"A",
"helper",
"to",
"get",
"the",
"gradients",
"out",
"at",
"each",
"step",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/beholder/beholder.py#L181-L196 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _create_key_func | def _create_key_func(extractor, none_is_largest):
"""Returns a key_func to be used in list.sort().
Returns a key_func to be used in list.sort() that sorts session groups
by the value extracted by extractor. 'None' extracted values will either
be considered largest or smallest as specified by the "none_is_largest"
boolean parameter.
Args:
extractor: An extractor function that extract the key from the session
group.
none_is_largest: bool. If true treats 'None's as largest; otherwise
smallest.
"""
if none_is_largest:
def key_func_none_is_largest(session_group):
value = extractor(session_group)
return (value is None, value)
return key_func_none_is_largest
def key_func_none_is_smallest(session_group):
value = extractor(session_group)
return (value is not None, value)
return key_func_none_is_smallest | python | def _create_key_func(extractor, none_is_largest):
"""Returns a key_func to be used in list.sort().
Returns a key_func to be used in list.sort() that sorts session groups
by the value extracted by extractor. 'None' extracted values will either
be considered largest or smallest as specified by the "none_is_largest"
boolean parameter.
Args:
extractor: An extractor function that extract the key from the session
group.
none_is_largest: bool. If true treats 'None's as largest; otherwise
smallest.
"""
if none_is_largest:
def key_func_none_is_largest(session_group):
value = extractor(session_group)
return (value is None, value)
return key_func_none_is_largest
def key_func_none_is_smallest(session_group):
value = extractor(session_group)
return (value is not None, value)
return key_func_none_is_smallest | [
"def",
"_create_key_func",
"(",
"extractor",
",",
"none_is_largest",
")",
":",
"if",
"none_is_largest",
":",
"def",
"key_func_none_is_largest",
"(",
"session_group",
")",
":",
"value",
"=",
"extractor",
"(",
"session_group",
")",
"return",
"(",
"value",
"is",
"None",
",",
"value",
")",
"return",
"key_func_none_is_largest",
"def",
"key_func_none_is_smallest",
"(",
"session_group",
")",
":",
"value",
"=",
"extractor",
"(",
"session_group",
")",
"return",
"(",
"value",
"is",
"not",
"None",
",",
"value",
")",
"return",
"key_func_none_is_smallest"
] | Returns a key_func to be used in list.sort().
Returns a key_func to be used in list.sort() that sorts session groups
by the value extracted by extractor. 'None' extracted values will either
be considered largest or smallest as specified by the "none_is_largest"
boolean parameter.
Args:
extractor: An extractor function that extract the key from the session
group.
none_is_largest: bool. If true treats 'None's as largest; otherwise
smallest. | [
"Returns",
"a",
"key_func",
"to",
"be",
"used",
"in",
"list",
".",
"sort",
"()",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L236-L259 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _create_extractors | def _create_extractors(col_params):
"""Creates extractors to extract properties corresponding to 'col_params'.
Args:
col_params: List of ListSessionGroupsRequest.ColParam protobufs.
Returns:
A list of extractor functions. The ith element in the
returned list extracts the column corresponding to the ith element of
_request.col_params
"""
result = []
for col_param in col_params:
result.append(_create_extractor(col_param))
return result | python | def _create_extractors(col_params):
"""Creates extractors to extract properties corresponding to 'col_params'.
Args:
col_params: List of ListSessionGroupsRequest.ColParam protobufs.
Returns:
A list of extractor functions. The ith element in the
returned list extracts the column corresponding to the ith element of
_request.col_params
"""
result = []
for col_param in col_params:
result.append(_create_extractor(col_param))
return result | [
"def",
"_create_extractors",
"(",
"col_params",
")",
":",
"result",
"=",
"[",
"]",
"for",
"col_param",
"in",
"col_params",
":",
"result",
".",
"append",
"(",
"_create_extractor",
"(",
"col_param",
")",
")",
"return",
"result"
] | Creates extractors to extract properties corresponding to 'col_params'.
Args:
col_params: List of ListSessionGroupsRequest.ColParam protobufs.
Returns:
A list of extractor functions. The ith element in the
returned list extracts the column corresponding to the ith element of
_request.col_params | [
"Creates",
"extractors",
"to",
"extract",
"properties",
"corresponding",
"to",
"col_params",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L264-L277 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _create_metric_extractor | def _create_metric_extractor(metric_name):
"""Returns function that extracts a metric from a session group or a session.
Args:
metric_name: tensorboard.hparams.MetricName protobuffer. Identifies the
metric to extract from the session group.
Returns:
A function that takes a tensorboard.hparams.SessionGroup or
tensorborad.hparams.Session protobuffer and returns the value of the metric
identified by 'metric_name' or None if the value doesn't exist.
"""
def extractor_fn(session_or_group):
metric_value = _find_metric_value(session_or_group,
metric_name)
return metric_value.value if metric_value else None
return extractor_fn | python | def _create_metric_extractor(metric_name):
"""Returns function that extracts a metric from a session group or a session.
Args:
metric_name: tensorboard.hparams.MetricName protobuffer. Identifies the
metric to extract from the session group.
Returns:
A function that takes a tensorboard.hparams.SessionGroup or
tensorborad.hparams.Session protobuffer and returns the value of the metric
identified by 'metric_name' or None if the value doesn't exist.
"""
def extractor_fn(session_or_group):
metric_value = _find_metric_value(session_or_group,
metric_name)
return metric_value.value if metric_value else None
return extractor_fn | [
"def",
"_create_metric_extractor",
"(",
"metric_name",
")",
":",
"def",
"extractor_fn",
"(",
"session_or_group",
")",
":",
"metric_value",
"=",
"_find_metric_value",
"(",
"session_or_group",
",",
"metric_name",
")",
"return",
"metric_value",
".",
"value",
"if",
"metric_value",
"else",
"None",
"return",
"extractor_fn"
] | Returns function that extracts a metric from a session group or a session.
Args:
metric_name: tensorboard.hparams.MetricName protobuffer. Identifies the
metric to extract from the session group.
Returns:
A function that takes a tensorboard.hparams.SessionGroup or
tensorborad.hparams.Session protobuffer and returns the value of the metric
identified by 'metric_name' or None if the value doesn't exist. | [
"Returns",
"function",
"that",
"extracts",
"a",
"metric",
"from",
"a",
"session",
"group",
"or",
"a",
"session",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L291-L307 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _find_metric_value | def _find_metric_value(session_or_group, metric_name):
"""Returns the metric_value for a given metric in a session or session group.
Args:
session_or_group: A Session protobuffer or SessionGroup protobuffer.
metric_name: A MetricName protobuffer. The metric to search for.
Returns:
A MetricValue protobuffer representing the value of the given metric or
None if no such metric was found in session_or_group.
"""
# Note: We can speed this up by converting the metric_values field
# to a dictionary on initialization, to avoid a linear search here. We'll
# need to wrap the SessionGroup and Session protos in a python object for
# that.
for metric_value in session_or_group.metric_values:
if (metric_value.name.tag == metric_name.tag and
metric_value.name.group == metric_name.group):
return metric_value | python | def _find_metric_value(session_or_group, metric_name):
"""Returns the metric_value for a given metric in a session or session group.
Args:
session_or_group: A Session protobuffer or SessionGroup protobuffer.
metric_name: A MetricName protobuffer. The metric to search for.
Returns:
A MetricValue protobuffer representing the value of the given metric or
None if no such metric was found in session_or_group.
"""
# Note: We can speed this up by converting the metric_values field
# to a dictionary on initialization, to avoid a linear search here. We'll
# need to wrap the SessionGroup and Session protos in a python object for
# that.
for metric_value in session_or_group.metric_values:
if (metric_value.name.tag == metric_name.tag and
metric_value.name.group == metric_name.group):
return metric_value | [
"def",
"_find_metric_value",
"(",
"session_or_group",
",",
"metric_name",
")",
":",
"# Note: We can speed this up by converting the metric_values field",
"# to a dictionary on initialization, to avoid a linear search here. We'll",
"# need to wrap the SessionGroup and Session protos in a python object for",
"# that.",
"for",
"metric_value",
"in",
"session_or_group",
".",
"metric_values",
":",
"if",
"(",
"metric_value",
".",
"name",
".",
"tag",
"==",
"metric_name",
".",
"tag",
"and",
"metric_value",
".",
"name",
".",
"group",
"==",
"metric_name",
".",
"group",
")",
":",
"return",
"metric_value"
] | Returns the metric_value for a given metric in a session or session group.
Args:
session_or_group: A Session protobuffer or SessionGroup protobuffer.
metric_name: A MetricName protobuffer. The metric to search for.
Returns:
A MetricValue protobuffer representing the value of the given metric or
None if no such metric was found in session_or_group. | [
"Returns",
"the",
"metric_value",
"for",
"a",
"given",
"metric",
"in",
"a",
"session",
"or",
"session",
"group",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L310-L327 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _create_hparam_extractor | def _create_hparam_extractor(hparam_name):
"""Returns an extractor function that extracts an hparam from a session group.
Args:
hparam_name: str. Identies the hparam to extract from the session group.
Returns:
A function that takes a tensorboard.hparams.SessionGroup protobuffer and
returns the value, as a native Python object, of the hparam identified by
'hparam_name'.
"""
def extractor_fn(session_group):
if hparam_name in session_group.hparams:
return _value_to_python(session_group.hparams[hparam_name])
return None
return extractor_fn | python | def _create_hparam_extractor(hparam_name):
"""Returns an extractor function that extracts an hparam from a session group.
Args:
hparam_name: str. Identies the hparam to extract from the session group.
Returns:
A function that takes a tensorboard.hparams.SessionGroup protobuffer and
returns the value, as a native Python object, of the hparam identified by
'hparam_name'.
"""
def extractor_fn(session_group):
if hparam_name in session_group.hparams:
return _value_to_python(session_group.hparams[hparam_name])
return None
return extractor_fn | [
"def",
"_create_hparam_extractor",
"(",
"hparam_name",
")",
":",
"def",
"extractor_fn",
"(",
"session_group",
")",
":",
"if",
"hparam_name",
"in",
"session_group",
".",
"hparams",
":",
"return",
"_value_to_python",
"(",
"session_group",
".",
"hparams",
"[",
"hparam_name",
"]",
")",
"return",
"None",
"return",
"extractor_fn"
] | Returns an extractor function that extracts an hparam from a session group.
Args:
hparam_name: str. Identies the hparam to extract from the session group.
Returns:
A function that takes a tensorboard.hparams.SessionGroup protobuffer and
returns the value, as a native Python object, of the hparam identified by
'hparam_name'. | [
"Returns",
"an",
"extractor",
"function",
"that",
"extracts",
"an",
"hparam",
"from",
"a",
"session",
"group",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L330-L345 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _create_filters | def _create_filters(col_params, extractors):
"""Creates filters for the given col_params.
Args:
col_params: List of ListSessionGroupsRequest.ColParam protobufs.
extractors: list of extractor functions of the same length as col_params.
Each element should extract the column described by the corresponding
element of col_params.
Returns:
A list of filter functions. Each corresponding to a single
col_params.filter oneof field of _request
"""
result = []
for col_param, extractor in zip(col_params, extractors):
a_filter = _create_filter(col_param, extractor)
if a_filter:
result.append(a_filter)
return result | python | def _create_filters(col_params, extractors):
"""Creates filters for the given col_params.
Args:
col_params: List of ListSessionGroupsRequest.ColParam protobufs.
extractors: list of extractor functions of the same length as col_params.
Each element should extract the column described by the corresponding
element of col_params.
Returns:
A list of filter functions. Each corresponding to a single
col_params.filter oneof field of _request
"""
result = []
for col_param, extractor in zip(col_params, extractors):
a_filter = _create_filter(col_param, extractor)
if a_filter:
result.append(a_filter)
return result | [
"def",
"_create_filters",
"(",
"col_params",
",",
"extractors",
")",
":",
"result",
"=",
"[",
"]",
"for",
"col_param",
",",
"extractor",
"in",
"zip",
"(",
"col_params",
",",
"extractors",
")",
":",
"a_filter",
"=",
"_create_filter",
"(",
"col_param",
",",
"extractor",
")",
"if",
"a_filter",
":",
"result",
".",
"append",
"(",
"a_filter",
")",
"return",
"result"
] | Creates filters for the given col_params.
Args:
col_params: List of ListSessionGroupsRequest.ColParam protobufs.
extractors: list of extractor functions of the same length as col_params.
Each element should extract the column described by the corresponding
element of col_params.
Returns:
A list of filter functions. Each corresponding to a single
col_params.filter oneof field of _request | [
"Creates",
"filters",
"for",
"the",
"given",
"col_params",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L352-L369 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _create_filter | def _create_filter(col_param, extractor):
"""Creates a filter for the given col_param and extractor.
Args:
col_param: A tensorboard.hparams.ColParams object identifying the column
and describing the filter to apply.
extractor: A function that extract the column value identified by
'col_param' from a tensorboard.hparams.SessionGroup protobuffer.
Returns:
A boolean function taking a tensorboard.hparams.SessionGroup protobuffer
returning True if the session group passes the filter described by
'col_param'. If col_param does not specify a filter (i.e. any session
group passes) returns None.
"""
include_missing_values = not col_param.exclude_missing_values
if col_param.HasField('filter_regexp'):
value_filter_fn = _create_regexp_filter(col_param.filter_regexp)
elif col_param.HasField('filter_interval'):
value_filter_fn = _create_interval_filter(col_param.filter_interval)
elif col_param.HasField('filter_discrete'):
value_filter_fn = _create_discrete_set_filter(col_param.filter_discrete)
elif include_missing_values:
# No 'filter' field and include_missing_values is True.
# Thus, the resulting filter always returns True, so to optimize for this
# common case we do not include it in the list of filters to check.
return None
else:
value_filter_fn = lambda _: True
def filter_fn(session_group):
value = extractor(session_group)
if value is None:
return include_missing_values
return value_filter_fn(value)
return filter_fn | python | def _create_filter(col_param, extractor):
"""Creates a filter for the given col_param and extractor.
Args:
col_param: A tensorboard.hparams.ColParams object identifying the column
and describing the filter to apply.
extractor: A function that extract the column value identified by
'col_param' from a tensorboard.hparams.SessionGroup protobuffer.
Returns:
A boolean function taking a tensorboard.hparams.SessionGroup protobuffer
returning True if the session group passes the filter described by
'col_param'. If col_param does not specify a filter (i.e. any session
group passes) returns None.
"""
include_missing_values = not col_param.exclude_missing_values
if col_param.HasField('filter_regexp'):
value_filter_fn = _create_regexp_filter(col_param.filter_regexp)
elif col_param.HasField('filter_interval'):
value_filter_fn = _create_interval_filter(col_param.filter_interval)
elif col_param.HasField('filter_discrete'):
value_filter_fn = _create_discrete_set_filter(col_param.filter_discrete)
elif include_missing_values:
# No 'filter' field and include_missing_values is True.
# Thus, the resulting filter always returns True, so to optimize for this
# common case we do not include it in the list of filters to check.
return None
else:
value_filter_fn = lambda _: True
def filter_fn(session_group):
value = extractor(session_group)
if value is None:
return include_missing_values
return value_filter_fn(value)
return filter_fn | [
"def",
"_create_filter",
"(",
"col_param",
",",
"extractor",
")",
":",
"include_missing_values",
"=",
"not",
"col_param",
".",
"exclude_missing_values",
"if",
"col_param",
".",
"HasField",
"(",
"'filter_regexp'",
")",
":",
"value_filter_fn",
"=",
"_create_regexp_filter",
"(",
"col_param",
".",
"filter_regexp",
")",
"elif",
"col_param",
".",
"HasField",
"(",
"'filter_interval'",
")",
":",
"value_filter_fn",
"=",
"_create_interval_filter",
"(",
"col_param",
".",
"filter_interval",
")",
"elif",
"col_param",
".",
"HasField",
"(",
"'filter_discrete'",
")",
":",
"value_filter_fn",
"=",
"_create_discrete_set_filter",
"(",
"col_param",
".",
"filter_discrete",
")",
"elif",
"include_missing_values",
":",
"# No 'filter' field and include_missing_values is True.",
"# Thus, the resulting filter always returns True, so to optimize for this",
"# common case we do not include it in the list of filters to check.",
"return",
"None",
"else",
":",
"value_filter_fn",
"=",
"lambda",
"_",
":",
"True",
"def",
"filter_fn",
"(",
"session_group",
")",
":",
"value",
"=",
"extractor",
"(",
"session_group",
")",
"if",
"value",
"is",
"None",
":",
"return",
"include_missing_values",
"return",
"value_filter_fn",
"(",
"value",
")",
"return",
"filter_fn"
] | Creates a filter for the given col_param and extractor.
Args:
col_param: A tensorboard.hparams.ColParams object identifying the column
and describing the filter to apply.
extractor: A function that extract the column value identified by
'col_param' from a tensorboard.hparams.SessionGroup protobuffer.
Returns:
A boolean function taking a tensorboard.hparams.SessionGroup protobuffer
returning True if the session group passes the filter described by
'col_param'. If col_param does not specify a filter (i.e. any session
group passes) returns None. | [
"Creates",
"a",
"filter",
"for",
"the",
"given",
"col_param",
"and",
"extractor",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L372-L407 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _create_regexp_filter | def _create_regexp_filter(regex):
"""Returns a boolean function that filters strings based on a regular exp.
Args:
regex: A string describing the regexp to use.
Returns:
A function taking a string and returns True if any of its substrings
matches regex.
"""
# Warning: Note that python's regex library allows inputs that take
# exponential time. Time-limiting it is difficult. When we move to
# a true multi-tenant tensorboard server, the regexp implementation here
# would need to be replaced by something more secure.
compiled_regex = re.compile(regex)
def filter_fn(value):
if not isinstance(value, six.string_types):
raise error.HParamsError(
'Cannot use a regexp filter for a value of type %s. Value: %s' %
(type(value), value))
return re.search(compiled_regex, value) is not None
return filter_fn | python | def _create_regexp_filter(regex):
"""Returns a boolean function that filters strings based on a regular exp.
Args:
regex: A string describing the regexp to use.
Returns:
A function taking a string and returns True if any of its substrings
matches regex.
"""
# Warning: Note that python's regex library allows inputs that take
# exponential time. Time-limiting it is difficult. When we move to
# a true multi-tenant tensorboard server, the regexp implementation here
# would need to be replaced by something more secure.
compiled_regex = re.compile(regex)
def filter_fn(value):
if not isinstance(value, six.string_types):
raise error.HParamsError(
'Cannot use a regexp filter for a value of type %s. Value: %s' %
(type(value), value))
return re.search(compiled_regex, value) is not None
return filter_fn | [
"def",
"_create_regexp_filter",
"(",
"regex",
")",
":",
"# Warning: Note that python's regex library allows inputs that take",
"# exponential time. Time-limiting it is difficult. When we move to",
"# a true multi-tenant tensorboard server, the regexp implementation here",
"# would need to be replaced by something more secure.",
"compiled_regex",
"=",
"re",
".",
"compile",
"(",
"regex",
")",
"def",
"filter_fn",
"(",
"value",
")",
":",
"if",
"not",
"isinstance",
"(",
"value",
",",
"six",
".",
"string_types",
")",
":",
"raise",
"error",
".",
"HParamsError",
"(",
"'Cannot use a regexp filter for a value of type %s. Value: %s'",
"%",
"(",
"type",
"(",
"value",
")",
",",
"value",
")",
")",
"return",
"re",
".",
"search",
"(",
"compiled_regex",
",",
"value",
")",
"is",
"not",
"None",
"return",
"filter_fn"
] | Returns a boolean function that filters strings based on a regular exp.
Args:
regex: A string describing the regexp to use.
Returns:
A function taking a string and returns True if any of its substrings
matches regex. | [
"Returns",
"a",
"boolean",
"function",
"that",
"filters",
"strings",
"based",
"on",
"a",
"regular",
"exp",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L410-L431 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _create_interval_filter | def _create_interval_filter(interval):
"""Returns a function that checkes whether a number belongs to an interval.
Args:
interval: A tensorboard.hparams.Interval protobuf describing the interval.
Returns:
A function taking a number (a float or an object of a type in
six.integer_types) that returns True if the number belongs to (the closed)
'interval'.
"""
def filter_fn(value):
if (not isinstance(value, six.integer_types) and
not isinstance(value, float)):
raise error.HParamsError(
'Cannot use an interval filter for a value of type: %s, Value: %s' %
(type(value), value))
return interval.min_value <= value and value <= interval.max_value
return filter_fn | python | def _create_interval_filter(interval):
"""Returns a function that checkes whether a number belongs to an interval.
Args:
interval: A tensorboard.hparams.Interval protobuf describing the interval.
Returns:
A function taking a number (a float or an object of a type in
six.integer_types) that returns True if the number belongs to (the closed)
'interval'.
"""
def filter_fn(value):
if (not isinstance(value, six.integer_types) and
not isinstance(value, float)):
raise error.HParamsError(
'Cannot use an interval filter for a value of type: %s, Value: %s' %
(type(value), value))
return interval.min_value <= value and value <= interval.max_value
return filter_fn | [
"def",
"_create_interval_filter",
"(",
"interval",
")",
":",
"def",
"filter_fn",
"(",
"value",
")",
":",
"if",
"(",
"not",
"isinstance",
"(",
"value",
",",
"six",
".",
"integer_types",
")",
"and",
"not",
"isinstance",
"(",
"value",
",",
"float",
")",
")",
":",
"raise",
"error",
".",
"HParamsError",
"(",
"'Cannot use an interval filter for a value of type: %s, Value: %s'",
"%",
"(",
"type",
"(",
"value",
")",
",",
"value",
")",
")",
"return",
"interval",
".",
"min_value",
"<=",
"value",
"and",
"value",
"<=",
"interval",
".",
"max_value",
"return",
"filter_fn"
] | Returns a function that checkes whether a number belongs to an interval.
Args:
interval: A tensorboard.hparams.Interval protobuf describing the interval.
Returns:
A function taking a number (a float or an object of a type in
six.integer_types) that returns True if the number belongs to (the closed)
'interval'. | [
"Returns",
"a",
"function",
"that",
"checkes",
"whether",
"a",
"number",
"belongs",
"to",
"an",
"interval",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L434-L452 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _value_to_python | def _value_to_python(value):
"""Converts a google.protobuf.Value to a native Python object."""
assert isinstance(value, struct_pb2.Value)
field = value.WhichOneof('kind')
if field == 'number_value':
return value.number_value
elif field == 'string_value':
return value.string_value
elif field == 'bool_value':
return value.bool_value
else:
raise ValueError('Unknown struct_pb2.Value oneof field set: %s' % field) | python | def _value_to_python(value):
"""Converts a google.protobuf.Value to a native Python object."""
assert isinstance(value, struct_pb2.Value)
field = value.WhichOneof('kind')
if field == 'number_value':
return value.number_value
elif field == 'string_value':
return value.string_value
elif field == 'bool_value':
return value.bool_value
else:
raise ValueError('Unknown struct_pb2.Value oneof field set: %s' % field) | [
"def",
"_value_to_python",
"(",
"value",
")",
":",
"assert",
"isinstance",
"(",
"value",
",",
"struct_pb2",
".",
"Value",
")",
"field",
"=",
"value",
".",
"WhichOneof",
"(",
"'kind'",
")",
"if",
"field",
"==",
"'number_value'",
":",
"return",
"value",
".",
"number_value",
"elif",
"field",
"==",
"'string_value'",
":",
"return",
"value",
".",
"string_value",
"elif",
"field",
"==",
"'bool_value'",
":",
"return",
"value",
".",
"bool_value",
"else",
":",
"raise",
"ValueError",
"(",
"'Unknown struct_pb2.Value oneof field set: %s'",
"%",
"field",
")"
] | Converts a google.protobuf.Value to a native Python object. | [
"Converts",
"a",
"google",
".",
"protobuf",
".",
"Value",
"to",
"a",
"native",
"Python",
"object",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L471-L483 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _set_avg_session_metrics | def _set_avg_session_metrics(session_group):
"""Sets the metrics for the group to be the average of its sessions.
The resulting session group metrics consist of the union of metrics across
the group's sessions. The value of each session group metric is the average
of that metric values across the sessions in the group. The 'step' and
'wall_time_secs' fields of the resulting MetricValue field in the session
group are populated with the corresponding averages (truncated for 'step')
as well.
Args:
session_group: A SessionGroup protobuffer.
"""
assert session_group.sessions, 'SessionGroup cannot be empty.'
# Algorithm: Iterate over all (session, metric) pairs and maintain a
# dict from _MetricIdentifier to _MetricStats objects.
# Then use the final dict state to compute the average for each metric.
metric_stats = collections.defaultdict(_MetricStats)
for session in session_group.sessions:
for metric_value in session.metric_values:
metric_name = _MetricIdentifier(group=metric_value.name.group,
tag=metric_value.name.tag)
stats = metric_stats[metric_name]
stats.total += metric_value.value
stats.count += 1
stats.total_step += metric_value.training_step
stats.total_wall_time_secs += metric_value.wall_time_secs
del session_group.metric_values[:]
for (metric_name, stats) in six.iteritems(metric_stats):
session_group.metric_values.add(
name=api_pb2.MetricName(group=metric_name.group, tag=metric_name.tag),
value=float(stats.total)/float(stats.count),
training_step=stats.total_step // stats.count,
wall_time_secs=stats.total_wall_time_secs / stats.count) | python | def _set_avg_session_metrics(session_group):
"""Sets the metrics for the group to be the average of its sessions.
The resulting session group metrics consist of the union of metrics across
the group's sessions. The value of each session group metric is the average
of that metric values across the sessions in the group. The 'step' and
'wall_time_secs' fields of the resulting MetricValue field in the session
group are populated with the corresponding averages (truncated for 'step')
as well.
Args:
session_group: A SessionGroup protobuffer.
"""
assert session_group.sessions, 'SessionGroup cannot be empty.'
# Algorithm: Iterate over all (session, metric) pairs and maintain a
# dict from _MetricIdentifier to _MetricStats objects.
# Then use the final dict state to compute the average for each metric.
metric_stats = collections.defaultdict(_MetricStats)
for session in session_group.sessions:
for metric_value in session.metric_values:
metric_name = _MetricIdentifier(group=metric_value.name.group,
tag=metric_value.name.tag)
stats = metric_stats[metric_name]
stats.total += metric_value.value
stats.count += 1
stats.total_step += metric_value.training_step
stats.total_wall_time_secs += metric_value.wall_time_secs
del session_group.metric_values[:]
for (metric_name, stats) in six.iteritems(metric_stats):
session_group.metric_values.add(
name=api_pb2.MetricName(group=metric_name.group, tag=metric_name.tag),
value=float(stats.total)/float(stats.count),
training_step=stats.total_step // stats.count,
wall_time_secs=stats.total_wall_time_secs / stats.count) | [
"def",
"_set_avg_session_metrics",
"(",
"session_group",
")",
":",
"assert",
"session_group",
".",
"sessions",
",",
"'SessionGroup cannot be empty.'",
"# Algorithm: Iterate over all (session, metric) pairs and maintain a",
"# dict from _MetricIdentifier to _MetricStats objects.",
"# Then use the final dict state to compute the average for each metric.",
"metric_stats",
"=",
"collections",
".",
"defaultdict",
"(",
"_MetricStats",
")",
"for",
"session",
"in",
"session_group",
".",
"sessions",
":",
"for",
"metric_value",
"in",
"session",
".",
"metric_values",
":",
"metric_name",
"=",
"_MetricIdentifier",
"(",
"group",
"=",
"metric_value",
".",
"name",
".",
"group",
",",
"tag",
"=",
"metric_value",
".",
"name",
".",
"tag",
")",
"stats",
"=",
"metric_stats",
"[",
"metric_name",
"]",
"stats",
".",
"total",
"+=",
"metric_value",
".",
"value",
"stats",
".",
"count",
"+=",
"1",
"stats",
".",
"total_step",
"+=",
"metric_value",
".",
"training_step",
"stats",
".",
"total_wall_time_secs",
"+=",
"metric_value",
".",
"wall_time_secs",
"del",
"session_group",
".",
"metric_values",
"[",
":",
"]",
"for",
"(",
"metric_name",
",",
"stats",
")",
"in",
"six",
".",
"iteritems",
"(",
"metric_stats",
")",
":",
"session_group",
".",
"metric_values",
".",
"add",
"(",
"name",
"=",
"api_pb2",
".",
"MetricName",
"(",
"group",
"=",
"metric_name",
".",
"group",
",",
"tag",
"=",
"metric_name",
".",
"tag",
")",
",",
"value",
"=",
"float",
"(",
"stats",
".",
"total",
")",
"/",
"float",
"(",
"stats",
".",
"count",
")",
",",
"training_step",
"=",
"stats",
".",
"total_step",
"//",
"stats",
".",
"count",
",",
"wall_time_secs",
"=",
"stats",
".",
"total_wall_time_secs",
"/",
"stats",
".",
"count",
")"
] | Sets the metrics for the group to be the average of its sessions.
The resulting session group metrics consist of the union of metrics across
the group's sessions. The value of each session group metric is the average
of that metric values across the sessions in the group. The 'step' and
'wall_time_secs' fields of the resulting MetricValue field in the session
group are populated with the corresponding averages (truncated for 'step')
as well.
Args:
session_group: A SessionGroup protobuffer. | [
"Sets",
"the",
"metrics",
"for",
"the",
"group",
"to",
"be",
"the",
"average",
"of",
"its",
"sessions",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L524-L558 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _set_median_session_metrics | def _set_median_session_metrics(session_group, aggregation_metric):
"""Sets the metrics for session_group to those of its "median session".
The median session is the session in session_group with the median value
of the metric given by 'aggregation_metric'. The median is taken over the
subset of sessions in the group whose 'aggregation_metric' was measured
at the largest training step among the sessions in the group.
Args:
session_group: A SessionGroup protobuffer.
aggregation_metric: A MetricName protobuffer.
"""
measurements = sorted(_measurements(session_group, aggregation_metric),
key=operator.attrgetter('metric_value.value'))
median_session = measurements[(len(measurements) - 1) // 2].session_index
del session_group.metric_values[:]
session_group.metric_values.MergeFrom(
session_group.sessions[median_session].metric_values) | python | def _set_median_session_metrics(session_group, aggregation_metric):
"""Sets the metrics for session_group to those of its "median session".
The median session is the session in session_group with the median value
of the metric given by 'aggregation_metric'. The median is taken over the
subset of sessions in the group whose 'aggregation_metric' was measured
at the largest training step among the sessions in the group.
Args:
session_group: A SessionGroup protobuffer.
aggregation_metric: A MetricName protobuffer.
"""
measurements = sorted(_measurements(session_group, aggregation_metric),
key=operator.attrgetter('metric_value.value'))
median_session = measurements[(len(measurements) - 1) // 2].session_index
del session_group.metric_values[:]
session_group.metric_values.MergeFrom(
session_group.sessions[median_session].metric_values) | [
"def",
"_set_median_session_metrics",
"(",
"session_group",
",",
"aggregation_metric",
")",
":",
"measurements",
"=",
"sorted",
"(",
"_measurements",
"(",
"session_group",
",",
"aggregation_metric",
")",
",",
"key",
"=",
"operator",
".",
"attrgetter",
"(",
"'metric_value.value'",
")",
")",
"median_session",
"=",
"measurements",
"[",
"(",
"len",
"(",
"measurements",
")",
"-",
"1",
")",
"//",
"2",
"]",
".",
"session_index",
"del",
"session_group",
".",
"metric_values",
"[",
":",
"]",
"session_group",
".",
"metric_values",
".",
"MergeFrom",
"(",
"session_group",
".",
"sessions",
"[",
"median_session",
"]",
".",
"metric_values",
")"
] | Sets the metrics for session_group to those of its "median session".
The median session is the session in session_group with the median value
of the metric given by 'aggregation_metric'. The median is taken over the
subset of sessions in the group whose 'aggregation_metric' was measured
at the largest training step among the sessions in the group.
Args:
session_group: A SessionGroup protobuffer.
aggregation_metric: A MetricName protobuffer. | [
"Sets",
"the",
"metrics",
"for",
"session_group",
"to",
"those",
"of",
"its",
"median",
"session",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L567-L584 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _set_extremum_session_metrics | def _set_extremum_session_metrics(session_group, aggregation_metric,
extremum_fn):
"""Sets the metrics for session_group to those of its "extremum session".
The extremum session is the session in session_group with the extremum value
of the metric given by 'aggregation_metric'. The extremum is taken over the
subset of sessions in the group whose 'aggregation_metric' was measured
at the largest training step among the sessions in the group.
Args:
session_group: A SessionGroup protobuffer.
aggregation_metric: A MetricName protobuffer.
extremum_fn: callable. Must be either 'min' or 'max'. Determines the type of
extremum to compute.
"""
measurements = _measurements(session_group, aggregation_metric)
ext_session = extremum_fn(
measurements,
key=operator.attrgetter('metric_value.value')).session_index
del session_group.metric_values[:]
session_group.metric_values.MergeFrom(
session_group.sessions[ext_session].metric_values) | python | def _set_extremum_session_metrics(session_group, aggregation_metric,
extremum_fn):
"""Sets the metrics for session_group to those of its "extremum session".
The extremum session is the session in session_group with the extremum value
of the metric given by 'aggregation_metric'. The extremum is taken over the
subset of sessions in the group whose 'aggregation_metric' was measured
at the largest training step among the sessions in the group.
Args:
session_group: A SessionGroup protobuffer.
aggregation_metric: A MetricName protobuffer.
extremum_fn: callable. Must be either 'min' or 'max'. Determines the type of
extremum to compute.
"""
measurements = _measurements(session_group, aggregation_metric)
ext_session = extremum_fn(
measurements,
key=operator.attrgetter('metric_value.value')).session_index
del session_group.metric_values[:]
session_group.metric_values.MergeFrom(
session_group.sessions[ext_session].metric_values) | [
"def",
"_set_extremum_session_metrics",
"(",
"session_group",
",",
"aggregation_metric",
",",
"extremum_fn",
")",
":",
"measurements",
"=",
"_measurements",
"(",
"session_group",
",",
"aggregation_metric",
")",
"ext_session",
"=",
"extremum_fn",
"(",
"measurements",
",",
"key",
"=",
"operator",
".",
"attrgetter",
"(",
"'metric_value.value'",
")",
")",
".",
"session_index",
"del",
"session_group",
".",
"metric_values",
"[",
":",
"]",
"session_group",
".",
"metric_values",
".",
"MergeFrom",
"(",
"session_group",
".",
"sessions",
"[",
"ext_session",
"]",
".",
"metric_values",
")"
] | Sets the metrics for session_group to those of its "extremum session".
The extremum session is the session in session_group with the extremum value
of the metric given by 'aggregation_metric'. The extremum is taken over the
subset of sessions in the group whose 'aggregation_metric' was measured
at the largest training step among the sessions in the group.
Args:
session_group: A SessionGroup protobuffer.
aggregation_metric: A MetricName protobuffer.
extremum_fn: callable. Must be either 'min' or 'max'. Determines the type of
extremum to compute. | [
"Sets",
"the",
"metrics",
"for",
"session_group",
"to",
"those",
"of",
"its",
"extremum",
"session",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L587-L608 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | _measurements | def _measurements(session_group, metric_name):
"""A generator for the values of the metric across the sessions in the group.
Args:
session_group: A SessionGroup protobuffer.
metric_name: A MetricName protobuffer.
Yields:
The next metric value wrapped in a _Measurement instance.
"""
for session_index, session in enumerate(session_group.sessions):
metric_value = _find_metric_value(session, metric_name)
if not metric_value:
continue
yield _Measurement(metric_value, session_index) | python | def _measurements(session_group, metric_name):
"""A generator for the values of the metric across the sessions in the group.
Args:
session_group: A SessionGroup protobuffer.
metric_name: A MetricName protobuffer.
Yields:
The next metric value wrapped in a _Measurement instance.
"""
for session_index, session in enumerate(session_group.sessions):
metric_value = _find_metric_value(session, metric_name)
if not metric_value:
continue
yield _Measurement(metric_value, session_index) | [
"def",
"_measurements",
"(",
"session_group",
",",
"metric_name",
")",
":",
"for",
"session_index",
",",
"session",
"in",
"enumerate",
"(",
"session_group",
".",
"sessions",
")",
":",
"metric_value",
"=",
"_find_metric_value",
"(",
"session",
",",
"metric_name",
")",
"if",
"not",
"metric_value",
":",
"continue",
"yield",
"_Measurement",
"(",
"metric_value",
",",
"session_index",
")"
] | A generator for the values of the metric across the sessions in the group.
Args:
session_group: A SessionGroup protobuffer.
metric_name: A MetricName protobuffer.
Yields:
The next metric value wrapped in a _Measurement instance. | [
"A",
"generator",
"for",
"the",
"values",
"of",
"the",
"metric",
"across",
"the",
"sessions",
"in",
"the",
"group",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L611-L624 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | Handler.run | def run(self):
"""Handles the request specified on construction.
Returns:
A ListSessionGroupsResponse object.
"""
session_groups = self._build_session_groups()
session_groups = self._filter(session_groups)
self._sort(session_groups)
return self._create_response(session_groups) | python | def run(self):
"""Handles the request specified on construction.
Returns:
A ListSessionGroupsResponse object.
"""
session_groups = self._build_session_groups()
session_groups = self._filter(session_groups)
self._sort(session_groups)
return self._create_response(session_groups) | [
"def",
"run",
"(",
"self",
")",
":",
"session_groups",
"=",
"self",
".",
"_build_session_groups",
"(",
")",
"session_groups",
"=",
"self",
".",
"_filter",
"(",
"session_groups",
")",
"self",
".",
"_sort",
"(",
"session_groups",
")",
"return",
"self",
".",
"_create_response",
"(",
"session_groups",
")"
] | Handles the request specified on construction.
Returns:
A ListSessionGroupsResponse object. | [
"Handles",
"the",
"request",
"specified",
"on",
"construction",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L53-L63 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | Handler._build_session_groups | def _build_session_groups(self):
"""Returns a list of SessionGroups protobuffers from the summary data."""
# Algorithm: We keep a dict 'groups_by_name' mapping a SessionGroup name
# (str) to a SessionGroup protobuffer. We traverse the runs associated with
# the plugin--each representing a single session. We form a Session
# protobuffer from each run and add it to the relevant SessionGroup object
# in the 'groups_by_name' dict. We create the SessionGroup object, if this
# is the first session of that group we encounter.
groups_by_name = {}
run_to_tag_to_content = self._context.multiplexer.PluginRunToTagToContent(
metadata.PLUGIN_NAME)
for (run, tag_to_content) in six.iteritems(run_to_tag_to_content):
if metadata.SESSION_START_INFO_TAG not in tag_to_content:
continue
start_info = metadata.parse_session_start_info_plugin_data(
tag_to_content[metadata.SESSION_START_INFO_TAG])
end_info = None
if metadata.SESSION_END_INFO_TAG in tag_to_content:
end_info = metadata.parse_session_end_info_plugin_data(
tag_to_content[metadata.SESSION_END_INFO_TAG])
session = self._build_session(run, start_info, end_info)
if session.status in self._request.allowed_statuses:
self._add_session(session, start_info, groups_by_name)
# Compute the session group's aggregated metrics for each group.
groups = groups_by_name.values()
for group in groups:
# We sort the sessions in a group so that the order is deterministic.
group.sessions.sort(key=operator.attrgetter('name'))
self._aggregate_metrics(group)
return groups | python | def _build_session_groups(self):
"""Returns a list of SessionGroups protobuffers from the summary data."""
# Algorithm: We keep a dict 'groups_by_name' mapping a SessionGroup name
# (str) to a SessionGroup protobuffer. We traverse the runs associated with
# the plugin--each representing a single session. We form a Session
# protobuffer from each run and add it to the relevant SessionGroup object
# in the 'groups_by_name' dict. We create the SessionGroup object, if this
# is the first session of that group we encounter.
groups_by_name = {}
run_to_tag_to_content = self._context.multiplexer.PluginRunToTagToContent(
metadata.PLUGIN_NAME)
for (run, tag_to_content) in six.iteritems(run_to_tag_to_content):
if metadata.SESSION_START_INFO_TAG not in tag_to_content:
continue
start_info = metadata.parse_session_start_info_plugin_data(
tag_to_content[metadata.SESSION_START_INFO_TAG])
end_info = None
if metadata.SESSION_END_INFO_TAG in tag_to_content:
end_info = metadata.parse_session_end_info_plugin_data(
tag_to_content[metadata.SESSION_END_INFO_TAG])
session = self._build_session(run, start_info, end_info)
if session.status in self._request.allowed_statuses:
self._add_session(session, start_info, groups_by_name)
# Compute the session group's aggregated metrics for each group.
groups = groups_by_name.values()
for group in groups:
# We sort the sessions in a group so that the order is deterministic.
group.sessions.sort(key=operator.attrgetter('name'))
self._aggregate_metrics(group)
return groups | [
"def",
"_build_session_groups",
"(",
"self",
")",
":",
"# Algorithm: We keep a dict 'groups_by_name' mapping a SessionGroup name",
"# (str) to a SessionGroup protobuffer. We traverse the runs associated with",
"# the plugin--each representing a single session. We form a Session",
"# protobuffer from each run and add it to the relevant SessionGroup object",
"# in the 'groups_by_name' dict. We create the SessionGroup object, if this",
"# is the first session of that group we encounter.",
"groups_by_name",
"=",
"{",
"}",
"run_to_tag_to_content",
"=",
"self",
".",
"_context",
".",
"multiplexer",
".",
"PluginRunToTagToContent",
"(",
"metadata",
".",
"PLUGIN_NAME",
")",
"for",
"(",
"run",
",",
"tag_to_content",
")",
"in",
"six",
".",
"iteritems",
"(",
"run_to_tag_to_content",
")",
":",
"if",
"metadata",
".",
"SESSION_START_INFO_TAG",
"not",
"in",
"tag_to_content",
":",
"continue",
"start_info",
"=",
"metadata",
".",
"parse_session_start_info_plugin_data",
"(",
"tag_to_content",
"[",
"metadata",
".",
"SESSION_START_INFO_TAG",
"]",
")",
"end_info",
"=",
"None",
"if",
"metadata",
".",
"SESSION_END_INFO_TAG",
"in",
"tag_to_content",
":",
"end_info",
"=",
"metadata",
".",
"parse_session_end_info_plugin_data",
"(",
"tag_to_content",
"[",
"metadata",
".",
"SESSION_END_INFO_TAG",
"]",
")",
"session",
"=",
"self",
".",
"_build_session",
"(",
"run",
",",
"start_info",
",",
"end_info",
")",
"if",
"session",
".",
"status",
"in",
"self",
".",
"_request",
".",
"allowed_statuses",
":",
"self",
".",
"_add_session",
"(",
"session",
",",
"start_info",
",",
"groups_by_name",
")",
"# Compute the session group's aggregated metrics for each group.",
"groups",
"=",
"groups_by_name",
".",
"values",
"(",
")",
"for",
"group",
"in",
"groups",
":",
"# We sort the sessions in a group so that the order is deterministic.",
"group",
".",
"sessions",
".",
"sort",
"(",
"key",
"=",
"operator",
".",
"attrgetter",
"(",
"'name'",
")",
")",
"self",
".",
"_aggregate_metrics",
"(",
"group",
")",
"return",
"groups"
] | Returns a list of SessionGroups protobuffers from the summary data. | [
"Returns",
"a",
"list",
"of",
"SessionGroups",
"protobuffers",
"from",
"the",
"summary",
"data",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L65-L96 | train |
tensorflow/tensorboard | tensorboard/plugins/hparams/list_session_groups.py | Handler._add_session | def _add_session(self, session, start_info, groups_by_name):
"""Adds a new Session protobuffer to the 'groups_by_name' dictionary.
Called by _build_session_groups when we encounter a new session. Creates
the Session protobuffer and adds it to the relevant group in the
'groups_by_name' dict. Creates the session group if this is the first time
we encounter it.
Args:
session: api_pb2.Session. The session to add.
start_info: The SessionStartInfo protobuffer associated with the session.
groups_by_name: A str to SessionGroup protobuffer dict. Representing the
session groups and sessions found so far.
"""
# If the group_name is empty, this session's group contains only
# this session. Use the session name for the group name since session
# names are unique.
group_name = start_info.group_name or session.name
if group_name in groups_by_name:
groups_by_name[group_name].sessions.extend([session])
else:
# Create the group and add the session as the first one.
group = api_pb2.SessionGroup(
name=group_name,
sessions=[session],
monitor_url=start_info.monitor_url)
# Copy hparams from the first session (all sessions should have the same
# hyperparameter values) into result.
# There doesn't seem to be a way to initialize a protobuffer map in the
# constructor.
for (key, value) in six.iteritems(start_info.hparams):
group.hparams[key].CopyFrom(value)
groups_by_name[group_name] = group | python | def _add_session(self, session, start_info, groups_by_name):
"""Adds a new Session protobuffer to the 'groups_by_name' dictionary.
Called by _build_session_groups when we encounter a new session. Creates
the Session protobuffer and adds it to the relevant group in the
'groups_by_name' dict. Creates the session group if this is the first time
we encounter it.
Args:
session: api_pb2.Session. The session to add.
start_info: The SessionStartInfo protobuffer associated with the session.
groups_by_name: A str to SessionGroup protobuffer dict. Representing the
session groups and sessions found so far.
"""
# If the group_name is empty, this session's group contains only
# this session. Use the session name for the group name since session
# names are unique.
group_name = start_info.group_name or session.name
if group_name in groups_by_name:
groups_by_name[group_name].sessions.extend([session])
else:
# Create the group and add the session as the first one.
group = api_pb2.SessionGroup(
name=group_name,
sessions=[session],
monitor_url=start_info.monitor_url)
# Copy hparams from the first session (all sessions should have the same
# hyperparameter values) into result.
# There doesn't seem to be a way to initialize a protobuffer map in the
# constructor.
for (key, value) in six.iteritems(start_info.hparams):
group.hparams[key].CopyFrom(value)
groups_by_name[group_name] = group | [
"def",
"_add_session",
"(",
"self",
",",
"session",
",",
"start_info",
",",
"groups_by_name",
")",
":",
"# If the group_name is empty, this session's group contains only",
"# this session. Use the session name for the group name since session",
"# names are unique.",
"group_name",
"=",
"start_info",
".",
"group_name",
"or",
"session",
".",
"name",
"if",
"group_name",
"in",
"groups_by_name",
":",
"groups_by_name",
"[",
"group_name",
"]",
".",
"sessions",
".",
"extend",
"(",
"[",
"session",
"]",
")",
"else",
":",
"# Create the group and add the session as the first one.",
"group",
"=",
"api_pb2",
".",
"SessionGroup",
"(",
"name",
"=",
"group_name",
",",
"sessions",
"=",
"[",
"session",
"]",
",",
"monitor_url",
"=",
"start_info",
".",
"monitor_url",
")",
"# Copy hparams from the first session (all sessions should have the same",
"# hyperparameter values) into result.",
"# There doesn't seem to be a way to initialize a protobuffer map in the",
"# constructor.",
"for",
"(",
"key",
",",
"value",
")",
"in",
"six",
".",
"iteritems",
"(",
"start_info",
".",
"hparams",
")",
":",
"group",
".",
"hparams",
"[",
"key",
"]",
".",
"CopyFrom",
"(",
"value",
")",
"groups_by_name",
"[",
"group_name",
"]",
"=",
"group"
] | Adds a new Session protobuffer to the 'groups_by_name' dictionary.
Called by _build_session_groups when we encounter a new session. Creates
the Session protobuffer and adds it to the relevant group in the
'groups_by_name' dict. Creates the session group if this is the first time
we encounter it.
Args:
session: api_pb2.Session. The session to add.
start_info: The SessionStartInfo protobuffer associated with the session.
groups_by_name: A str to SessionGroup protobuffer dict. Representing the
session groups and sessions found so far. | [
"Adds",
"a",
"new",
"Session",
"protobuffer",
"to",
"the",
"groups_by_name",
"dictionary",
"."
] | 8e5f497b48e40f2a774f85416b8a35ac0693c35e | https://github.com/tensorflow/tensorboard/blob/8e5f497b48e40f2a774f85416b8a35ac0693c35e/tensorboard/plugins/hparams/list_session_groups.py#L98-L130 | train |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.