def run_decoder_step(_st, _ct, _state, len_seq): # one hot vector of current true action onehot_act = np.zeros((1, self._num_actions), dtype=np.float32) if len_seq < n_dec_unrolls: onehot_act[0, decoder_output[len_seq]] = 1.0 # get world vector for current position x, y, pose = _state place = _map.locationByCoord[(x, y)] yt = get_sparse_world_context(_map, place, pose, self._map_feature_dict, self._map_objects_dict) # set placeholder for current roll feed_dict[self._test_decoder_output] = onehot_act feed_dict[self._test_st] = _st feed_dict[self._test_ct] = _ct feed_dict[self._test_yt] = yt output_feed = [ self._next_st, self._next_ct, self._test_prediction, self._test_loss, ] st, ct, prediction, step_loss = session.run(output_feed, feed_dict=feed_dict) return [st, ct, prediction, step_loss]
def inference_step(self,session,encoder_input,decoder_output,sample_input): """ Performs inference with beam search in the decoder. session: tensorflow session encoder_input: [1 x K]*enc_unroll decoder_output: true actions [dec_unroll] sample_input: Sample instance of current sample return : loss, correct_pred (True|False) """ feed_dict = {} end_state = sample_input._path[-1] for i in xrange(self._encoder_unrollings): feed_dict[self._test_encoder_inputs[i]] = encoder_input[i] # initial values for cell variables [st,ct] = session.run([self._test_s0,self._test_c0],feed_dict=feed_dict) state = sample_input._path[0] prev_state = sample_input._path[0] _map = self._maps[sample_input._map_name] loss = 0.0 # must be averaged over predicted sequence n_preds = -1 for i in xrange(self._decoder_unrollings): # one hot vector of current true action onehot_act = np.zeros((1,self._num_actions),dtype=np.float32) onehot_act[0,decoder_output[i]] = 1.0 # get world vector for current position x,y,pose = state place = _map.locationByCoord[(x,y)] yt = get_sparse_world_context(_map, place, pose, self._map_feature_dict,self._map_objects_dict) # set placeholder for current roll feed_dict[self._test_decoder_output] = onehot_act feed_dict[self._test_st] = st feed_dict[self._test_ct] = ct feed_dict[self._test_yt] = yt output_feed = [ self._next_st, self._next_ct, self._test_prediction, self._test_loss, ] st,ct,prediction,step_loss = session.run(output_feed,feed_dict=feed_dict) loss += step_loss # greedy prediction pred_act = prediction.argmax() # move according to prediction prev_state = state state = move(state,pred_act,_map) if state == -1: n_preds = i+1 break if state != -1: prev_state = state loss /= (n_preds if n_preds!=-1 else self._decoder_unrollings) return loss,int(end_state==prev_state) # for single-sentence
def training_step(self, session, encoder_inputs, decoder_outputs, sample_inputs): """ Runs training step for one simple sample returns: loss, summary_string, correct (1:correctly predicted| 0: mispredicted) """ feed_dict = {} n_enc_unrolls = len(encoder_inputs) n_dec_unrolls = len(decoder_outputs) feed_dict[self._encoder_unrollings] = n_enc_unrolls feed_dict[self._decoder_unrollings] = n_dec_unrolls sample_input = sample_inputs[0] for i in xrange(self._max_encoder_unrollings): if i < n_enc_unrolls: feed_dict[self._encoder_inputs[i]] = encoder_inputs[i] else: feed_dict[self._encoder_inputs[i]] = np.zeros( shape=(1, self._vocab_size), dtype=np.float32) _map = self._maps[sample_input._map_name] for i in xrange(self._max_decoder_unrollings): if i < n_dec_unrolls: # world_state vector Y x, y, pose = sample_input._path[i] place = _map.locationByCoord[(x, y)] y_roll = get_sparse_world_context(_map, place, pose, self._map_feature_dict, self._map_objects_dict) feed_dict[self._decoder_outputs[i]] = [decoder_outputs[i]] feed_dict[self._world_state_vectors[i]] = y_roll else: feed_dict[self._decoder_outputs[i]] = np.zeros( shape=(1), dtype=np.float32) feed_dict[self._world_state_vectors[i]] = np.zeros( shape=(1, self._y_size), dtype=np.float32) output_feed = [ self._optimizer, self._loss, self._learning_rate, self._merged ] + self._train_predictions outputs = session.run(output_feed, feed_dict=feed_dict) predictions = outputs[4:] correct = self.get_endpoint_accuracy(sample_inputs, predictions) """ temp =[] for act in sample_input._actions: tt = np.zeros((1,self._num_actions)) tt[0,act] = 1.0 temp.append(tt) correct = self.get_endpoint_accuracy(sample_inputs,temp) if correct==0.0: ipdb.set_trace() correct = self.get_endpoint_accuracy(sample_inputs,temp) """ return (outputs[1], outputs[3], correct) #loss, summary_str, correct
def training_step(self,session,encoder_inputs,decoder_outputs,sample_inputs): """ Runs training step for one simple sample returns: loss, summary_string, correct (1:correctly predicted| 0: mispredicted) """ feed_dict = {} n_enc_unrolls = len(encoder_inputs) n_dec_unrolls = len(decoder_outputs) feed_dict[self._encoder_unrollings] = n_enc_unrolls feed_dict[self._decoder_unrollings] = n_dec_unrolls sample_input = sample_inputs[0] for i in xrange(self._max_encoder_unrollings): if i < n_enc_unrolls: feed_dict[self._encoder_inputs[i]] = encoder_inputs[i] else: feed_dict[self._encoder_inputs[i]] = np.zeros(shape=(1,self._vocab_size),dtype=np.float32) _map = self._maps[sample_input._map_name] for i in xrange(self._max_decoder_unrollings): if i < n_dec_unrolls: # world_state vector Y x,y,pose = sample_input._path[i] place = _map.locationByCoord[(x,y)] y_roll = get_sparse_world_context(_map, place, pose, self._map_feature_dict, self._map_objects_dict) feed_dict[self._decoder_outputs[i]] = [decoder_outputs[i]] feed_dict[self._world_state_vectors[i]] = y_roll else: feed_dict[self._decoder_outputs[i]] = np.zeros(shape=(1),dtype=np.float32) feed_dict[self._world_state_vectors[i]] = np.zeros(shape=(1,self._y_size),dtype=np.float32) output_feed = [ self._optimizer, self._loss, self._learning_rate, self._merged ] + self._train_predictions outputs = session.run(output_feed,feed_dict=feed_dict) predictions = outputs[4:] correct = self.get_endpoint_accuracy(sample_inputs,predictions) """ temp =[] for act in sample_input._actions: tt = np.zeros((1,self._num_actions)) tt[0,act] = 1.0 temp.append(tt) correct = self.get_endpoint_accuracy(sample_inputs,temp) if correct==0.0: ipdb.set_trace() correct = self.get_endpoint_accuracy(sample_inputs,temp) """ return (outputs[1],outputs[3],correct) #loss, summary_str, correct
def run_decoder_step(_st,_ct,_state,len_seq): # one hot vector of current true action onehot_act = np.zeros((1,self._num_actions),dtype=np.float32) if len_seq < n_dec_unrolls: onehot_act[0,decoder_output[len_seq]] = 1.0 # get world vector for current position x,y,pose = _state place = _map.locationByCoord[(x,y)] yt = get_sparse_world_context(_map, place, pose, self._map_feature_dict, self._map_objects_dict) # set placeholder for current roll feed_dict[self._test_decoder_output] = onehot_act feed_dict[self._test_st] = _st feed_dict[self._test_ct] = _ct feed_dict[self._test_yt] = yt output_feed = [ self._next_st, self._next_ct, self._test_prediction, self._test_loss, ] st,ct,prediction,step_loss = session.run(output_feed,feed_dict=feed_dict) return [st,ct,prediction,step_loss]
def inference_step(self, session, encoder_input, decoder_output, sample_input): """ Performs inference with beam search in the decoder. session: tensorflow session encoder_input: [1 x K]*enc_unroll decoder_output: true actions [dec_unroll] sample_input: Sample instance of current sample return : loss, correct_pred (True|False) """ feed_dict = {} end_state = sample_input._path[-1] for i in xrange(self._encoder_unrollings): feed_dict[self._test_encoder_inputs[i]] = encoder_input[i] # initial values for cell variables [st, ct] = session.run([self._test_s0, self._test_c0], feed_dict=feed_dict) state = sample_input._path[0] prev_state = sample_input._path[0] _map = self._maps[sample_input._map_name] loss = 0.0 # must be averaged over predicted sequence n_preds = -1 for i in xrange(self._decoder_unrollings): # one hot vector of current true action onehot_act = np.zeros((1, self._num_actions), dtype=np.float32) onehot_act[0, decoder_output[i]] = 1.0 # get world vector for current position x, y, pose = state place = _map.locationByCoord[(x, y)] yt = get_sparse_world_context(_map, place, pose, self._map_feature_dict, self._map_objects_dict) # set placeholder for current roll feed_dict[self._test_decoder_output] = onehot_act feed_dict[self._test_st] = st feed_dict[self._test_ct] = ct feed_dict[self._test_yt] = yt output_feed = [ self._next_st, self._next_ct, self._test_prediction, self._test_loss, ] st, ct, prediction, step_loss = session.run(output_feed, feed_dict=feed_dict) loss += step_loss # greedy prediction pred_act = prediction.argmax() # move according to prediction prev_state = state state = move(state, pred_act, _map) if state == -1: n_preds = i + 1 break if state != -1: prev_state = state loss /= (n_preds if n_preds != -1 else self._decoder_unrollings) return loss, int(end_state == prev_state) # for single-sentence