def predict_proba1(self, X): near_locs, near_dists = af.vision.nearest_neighbour(X, self._data, self._dim, \ self._num_nearest, self._match_type) weights = self._get_neighbor_weights(near_dists) top_labels = af.moddims(self._labels[near_locs], \ get_dims(near_locs)[0], get_dims(near_locs)[1]) accum_weights = af.scan_by_key( top_labels, weights) # reduce by key would be more ideal probs, _ = af.imax(accum_weights, dim=0) return probs.T
def _predict_proba(self, query, train_feats, train_labels, k, dist_type, weight_by_dist): near_locs, near_dists = af.vision.nearest_neighbour(query, train_feats, 1, \ k, dist_type) weights = self._get_neighbor_weights(near_dists, weight_by_dist, k) top_labels = af.moddims(train_labels[near_locs], \ near_locs.dims()[0], near_locs.dims()[1]) accum_weights = af.scan_by_key( top_labels, weights) # reduce by key would be more ideal probs, _ = af.imax(accum_weights, dim=0) return probs.T
def predict(self, X): near_locs, near_dists = af.vision.nearest_neighbour(X, self._data, self._dim, \ self._num_nearest, self._match_type) weights = self._get_neighbor_weights(near_dists) top_labels = af.moddims(self._labels[near_locs], \ get_dims(near_locs)[0], get_dims(near_locs)[1]) accum_weights = af.scan_by_key( top_labels, weights) # reduce by key would be more ideal _, max_weight_locs = af.imax(accum_weights, dim=0) pred_idxs = af.range(get_dims(accum_weights)[1]) * get_dims( accum_weights)[0] + max_weight_locs.T top_labels_flat = af.flat(top_labels) pred_classes = top_labels_flat[pred_idxs] return pred_classes
def _predict(self, query, train_feats, train_labels, k, dist_type, weight_by_dist): near_locs, near_dists = af.vision.nearest_neighbour(query, train_feats, 1, \ k, dist_type) weights = self._get_neighbor_weights(near_dists, weight_by_dist, k) top_labels = af.moddims(train_labels[near_locs], \ near_locs.dims()[0], near_locs.dims()[1]) accum_weights = af.scan_by_key( top_labels, weights) # reduce by key would be more ideal _, max_weight_locs = af.imax(accum_weights, dim=0) pred_idxs = af.range(accum_weights.dims() [1]) * accum_weights.dims()[0] + max_weight_locs.T top_labels_flat = af.flat(top_labels) pred_classes = top_labels_flat[pred_idxs] return pred_classes
def histogram_deposition(current_indices_flat, currents_flat, grid_elements): ''' function: histogram_deposition(current_indices_flat, currents_flat, grid) inputs: current_indices_flat, currents_flat, grid_elements current_indices_flat, currents_flat: They denote the indices and the currents to be deposited on the flattened current vector. grid_elements: The number of elements present the matrix/vector representing the currents. ''' # setting default indices and current for histogram deposition indices_fix = af.data.range(grid_elements + 1, dtype=af.Dtype.s64) currents_fix = 0 * af.data.range(grid_elements + 1, dtype=af.Dtype.f64) # Concatenating the indices and currents in a single vector combined_indices_flat = af.join(0, indices_fix, current_indices_flat) combined_currents_flat = af.join(0, currents_fix, currents_flat) # Sort by key operation indices, currents = af.sort_by_key(combined_indices_flat, combined_currents_flat, dim=0) # scan by key operation with default binary addition operation which sums up currents # for the respective indices Histogram_scan = af.scan_by_key(indices, currents) # diff1 operation to determine the uniques indices in the current diff1_op = af.diff1(indices, dim=0) # Determining the uniques indices for current deposition indices_unique = af.where(diff1_op > 0) # Determining the current vector J_flat = Histogram_scan[indices_unique] af.eval(J_flat) return J_flat
def simple_algorithm(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3, 3) k = af.constant(1, 3, 3, dtype=af.Dtype.u32) af.eval(k) print_func(af.sum(a), af.product(a), af.min(a), af.max(a), af.count(a), af.any_true(a), af.all_true(a)) display_func(af.sum(a, 0)) display_func(af.sum(a, 1)) display_func(af.product(a, 0)) display_func(af.product(a, 1)) display_func(af.min(a, 0)) display_func(af.min(a, 1)) display_func(af.max(a, 0)) display_func(af.max(a, 1)) display_func(af.count(a, 0)) display_func(af.count(a, 1)) display_func(af.any_true(a, 0)) display_func(af.any_true(a, 1)) display_func(af.all_true(a, 0)) display_func(af.all_true(a, 1)) display_func(af.accum(a, 0)) display_func(af.accum(a, 1)) display_func(af.scan(a, 0, af.BINARYOP.ADD)) display_func(af.scan(a, 1, af.BINARYOP.MAX)) display_func(af.scan_by_key(k, a, 0, af.BINARYOP.ADD)) display_func(af.scan_by_key(k, a, 1, af.BINARYOP.MAX)) display_func(af.sort(a, is_ascending=True)) display_func(af.sort(a, is_ascending=False)) b = (a > 0.1) * a c = (a > 0.4) * a d = b / c print_func(af.sum(d)); print_func(af.sum(d, nan_val=0.0)); display_func(af.sum(d, dim=0, nan_val=0.0)); val,idx = af.sort_index(a, is_ascending=True) display_func(val) display_func(idx) val,idx = af.sort_index(a, is_ascending=False) display_func(val) display_func(idx) b = af.randu(3,3) keys,vals = af.sort_by_key(a, b, is_ascending=True) display_func(keys) display_func(vals) keys,vals = af.sort_by_key(a, b, is_ascending=False) display_func(keys) display_func(vals) c = af.randu(5,1) d = af.randu(5,1) cc = af.set_unique(c, is_sorted=False) dd = af.set_unique(af.sort(d), is_sorted=True) display_func(cc) display_func(dd) display_func(af.set_union(cc, dd, is_unique=True)) display_func(af.set_union(cc, dd, is_unique=False)) display_func(af.set_intersect(cc, cc, is_unique=True)) display_func(af.set_intersect(cc, cc, is_unique=False))
def simple_algorithm(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3, 3) k = af.constant(1, 3, 3, dtype=af.Dtype.u32) af.eval(k) print_func(af.sum(a), af.product(a), af.min(a), af.max(a), af.count(a), af.any_true(a), af.all_true(a)) display_func(af.sum(a, 0)) display_func(af.sum(a, 1)) display_func(af.product(a, 0)) display_func(af.product(a, 1)) display_func(af.min(a, 0)) display_func(af.min(a, 1)) display_func(af.max(a, 0)) display_func(af.max(a, 1)) display_func(af.count(a, 0)) display_func(af.count(a, 1)) display_func(af.any_true(a, 0)) display_func(af.any_true(a, 1)) display_func(af.all_true(a, 0)) display_func(af.all_true(a, 1)) display_func(af.accum(a, 0)) display_func(af.accum(a, 1)) display_func(af.scan(a, 0, af.BINARYOP.ADD)) display_func(af.scan(a, 1, af.BINARYOP.MAX)) display_func(af.scan_by_key(k, a, 0, af.BINARYOP.ADD)) display_func(af.scan_by_key(k, a, 1, af.BINARYOP.MAX)) display_func(af.sort(a, is_ascending=True)) display_func(af.sort(a, is_ascending=False)) b = (a > 0.1) * a c = (a > 0.4) * a d = b / c print_func(af.sum(d)) print_func(af.sum(d, nan_val=0.0)) display_func(af.sum(d, dim=0, nan_val=0.0)) val, idx = af.sort_index(a, is_ascending=True) display_func(val) display_func(idx) val, idx = af.sort_index(a, is_ascending=False) display_func(val) display_func(idx) b = af.randu(3, 3) keys, vals = af.sort_by_key(a, b, is_ascending=True) display_func(keys) display_func(vals) keys, vals = af.sort_by_key(a, b, is_ascending=False) display_func(keys) display_func(vals) c = af.randu(5, 1) d = af.randu(5, 1) cc = af.set_unique(c, is_sorted=False) dd = af.set_unique(af.sort(d), is_sorted=True) display_func(cc) display_func(dd) display_func(af.set_union(cc, dd, is_unique=True)) display_func(af.set_union(cc, dd, is_unique=False)) display_func(af.set_intersect(cc, cc, is_unique=True)) display_func(af.set_intersect(cc, cc, is_unique=False))