def update(self, phi, observation, *args, **kwargs): """Updates :py:attr:`~policy.Policy.pi`.""" phi = phi[self.feature_indices] p = self.value_function(phi) if observation['bump']: self.pi *= 0 self.pi[self.TURN] = 1 else: # Joseph Modayil's constants # T = 1 / self.time_scale / 1 T = 6.0 k1 = np.log((T - 1) * (self.action_space.size - 1)) k2 = k1 * 4.0 # make preferences for each action prefs = np.zeros(2) def last(index): return self.last_index == index prefs[self.TURN] = k1 * last(self.TURN) prefs[self.FORWARD] = k2 * (0.5 - p) + k1 * last(self.FORWARD) self.pi = tools.softmax(prefs)
def probabilities(self, agent, contexts): self.q = agent.ucb_values(contexts) self.q = self.q - np.max(self.q) self._probabilities = softmax(1 * self.q) self.pi = self._probabilities return self._probabilities
def measurement_model_update(MAP, P, data, angles): ''' measurement model update Input: MAP - map object P - list of particle states data - current scan data angles - lidar scan angles (from -135 ~ 135 degree) Outputs: best_particle - chosen best particle (x, y, theta) ''' # calculate map correlation for each particle l = 2 corrs = [] res = MAP['res'] particles = P['states'] grid_tmp = np.zeros_like(MAP['map']) # for calculate correlation grid_tmp[MAP['map'] > 0] = 1 # occupied grid_tmp[MAP['map'] < 0] = 0 # free X, Y = polar2cart(data['scan'], angles) # polar coord -> cartesian coord x_im, y_im = np.arange(MAP['xmin'], MAP['xmax'] + res, res), np.arange(MAP['ymin'], MAP['ymax'] + res, res) x_range, y_range = np.arange(-res * l, res * l + res, res), np.arange(-res * l, res * l + res, res) for i in range(len(particles)): scan = transform(X, Y, data['joint'], particles[i]) scan = filtering(scan, particles[i]) x, y = scan[0], scan[1] corr = mapCorrelation(grid_tmp, x_im, y_im, np.vstack( (x, y)), particles[i][0] + x_range, particles[i][1] + y_range) corrs.append(np.max(corr)) # get the particle with largest weight corrs = np.array(corrs) P['weight'] = softmax(P['weight'] * corrs) best_idx = np.where(P['weight'] == np.max(P['weight']))[0][0] best_particle = particles[best_idx] return best_particle
def recog(self, fname_or_list): """ 识别 demo,输入一个或多个视频文件路径,返回识别结果 """ if not isinstance(fname_or_list, list): fname_or_list = [fname_or_list] j = self.batch_generator.test_batch_size copies_num = ceil(len(fname_or_list) / j) logits_list = [] for i in range(0, copies_num * j, j): fnames = fname_or_list[i:i + j] batch, _ = self.batch_generator.handle_batch(fnames, is_test=True) # 用于 feed 数据 labels = [0] * len(batch) values = [batch, labels, 1.0] feed_dict = self.feed(values) logits = self.sess.run(self.logits, feed_dict=feed_dict) logits_list.append(logits) logits = np.concatenate(logits_list) probs = tools.softmax(logits) labels = np.argmax(logits, axis=-1) return probs, labels
sys.stdout.flush() y_true = Y_test[:, j].toarray().reshape(-1) npos = y_true.sum() if npos < 1: continue u = clf.pl2u[j] wk = clf.V[u, :] + clf.W[j, :] + clf.mu y_pred = np.dot(X_test, wk) rp, hr_dict, auc = calc_metrics(y_true, y_pred, tops=TOPs) rps.append(rp) for top in TOPs: hitrates[top].append(hr_dict[top]) aucs.append(auc) # spread y_pred_prob = softmax(y_pred) # spreads.append(-np.multiply(y_pred_prob, np.log(y_pred_prob)).sum()) spreads.append(-np.dot(y_pred_prob, np.log(y_pred_prob))) # novelty sortix = np.argsort(-y_pred) for top in TOPs: nov = np.mean( [-np.log2(song2pop[index2song[ix]]) for ix in sortix[:top]]) try: novelties[top][u].append(nov) except KeyError: novelties[top][u] = [nov] # compute diversity@100 # csd = 1. / cosine_similarity(X_test[sortix[:100], :])
def translate_ensemble(models, config, x, beam_size=10, length_norm=True): num_models = len(models) result = [[]] loss = [0.] result_eos = [] loss_eos = [] beam = beam_size c = [] state = [] emb_y = [] for i in range(num_models): tmpc, tmpstate = models[i].get_context_and_init(x) c.append(tmpc) state.append(tmpstate) emb_y.append(numpy.zeros((1, config['dim_emb_trg']), dtype='float32')) for l in range(x.shape[0] * 3): ctx = [] probs = [] for i in range(num_models): tmpenergy, tmpctx = models[i].get_probs( numpy.repeat(c[i], len(result), axis=1), state[i], emb_y[i]) ctx.append(tmpctx) probs.append(tools.softmax(tmpenergy)) losses = -numpy.log(probs[0]) for i in range(1, num_models): losses -= numpy.log(probs[i]) if l < x.shape[0] / 2: losses[:, config['index_eos_trg']] = numpy.inf for i in range(len(loss)): if length_norm: losses[i] += (loss[i] * l) losses[i] /= (l + 1) else: losses[i] += loss[i] best_index_flatten = numpy.argpartition(losses.flatten(), beam)[:beam] best_index = [(index / config['num_vocab_trg'], index % config['num_vocab_trg']) for index in best_index_flatten] new_y = [] new_result = [] new_loss = [] new_ctx = [] new_state = [] for i in range(num_models): new_ctx.append( numpy.zeros((beam, 2 * config['dim_rec_enc']), dtype='float32')) new_state.append( numpy.zeros((beam, config['dim_rec_dec']), dtype='float32')) for i in range(beam): index = best_index[i] new_result.append(result[index[0]] + [index[1]]) new_loss.append(losses[index[0], index[1]]) new_y.append(index[1]) for j in range(num_models): new_ctx[j][i] = ctx[j][index[0]] new_state[j][i] = state[j][index[0]] new_emby = [] for i in range(num_models): new_emby.append(models[i].get_trg_embedding( numpy.asarray(new_y, dtype='int64'))[0]) new_state[i] = models[i].get_next(new_ctx[i], new_state[i], new_emby[i]) state = [] emb_y = [] result = [] loss = [] for i in range(num_models): state.append([]) emb_y.append([]) for i in range(beam): if new_result[i][-1] == config['index_eos_trg']: result_eos.append(new_result[i]) loss_eos.append(new_loss[i]) beam -= 1 else: result.append(new_result[i]) loss.append(new_loss[i]) for j in range(num_models): state[j].append(new_state[j][i]) emb_y[j].append(new_emby[j][i]) if beam <= 0: break for i in range(num_models): state[i] = numpy.asarray(state[i], dtype='float32') emb_y[i] = numpy.asarray(emb_y[i], dtype='float32') if len(result_eos) > 0: return result_eos[numpy.argmin(loss_eos)] elif beam_size > 100: logging.warning('cannot find translation in beam size %d' % beam_size) return [] else: logging.info('cannot find translation in beam size %d, try %d' % (beam_size, beam_size * 2)) return translate_ensemble(models, config, x, beam_size=beam_size * 2)
network.layers[3].add_bias() ns_probs = [0 for _ in range(len(Y[:, 0]))] val_loss, loss, lr_val_auc, lr_auc = [], [], [], [] for i in range(epochs): ############# feedforward ### Hidden layer 1 ### z_h_1 = X.dot(network.layers[1].weights.T) + network.layers[1].bias network.layers[1].outputs = sigmoid(z_h_1) ### Hidden layer 2 ### z_h_2 = network.layers[1].outputs.dot( network.layers[2].weights.T) + network.layers[2].bias network.layers[2].outputs = sigmoid(z_h_2) ###output### z_o = np.dot(network.layers[2].outputs, network.layers[3].weights.T) + network.layers[3].bias network.layers[3].outputs = softmax(z_o) ### Backpropagation ## Output layer delta_z_o = network.layers[3].outputs - Y delta_w13 = network.layers[2].outputs dw_o = np.dot(delta_z_o.T, delta_w13) / X.shape[0] db_o = np.sum(delta_z_o, axis=0, keepdims=True) / X.shape[0] ## Hidden layer 2 delta_a_h_2 = np.dot(delta_z_o, network.layers[3].weights) delta_z_h_2 = sigmoid_(network.layers[2].outputs) d = delta_a_h_2 * delta_z_h_2 delta_w12 = network.layers[1].outputs dw_h_2 = np.dot(d.T, delta_w12) / X.shape[0] db_h_2 = np.sum(d, axis=0, keepdims=True) / X.shape[0] ## Hidden layer 1 delta_a_h_1 = delta_z_h_2.dot(network.layers[2].weights)
def do_eval_slidingclips(self, save_name): """Do evaluation based on proposals and save the coresponding score and offset to a pickle file in './eval/test_results' folder """ test_len_dict = tools.load_length_dict(type='test') reg_result_dict = {} for k, test_sample in enumerate(self.test_set.test_samples): reg_result_dict[k] = [] if k % 1000 == 0: print(str(k) + "/" + str(len(self.test_set.test_samples))) movie_name = test_sample[0] init_clip_start = test_sample[1] init_clip_end = test_sample[2] clip_start = init_clip_start clip_end = init_clip_end final_action_prob = np.zeros([ (self.config.action_class_num + 1) * 3 * self.config.cas_step ]) if clip_start >= clip_end: reg_result_dict[k].append(final_action_prob) continue for i in range(self.config.cas_step): if clip_start >= clip_end: break if self.config.feat_type == 'Pool': featmap = dataset.get_pooling_feature( self.test_set.flow_feat_dir, self.test_set.appr_feat_dir, movie_name, clip_start, clip_end, self.config.pool_level, self.config.unit_size, self.config.unit_feature_size, self.config.fusion_type) left_feat = dataset.get_left_context_feature( self.test_set.flow_feat_dir, self.test_set.appr_feat_dir, movie_name, clip_start, clip_end, self.config.ctx_num, self.config.unit_size, self.config.unit_feature_size, self.config.fusion_type) right_feat = dataset.get_right_context_feature( self.test_set.flow_feat_dir, self.test_set.appr_feat_dir, movie_name, clip_start, clip_end, self.config.ctx_num, self.config.unit_size, self.config.unit_feature_size, self.config.fusion_type) mean_ = np.hstack((left_feat, featmap, right_feat)) feat = mean_ elif self.config.feat_type == 'SSN': feat = dataset.get_SSN_feature( self.test_set.flow_feat_dir, self.test_set.appr_feat_dir, movie_name, clip_start, clip_end, self.config.unit_size, self.config.unit_feature_size, self.config.fusion_type) else: feat = dataset.get_BSP_feature( self.test_set.flow_feat_dir, self.test_set.appr_feat_dir, movie_name, clip_start, clip_end, self.config.unit_size, self.config.unit_feature_size, self.config.bsp_level) feat = np.reshape(feat, [1, self.config.visual_feature_dim]) feed_dict = {self.visual_featmap_ph_test: feat} outputs = self.sess.run(self.vs_eval_op, feed_dict=feed_dict) action_score = outputs[1:self.config.action_class_num + 1] action_prob = tools.softmax(action_score) final_action_prob[(i) * (self.config.action_class_num + 1) * 3:(i + 1) * (self.config.action_class_num + 1) * 3] = outputs action_cat = np.argmax(action_prob) + 1 round_reg_end = clip_end + round(outputs[ (self.config.action_class_num + 1) * 2 + action_cat]) * self.config.unit_size round_reg_start = clip_start + round( outputs[self.config.action_class_num + 1 + action_cat]) * self.config.unit_size if round_reg_start < 0 or round_reg_end > test_len_dict[ movie_name] - 15 or round_reg_start >= round_reg_end: round_reg_end = clip_end round_reg_start = clip_start reg_end = clip_end + outputs[ (self.config.action_class_num + 1) * 2 + action_cat] * self.config.unit_size reg_start = clip_start + outputs[ self.config.action_class_num + 1 + action_cat] * self.config.unit_size clip_start = round_reg_start clip_end = round_reg_end reg_result_dict[k].append(final_action_prob) pickle.dump( reg_result_dict, open("./eval/test_results/" + save_name + "_outputs.pkl", "wb"))
def translate(self, x, beam_size=10, return_array=False, rerank=False): ''' Decode with beam search. :type x: numpy array :param x: the indexed source sentence :type beam_size: int :param beam_size: beam size :returns: a numpy array, the indexed translation result ''' # initialize variables result = [[]] loss = [0.] result_eos = [] loss_eos = [] beam = beam_size # get encoder states c, state = self.get_context_and_init(x) emb_y = numpy.zeros((1, self.config['dim_emb_trg']), dtype='float32') for l in range(x.shape[0] * 3): # get word probability energy, ctx = self.get_probs(numpy.repeat(c, len(result), axis=1), state, emb_y) probs = tools.softmax(energy) losses = -numpy.log(probs) # prevent translation to be too short. if l < x.shape[0] / 2: losses[:, self.config['index_eos_trg']] = numpy.inf for i in range(len(loss)): losses[i] += loss[i] # get the n-best partial translations best_index_flatten = numpy.argpartition(losses.flatten(), beam)[:beam] best_index = [(index / self.config['num_vocab_trg'], index % self.config['num_vocab_trg']) for index in best_index_flatten] # save the partial translations in the beam new_ctx = numpy.zeros((beam, 2 * self.config['dim_rec_enc']), dtype='float32') new_y = [] new_state = numpy.zeros((beam, self.config['dim_rec_dec']), dtype='float32') new_result = [] new_loss = [] for i in range(beam): index = best_index[i] new_result.append(result[index[0]] + [index[1]]) new_loss.append(losses[index[0], index[1]]) new_ctx[i] = ctx[index[0]] new_y.append(index[1]) new_state[i] = state[index[0]] # get the next decoder hidden state new_emby = self.get_trg_embedding( numpy.asarray(new_y, dtype='int64'))[0] new_state = self.get_next(new_ctx, new_state, new_emby) # remove finished translation from the beam state = [] emb_y = [] result = [] loss = [] for i in range(beam): if new_result[i][-1] == self.config['index_eos_trg']: result_eos.append(new_result[i]) loss_eos.append(new_loss[i]) beam -= 1 else: result.append(new_result[i]) loss.append(new_loss[i]) state.append(new_state[i]) emb_y.append(new_emby[i]) if beam <= 0: break state = numpy.asarray(state, dtype='float32') emb_y = numpy.asarray(emb_y, dtype='float32') # only used in semi-supervised training #print(self.feature_weight.get_value()) #logging.info(self.feature_weight.get_value()) if return_array: if len(result_eos) > 0: return result_eos else: return [result[-1][:1]] #print(len(result_eos)) if len(result_eos) > 0: # return the best translation if rerank: #print("rerank") #reranking for i in range(len(result_eos)): feature_value = numpy.asarray([], dtype='float32') for j in range(len(self.fls)): fl = self.fls[j] #print(x) #print(result_eos[i]) fe = fl.getFeatures(x, result_eos[i], 0, 0, 2) #print(fe) #fe = fl.getFeatures(x, result_eos[i],2) feature_value = numpy.concatenate((feature_value, fe)) loss_eos[i] -= (feature_value * self.feature_weight.get_value()).sum() return result_eos[numpy.argmin(loss_eos)] elif beam_size > 100: # double the beam size on failure logging.warning('cannot find translation in beam size %d' % beam_size) return [] else: logging.info('cannot find translation in beam size %d, try %d' % (beam_size, beam_size * 2)) return self.translate(x, beam_size=beam_size * 2)
def translate(self, x, T, beam_size=10, return_array=False): ''' Decode with beam search. :type x: numpy array :param x: the indexed source sentence :type beam_size: int :param beam_size: beam size :returns: a numpy array, the indexed translation result ''' # initialize variables result = [[]] loss = [0.] result_eos = [] loss_eos = [] beam = beam_size nonterms = [ ['S'] ] # same length as result, nonterms for each hypothesis # (n_hyps, nonterm for each hyp) par_state_time = [[0]] # (n_hyps, len(nonterm) for each hyp) # get encoder states c, state = self.get_context_and_init(x) emb_y = numpy.zeros((1, self.config['dim_emb_trg']), dtype='float32') state_hist = [[ numpy.zeros((1, self.config['dim_rec_enc']), dtype='float32') ]] # (n_hyps, l) for l in range(x.shape[0] * 3): cur_nonterm_idx = [ ] # length lists, each list is the rule indices for expanding LHS #print result for i in range(len(nonterms)): if len(nonterms[i]) > 0: potent_rules = T.rule_idx_with_root( nonterms[i][-1] ) # list of potential rules with the given lhs as root #print potent_rules + i * self.config['dim_emb_trg'] cur_nonterm_idx += [ r + i * self.config['num_vocab_trg'] for r in potent_rules ] nonterms[i].pop() # only take the first k results if we have k < beam_size potential nonterms if len(cur_nonterm_idx) < beam_size: beam = len(cur_nonterm_idx) else: beam = beam_size # get word probability energy, ctx = self.get_probs(numpy.repeat(c, len(result), axis=1), state, emb_y) # multiply energy by cur_nonterm_idx mask energy_mask = numpy.zeros((energy.shape[0] * energy.shape[1]), dtype='float32') energy_mask[cur_nonterm_idx] = 1. energy_mask = energy_mask.reshape( (energy.shape[0], energy.shape[1])) energy = energy * energy_mask probs = tools.softmax(energy) losses = -numpy.log(probs) # prevent translation to be too short. if l < x.shape[0] / 2: losses[:, self.config['index_eos_trg']] = numpy.inf # prevent rules that do not have required lhs #losses[:, not_cur_nonterm_idx] = numpy.inf for i in range(len(loss)): losses[i] += loss[i] # get the n-best partial translations best_index_flatten = numpy.argpartition(losses.flatten(), beam)[:beam] best_index = [(index / self.config['num_vocab_trg'], index % self.config['num_vocab_trg']) for index in best_index_flatten] # save the partial translations in the beam new_ctx = numpy.zeros((beam, 2 * self.config['dim_rec_enc']), dtype='float32') new_y = [] new_state = numpy.zeros((beam, self.config['dim_rec_dec']), dtype='float32') new_result = [] new_loss = [] new_nonterms = [] new_par_state_time = [] new_state_hist = [] new_par_state = numpy.zeros((beam, self.config['dim_rec_dec']), dtype='float32') #print best_index #print len(result), len(state_hist), len(par_state_time) for i in range(beam): index = best_index[i] new_result.append(result[index[0]] + [index[1]]) new_loss.append(losses[index[0], index[1]]) new_ctx[i] = ctx[index[0]] new_y.append(index[1]) new_state[i] = state[index[0]] par_state_t = par_state_time[index[0]][-1] new_par_state[i] = state_hist[index[0]][par_state_t] r = T.get_rule_from_idx(index[1]) if r: add_nonterms = r.get_expand_tags()[::-1] else: add_nonterms = [] new_nonterms.append(nonterms[index[0]] + add_nonterms) # set the parent of expanded tags to be current # do not include last par_state_time[] for current hyp new_par_state_time.append(par_state_time[index[0]][:-1] + [l + 1] * len(add_nonterms)) new_state_hist.append(state_hist[index[0]] + [state[index[0]]]) # get the next decoder hidden state new_emby = self.get_trg_embedding( numpy.asarray(new_y, dtype='int64'))[0] new_state = self.get_next(new_ctx, new_state, new_par_state, new_emby) # remove finished translation from the beam state = [] emb_y = [] result = [] loss = [] nonterms = [] state_hist = [] par_state_time = [] for i in range(beam): if len(new_nonterms[i]) == 0: # par_state_time and nonterms should have same length for each hyp # par_state_time records parent state timestep for each nonterms that needs to be expanded assert len(new_par_state_time[i]) == 0 result_eos.append(new_result[i]) #print new_result[i] loss_eos.append(new_loss[i]) beam -= 1 else: result.append(new_result[i]) loss.append(new_loss[i]) state.append(new_state[i]) emb_y.append(new_emby[i]) nonterms.append(new_nonterms[i]) state_hist.append(new_state_hist[i]) par_state_time.append(new_par_state_time[i]) #print len(result), len(state_hist), len(par_state_time) if beam <= 0: break state = numpy.asarray(state, dtype='float32') emb_y = numpy.asarray(emb_y, dtype='float32') # only used in semi-supervised training if return_array: if len(result_eos) > 0: return result_eos else: return [result[-1][:1]] if len(result_eos) > 0: # return the best translation return result_eos[numpy.argmin(loss_eos)] elif beam_size > 100: # double the beam size on failure logging.warning('cannot find translation in beam size %d' % beam_size) return [] else: logging.info('cannot find translation in beam size %d, try %d' % (beam_size, beam_size * 2)) return self.translate(x, beam_size=beam_size * 2)
def probabilities(self, agent, contexts): a = agent.value_estimates(contexts) return softmax(a * self.b)
import sys sys.path.append('../helper') import tensorflow as tf import pandas as pd import numpy as np import tools import wordhash class CDSSM(object): pass if __name__ == '__main__': a = np.array(range(12)).reshape(3, 4) print a print tools.softmax(a)