MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / ptb_producer

Function ptb_producer

examples/PennTreebank/reader.py:78–119  ·  view source on GitHub ↗

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: th

(raw_data, batch_size, num_steps, name=None)

Source from the content-addressed store, hash-verified

76
77
78def ptb_producer(raw_data, batch_size, num_steps, name=None):
79 """Iterate on the raw PTB data.
80
81 This chunks up raw_data into batches of examples and returns Tensors that
82 are drawn from these batches.
83
84 Args:
85 raw_data: one of the raw data outputs from ptb_raw_data.
86 batch_size: int, the batch size.
87 num_steps: int, the number of unrolls.
88 name: the name of this operation (optional).
89
90 Returns:
91 A pair of Tensors, each shaped [batch_size, num_steps]. The second element
92 of the tuple is the same data time-shifted to the right by one.
93
94 Raises:
95 tf.errors.InvalidArgumentError: if batch_size or num_steps are too high.
96 """
97 with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]):
98 raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32)
99
100 data_len = tf.size(raw_data)
101 batch_len = data_len // batch_size
102 data = tf.reshape(raw_data[0 : batch_size * batch_len],
103 [batch_size, batch_len])
104
105 epoch_size = (batch_len - 1) // num_steps
106 assertion = tf.assert_positive(
107 epoch_size,
108 message="epoch_size == 0, decrease batch_size or num_steps")
109 with tf.control_dependencies([assertion]):
110 epoch_size = tf.identity(epoch_size, name="epoch_size")
111
112 i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue()
113 x = tf.strided_slice(data, [0, i * num_steps],
114 [batch_size, (i + 1) * num_steps])
115 x.set_shape([batch_size, num_steps])
116 y = tf.strided_slice(data, [0, i * num_steps + 1],
117 [batch_size, (i + 1) * num_steps + 1])
118 y.set_shape([batch_size, num_steps])
119 return x, y

Callers 1

get_configFunction · 0.90

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…