本文整理汇总了Python中tensorflow.NodeDef方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.NodeDef方法的具体用法?Python tensorflow.NodeDef怎么用?Python tensorflow.NodeDef使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.NodeDef方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tf_obj_shape
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def tf_obj_shape(input):
"""
Convert tf objects to shape tuple.
Arguments:
input: tf.TensorShape, tf.Tensor, tf.AttrValue or tf.NodeDef
the corresponding tensorflow object
Returns:
tuple: shape of the tensorflow object
"""
if isinstance(input, tf.TensorShape):
return tuple([int(i.value) for i in input])
elif isinstance(input, tf.Tensor):
return tf_obj_shape(input.get_shape())
elif isinstance(input, tf.AttrValue):
return tuple([int(d.size) for d in input.shape.dim])
elif isinstance(input, tf.NodeDef):
return tf_obj_shape(input.attr['shape'])
else:
raise TypeError("Input to `tf_obj_shape` has the wrong type.")
示例2: assign_to_device
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def assign_to_device(self, op_dev, var_dev='/cpu:0'):
"""Returns a function to place variables on the var_dev, and the ops in the
op_dev.
Args:
op_dev: Device for ops
var_dev: Device for variables
"""
VAR_OPS = ['Variable', 'VariableV2', 'AutoReloadVariable',
'MutableHashTable', 'MutableHashTableOfTensors',
'MutableDenseHashTable']
def _assign(op):
node_def = op if isinstance(op, tf.NodeDef) else op.node_def
if node_def.op in VAR_OPS:
return "/" + var_dev
else:
return op_dev
return _assign
示例3: node_from_map
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def node_from_map(node_map, name):
"""Pulls a node def from a dictionary for a given name.
Args:
node_map: Dictionary containing an entry indexed by name for every node.
name: Identifies the node we want to find.
Returns:
NodeDef of the node with the given name.
Raises:
ValueError: If the node isn't present in the dictionary.
"""
stripped_name = node_name_from_input(name)
if stripped_name not in node_map:
raise ValueError("No node named '%s' found in map." % name)
return node_map[stripped_name]
示例4: values_from_const
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def values_from_const(node_def):
"""Extracts the values from a const NodeDef as a numpy ndarray.
Args:
node_def: Const NodeDef that has the values we want to access.
Returns:
Numpy ndarray containing the values.
Raises:
ValueError: If the node isn't a Const.
"""
if node_def.op != "Const":
raise ValueError(
"Node named '%s' should be a Const op for values_from_const." %
node_def.name)
input_tensor = node_def.attr["value"].tensor
tensor_value = tensor_util.MakeNdarray(input_tensor)
return tensor_value
示例5: quantize_nodes_recursively
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def quantize_nodes_recursively(self, current_node):
"""The entry point for quantizing nodes to eight bit and back."""
if self.already_visited[current_node.name]:
return
self.already_visited[current_node.name] = True
for input_node_name in current_node.input:
input_node_name = node_name_from_input(input_node_name)
input_node = self.nodes_map[input_node_name]
self.quantize_nodes_recursively(input_node)
nodes_to_quantize = ["Conv2D", "BiasAdd", "MatMul"]
if any(current_node.op in s for s in nodes_to_quantize):
for input_name in current_node.input:
input_name = node_name_from_input(input_name)
input_node = self.nodes_map[input_name]
self.quantize_node(input_node)
self.quantize_node(current_node)
else:
new_node = tf.NodeDef()
new_node.CopyFrom(current_node)
self.add_output_graph_node(new_node)
示例6: _split
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def _split(converter, node: Any, inputs: List[str]) -> Any:
if node.op == "SplitV":
# node.op is SplitV when num_or_size_splits is a list
x_in = converter.outputs[inputs[0]]
size_splits = converter.outputs[inputs[1]]
axis = converter.outputs[inputs[2]]
size_splits = size_splits.attr["value"].tensor
num_or_size_splits = list(array.array("I", size_splits.tensor_content))
else:
# node.op is Split when num_or_size_splits is an integer
axis = converter.outputs[inputs[0]]
x_in = converter.outputs[inputs[1]]
num_or_size_splits = node.attr["num_split"].i
if isinstance(x_in, tf.NodeDef):
input_out = _nodef_to_private_pond(converter, x_in)
else:
input_out = x_in
axis_val = axis.attr["value"].tensor.int_val[0]
return tfe.split(input_out, num_or_size_splits, axis_val)
示例7: _nodef_to_numpy_array
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def _nodef_to_numpy_array(x):
"""Map a NodeDef x to a np.array."""
dtype = x.attr["dtype"].type
x_shape = [i.size for i in x.attr["value"].tensor.tensor_shape.dim]
content = x.attr["value"].tensor.tensor_content
if dtype == tf.float32:
type_code = "f"
if not content:
content = x.attr["value"].tensor.float_val
elif dtype == tf.float64:
type_code = "d"
if not content:
content = x.attr["value"].tensor.double_val
elif dtype == tf.int32:
type_code = "i"
if not content:
content = x.attr["value"].tensor.int_val
else:
raise TypeError("Unsupported dtype")
nums = array.array(type_code, content)
return np.array(nums).reshape(x_shape)
示例8: variables_device
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def variables_device(self):
"""Returns the device to use for variables created inside the clone.
Returns:
A value suitable for `tf.device()`.
"""
device = ''
if self._num_ps_tasks > 0:
device += self._ps_device
device += '/device:CPU:0'
class _PSDeviceChooser(object):
"""Slim device chooser for variables when using PS."""
def __init__(self, device, tasks):
self._device = device
self._tasks = tasks
self._task = 0
def choose(self, op):
if op.device:
return op.device
node_def = op if isinstance(op, tf.NodeDef) else op.node_def
if node_def.op.startswith('Variable'):
t = self._task
self._task = (self._task + 1) % self._tasks
d = '%s/task:%d' % (self._device, t)
return d
else:
return op.device
if not self._num_ps_tasks:
return device
else:
chooser = _PSDeviceChooser(device, self._num_ps_tasks)
return chooser.choose
示例9: variable_device
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def variable_device(device, name):
"""Fix the variable device to colocate its ops."""
if callable(device):
var_name = tf.get_variable_scope().name + '/' + name
var_def = tf.NodeDef(name=var_name, op='Variable')
device = device(var_def)
if device is None:
device = ''
return device
示例10: assign_to_gpu
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def assign_to_gpu(gpu=0, ps_dev="/device:CPU:0"):
def _assign(op):
node_def = op if isinstance(op, tf.NodeDef) else op.node_def
if node_def.op == "Variable":
return ps_dev
else:
return "/gpu:%d" % gpu
return _assign
示例11: assign_to_device
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def assign_to_device(self, device, ps_device="/cpu:0"):
def _assign(op):
node_def = op if isinstance(op, tf.NodeDef) else op.node_def
if node_def.op in self.ps_ops:
device_name = ps_device
else:
device_name = device
# if device_name == "/cpu:0":
# print(op.name)
# print(device_name)
# print('-----------------------------------')
return device_name
return _assign
示例12: assign_to_device
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import NodeDef [as 别名]
def assign_to_device(self, device, ps_device='/cpu:0'):
def _assign(op):
node_def = op if isinstance(op, tf.NodeDef) else op.node_def
if node_def.op in PS_OPS:
return "/" + ps_device
else:
return device
return _assign