Run a step of the model feeding the given inputs. Args: session: tensorflow session to use. encoder_inputs: list of numpy int vectors to feed as encoder inputs. decoder_inputs: list of numpy int vectors to feed as decoder inputs. target_weights: list of numpy float vecto
(self, session, encoder_inputs, decoder_inputs, target_weights,
bucket_id, forward_only)
| 197 | self.saver = tf.train.Saver(tf.global_variables()) |
| 198 | |
| 199 | def step(self, session, encoder_inputs, decoder_inputs, target_weights, |
| 200 | bucket_id, forward_only): |
| 201 | """Run a step of the model feeding the given inputs. |
| 202 | |
| 203 | Args: |
| 204 | session: tensorflow session to use. |
| 205 | encoder_inputs: list of numpy int vectors to feed as encoder inputs. |
| 206 | decoder_inputs: list of numpy int vectors to feed as decoder inputs. |
| 207 | target_weights: list of numpy float vectors to feed as target weights. |
| 208 | bucket_id: which bucket of the model to use. |
| 209 | forward_only: whether to do the backward step or only forward. |
| 210 | |
| 211 | Returns: |
| 212 | A triple consisting of gradient norm (or None if we did not do backward), |
| 213 | average perplexity, and the outputs. |
| 214 | |
| 215 | Raises: |
| 216 | ValueError: if length of encoder_inputs, decoder_inputs, or |
| 217 | target_weights disagrees with bucket size for the specified bucket_id. |
| 218 | """ |
| 219 | # Check if the sizes match. |
| 220 | encoder_size, decoder_size = self.buckets[bucket_id] |
| 221 | if len(encoder_inputs) != encoder_size: |
| 222 | raise ValueError("Encoder length must be equal to the one in bucket," |
| 223 | " %d != %d." % (len(encoder_inputs), encoder_size)) |
| 224 | if len(decoder_inputs) != decoder_size: |
| 225 | raise ValueError("Decoder length must be equal to the one in bucket," |
| 226 | " %d != %d." % (len(decoder_inputs), decoder_size)) |
| 227 | if len(target_weights) != decoder_size: |
| 228 | raise ValueError("Weights length must be equal to the one in bucket," |
| 229 | " %d != %d." % (len(target_weights), decoder_size)) |
| 230 | |
| 231 | # Input feed: encoder inputs, decoder inputs, target_weights, as provided. |
| 232 | input_feed = {} |
| 233 | for l in xrange(encoder_size): |
| 234 | input_feed[self.encoder_inputs[l].name] = encoder_inputs[l] |
| 235 | for l in xrange(decoder_size): |
| 236 | input_feed[self.decoder_inputs[l].name] = decoder_inputs[l] |
| 237 | input_feed[self.target_weights[l].name] = target_weights[l] |
| 238 | |
| 239 | # Since our targets are decoder inputs shifted by one, we need one more. |
| 240 | last_target = self.decoder_inputs[decoder_size].name |
| 241 | input_feed[last_target] = np.zeros([self.batch_size], dtype=np.int32) |
| 242 | |
| 243 | # Output feed: depends on whether we do a backward step or not. |
| 244 | if not forward_only: |
| 245 | output_feed = [self.updates[bucket_id], # Update Op that does SGD. |
| 246 | self.gradient_norms[bucket_id], # Gradient norm. |
| 247 | self.losses[bucket_id]] # Loss for this batch. |
| 248 | else: |
| 249 | output_feed = [self.losses[bucket_id]] # Loss for this batch. |
| 250 | for l in xrange(decoder_size): # Output logits. |
| 251 | output_feed.append(self.outputs[bucket_id][l]) |
| 252 | |
| 253 | outputs = session.run(output_feed, input_feed) |
| 254 | if not forward_only: |
| 255 | return outputs[1], outputs[2], None # Gradient norm, loss, no outputs. |
| 256 | else: |