我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.python.framework.dtypes.float32()。
def __init__(self, images, labels, dtype=dtypes.float32, reshape=True): dtype = dtypes.as_dtype(dtype).base_dtype if dtype not in (dtypes.uint8, dtypes.float32): raise TypeError('Invalid image dtype %r, expected uint8 or float32' %dtype) self._num_examples = images.shape[0] if dtype == dtypes.float32: # Convert from [0, 255] -> [0.0, 1.0]. images = images.astype(np.float32) images = np.multiply(images, 1.0 / 255.0) self._images = images self._labels = labels self._epochs_completed = 0 self._index_in_epoch = 0
def read_data_sets(path, dtype=dtypes.float32, reshape=False): images, labels = extract_data(path) for i in range(images.shape[0]): j = random.randint(i, images.shape[0]-1) images[i], images[j] = images[j], images[i] labels[i], labels[j] = labels[j], labels[i] num_images = images.shape[0] TRAIN_SIZE = int(num_images * 0.8) VALIDATION_SIZE = int(num_images * 0.1) train_images = images[:TRAIN_SIZE] train_labels = labels[:TRAIN_SIZE] validation_images = images[TRAIN_SIZE:TRAIN_SIZE+VALIDATION_SIZE] validation_labels = labels[TRAIN_SIZE:TRAIN_SIZE+VALIDATION_SIZE] test_images = images[TRAIN_SIZE+VALIDATION_SIZE:] test_labels = labels[TRAIN_SIZE+VALIDATION_SIZE:] train = DataSet(train_images, train_labels, dtype=dtype, reshape=reshape) validation = DataSet(validation_images, validation_labels, dtype=dtype, reshape=reshape) test = DataSet(test_images, test_labels, dtype=dtype, reshape=reshape) return base.Datasets(train=train, validation=validation, test=test)
def get_classification_loss(logits, targets, softmax_loss_function=None): bucket_outputs = logits if softmax_loss_function is None: assert len(bucket_outputs) == len(targets) == 1 # We need to make target an int64-tensor and set its shape. bucket_target = array_ops.reshape(math_ops.to_int64(targets[0]), [-1]) crossent = nn_ops.sparse_softmax_cross_entropy_with_logits(logits=bucket_outputs[0], labels=bucket_target) else: assert len(bucket_outputs) == len(targets) == 1 crossent = softmax_loss_function(bucket_outputs[0], targets[0]) batch_size = array_ops.shape(targets[0])[0] loss = tf.reduce_sum(crossent) / math_ops.cast(batch_size, dtypes.float32) return loss
def create_queues(hypes, phase): """Create Queues.""" arch = hypes['arch'] dtypes = [tf.float32, tf.int32] height = 224 width = 224 channel = 3 shapes = [[height, width, channel], []] capacity = 50 q = tf.FIFOQueue(capacity=50, dtypes=dtypes, shapes=shapes) tf.summary.scalar("queue/%s/fraction_of_%d_full" % (q.name + "_" + phase, capacity), math_ops.cast(q.size(), tf.float32) * (1. / capacity)) return q
def shuffle_join(tensor_list_list, capacity, min_ad, phase): name = 'shuffel_input' types = _dtypes(tensor_list_list) queue = data_flow_ops.RandomShuffleQueue( capacity=capacity, min_after_dequeue=min_ad, dtypes=types) # Build enque Operations _enqueue_join(queue, tensor_list_list) full = (math_ops.cast(math_ops.maximum(0, queue.size() - min_ad), dtypes.float32) * (1. / (capacity - min_ad))) # Note that name contains a '/' at the end so we intentionally do not place # a '/' after %s below. summary_name = ( "queue/%s/fraction_over_%d_of_%d_full" % (name + '_' + phase, min_ad, capacity - min_ad)) tf.summary.scalar(summary_name, full) dequeued = queue.dequeue(name='shuffel_deqeue') # dequeued = _deserialize_sparse_tensors(dequeued, sparse_info) return dequeued
def create_queues(hypes, phase): """Create Queues.""" arch = hypes['arch'] dtypes = [tf.float32, tf.int32] shape_known = hypes['jitter']['fix_shape'] or \ hypes['jitter']['resize_image'] if shape_known: height = hypes['jitter']['image_height'] width = hypes['jitter']['image_width'] channel = hypes['arch']['num_channels'] shapes = [[height, width, channel], []] else: shapes = None capacity = 50 q = tf.FIFOQueue(capacity=50, dtypes=dtypes, shapes=shapes) tf.summary.scalar("queue/%s/fraction_of_%d_full" % (q.name + "_" + phase, capacity), math_ops.cast(q.size(), tf.float32) * (1. / capacity)) return q
def _create_local(name, shape, collections=None, validate_shape=True, dtype=dtypes.float32): """Creates a new local variable. Args: name: The name of the new or existing variable. shape: Shape of the new or existing variable. collections: A list of collection names to which the Variable will be added. validate_shape: Whether to validate the shape of the variable. dtype: Data type of the variables. Returns: The created variable. """ # Make sure local variables are added to tf.GraphKeys.LOCAL_VARIABLES collections = list(collections or []) collections += [ops.GraphKeys.LOCAL_VARIABLES] return variables.Variable( initial_value=array_ops.zeros(shape, dtype=dtype), name=name, trainable=False, collections=collections, validate_shape=validate_shape)
def average_impurity(self): """Constructs a TF graph for evaluating the average leaf impurity of a tree. If in regression mode, this is the leaf variance. If in classification mode, this is the gini impurity. Returns: The last op in the graph. """ children = array_ops.squeeze(array_ops.slice( self.variables.tree, [0, 0], [-1, 1]), squeeze_dims=[1]) is_leaf = math_ops.equal(constants.LEAF_NODE, children) leaves = math_ops.to_int32(array_ops.squeeze(array_ops.where(is_leaf), squeeze_dims=[1])) counts = array_ops.gather(self.variables.node_sums, leaves) gini = self._weighted_gini(counts) # Guard against step 1, when there often are no leaves yet. def impurity(): return gini # Since average impurity can be used for loss, when there's no data just # return a big number so that loss always decreases. def big(): return array_ops.ones_like(gini, dtype=dtypes.float32) * 10000000. return control_flow_ops.cond(math_ops.greater( array_ops.shape(leaves)[0], 0), impurity, big)
def basic_rnn_seq2seq( encoder_inputs, decoder_inputs, cell, dtype=dtypes.float32, scope=None): """Basic RNN sequence-to-sequence model. This model first runs an RNN to encode encoder_inputs into a state vector, then runs decoder, initialized with the last encoder state, on decoder_inputs. Encoder and decoder use the same RNN cell type, but don't share parameters. Args: encoder_inputs: A list of 2D Tensors [batch_size x input_size]. decoder_inputs: A list of 2D Tensors [batch_size x input_size]. cell: rnn_cell.RNNCell defining the cell function and size. dtype: The dtype of the initial state of the RNN cell (default: tf.float32). scope: VariableScope for the created subgraph; default: "basic_rnn_seq2seq". Returns: A tuple of the form (outputs, state), where: outputs: A list of the same length as decoder_inputs of 2D Tensors with shape [batch_size x output_size] containing the generated outputs. state: The state of each decoder cell in the final time-step. It is a 2D Tensor of shape [batch_size x cell.state_size]. """ with variable_scope.variable_scope(scope or "basic_rnn_seq2seq"): _, enc_state = rnn.rnn(cell, encoder_inputs, dtype=dtype) return rnn_decoder(decoder_inputs, enc_state, cell)
def __init__(self, images, points, factors, crds, widths, dtype=dtypes.float32): """Construct a DataSet.""" self._num_examples = len(images) self._images = images self._points = points self._factors = factors self._widths = widths self._crds = crds self._epochs_completed = 0 self._index_in_epoch = 0 self._current_batch = []
def shuffle_join(tensor_list_list, capacity, min_ad, phase): name = 'shuffel_input' types = _dtypes(tensor_list_list) queue = data_flow_ops.RandomShuffleQueue(capacity=capacity, min_after_dequeue=min_ad, dtypes=types) # Build enque Operations _enqueue_join(queue, tensor_list_list) full = (math_ops.cast(math_ops.maximum(0, queue.size() - min_ad), dtypes.float32) * (1. / (capacity - min_ad))) # Note that name contains a '/' at the end so we intentionally do not place # a '/' after %s below. summary_name = ( "queue/%s/fraction_over_%d_of_%d_full" % (name + '_' + phase, min_ad, capacity - min_ad)) tf.summary.scalar(summary_name, full) dequeued = queue.dequeue(name='shuffel_deqeue') # dequeued = _deserialize_sparse_tensors(dequeued, sparse_info) return dequeued
def set_floatx(value): """Sets the default float type. Arguments: value: String; 'float16', 'float32', or 'float64'. Example: ```python >>> from keras import backend as K >>> K.floatx() 'float32' >>> K.set_floatx('float16') >>> K.floatx() 'float16'
Raises: ValueError: In case of invalid value. """ global _FLOATX if value not in {'float16', 'float32', 'float64'}: raise ValueError('Unknown floatx type: ' + str(value)) _FLOATX = str(value)
```
def cast_to_floatx(x): """Cast a Numpy array to the default Keras float type. Arguments: x: Numpy array. Returns: The same Numpy array, cast to its new type. Example: ```python >>> from keras import backend as K >>> K.floatx() 'float32' >>> arr = numpy.array([1.0, 2.0], dtype='float64') >>> arr.dtype dtype('float64') >>> new_arr = K.cast_to_floatx(arr) >>> new_arr array([ 1., 2.], dtype=float32) >>> new_arr.dtype dtype('float32')
""" return np.asarray(x, dtype=_FLOATX)
def eval(x): """Evaluates the value of a variable. Arguments: x: A variable. Returns: A Numpy array. Examples: ```python >>> from keras import backend as K >>> kvar = K.variable(np.array([[1, 2], [3, 4]]), dtype='float32') >>> K.eval(kvar) array([[ 1., 2.], [ 3., 4.]], dtype=float32)
""" return to_dense(x).eval(session=get_session())
def eye(size, dtype=None, name=None): """Instantiate an identity matrix and returns it. Arguments: size: Integer, number of rows/columns. dtype: String, data type of returned Keras variable. name: String, name of returned Keras variable. Returns: A Keras variable, an identity matrix. Example: ```python >>> from keras import backend as K >>> kvar = K.eye(3) >>> K.eval(kvar) array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]], dtype=float32)
""" return variable(np.eye(size), dtype, name)
def ones_like(x, dtype=None, name=None): """Instantiates an all-ones variable of the same shape as another tensor. Arguments: x: Keras variable or tensor. dtype: String, dtype of returned Keras variable. None uses the dtype of x. name: String, name for the variable to create. Returns: A Keras variable with the shape of x filled with ones. Example: ```python >>> from keras import backend as K >>> kvar = K.variable(np.random.random((2,3))) >>> kvar_ones = K.ones_like(kvar) >>> K.eval(kvar_ones) array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32)
""" return array_ops.ones_like(x, dtype=dtype, name=name)
def count_params(x): """Returns the number of scalars in a Keras variable. Arguments: x: Keras variable. Returns: Integer, the number of scalars in `x`. Example: ```python >>> kvar = K.zeros((2,3)) >>> K.count_params(kvar) 6 >>> K.eval(kvar) array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32)
""" shape = x.get_shape() return np.prod([shape[i]._value for i in range(len(shape))])
def dropout(x, level, noise_shape=None, seed=None): """Sets entries in `x` to zero at random, while scaling the entire tensor. Arguments: x: tensor level: fraction of the entries in the tensor that will be set to 0. noise_shape: shape for randomly generated keep/drop flags, must be broadcastable to the shape of `x` seed: random seed to ensure determinism. Returns: A tensor. """ retain_prob = 1. - level if seed is None: seed = np.random.randint(10e6) # the dummy 1. works around a TF bug # (float32_ref vs. float32 incomptability) return nn.dropout(x * 1., retain_prob, noise_shape, seed=seed)
def in_top_k(predictions, targets, k): """Returns whether the `targets` are in the top `k` `predictions`. Arguments: predictions: A tensor of shape `(batch_size, classes)` and type `float32`. targets: A 1D tensor of length `batch_size` and type `int32` or `int64`. k: An `int`, number of top elements to consider. Returns: A 1D tensor of length `batch_size` and type `bool`. `output[i]` is `True` if `predictions[i, targets[i]]` is within top-`k` values of `predictions[i]`. """ return nn.in_top_k(predictions, targets, k) # CONVOLUTIONS
def _preprocess_conv2d_input(x, data_format): """Transpose and cast the input before the conv2d. Arguments: x: input tensor. data_format: string, one of 'channels_last', 'channels_first'. Returns: A tensor. """ if dtype(x) == 'float64': x = math_ops.cast(x, 'float32') if data_format == 'channels_first': # TF uses the last dimension as channel dimension, # instead of the 2nd one. # TH input shape: (samples, input_depth, rows, cols) # TF input shape: (samples, rows, cols, input_depth) x = array_ops.transpose(x, (0, 2, 3, 1)) return x
def rnn_seq2seq(encoder_inputs, decoder_inputs, encoder_cell, decoder_cell=None, dtype=dtypes.float32, scope=None): """RNN Sequence to Sequence model. Args: encoder_inputs: List of tensors, inputs for encoder. decoder_inputs: List of tensors, inputs for decoder. encoder_cell: RNN cell to use for encoder. decoder_cell: RNN cell to use for decoder, if None encoder_cell is used. dtype: Type to initialize encoder state with. scope: Scope to use, if None new will be produced. Returns: List of tensors for outputs and states for trianing and sampling sub-graphs. """ with vs.variable_scope(scope or "rnn_seq2seq"): _, last_enc_state = nn.rnn(encoder_cell, encoder_inputs, dtype=dtype) return rnn_decoder(decoder_inputs, last_enc_state, decoder_cell or encoder_cell)
def _assert_float32(tensors): """Assert all tensors are float32. Args: tensors: `Tensor` or `dict` of `Tensor` objects. Raises: TypeError: if any tensor is not float32. """ if not isinstance(tensors, dict): tensors = [tensors] else: tensors = tensors.values() for tensor in tensors: if tensor.dtype.base_dtype != dtypes.float32: raise TypeError('Expected dtype=float32, %s.' % tensor)
def _multiply_gradients(grads_and_vars, gradient_multipliers): """Multiply specified gradients.""" multiplied_grads_and_vars = [] for grad, var in grads_and_vars: if (grad is not None and (var in gradient_multipliers or var.name in gradient_multipliers)): key = var if var in gradient_multipliers else var.name multiplier = constant_op.constant( gradient_multipliers[key], dtype=dtypes.float32) if isinstance(grad, ops.IndexedSlices): grad_values = grad.values * multiplier grad = ops.IndexedSlices(grad_values, grad.indices, grad.dense_shape) else: grad *= multiplier multiplied_grads_and_vars.append((grad, var)) return multiplied_grads_and_vars
def _create_local(name, shape=None, collections=None, dtype=dtypes.float32): """Creates a new local variable. Args: name: The name of the new or existing variable. shape: Shape of the new or existing variable. collections: A list of collection names to which the Variable will be added. dtype: Data type of the variables. Returns: The created variable. """ # Make sure local variables are added to tf.GraphKeys.LOCAL_VARIABLES collections = list(collections or []) collections += [ops.GraphKeys.LOCAL_VARIABLES] return variables.Variable( initial_value=array_ops.zeros(shape, dtype=dtype), name=name, trainable=False, collections=collections)
def _multi_value_loss( activations, labels, sequence_length, target_column, features): """Maps `activations` from the RNN to loss for multi value models. Args: activations: Output from an RNN. Should have dtype `float32` and shape `[batch_size, padded_length, ?]`. labels: A `Tensor` with length `[batch_size, padded_length]`. sequence_length: A `Tensor` with shape `[batch_size]` and dtype `int32` containing the length of each sequence in the batch. If `None`, sequences are assumed to be unpadded. target_column: An initialized `TargetColumn`, calculate predictions. features: A `dict` containing the input and (optionally) sequence length information and initial state. Returns: A scalar `Tensor` containing the loss. """ with ops.name_scope('MultiValueLoss'): activations_masked, labels_masked = mask_activations_and_labels( activations, labels, sequence_length) return target_column.loss(activations_masked, labels_masked, features)
def _single_value_loss( activations, labels, sequence_length, target_column, features): """Maps `activations` from the RNN to loss for multi value models. Args: activations: Output from an RNN. Should have dtype `float32` and shape `[batch_size, padded_length, ?]`. labels: A `Tensor` with length `[batch_size]`. sequence_length: A `Tensor` with shape `[batch_size]` and dtype `int32` containing the length of each sequence in the batch. If `None`, sequences are assumed to be unpadded. target_column: An initialized `TargetColumn`, calculate predictions. features: A `dict` containing the input and (optionally) sequence length information and initial state. Returns: A scalar `Tensor` containing the loss. """ with ops.name_scope('SingleValueLoss'): last_activations = select_last_activations(activations, sequence_length) return target_column.loss(last_activations, labels, features)
def _get_loss(self, features, labels, data_spec=None): """Constructs, caches, and returns the inference-based loss.""" if self._loss is not None: return self._loss def _average_loss(): probs = self.inference_graph(features, data_spec=data_spec) return math_ops.reduce_sum(self.loss_fn( probs, labels)) / math_ops.to_float( array_ops.shape(features)[0]) self._loss = control_flow_ops.cond( self.average_size() > 0, _average_loss, lambda: constant_op.constant(sys.maxsize, dtype=dtypes.float32)) return self._loss
def quantize_weight_rounded(input_node): """Returns a replacement node for input_node containing bucketed floats.""" input_tensor = input_node.attr["value"].tensor tensor_value = tensor_util.MakeNdarray(input_tensor) shape = input_tensor.tensor_shape # Currently, the parameter FLAGS.bitdepth is used to compute the # number of buckets as 1 << FLAGS.bitdepth, meaning the number of # buckets can only be a power of 2. # This could be fixed by introducing a new parameter, num_buckets, # which would allow for more flexibility in chosing the right model # size/accuracy tradeoff. But I didn't want to add more parameters # to this script than absolutely necessary. num_buckets = 1 << FLAGS.bitdepth tensor_value_rounded = quantize_array(tensor_value, num_buckets) tensor_shape_list = tensor_util.TensorShapeProtoToList(shape) return [ create_constant_node( input_node.name, tensor_value_rounded, dtypes.float32, shape=tensor_shape_list) ]
def rnn_data(data, time_steps, labels=False): """ creates new data frame based on previous observation * example: l = [1, 2, 3, 4, 5] time_steps = 2 -> labels == False [[1, 2], [2, 3], [3, 4]] #Data frame for input with 2 timesteps -> labels == True [3, 4, 5] # labels for predicting the next timestep """ rnn_df = [] for i in range(len(data) - time_steps): if labels: try: rnn_df.append(data.iloc[i + time_steps].as_matrix()) except AttributeError: rnn_df.append(data.iloc[i + time_steps]) else: data_ = data.iloc[i: i + time_steps].as_matrix() rnn_df.append(data_ if len(data_.shape) > 1 else [[i] for i in data_]) return np.array(rnn_df, dtype=np.float32)
def basic_rnn_seq2seq(encoder_inputs, decoder_inputs, cell, dtype=dtypes.float32, scope=None): """Basic RNN sequence-to-sequence model. This model first runs an RNN to encode encoder_inputs into a state vector, then runs decoder, initialized with the last encoder state, on decoder_inputs. Encoder and decoder use the same RNN cell type, but don't share parameters. Args: encoder_inputs: A list of 2D Tensors [batch_size x input_size]. decoder_inputs: A list of 2D Tensors [batch_size x input_size]. cell: rnn_cell.RNNCell defining the cell function and size. dtype: The dtype of the initial state of the RNN cell (default: tf.float32). scope: VariableScope for the created subgraph; default: "basic_rnn_seq2seq". Returns: A tuple of the form (outputs, state), where: outputs: A list of the same length as decoder_inputs of 2D Tensors with shape [batch_size x output_size] containing the generated outputs. state: The state of each decoder cell in the final time-step. It is a 2D Tensor of shape [batch_size x cell.state_size]. """ with variable_scope.variable_scope(scope or "basic_rnn_seq2seq"): _, enc_state = rnn.rnn(cell, encoder_inputs, dtype=dtype) return rnn_decoder(decoder_inputs, enc_state, cell)
def embedding_rnn_decoder(decoder_inputs, initial_state, cell, num_symbols, embedding_size, output_projection=None, feed_previous=False, update_embedding_for_previous=True, scope=None): """RNN decoder with embedding and a pure-decoding option. """ if output_projection is not None: proj_weights = ops.convert_to_tensor(output_projection[0], dtype=dtypes.float32) proj_weights.get_shape().assert_is_compatible_with([None, num_symbols]) proj_biases = ops.convert_to_tensor( output_projection[1], dtype=dtypes.float32) proj_biases.get_shape().assert_is_compatible_with([num_symbols]) with variable_scope.variable_scope(scope or "embedding_rnn_decoder"): embedding = variable_scope.get_variable("embedding", [num_symbols, embedding_size]) loop_function = _extract_argmax_and_embed( embedding, output_projection, update_embedding_for_previous) if feed_previous else None emb_inp = ( embedding_ops.embedding_lookup(embedding, i) for i in decoder_inputs) return rnn_decoder(emb_inp, initial_state, cell, loop_function=loop_function)
def sequence_loss(logits, targets, weights, average_across_timesteps=True, average_across_batch=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed. Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch to be used instead of the standard softmax (the default if this is None). name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.name_scope( name, "sequence_loss",logits + targets + weights): cost = math_ops.reduce_sum(sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) if average_across_batch: batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, dtypes.float32) else: return cost
def basic_rnn_seq2seq(encoder_inputs, decoder_inputs, cell, dtype=dtypes.float32, scope=None): """Basic RNN sequence-to-sequence model. This model first runs an RNN to encode encoder_inputs into a state vector, then runs decoder, initialized with the last encoder state, on decoder_inputs. Encoder and decoder use the same RNN cell type, but don't share parameters. Args: encoder_inputs: A list of 2D Tensors [batch_size x input_size]. decoder_inputs: A list of 2D Tensors [batch_size x input_size]. cell: core_rnn_cell.RNNCell defining the cell function and size. dtype: The dtype of the initial state of the RNN cell (default: tf.float32). scope: VariableScope for the created subgraph; default: "basic_rnn_seq2seq". Returns: A tuple of the form (outputs, state), where: outputs: A list of the same length as decoder_inputs of 2D Tensors with shape [batch_size x output_size] containing the generated outputs. state: The state of each decoder cell in the final time-step. It is a 2D Tensor of shape [batch_size x cell.state_size]. """ with variable_scope.variable_scope(scope or "basic_rnn_seq2seq"): enc_cell = copy.deepcopy(cell) _, enc_state = core_rnn.static_rnn(enc_cell, encoder_inputs, dtype=dtype) return rnn_decoder(decoder_inputs, enc_state, cell)
def tied_rnn_seq2seq(encoder_inputs, decoder_inputs, cell, loop_function=None, dtype=dtypes.float32, scope=None): """RNN sequence-to-sequence model with tied encoder and decoder parameters. This model first runs an RNN to encode encoder_inputs into a state vector, and then runs decoder, initialized with the last encoder state, on decoder_inputs. Encoder and decoder use the same RNN cell and share parameters. Args: encoder_inputs: A list of 2D Tensors [batch_size x input_size]. decoder_inputs: A list of 2D Tensors [batch_size x input_size]. cell: core_rnn_cell.RNNCell defining the cell function and size. loop_function: If not None, this function will be applied to i-th output in order to generate i+1-th input, and decoder_inputs will be ignored, except for the first element ("GO" symbol), see rnn_decoder for details. dtype: The dtype of the initial state of the rnn cell (default: tf.float32). scope: VariableScope for the created subgraph; default: "tied_rnn_seq2seq". Returns: A tuple of the form (outputs, state), where: outputs: A list of the same length as decoder_inputs of 2D Tensors with shape [batch_size x output_size] containing the generated outputs. state: The state of each decoder cell in each time-step. This is a list with length len(decoder_inputs) -- one item for each time-step. It is a 2D Tensor of shape [batch_size x cell.state_size]. """ with variable_scope.variable_scope("combined_tied_rnn_seq2seq"): scope = scope or "tied_rnn_seq2seq" _, enc_state = core_rnn.static_rnn( cell, encoder_inputs, dtype=dtype, scope=scope) variable_scope.get_variable_scope().reuse_variables() return rnn_decoder( decoder_inputs, enc_state, cell, loop_function=loop_function, scope=scope)
def __init__(self, images, labels, start_id=0, fake_data=False, one_hot=False, dtype=dtypes.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 = dtypes.as_dtype(dtype).base_dtype if dtype not in (dtypes.uint8, dtypes.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] # Convert shape from [num examples, rows, columns, depth] # to [num examples, rows*columns] (assuming depth == 1) assert images.shape[3] == 1 images = images.reshape(images.shape[0], images.shape[1] * images.shape[2]) if dtype == dtypes.float32: # Convert from [0, 255] -> [0.0, 1.0]. images = images.astype(numpy.float32) images = numpy.multiply(images, 1.0 / 255.0) self._ids = numpy.arange(start_id, start_id + self._num_examples) self._images = images self._labels = labels self._epochs_completed = 0 self._index_in_epoch = 0
def sequence_loss_by_batch(logits, targets, weights, average_across_timesteps=True, softmax_loss_function=None, name=None): """Weighted cross-entropy loss for a sequence of logits, batch-collapsed (averaged). Args: logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols]. targets: List of 1D batch-sized int32 Tensors of the same length as logits. weights: List of 1D batch-sized float-Tensors of the same length as logits. average_across_timesteps: If set, divide the returned cost by the total label weight. average_across_batch: If set, divide the returned cost by the batch size. softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch to be used instead of the standard softmax (the default if this is None). name: Optional name for this operation, defaults to "sequence_loss". Returns: A scalar float Tensor: The average log-perplexity per symbol (weighted). Raises: ValueError: If len(logits) is different from len(targets) or len(weights). """ with ops.op_scope(logits + targets + weights, name, "sequence_loss_by_batch"): cost = math_ops.reduce_sum(sequence_loss_by_example( logits, targets, weights, average_across_timesteps=average_across_timesteps, softmax_loss_function=softmax_loss_function)) batch_size = array_ops.shape(targets[0])[0] return cost / math_ops.cast(batch_size, dtypes.float32)
def float32(k): return np.cast['float32'](k)