我们从Python开源项目中,提取了以下36个代码示例,用于说明如何使用tensorflow.strided_slice()。
def ptb_producer(raw_data, batch_size, num_steps, name=None): with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]): raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32) data_len = tf.size(raw_data) batch_len = data_len // batch_size data = tf.reshape(raw_data[0 : batch_size * batch_len], [batch_size, batch_len]) epoch_size = (batch_len - 1) // num_steps epoch_size = tf.identity(epoch_size, name="epoch_size") i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() x = tf.strided_slice(data, [0, i * num_steps], [batch_size, (i + 1) * num_steps], #tf.ones_like([0, i * num_steps])) [1,1]) x.set_shape([batch_size, num_steps]) y = tf.strided_slice(data, [0, i * num_steps + 1], [batch_size, (i + 1) * num_steps + 1], #tf.ones_like([0, i * num_steps])) [1,1]) y.set_shape([batch_size, num_steps]) return x, y
def decode(self, memories, keys, num_keys=None): keys = self._normalize(keys) num_memories = memories.get_shape().as_list() num_memories[0] = self.num_models if num_memories[0] is None else num_memories[0] num_keys = keys.get_shape().as_list()[0] if num_keys is None else num_keys print 'decode: numkeys = ', num_keys, ' | num_memories = ', num_memories # re-gather keys to avoid mixing between different keys. perms = self.perm_keys(keys, self.perms, num_keys=num_keys) pshp = perms.get_shape().as_list() pshp[0] = num_keys*self.num_models if pshp[0] is None else pshp[0] pshp[1] = num_memories[1] if pshp[1] is None else pshp[1] permed_keys = tf.concat(0, [tf.strided_slice(perms, [i, 0], pshp, [num_keys, 1]) for i in range(num_keys)]) print 'memories = ', num_memories, \ '| dec_perms =', permed_keys.get_shape().as_list() return self.conv_func(memories, permed_keys, num_memories[0], self.num_models, num_keys=num_keys*self.num_models, conj=True)
def __init__(self, cfg, data, name): self.steps = ((len(data) // cfg.batch_size) - 1) // cfg.num_steps with tf.name_scope(name, values=[data, cfg.batch_size, cfg.num_steps]): raw_data = tf.convert_to_tensor(data) data_len = tf.size(raw_data) batch_len = data_len // cfg.batch_size data = tf.reshape(raw_data[0: cfg.batch_size * batch_len], [cfg.batch_size, batch_len]) epoch_size = (batch_len - 1) // cfg.num_steps epoch_size = tf.identity(epoch_size, name="epoch_size") i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() begin_x = [0, i * cfg.num_steps] self.inputs = tf.strided_slice( data, begin_x, [cfg.batch_size, (i + 1) * cfg.num_steps], tf.ones_like(begin_x)) self.inputs.set_shape([cfg.batch_size, cfg.num_steps]) begin_y = [0, i * cfg.num_steps + 1] self.targets = tf.strided_slice( data, begin_y, [cfg.batch_size, (i + 1) * cfg.num_steps + 1], tf.ones_like(begin_y)) self.targets.set_shape([cfg.batch_size, cfg.num_steps])
def process_decoder_input(target_data, target_vocab_to_int, batch_size): # Take off the last column sliced = tf.strided_slice(target_data, [0, 0], [batch_size, -1], [1, 1]) # Append a column filled with <GO> decoder_input = tf.concat([tf.fill([batch_size, 1], target_vocab_to_int['<GO>']), sliced], 1) return decoder_input
def StridedSlice_FwGrad(op, dx, dy, dz, du, _op_table=None, _grad_table=None): if dx is None: return None y = op.inputs[1] z = op.inputs[2] u = op.inputs[3] return tf.strided_slice(dx, begin=y, end=z, strides=u) ############################################################################### # Element-wise operators. elemwise. ###############################################################################
def process_decoding_input(target_data, target_vocab_to_int, batch_size): l_word = tf.strided_slice(target_data, [0, 0], [batch_size, -1], [1, 1]) return tf.concat([tf.fill([batch_size, 1], target_vocab_to_int['<GO>']), l_word], 1)
def unit_norm(inputs, dim, epsilon=1e-7, scope=None): """Normalizes the given input across the specified dimension to unit length. Note that the rank of `input` must be known. Args: inputs: A `Tensor` of arbitrary size. dim: The dimension along which the input is normalized. epsilon: A small value to add to the inputs to avoid dividing by zero. scope: Optional scope for variable_scope. Returns: The normalized `Tensor`. Raises: ValueError: If dim is larger than the number of dimensions in 'inputs'. """ with tf.variable_scope(scope, 'UnitNorm', [inputs]): if not inputs.get_shape(): raise ValueError('The input rank must be known.') input_rank = len(inputs.get_shape().as_list()) if dim < 0 or dim >= input_rank: raise ValueError( 'dim must be positive but smaller than the input rank.') lengths = tf.sqrt( epsilon + tf.reduce_sum(tf.square(inputs), dim, True)) multiples = [] if dim > 0: multiples.append(tf.ones([dim], tf.int32)) multiples.append(tf.strided_slice( tf.shape(inputs), [dim], [dim + 1], [1])) if dim < (input_rank - 1): multiples.append(tf.ones([input_rank - 1 - dim], tf.int32)) multiples = tf.concat(multiples, 0) return tf.div(inputs, tf.tile(lengths, multiples))
def call(self, data, mask=None): tmp1 = tf.strided_slice(data,[0,0,0,0],[1024,tf.to_int32(data.get_shape()[1]),tf.to_int32(data.get_shape()[2]),tf.to_int32(data.get_shape()[3])],[1,1,2,2]) tmp2 = tf.strided_slice(data,[0,0,0,1],[1024,tf.to_int32(data.get_shape()[1]),tf.to_int32(data.get_shape()[2]),tf.to_int32(data.get_shape()[3])],[1,1,2,2]) tmp3 = tf.strided_slice(data,[0,0,1,0],[1024,tf.to_int32(data.get_shape()[1]),tf.to_int32(data.get_shape()[2]),tf.to_int32(data.get_shape()[3])],[1,1,2,2]) tmp4 = tf.strided_slice(data,[0,0,1,1],[1024,tf.to_int32(data.get_shape()[1]),tf.to_int32(data.get_shape()[2]),tf.to_int32(data.get_shape()[3])],[1,1,2,2]) if int(tf.__version__[0]) < 1: return tf.concat(1,[tmp1, tmp2, tmp3, tmp4]) else: return tf.concat([tmp1, tmp2, tmp3, tmp4],1)
def ptb_producer(raw_data, batch_size, num_steps, name=None): """Iterate on the raw PTB data. This chunks up raw_data into batches of examples and returns Tensors that are drawn from these batches. Args: raw_data: one of the raw data outputs from ptb_raw_data. batch_size: int, the batch size. num_steps: int, the number of unrolls. name: the name of this operation (optional). Returns: A pair of Tensors, each shaped [batch_size, num_steps]. The second element of the tuple is the same data time-shifted to the right by one. Raises: tf.errors.InvalidArgumentError: if batch_size or num_steps are too high. """ with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]): raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32) data_len = tf.size(raw_data) batch_len = data_len // batch_size data = tf.reshape(raw_data[0 : batch_size * batch_len], [batch_size, batch_len]) epoch_size = (batch_len - 1) // num_steps assertion = tf.assert_positive( epoch_size, message="epoch_size == 0, decrease batch_size or num_steps") with tf.control_dependencies([assertion]): epoch_size = tf.identity(epoch_size, name="epoch_size") i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() x = tf.strided_slice(data, [0, i * num_steps], [batch_size, (i + 1) * num_steps]) x.set_shape([batch_size, num_steps]) y = tf.strided_slice(data, [0, i * num_steps + 1], [batch_size, (i + 1) * num_steps + 1]) y.set_shape([batch_size, num_steps]) return x, y
def _crop(image, offset_height, offset_width, crop_height, crop_width): """Crops the given image using the provided offsets and sizes. Note that the method doesn't assume we know the input image size but it does assume we know the input image rank. Args: image: an image of shape [height, width, channels]. offset_height: a scalar tensor indicating the height offset. offset_width: a scalar tensor indicating the width offset. crop_height: the height of the cropped image. crop_width: the width of the cropped image. Returns: the cropped (and resized) image. Raises: InvalidArgumentError: if the rank is not 3 or if the image dimensions are less than the crop size. """ original_shape = tf.shape(image) rank_assertion = tf.Assert( tf.equal(tf.rank(image), 3), ['Rank of image must be equal to 3.']) with tf.control_dependencies([rank_assertion]): cropped_shape = tf.stack([crop_height, crop_width, original_shape[2]]) size_assertion = tf.Assert( tf.logical_and( tf.greater_equal(original_shape[0], crop_height), tf.greater_equal(original_shape[1], crop_width)), ['Crop size greater than the image size.']) offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0])) # Use tf.strided_slice instead of crop_to_bounding box as it accepts tensors # to define the crop size. with tf.control_dependencies([size_assertion]): image = tf.strided_slice(image, offsets, offsets + cropped_shape, strides=tf.ones_like(offsets)) return tf.reshape(image, cropped_shape)
def __init__(self, config): self.config = config tf.reset_default_graph() self.X1 = tf.placeholder(tf.int32, name='X1', shape=(None, config['data1_maxlen'])) self.X2 = tf.placeholder(tf.int32, name='X2', shape=(None, config['data2_maxlen'])) self.X1_len = tf.placeholder(tf.int32, name='X1_len', shape=(None, )) self.X2_len = tf.placeholder(tf.int32, name='X2_len', shape=(None, )) self.Y = tf.placeholder(tf.int32, name='Y', shape=(None, )) self.F = tf.placeholder(tf.float32, name='F', shape=(None, config['feat_size'])) self.dpool_index = tf.placeholder(tf.int32, name='dpool_index', shape=(None, config['data1_maxlen'], config['data2_maxlen'], 3)) self.batch_size = tf.shape(self.X1)[0] self.embedding = tf.get_variable('embedding', initializer = config['embedding'], dtype=tf.float32, trainable=False) self.embed1 = tf.nn.embedding_lookup(self.embedding, self.X1) self.embed2 = tf.nn.embedding_lookup(self.embedding, self.X2) # batch_size * X1_maxlen * X2_maxlen self.cross = tf.einsum('abd,acd->abc', self.embed1, self.embed2) self.cross_img = tf.expand_dims(self.cross, 3) # convolution self.w1 = tf.get_variable('w1', initializer=tf.truncated_normal_initializer(mean=0.0, stddev=0.2, dtype=tf.float32) , dtype=tf.float32, shape=[2, 10, 1, 8]) self.b1 = tf.get_variable('b1', initializer=tf.constant_initializer() , dtype=tf.float32, shape=[8]) # batch_size * X1_maxlen * X2_maxlen * feat_out self.conv1 = tf.nn.relu(tf.nn.conv2d(self.cross_img, self.w1, [1, 1, 1, 1], "SAME") + self.b1) # dynamic pooling self.conv1_expand = tf.gather_nd(self.conv1, self.dpool_index) self.pool1 = tf.nn.max_pool(self.conv1_expand, [1, config['data1_maxlen'] / config['data1_psize'], config['data2_maxlen'] / config['data2_psize'], 1], [1, config['data1_maxlen'] / config['data1_psize'], config['data2_maxlen'] / config['data2_psize'], 1], "VALID") with tf.variable_scope('fc1'): self.fc1 = tf.nn.relu(tf.contrib.layers.linear(tf.reshape(self.pool1, [self.batch_size, config['data1_psize'] * config['data2_psize'] * 8]), 20)) self.pred = tf.contrib.layers.linear(self.fc1, 1) pos = tf.strided_slice(self.pred, [0], [self.batch_size], [2]) neg = tf.strided_slice(self.pred, [1], [self.batch_size], [2]) self.loss = tf.reduce_mean(tf.maximum(1.0 + neg - pos, 0.0)) self.train_model = tf.train.AdamOptimizer().minimize(self.loss) self.saver = tf.train.Saver(max_to_keep=20)
def read_cifar10(filename_queue): """Reads and parses examples from CIFAR10 data files. Recommendation: if you want N-way read parallelism, call this function N times. This will give you N independent Readers reading different files & positions within those files, which will give better mixing of examples. Args: filename_queue: A queue of strings with the filenames to read from. Returns: An object representing a single example, with the following fields: height: number of rows in the result (32) width: number of columns in the result (32) depth: number of color channels in the result (3) key: a scalar string Tensor describing the filename & record number for this example. label: an int32 Tensor with the label in the range 0..9. uint8image: a [height, width, depth] uint8 Tensor with the image data """ class CIFAR10Record(object): pass result = CIFAR10Record() # Dimensions of the images in the CIFAR-10 dataset. # See http://www.cs.toronto.edu/~kriz/cifar.html for a description of the # input format. label_bytes = 1 # 2 for CIFAR-100 result.height = 32 result.width = 32 result.depth = 3 image_bytes = result.height * result.width * result.depth # Every record consists of a label followed by the image, with a # fixed number of bytes for each. record_bytes = label_bytes + image_bytes # Read a record, getting filenames from the filename_queue. No # header or footer in the CIFAR-10 format, so we leave header_bytes # and footer_bytes at their default of 0. reader = tf.FixedLengthRecordReader(record_bytes=record_bytes) result.key, value = reader.read(filename_queue) # Convert from a string to a vector of uint8 that is record_bytes long. record_bytes = tf.decode_raw(value, tf.uint8) # The first bytes represent the label, which we convert from uint8->int32. result.label = tf.cast( tf.strided_slice(record_bytes, [0], [label_bytes]), tf.int32) # The remaining bytes after the label represent the image, which we reshape # from [depth * height * width] to [depth, height, width]. depth_major = tf.reshape( tf.strided_slice(record_bytes, [label_bytes], [label_bytes + image_bytes]), [result.depth, result.height, result.width]) # Convert from [depth, height, width] to [height, width, depth]. result.uint8image = tf.transpose(depth_major, [1, 2, 0]) return result
def batch_producer(enc, dec, batch_size, name=None): data_len = enc.shape[0] seq_len = enc.shape[1] epoch_size = data_len // batch_size print("epoch size: %d " % epoch_size) with tf.name_scope(name, "batch", [enc, dec, batch_size]): enc = tf.convert_to_tensor(enc, name="enc", dtype=tf.float32) dec = tf.convert_to_tensor(dec, name="dec", dtype=tf.int32) # generator i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() x = tf.strided_slice(enc, [0, 0, 0], [batch_size, seq_len, 2], [1, 1, 1]) x.set_shape([batch_size, seq_len, 2 ]) y = tf.strided_slice(dec, [0, 0], [batch_size, seq_len], [1, 1]) y.set_shape([batch_size, seq_len]) return x, y # for test #if __name__ == "__main__": # enc_in, dec_out = _load_data("./convex_hull_50_train.txt") # print(enc_in.shape) # print(dec_out.shape) # #print(enc_in) # x_batch, y_batch = batch_producer(enc_in, dec_out, batch_size=20) # with tf.Session() as sess: # coord = tf.train.Coordinator() # threads = tf.train.start_queue_runners(sess=sess, coord=coord) # print(sess.run([x_batch, y_batch])) # coord.request_stop() # coord.join(threads) # ==================== # visualization # ====================
def read_data(file_q): # Code from https://github.com/tensorflow/models/blob/master/tutorials/image/cifar10/cifar10_input.py class CIFAR10Record(object): pass result = CIFAR10Record() # Dimensions of the images in the CIFAR-10 dataset. # See http://www.cs.toronto.edu/~kriz/cifar.html for a description of the # input format. label_bytes = 1 # 2 for CIFAR-100 result.height = 32 result.width = 32 result.depth = 3 image_bytes = result.height * result.width * result.depth # Every record consists of a label followed by the image, with a # fixed number of bytes for each. record_bytes = label_bytes + image_bytes # Read a record, getting filenames from the filename_queue. No # header or footer in the CIFAR-10 format, so we leave header_bytes # and footer_bytes at their default of 0. reader = tf.FixedLengthRecordReader(record_bytes=record_bytes) result.key, value = reader.read(file_q) # Convert from a string to a vector of uint8 that is record_bytes long. record_bytes = tf.decode_raw(value, tf.uint8) # The first bytes represent the label, which we convert from uint8->int32. result.label = tf.cast( tf.strided_slice(record_bytes, [0], [label_bytes]), tf.int32) # The remaining bytes after the label represent the image, which we reshape # from [depth * height * width] to [depth, height, width]. depth_major = tf.reshape( tf.strided_slice(record_bytes, [label_bytes], [label_bytes + image_bytes]), [result.depth, result.height, result.width]) # Convert from [depth, height, width] to [height, width, depth]. result.uint8image = tf.transpose(depth_major, [1, 2, 0]) reshaped_image = tf.cast(result.uint8image, tf.float32) height = 24 width = 24 # Image processing for evaluation. # Crop the central [height, width] of the image. resized_image = tf.image.resize_image_with_crop_or_pad(reshaped_image, height, width) # Subtract off the mean and divide by the variance of the pixels. float_image = tf.image.per_image_standardization(resized_image) # Set the shapes of tensors. float_image.set_shape([height, width, 3]) result.label.set_shape([1]) return float_image, result.label
def ptb_producer(raw_data, batch_size, num_steps, name=None): """Iterate on the raw PTB data. This chunks up raw_data into batches of examples and returns Tensors that are drawn from these batches. Args: raw_data: one of the raw data outputs from ptb_raw_data. batch_size: int, the batch size. num_steps: int, the number of unrolls. name: the name of this operation (optional). Returns: A pair of Tensors, each shaped [batch_size, num_steps]. The second element of the tuple is the same data time-shifted to the right by one. Raises: tf.errors.InvalidArgumentError: if batch_size or num_steps are too high. """ with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]): raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32) data_len = tf.size(raw_data) batch_len = data_len // batch_size data = tf.reshape(raw_data[0: batch_size * batch_len], [batch_size, batch_len]) epoch_size = (batch_len - 1) // num_steps assertion = tf.assert_positive( epoch_size, message="epoch_size == 0, decrease batch_size or num_steps") with tf.control_dependencies([assertion]): epoch_size = tf.identity(epoch_size, name="epoch_size") i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() x = tf.strided_slice(data, [0, i * num_steps], [batch_size, (i + 1) * num_steps]) x.set_shape([batch_size, num_steps]) y = tf.strided_slice(data, [0, i * num_steps + 1], [batch_size, (i + 1) * num_steps + 1]) y.set_shape([batch_size, num_steps]) return x, y
def gather(self, src, force_copy=False): """Fetches the data corresponding to ``src`` from the base array. Parameters ---------- src : :class:`.TensorSignal` Signal indicating the data to be read from base array force_copy : bool, optional If True, always perform a gather, not a slice (this forces a copy). Note that setting ``force_copy=False`` does not guarantee that a copy won't be performed. Returns ------- ``tf.Tensor`` Tensor object corresponding to a dense subset of data from the base array """ if src.tf_indices is None: raise BuildError("Indices for %s have not been loaded into " "TensorFlow" % src) logger.debug("gather") logger.debug("src %s", src) logger.debug("indices %s", src.indices) logger.debug("src base %s", self.bases[src.key]) var = self.bases[src.key] # we prefer to get the data via `strided_slice` or `identity` if # possible, as it is more efficient if force_copy or src.as_slice is None: result = tf.gather(var, src.tf_indices) elif (src.indices[0] == 0 and src.indices[-1] == var.get_shape()[0].value - 1 and len(src.indices) == var.get_shape()[0]): result = var else: result = tf.strided_slice(var, *src.as_slice) # for some reason the shape inference doesn't work in some cases result.set_shape(src.tf_indices.get_shape()[:1].concatenate( var.get_shape()[1:])) # reshape the data according to the shape set in `src`, if there is # one, otherwise keep the shape of the base array if result.get_shape() != src.full_shape: result = tf.reshape(result, src.tf_shape) result.set_shape(src.full_shape) # whenever we read from an array we use this to mark it as "read" # (so that any future writes to the array will be scheduled after # the read) self.mark_gather(src) return result
def resnet_atrous_conv(x, channels, size=3, padding='SAME', stride=1, hole=1, batch_norm=False, phase_test=None, activation=tf.nn.relu, name=None, parameter_name=None, bn_name=None, scale_name=None, summarize_scale=False, info=DummyDict(), parameters={}, pre_adjust_batch_norm=False): if parameter_name is None: parameter_name = name if scale_name is None: scale_name = parameter_name with tf.name_scope(name): features = int(x.get_shape()[3]) f = channels shape = [size, size, features, f] W_init, W_shape = _pretrained_resnet_conv_weights_initializer(parameter_name, parameters, info=info.get('init'), pre_adjust_batch_norm=pre_adjust_batch_norm, bn_name=bn_name, scale_name=scale_name) b_init, b_shape = _pretrained_resnet_biases_initializer(scale_name, parameters, info=info.get('init'), pre_adjust_batch_norm=pre_adjust_batch_norm, bn_name=bn_name) assert W_shape is None or tuple(W_shape) == tuple(shape), "Incorrect weights shape for {} (file: {}, spec: {})".format(name, W_shape, shape) assert b_shape is None or tuple(b_shape) == (f,), "Incorrect bias shape for {} (file: {}, spec; {})".format(name, b_shape, (f,)) with tf.variable_scope(name): W = tf.get_variable('weights', shape, dtype=tf.float32, initializer=W_init) b = tf.get_variable('biases', [f], dtype=tf.float32, initializer=b_init) if hole == 1: raw_conv0 = tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding=padding) else: assert stride == 1 raw_conv0 = tf.nn.atrous_conv2d(x, W, rate=hole, padding=padding) #conv0 = tf.nn.conv2d(x, W, strides=[1, stride, stride, 1], padding=padding) if stride > 1: conv0 = tf.strided_slice(raw_conv0, [0, 0, 0, 0], raw_conv0.get_shape(), [1, stride, stride, 1]) else: conv0 = raw_conv0 h1 = tf.reshape(tf.nn.bias_add(conv0, b), conv0.get_shape()) z = h1 if activation is not None: z = activation(z) if info.get('scale_summary'): with tf.name_scope('activation'): tf.summary.scalar('activation/' + name, tf.sqrt(tf.reduce_mean(z**2))) info['activations'][name] = z return z
def ptb_producer(raw_data, batch_size, num_steps, name=None): """Iterate on the raw data. This chunks up raw_data into batches of examples and returns Tensors that are drawn from these batches. Args: raw_data: one of the raw data outputs from ptb_raw_data. batch_size: int, the batch size. num_steps: int, the number of unrolls. name: the name of this operation (optional). Returns: A pair of Tensors, each shaped [batch_size, num_steps]. The second element of the tuple is the same data time-shifted to the right by one. Raises: tf.errors.InvalidArgumentError: if batch_size or num_steps are too high. """ with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]): raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32) data_len = tf.size(raw_data) batch_len = data_len // batch_size data = tf.reshape(raw_data[0: batch_size * batch_len], [batch_size, batch_len]) epoch_size = (batch_len - 1) // num_steps assertion = tf.assert_positive( epoch_size, message="epoch_size == 0, decrease batch_size or num_steps") with tf.control_dependencies([assertion]): epoch_size = tf.identity(epoch_size, name="epoch_size") i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() x = tf.strided_slice(data, [0, i * num_steps], [batch_size, (i + 1) * num_steps], [1, 1]) x.set_shape([batch_size, num_steps]) y = tf.strided_slice(data, [0, i * num_steps + 1], [batch_size, (i + 1) * num_steps + 1], [1, 1]) y.set_shape([batch_size, num_steps]) # print(y, "label size") # print(x, "input size") return x, y
def ptb_producer(raw_data, batch_size, num_steps, name=None): """Iterate on the raw PTB data. This chunks up raw_data into batches of examples and returns Tensors that are drawn from these batches. Args: raw_data: one of the raw data outputs from ptb_raw_data. batch_size: int, the batch size. num_steps: int, the number of unrolls. name: the name of this operation (optional). Returns: A pair of Tensors, each shaped [batch_size, num_steps]. The second element of the tuple is the same data time-shifted to the right by one. Raises: tf.errors.InvalidArgumentError: if batch_size or num_steps are too high. """ with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]): raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32) data_len = tf.size(raw_data) # number of elements # Separate the whole data into batch_size parts (each part has # batch_len elements), # so that the batches retrieve one sample from every part at a time to # build a batch of size batch_size batch_len = data_len // batch_size data = tf.reshape(raw_data[0: batch_size * batch_len], [batch_size, batch_len]) # The size of epoch, which means the number of batches to run through the # whole data for once epoch_size = (batch_len - 1) // num_steps assertion = tf.assert_positive( epoch_size, message="epoch_size == 0, decrease batch_size or num_steps") with tf.control_dependencies([assertion]): epoch_size = tf.identity(epoch_size, name="epoch_size") # Read the document for reading data: # https: // www.tensorflow.org / programmers_guide / reading_data i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue() x = tf.strided_slice(data, [0, i * num_steps], [batch_size, (i + 1) * num_steps]) x.set_shape([batch_size, num_steps]) y = tf.strided_slice(data, [0, i * num_steps + 1], [batch_size, (i + 1) * num_steps + 1]) y.set_shape([batch_size, num_steps]) return x, y