本文整理汇总了Python中tensorflow.as_dtype函数的典型用法代码示例。如果您正苦于以下问题:Python as_dtype函数的具体用法?Python as_dtype怎么用?Python as_dtype使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了as_dtype函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _setup_training
def _setup_training(self):
"""Sets up graph, model and trainer."""
self._graph = tf.Graph()
with self._graph.as_default():
tf.set_random_seed(self.tf_random_seed)
self._global_step = tf.Variable(0, name="global_step", trainable=False)
# Setting up input and output placeholders.
input_shape = [None] + self._data_feeder.input_shape[1:]
output_shape = [None] + self._data_feeder.output_shape[1:]
self._inp = tf.placeholder(
tf.as_dtype(self._data_feeder.input_dtype), input_shape,
name="input")
self._out = tf.placeholder(
tf.as_dtype(self._data_feeder.output_dtype), output_shape,
name="output")
# Create model's graph.
self._model_predictions, self._model_loss = self.model_fn(self._inp, self._out)
# Create trainer and augment graph with gradients and optimizer.
self._trainer = TensorFlowTrainer(self._model_loss,
self._global_step, self.optimizer, self.learning_rate)
self._session = tf.Session(self.tf_master,
config=tf.ConfigProto(log_device_placement=self.log_device_placement))
示例2: _setup_training
def _setup_training(self):
"""Sets up graph, model and trainer."""
self._graph = tf.Graph()
self._graph.add_to_collection("IS_TRAINING", True)
with self._graph.as_default():
tf.set_random_seed(self.tf_random_seed)
self._global_step = tf.Variable(
0, name="global_step", trainable=False)
# Setting up input and output placeholders.
input_shape = [None] + self._data_feeder.input_shape[1:]
output_shape = [None] + self._data_feeder.output_shape[1:]
self._inp = tf.placeholder(
tf.as_dtype(self._data_feeder.input_dtype), input_shape,
name="input")
self._out = tf.placeholder(
tf.as_dtype(self._data_feeder.output_dtype), output_shape,
name="output")
# If class weights are provided, add them to the graph.
# Different loss functions can use this tensor by name.
if self.class_weight:
self._class_weight_node = tf.constant(
self.class_weight, name='class_weight')
# Add histograms for X and y if they are floats.
if self._data_feeder.input_dtype in (np.float32, np.float64):
tf.histogram_summary("X", self._inp)
if self._data_feeder.output_dtype in (np.float32, np.float64):
tf.histogram_summary("y", self._out)
# Create model's graph.
self._model_predictions, self._model_loss = self.model_fn(
self._inp, self._out)
# Create summary to monitor loss
tf.scalar_summary("loss", self._model_loss)
# Set up a single operator to merge all the summaries
self._summaries = tf.merge_all_summaries()
# Create trainer and augment graph with gradients and optimizer.
# Additionally creates initialization ops.
self._trainer = TensorFlowTrainer(
loss=self._model_loss, global_step=self._global_step,
optimizer=self.optimizer, learning_rate=self.learning_rate)
# Create model's saver capturing all the nodes created up until now.
self._saver = tf.train.Saver(
max_to_keep=self.max_to_keep,
keep_checkpoint_every_n_hours=self.keep_checkpoint_every_n_hours)
# Enable monitor to create validation data dict with appropriate tf placeholders
self._monitor.create_val_feed_dict(self._inp, self._out)
# Create session to run model with.
if self.config_addon is None:
self.config_addon = ConfigAddon(verbose=self.verbose)
self._session = tf.Session(self.tf_master, config=self.config_addon.config)
示例3: _setup_training
def _setup_training(self):
"""Sets up graph, model and trainer."""
self._graph = tf.Graph()
self._graph.add_to_collection("IS_TRAINING", True)
with self._graph.as_default():
tf.set_random_seed(self.tf_random_seed)
self._global_step = tf.Variable(
0, name="global_step", trainable=False)
# Setting up input and output placeholders.
input_shape = [None] + self._data_feeder.input_shape[1:]
output_shape = [None] + self._data_feeder.output_shape[1:]
self._inp = tf.placeholder(
tf.as_dtype(self._data_feeder.input_dtype), input_shape,
name="input")
self._out = tf.placeholder(
tf.as_dtype(self._data_feeder.output_dtype), output_shape,
name="output")
# Add histograms for X and y if they are floats.
if self._data_feeder.input_dtype in (np.float32, np.float64):
tf.histogram_summary("X", self._inp)
if self._data_feeder.output_dtype in (np.float32, np.float64):
tf.histogram_summary("y", self._out)
# Create model's graph.
# pdb.set_trace()
self._model_predictions, self._model_loss = self.model_fn(
self._inp, self._out)
# Create summary to monitor loss
tf.scalar_summary("loss", self._model_loss)
# Set up a single operator to merge all the summaries
self._summaries = tf.merge_all_summaries()
with tf.name_scope("test") as scope:
if self.n_classes > 0:
correct_prediction = tf.equal(tf.argmax(self._out,1), tf.argmax(self._model_predictions,1))
self._cv_accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
self._cv_accuracy_summary = tf.scalar_summary("cv accuracy", self._cv_accuracy)
# Create trainer and augment graph with gradients and optimizer.
# Additionally creates initialization ops.
self._trainer = TensorFlowTrainer(
loss=self._model_loss, global_step=self._global_step,
optimizer=self.optimizer, learning_rate=self.learning_rate)
# Create model's saver capturing all the nodes created up until now.
self._saver = tf.train.Saver(
max_to_keep=self.max_to_keep,
keep_checkpoint_every_n_hours=self.keep_checkpoint_every_n_hours)
# Create session to run model with.
self._session = tf.Session(self.tf_master,
config=tf.ConfigProto(
log_device_placement=self.verbose > 1,
inter_op_parallelism_threads=self.num_cores,
intra_op_parallelism_threads=self.num_cores))
示例4: testAllTypesConvertibleToNumpyDtype
def testAllTypesConvertibleToNumpyDtype(self):
for datatype_enum in types_pb2.DataType.values():
if not _is_numeric_dtype_enum(datatype_enum):
continue
dtype = tf.as_dtype(datatype_enum)
numpy_dtype = dtype.as_numpy_dtype
_ = np.empty((1, 1, 1, 1), dtype=numpy_dtype)
if dtype.base_dtype != tf.bfloat16:
# NOTE(touts): Intentionally no way to feed a DT_BFLOAT16.
self.assertEqual(tf.as_dtype(datatype_enum).base_dtype, tf.as_dtype(numpy_dtype))
示例5: testIsInteger
def testIsInteger(self):
self.assertEqual(tf.as_dtype("int8").is_integer, True)
self.assertEqual(tf.as_dtype("int16").is_integer, True)
self.assertEqual(tf.as_dtype("int32").is_integer, True)
self.assertEqual(tf.as_dtype("int64").is_integer, True)
self.assertEqual(tf.as_dtype("uint8").is_integer, True)
self.assertEqual(tf.as_dtype("complex64").is_integer, False)
self.assertEqual(tf.as_dtype("float").is_integer, False)
self.assertEqual(tf.as_dtype("double").is_integer, False)
self.assertEqual(tf.as_dtype("string").is_integer, False)
self.assertEqual(tf.as_dtype("bool").is_integer, False)
示例6: testIsFloating
def testIsFloating(self):
self.assertEqual(tf.as_dtype("int8").is_floating, False)
self.assertEqual(tf.as_dtype("int16").is_floating, False)
self.assertEqual(tf.as_dtype("int32").is_floating, False)
self.assertEqual(tf.as_dtype("int64").is_floating, False)
self.assertEqual(tf.as_dtype("uint8").is_floating, False)
self.assertEqual(tf.as_dtype("complex64").is_floating, False)
self.assertEqual(tf.as_dtype("float32").is_floating, True)
self.assertEqual(tf.as_dtype("float64").is_floating, True)
self.assertEqual(tf.as_dtype("string").is_floating, False)
self.assertEqual(tf.as_dtype("bool").is_floating, False)
示例7: testIsUnsigned
def testIsUnsigned(self):
self.assertEqual(tf.as_dtype("int8").is_unsigned, False)
self.assertEqual(tf.as_dtype("int16").is_unsigned, False)
self.assertEqual(tf.as_dtype("int32").is_unsigned, False)
self.assertEqual(tf.as_dtype("int64").is_unsigned, False)
self.assertEqual(tf.as_dtype("uint8").is_unsigned, True)
self.assertEqual(tf.as_dtype("float32").is_unsigned, False)
self.assertEqual(tf.as_dtype("float64").is_unsigned, False)
self.assertEqual(tf.as_dtype("bool").is_unsigned, False)
self.assertEqual(tf.as_dtype("string").is_unsigned, False)
self.assertEqual(tf.as_dtype("complex64").is_unsigned, False)
示例8: fprop
def fprop(self, x, **kwargs):
del kwargs
with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE):
w1 = tf.constant([[1.5, .3], [-2, 0.3]],
dtype=tf.as_dtype(x.dtype))
w2 = tf.constant([[-2.4, 1.2], [0.5, -2.3]],
dtype=tf.as_dtype(x.dtype))
h1 = tf.nn.sigmoid(tf.matmul(x, w1))
res = tf.matmul(h1, w2)
return {self.O_LOGITS: res,
self.O_PROBS: tf.nn.softmax(res)}
示例9: __init__
def __init__(self, band_count, images, labels, dtype=tf.float32):
"""
Construct a DataSet.
`dtype` can be either `uint8` to leave the input as `[0, 255]`,
or `float32` to rescale into `[0, 1]`.
"""
dtype = tf.as_dtype(dtype).base_dtype
if dtype not in (tf.uint8, tf.float32):
raise TypeError('Invalid image dtype %r, expected uint8 or float32' % dtype)
assert images.shape[0] == labels.shape[0], (
'images.shape: %s labels.shape: %s' % (images.shape, labels.shape))
self._num_examples = images.shape[0]
# Convert shape from [num examples, rows, columns, depth]
# to [num examples, rows*columns] (assuming depth == 1)
assert images.shape[3] == band_count
# Store the width and height of the images before flattening it, if only for reference.
image_height, image_width = images.shape[1], images.shape[2]
self.original_image_width = image_width
self.original_image_height = image_height
images = images.reshape(images.shape[0], images.shape[1] * images.shape[2]*images.shape[3])
if dtype == tf.float32:
# Convert from [0, 255] -> [0.0, 1.0]
images = images.astype(numpy.float32)
images = numpy.multiply(images, 1.0 / 255.0)
self._images = images
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0
示例10: random_sign_uniform
def random_sign_uniform(
shape, minval=None, maxval=None, dtype=tf.float32, seed=None):
"""Tensor with (possibly complex) random entries from a "sign Uniform".
Letting `Z` be a random variable equal to `-1` and `1` with equal probability,
Samples from this `Op` are distributed like
```
Z * X, where X ~ Uniform[minval, maxval], if dtype is real,
Z * (X + iY), where X, Y ~ Uniform[minval, maxval], if dtype is complex.
```
Args:
shape: `TensorShape` or Python list. Shape of the returned tensor.
minval: `0-D` `Tensor` giving the minimum values.
maxval: `0-D` `Tensor` giving the maximum values.
dtype: `TensorFlow` `dtype` or Python dtype
seed: Python integer seed for the RNG.
Returns:
`Tensor` with desired shape and dtype.
"""
dtype = tf.as_dtype(dtype)
with tf.name_scope("random_sign_uniform"):
unsigned_samples = random_uniform(
shape, minval=minval, maxval=maxval, dtype=dtype, seed=seed)
if seed is not None:
seed += 12
signs = tf.sign(tf.random_uniform(shape, minval=-1., maxval=1., seed=seed))
return unsigned_samples * tf.cast(signs, unsigned_samples.dtype)
示例11: random_normal
def random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None):
"""Tensor with (possibly complex) Gaussian entries.
Samples are distributed like
```
N(mean, stddev^2), if dtype is real,
X + iY, where X, Y ~ N(mean, stddev^2) if dtype is complex.
```
Args:
shape: `TensorShape` or Python list. Shape of the returned tensor.
mean: `Tensor` giving mean of normal to sample from.
stddev: `Tensor` giving stdev of normal to sample from.
dtype: `TensorFlow` `dtype` or numpy dtype
seed: Python integer seed for the RNG.
Returns:
`Tensor` with desired shape and dtype.
"""
dtype = tf.as_dtype(dtype)
with tf.name_scope("random_normal"):
samples = tf.random_normal(
shape, mean=mean, stddev=stddev, dtype=dtype.real_dtype, seed=seed)
if dtype.is_complex:
if seed is not None:
seed += 1234
more_samples = tf.random_normal(
shape, mean=mean, stddev=stddev, dtype=dtype.real_dtype, seed=seed)
samples = tf.complex(samples, more_samples)
return samples
示例12: __init__
def __init__(self, instances, labels, xlength, ylength, imagedata,
patch_image_width, dtype = tf.float32):
dtype = tf.as_dtype(dtype).base_dtype
if dtype not in (tf.uint8, tf.float32):
raise TypeError('Invalid image dtype %r, expected uint8 or float32'
% dtype)
assert instances.shape[0] == labels.shape[0], (
'instances.shape: %s labels.shape: %s' % (instances.shape,
labels.shape))
self._num_examples = instances.shape[0]
if dtype == tf.float32:
for i in range(len(imagedata)):
imagedata[i] = imagedata[i].astype(np.float32)
imagedata[i] = np.multiply(imagedata[i], 1.0 / 255.0)
self._instances = instances
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0
self._xlength = xlength
self._ylength = ylength
self._imagedata = imagedata
self._patch_image_width= patch_image_width
示例13: __init__
def __init__(self, expression_profile, labels, feature_name, label_name, dtype=tf.float32):
"""Construct a DataSet.
`dtype` can be either
`uint8` to leave the input as `[0, 255]`, or `float32` to rescale into
`[0, 1]`.
"""
dtype = tf.as_dtype(dtype).base_dtype
if dtype not in (tf.uint8, tf.float32):
raise TypeError('Invalid Dataset input dtype %r, expected uint8 or float32' %
dtype)
assert expression_profile.shape[0] == labels.shape[0], (
'expression_profile.shape: %s labels.shape: %s' % (expression_profile.shape,
labels.shape))
self._num_examples = expression_profile.shape[0]
# Convert shape from [num examples, rows, columns, depth]
# to [num examples, rows*columns] (assuming depth == 1)
assert expression_profile.shape[1] == 77
expression_profile = expression_profile.reshape(expression_profile.shape[0], expression_profile.shape[1])
self._expression_profile = expression_profile
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0
self._feature_name = feature_name
self._label_name = label_name
示例14: _ProcessHealthPill
def _ProcessHealthPill(self, wall_time, step, device_name, node_name,
output_slot, elements):
"""Processes a health pill value by adding it to accumulated state.
Args:
wall_time: The time at which the health pill was created. Provided by the
debugger.
step: The step at which the health pill was created. Provided by the
debugger.
device_name: The name of the node's device.
node_name: The name of the node for this health pill.
output_slot: The output slot for this health pill.
elements: An ND array of 20 floats. The elements of the health pill.
"""
# Key by the node name for fast retrieval of health pills by node name. The
# array is cast to a list so that it is JSON-able. The debugger data plugin
# serves a JSON response.
self._health_pills.AddItem(node_name,
HealthPillEvent(
wall_time=wall_time,
step=step,
device_name=device_name,
node_name=node_name,
output_slot=output_slot,
dtype=repr(tf.as_dtype(elements[12])),
shape=list(elements[14:]),
value=list(elements)))
示例15: __init__
def __init__(self, images, labels, fake_data=False, one_hot=False,
dtype=tf.float32):
"""Construct a DataSet.
one_hot arg is used only if fake_data is true. `dtype` can be either
`uint8` to leave the input as `[0, 255]`, or `float32` to rescale into
`[0, 1]`.
"""
dtype = tf.as_dtype(dtype).base_dtype
if dtype not in (tf.uint8, tf.float32):
raise TypeError('Invalid image dtype %r, expected uint8 or float32' %
dtype)
if fake_data:
self._num_examples = 10000
self.one_hot = one_hot
else:
assert images.shape[0] == labels.shape[0], (
'images.shape: %s labels.shape: %s' % (images.shape,
labels.shape))
self._num_examples = images.shape[0]
self._images = images
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0