def build_fa_from_disk(nn_stack, nn, path, shuffle=False): if not path.endswith('/'): path += '/' files = glob.glob(path + 'sars_*.npy') print 'Got %s files' % len(files) for idx, f in enumerate(files): sars = np.load(f) if shuffle: np.random.shuffle(sars) S = pds_to_npa(sars[:, 0]) SS = pds_to_npa(sars[:, 3]) if idx == 0: F = np.column_stack((nn_stack.s_features(S, SS), nn.all_features(S))) A = pds_to_npa(sars[:, 1]) else: new_F = np.column_stack( (nn_stack.s_features(S, SS), nn.all_features(S))) new_A = pds_to_npa(sars[:, 1]) F = np.append(F, new_F, axis=0) A = np.append(A, new_A, axis=0) A = A.reshape(-1, 1) FA = np.concatenate((F, A), axis=1) return FA
def get_sample_weight(target, class_weight): """ Returns a list with the class weight of each sample. The return value can be passed directly to Keras's sample_weight parameter in model.fit Args target (pd.DataFrame or pd.Series): a SARS' dataset in pandas format or a pd.Series with rewards. class_weight (dict, None): dictionary with classes as key and weights as values. If None, the dictionary will be computed using sklearn's method. round (bool, False): round the rewards to the nearest integer before applying the class weights. """ if isinstance(target, pd.DataFrame): target = pds_to_npa(target.R) else: target = pds_to_npa(target) if target.ndim == 2 and target.shape[1] == 1: target = target.ravel() sample_weight = [class_weight[r] for r in target] return np.array(sample_weight)
def sar_generator_from_disk(path, model, batch_size=32, binarize=False, shuffle=False, weights=None): """ Generator of S, A, R arrays from SARS datasets saved in path. Args path (str): path to folder containing 'sars_*.pkl' files (as collected with collect_sars_to_disk) Yield (S, A, R) (np.array, np.array, np.array): np.arrays with states, actions and rewards from each SARS dataset in path. """ if not path.endswith('/'): path += '/' files = glob.glob(path + 'sars_*.npy') print 'Got %s files' % len(files) while True: for idx, f in enumerate(files): sars = np.load(f) if idx > 0: sars = np.append(excess_sars, sars, axis=0) if shuffle: np.random.shuffle(sars) excess = len(sars) % batch_size if excess > 0: excess_sars = sars[-excess:] sars = sars[:-excess] else: excess_sars = sars[0:0] # just to preserve shapes nb_batches = len(sars) / batch_size if weights is not None: sample_weight = get_sample_weight(pds_to_npa(sars[:, 2]), class_weight=weights) for i in range(nb_batches): start = i * batch_size stop = (i + 1) * batch_size S = pds_to_npa(sars[start:stop, 3]) # S' A = pds_to_npa(sars[start:stop, 1]) R = pds_to_npa(sars[start:stop, 2]) # Preprocess data S = model.preprocess_state(S, binarize=binarize) if weights is not None: yield ([S, A], R, sample_weight[start:stop]) else: yield ([S, A], R)
def build_fd(nn_stack, nn, support, sars, shuffle=False): if shuffle: np.random.shuffle(sars) S = pds_to_npa(sars[:, 0]) SS = pds_to_npa(sars[:, 3]) F = nn_stack.s_features(S, SS) D = nn.s_features(S, support) - nn.s_features(SS, support) return F, D
def ss_generator_from_disk(path, model, batch_size=32, binarize=False, binarization_threshold=0.1, weights=None, shuffle=False, clip=False): if not path.endswith('/'): path += '/' files = glob.glob(path + 'sars_*.npy') print 'Got %s files' % len(files) while True: for idx, f in enumerate(files): sars = np.load(f) if shuffle: np.random.shuffle(sars) if idx > 0: sars = np.append(excess_sars, sars, axis=0) excess = len(sars) % batch_size if excess > 0: excess_sars = sars[-excess:] sars = sars[:-excess] else: excess_sars = sars[0:0] # just to preserve shapes nb_batches = len(sars) / batch_size if weights is not None: R = pds_to_npa(sars[:, 2]) if clip: R = np.clip(R, -1, 1) sample_weight = get_sample_weight(R, class_weight=weights) for i in range(nb_batches): start = i * batch_size stop = (i + 1) * batch_size S = pds_to_npa(sars[start:stop, 0]) # Preprocess data S = model.preprocess_state( S, binarize=binarize, binarization_threshold=binarization_threshold) if weights is not None: yield (S, S, sample_weight[start:stop]) else: yield (S, S)
def build_farf_from_disk(model, path, shuffle=False): if not path.endswith('/'): path += '/' files = glob.glob(path + 'sars_*.npy') print 'Got %s files' % len(files) for idx, f in enumerate(files): sars = np.load(f) if shuffle: np.random.shuffle(sars) if idx == 0: F = model.all_features(pds_to_npa(sars[:, 0])) A = pds_to_npa(sars[:, 1]) R = pds_to_npa(sars[:, 2]) FF = model.all_features(pds_to_npa(sars[:, 3])) else: new_F = model.all_features(pds_to_npa(sars[:, 0])) new_A = pds_to_npa(sars[:, 1]) new_R = pds_to_npa(sars[:, 2]) new_FF = model.all_features(pds_to_npa(sars[:, 3])) F = np.append(F, new_F, axis=0) A = np.append(A, new_A, axis=0) R = np.append(R, new_R, axis=0) FF = np.append(FF, new_FF, axis=0) A = A.reshape(-1, 1) # Post processing R = R.reshape(-1, 1) # Sklearn version < 0.19 will throw a warning return F, A, R, FF
def build_r_from_disk(path, shuffle=False): if not path.endswith('/'): path += '/' files = glob.glob(path + 'sars_*.npy') print 'Got %s files' % len(files) for idx, f in enumerate(files): sars = np.load(f) if shuffle: np.random.shuffle(sars) if idx == 0: R = pds_to_npa(sars[:, 2]) else: R = np.append(R, pds_to_npa(sars[:, 2])) return R
def get_class_weight_from_disk(path, clip=False): if not path.endswith('/'): path += '/' files = glob.glob(path + 'sars_*.npy') print 'Got %s files' % len(files) for idx, f in enumerate(files): sars = np.load(f) if idx == 0: target = pds_to_npa(sars[:, 2]) else: target = np.append(target, pds_to_npa(sars[:, 2])) if clip: target = np.clip(target, -1, 1) class_weight = dict() reward_classes = np.unique(target) for r in reward_classes: class_weight[r] = target.size / float(np.argwhere(target == r).size) return class_weight
def build_far_from_disk(nn, path, use_ss=False, shuffle=False): if not path.endswith('/'): path += '/' files = glob.glob(path + 'sars_*.npy') print 'Got %s files' % len(files) state_idx = 3 if use_ss else 0 for idx, f in enumerate(files): sars = np.load(f) if shuffle: np.random.shuffle(sars) if idx == 0: F = nn.all_features(pds_to_npa(sars[:, state_idx])) A = pds_to_npa(sars[:, 1]) R = pds_to_npa(sars[:, 2]) else: new_F = nn.all_features(pds_to_npa(sars[:, state_idx])) new_A = pds_to_npa(sars[:, 1]) new_R = pds_to_npa(sars[:, 2]) F = np.append(F, new_F, axis=0) A = np.append(A, new_A, axis=0) R = np.append(R, new_R, axis=0) A = A.reshape(-1, 1) FA = np.concatenate((F, A), axis=1) # Post processing R = R.reshape(-1, 1) # Sklearn version < 0.19 will throw a warning return FA, R
def build_fd_from_disk(nn_stack, nn, support, path, shuffle=False): if not path.endswith('/'): path += '/' files = glob.glob(path + 'sars_*.npy') print 'Got %s files' % len(files) for idx, f in enumerate(files): sars = np.load(f) if shuffle: np.random.shuffle(sars) S = pds_to_npa(sars[:, 0]) SS = pds_to_npa(sars[:, 3]) if idx == 0: F = nn_stack.s_features(S, SS) D = nn.s_features(S, support) - nn.s_features(SS, support) else: new_F = nn_stack.s_features(S, SS) new_D = nn.s_features(S, support) - nn.s_features(SS, support) F = np.append(F, new_F, axis=0) D = np.append(D, new_D, axis=0) return F, D
def build_faft_r_from_disk(nn_stack, path, shuffle=False): """ Builds FARF' dataset using all SARS' datasets saved in path: F = NN_stack.s_features(S) A = A R = R F' = NN_stack.s_features(S') DONE = DONE """ if not path.endswith('/'): path += '/' files = glob.glob(path + 'sars_*.npy') print 'Got %s files' % len(files) for idx, f in enumerate(files): sars = np.load(f) if shuffle: np.random.shuffle(sars) S = pds_to_npa(sars[:, 0]) SS = pds_to_npa(sars[:, 3]) if idx == 0: F = nn_stack.s_features(S) A = pds_to_npa(sars[:, 1]) R = pds_to_npa(sars[:, 2]) FF = nn_stack.s_features(SS) DONE = pds_to_npa(sars[:, 4]) else: new_F = nn_stack.s_features(S) new_A = pds_to_npa(sars[:, 1]) new_R = pds_to_npa(sars[:, 2]) new_FF = nn_stack.s_features(SS) new_DONE = pds_to_npa(sars[:, 4]) F = np.append(F, new_F, axis=0) A = np.append(A, new_A, axis=0) R = np.append(R, new_R, axis=0) FF = np.append(FF, new_FF, axis=0) DONE = np.append(DONE, new_DONE, axis=0) faft = np.column_stack((F, A, FF, DONE)) action_values = np.unique(A) return faft, R, action_values
def sares_generator_from_disk(model, nn_stack, nn, support, path, batch_size=32, binarize=False, no_residuals=False, weights=None, scale_coeff=1, round_decimal=1, shuffle=False): """ Generator of S, A, RES arrays from SARS datasets saved in path. Args model: residual model M: F -> D nn_stack (NNStack) nn (ConvNet or GenericEncoder) support (np.array): support mask for nn path (str): path to folder containing 'sars_*.pkl' files (as collected with collect_sars_to_disk) no_residuals (bool, False): whether to return residuals or dynamics in the RES column of the sares dataset. class_weigth (dict, None): passed to the get_sample_weight method test_sfadf (pd.DataFrame, None): compute the test SARES dataset from this dataset. """ if not path.endswith('/'): path += '/' files = glob.glob(path + 'sars_*.npy') print 'Got %s files' % len(files) while True: for idx, f in enumerate(files): sars = np.load(f) if shuffle: np.random.shuffle(sars) if idx > 0: sars = np.append(excess_sars, sars, axis=0) excess = len(sars) % batch_size if excess > 0: excess_sars = sars[-excess:] sars = sars[:-excess] else: excess_sars = sars[0:0] # just to preserve shapes nb_batches = len(sars) / batch_size # Compute residuals F, D = build_fd(nn_stack, nn, support, sars) RES = build_res(model, F, D, no_residuals=no_residuals) for i in range(nb_batches): start = i * batch_size stop = (i + 1) * batch_size S = pds_to_npa(sars[start:stop, 0]) A = pds_to_npa(sars[start:stop, 1]) if weights is not None: if callable(weights): # it's a PDF function sample_weight = 1. / weights( np.round(RES[start:stop], round_decimal).T) sample_weight /= scale_coeff else: # it's a class weight dict sample_weight = get_sample_weight( np.round(RES[start:stop], round_decimal), weights) # Preprocess data S = model.preprocess_state(S, binarize=binarize) if weights is not None: yield ([S, A], RES[start:stop], sample_weight) else: yield ([S, A], RES[start:stop])