Python numpy.arange() Examples
The following are 30 code examples for showing how to use numpy.arange(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: libTLDA Author: wmkouw File: tcpr.py License: MIT License | 6 votes |
def add_intercept(self, X): """Add 1's to data as last features.""" # Data shape N, D = X.shape # Check if there's not already an intercept column if np.any(np.sum(X, axis=0) == N): # Report print('Intercept is not the last feature. Swapping..') # Find which column contains the intercept intercept_index = np.argwhere(np.sum(X, axis=0) == N) # Swap intercept to last X = X[:, np.setdiff1d(np.arange(D), intercept_index)] # Add intercept as last column X = np.hstack((X, np.ones((N, 1)))) # Append column of 1's to data, and increment dimensionality return X, D+1
Example 2
Project: osqf2015 Author: mvaz File: stock.py License: MIT License | 6 votes |
def create(clz): """One-time creation of app's objects. This function is called once, and is responsible for creating all objects (plots, datasources, etc) """ self = clz() n_vals = 1000 self.source = ColumnDataSource( data=dict( top=[], bottom=0, left=[], right=[], x= np.arange(n_vals), values= np.random.randn(n_vals) )) # Generate a figure container self.stock_plot = clz.create_stock(self.source) self.update_data() self.children.append(self.stock_plot)
Example 3
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: snippets.py License: MIT License | 6 votes |
def generate_anchors_pre(height, width, feat_stride, anchor_scales=(8,16,32), anchor_ratios=(0.5,1,2)): """ A wrapper function to generate anchors given different scales Also return the number of anchors in variable 'length' """ anchors = generate_anchors(ratios=np.array(anchor_ratios), scales=np.array(anchor_scales)) A = anchors.shape[0] shift_x = np.arange(0, width) * feat_stride shift_y = np.arange(0, height) * feat_stride shift_x, shift_y = np.meshgrid(shift_x, shift_y) shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose() K = shifts.shape[0] # width changes faster, so here it is H, W, C anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2)) anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False) length = np.int32(anchors.shape[0]) return anchors, length
Example 4
Project: deep-siamese-text-similarity Author: dhwajraj File: input_helpers.py License: MIT License | 6 votes |
def batch_iter(self, data, batch_size, num_epochs, shuffle=True): """ Generates a batch iterator for a dataset. """ data = np.asarray(data) print(data) print(data.shape) data_size = len(data) num_batches_per_epoch = int(len(data)/batch_size) + 1 for epoch in range(num_epochs): # Shuffle the data at each epoch if shuffle: shuffle_indices = np.random.permutation(np.arange(data_size)) shuffled_data = data[shuffle_indices] else: shuffled_data = data for batch_num in range(num_batches_per_epoch): start_index = batch_num * batch_size end_index = min((batch_num + 1) * batch_size, data_size) yield shuffled_data[start_index:end_index]
Example 5
Project: cgp-cnn Author: sg-nm File: cgp.py License: MIT License | 6 votes |
def active_net_list(self): net_list = [["input", 0, 0]] active_cnt = np.arange(self.net_info.input_num + self.net_info.node_num + self.net_info.out_num) active_cnt[self.net_info.input_num:] = np.cumsum(self.is_active) for n, is_a in enumerate(self.is_active): if is_a: t = self.gene[n][0] if n < self.net_info.node_num: # intermediate node type_str = self.net_info.func_type[t] else: # output node type_str = self.net_info.out_type[t] connections = [active_cnt[self.gene[n][i+1]] for i in range(self.net_info.max_in_num)] net_list.append([type_str] + connections) return net_list # CGP with (1 + \lambda)-ES
Example 6
Project: disentangling_conditional_gans Author: zalandoresearch File: dataset_tool.py License: MIT License | 6 votes |
def create_mnist(tfrecord_dir, mnist_dir): print('Loading MNIST from "%s"' % mnist_dir) import gzip with gzip.open(os.path.join(mnist_dir, 'train-images-idx3-ubyte.gz'), 'rb') as file: images = np.frombuffer(file.read(), np.uint8, offset=16) with gzip.open(os.path.join(mnist_dir, 'train-labels-idx1-ubyte.gz'), 'rb') as file: labels = np.frombuffer(file.read(), np.uint8, offset=8) images = images.reshape(-1, 1, 28, 28) images = np.pad(images, [(0,0), (0,0), (2,2), (2,2)], 'constant', constant_values=0) assert images.shape == (60000, 1, 32, 32) and images.dtype == np.uint8 assert labels.shape == (60000,) and labels.dtype == np.uint8 assert np.min(images) == 0 and np.max(images) == 255 assert np.min(labels) == 0 and np.max(labels) == 9 onehot = np.zeros((labels.size, np.max(labels) + 1), dtype=np.float32) onehot[np.arange(labels.size), labels] = 1.0 with TFRecordExporter(tfrecord_dir, images.shape[0]) as tfr: order = tfr.choose_shuffled_order() for idx in range(order.size): tfr.add_image(images[order[idx]]) tfr.add_labels(onehot[order]) #----------------------------------------------------------------------------
Example 7
Project: disentangling_conditional_gans Author: zalandoresearch File: dataset_tool.py License: MIT License | 6 votes |
def create_cifar100(tfrecord_dir, cifar100_dir): print('Loading CIFAR-100 from "%s"' % cifar100_dir) import pickle with open(os.path.join(cifar100_dir, 'train'), 'rb') as file: data = pickle.load(file, encoding='latin1') images = data['data'].reshape(-1, 3, 32, 32) labels = np.array(data['fine_labels']) assert images.shape == (50000, 3, 32, 32) and images.dtype == np.uint8 assert labels.shape == (50000,) and labels.dtype == np.int32 assert np.min(images) == 0 and np.max(images) == 255 assert np.min(labels) == 0 and np.max(labels) == 99 onehot = np.zeros((labels.size, np.max(labels) + 1), dtype=np.float32) onehot[np.arange(labels.size), labels] = 1.0 with TFRecordExporter(tfrecord_dir, images.shape[0]) as tfr: order = tfr.choose_shuffled_order() for idx in range(order.size): tfr.add_image(images[order[idx]]) tfr.add_labels(onehot[order]) #----------------------------------------------------------------------------
Example 8
Project: neural-fingerprinting Author: StephanZheng File: test_attacks.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_np_targeted_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), np.random.randint(0, 1, 100)] = 1 x_adv = self.attack.generate_np(x_val, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y_target=feed_labs) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(np.argmax(feed_labs, axis=1) == new_labs) > 0.9)
Example 9
Project: neural-fingerprinting Author: StephanZheng File: test_attacks.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) orig_labs = np.argmax(self.sess.run(self.model(x_val)), axis=1) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), orig_labs] = 1 x = tf.placeholder(tf.float32, x_val.shape) y = tf.placeholder(tf.float32, feed_labs.shape) x_adv_p = self.attack.generate(x, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y=y) self.assertEqual(x_val.shape, x_adv_p.shape) x_adv = self.sess.run(x_adv_p, {x: x_val, y: feed_labs}) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(orig_labs == new_labs) < 0.1)
Example 10
Project: neural-fingerprinting Author: StephanZheng File: test_attacks.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_np_targeted_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), np.random.randint(0, 1, 100)] = 1 x_adv = self.attack.generate_np(x_val, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y_target=feed_labs) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(np.argmax(feed_labs, axis=1) == new_labs) > 0.9)
Example 11
Project: neural-fingerprinting Author: StephanZheng File: test_attacks.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) orig_labs = np.argmax(self.sess.run(self.model(x_val)), axis=1) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), orig_labs] = 1 x = tf.placeholder(tf.float32, x_val.shape) y = tf.placeholder(tf.float32, feed_labs.shape) x_adv_p = self.attack.generate(x, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y=y) self.assertEqual(x_val.shape, x_adv_p.shape) x_adv = self.sess.run(x_adv_p, {x: x_val, y: feed_labs}) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(orig_labs == new_labs) < 0.1)
Example 12
Project: neural-fingerprinting Author: StephanZheng File: test_attacks.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_targeted_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), np.random.randint(0, 1, 100)] = 1 x = tf.placeholder(tf.float32, x_val.shape) y = tf.placeholder(tf.float32, feed_labs.shape) x_adv_p = self.attack.generate(x, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y_target=y) self.assertEqual(x_val.shape, x_adv_p.shape) x_adv = self.sess.run(x_adv_p, {x: x_val, y: feed_labs}) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(np.argmax(feed_labs, axis=1) == new_labs) > 0.9)
Example 13
Project: neural-fingerprinting Author: StephanZheng File: utils.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def to_categorical(y, num_classes=None): """ Converts a class vector (integers) to binary class matrix. This is adapted from the Keras function with the same name. :param y: class vector to be converted into a matrix (integers from 0 to num_classes). :param num_classes: num_classes: total number of classes. :return: A binary matrix representation of the input. """ y = np.array(y, dtype='int').ravel() if not num_classes: num_classes = np.max(y) + 1 warnings.warn("FutureWarning: the default value of the second" "argument in function \"to_categorical\" is deprecated." "On 2018-9-19, the second argument" "will become mandatory.") n = y.shape[0] categorical = np.zeros((n, num_classes)) categorical[np.arange(n), y] = 1 return categorical
Example 14
Project: Black-Box-Audio Author: rtaori File: tf_logits.py License: MIT License | 5 votes |
def compute_mfcc(audio, **kwargs): """ Compute the MFCC for a given audio waveform. This is identical to how DeepSpeech does it, but does it all in TensorFlow so that we can differentiate through it. """ batch_size, size = audio.get_shape().as_list() audio = tf.cast(audio, tf.float32) # 1. Pre-emphasizer, a high-pass filter audio = tf.concat((audio[:, :1], audio[:, 1:] - 0.97*audio[:, :-1], np.zeros((batch_size,1000),dtype=np.float32)), 1) # 2. windowing into frames of 320 samples, overlapping windowed = tf.stack([audio[:, i:i+400] for i in range(0,size-320,160)],1) # 3. Take the FFT to convert to frequency space ffted = tf.spectral.rfft(windowed, [512]) ffted = 1.0 / 512 * tf.square(tf.abs(ffted)) # 4. Compute the Mel windowing of the FFT energy = tf.reduce_sum(ffted,axis=2)+1e-30 filters = np.load("filterbanks.npy").T feat = tf.matmul(ffted, np.array([filters]*batch_size,dtype=np.float32))+1e-30 # 5. Take the DCT again, because why not feat = tf.log(feat) feat = tf.spectral.dct(feat, type=2, norm='ortho')[:,:,:26] # 6. Amplify high frequencies for some reason _,nframes,ncoeff = feat.get_shape().as_list() n = np.arange(ncoeff) lift = 1 + (22/2.)*np.sin(np.pi*n/22) feat = lift*feat width = feat.get_shape().as_list()[1] # 7. And now stick the energy next to the features feat = tf.concat((tf.reshape(tf.log(energy),(-1,width,1)), feat[:, :, 1:]), axis=2) return feat
Example 15
Project: indras_net Author: gcallah File: display_methods.py License: GNU General Public License v3.0 | 5 votes |
def draw_graph(self, data_points, varieties): """ Draw all elements of the graph. """ self.fig, self.ax = plt.subplots() x = np.arange(0, data_points) self.create_lines(x, self.ax, varieties) self.ax.legend() self.ax.set_title(self.title)
Example 16
Project: libTLDA Author: wmkouw File: tcpr.py License: MIT License | 5 votes |
def remove_intercept(self, X): """Remove 1's from data as last features.""" # Data shape N, D = X.shape # Find which column contains the intercept intercept_index = [] for d in range(D): if np.all(X[:, d] == 0): intercept_index.append(d) # Remove intercept columns X = X[:, np.setdiff1d(np.arange(D), intercept_index)] return X, D-len(intercept_index)
Example 17
Project: libTLDA Author: wmkouw File: tcpr.py License: MIT License | 5 votes |
def project_simplex(self, v, z=1.0): """ Project vector onto simplex using sorting. Reference: "Efficient Projections onto the L1-Ball for Learning in High Dimensions (Duchi, Shalev-Shwartz, Singer, Chandra, 2006)." Parameters ---------- v : array vector to be projected (n dimensions by 0) z : float constant (def: 1.0) Returns ------- w : array projected vector (n dimensions by 0) """ # Number of dimensions n = v.shape[0] # Sort vector mu = np.sort(v, axis=0)[::-1] # Find rho C = np.cumsum(mu) - z j = np.arange(n) + 1 rho = j[mu - C/j > 0][-1] # Define theta theta = C[mu - C/j > 0][-1] / float(rho) # Subtract theta from original vector and cap at 0 w = np.maximum(v - theta, 0) # Return projected vector return w
Example 18
Project: EDeN Author: fabriziocosta File: __init__.py License: MIT License | 5 votes |
def heatmap(values, xlabel, ylabel, xticklabels, yticklabels, cmap=None, vmin=None, vmax=None, ax=None, fmt="%0.2f"): """heatmap.""" if ax is None: ax = plt.gca() # plot the mean cross-validation scores img = ax.pcolor(values, cmap=cmap, vmin=vmin, vmax=vmax) img.update_scalarmappable() ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_xticks(np.arange(len(xticklabels)) + .5) ax.set_yticks(np.arange(len(yticklabels)) + .5) ax.set_xticklabels(xticklabels) ax.set_yticklabels(yticklabels) ax.set_aspect(1) for p, color, value in zip(img.get_paths(), img.get_facecolors(), img.get_array()): x, y = p.vertices[:-2, :].mean(0) if np.mean(color[:3]) > 0.5: c = 'k' else: c = 'w' ax.text(x, y, fmt % value, color=c, ha="center", va="center") return img
Example 19
Project: EDeN Author: fabriziocosta File: link_prediction_utils.py License: MIT License | 5 votes |
def show_graph(g, vertex_color='typeof', size=15, vertex_label=None): """show_graph.""" degrees = [len(g.neighbors(u)) for u in g.nodes()] print(('num nodes=%d' % len(g))) print(('num edges=%d' % len(g.edges()))) print(('num non edges=%d' % len(list(nx.non_edges(g))))) print(('max degree=%d' % max(degrees))) print(('median degree=%d' % np.percentile(degrees, 50))) draw_graph(g, size=size, vertex_color=vertex_color, vertex_label=vertex_label, vertex_size=200, edge_label=None) # display degree distribution size = int((max(degrees) - min(degrees)) / 1.5) plt.figure(figsize=(size, 3)) plt.title('Degree distribution') _bins = np.arange(min(degrees), max(degrees) + 2) - .5 n, bins, patches = plt.hist(degrees, _bins, alpha=0.3, facecolor='navy', histtype='bar', rwidth=0.8, edgecolor='k') labels = np.array([str(int(i)) for i in n]) for xi, yi, label in zip(bins, n, labels): plt.text(xi + 0.5, yi, label, ha='center', va='bottom') plt.xticks(bins + 0.5) plt.xlim((min(degrees) - 1, max(degrees) + 1)) plt.ylim((0, max(n) * 1.1)) plt.xlabel('Node degree') plt.ylabel('Counts') plt.grid(linestyle=":") plt.show()
Example 20
Project: osqf2015 Author: mvaz File: stock.py License: MIT License | 5 votes |
def update_data(self): """Called each time that any watched property changes. This updates the sin wave data with the most recent values of the sliders. This is stored as two numpy arrays in a dict into the app's data histogram_source property. """ logging.debug("update_data") n_vals = 1000 self.source.data = dict(top=hist, bottom=0, left=0, right = 0, x=np.arange(n_vals), values=np.random.randn(n_vals))
Example 21
Project: fenics-topopt Author: zfergus File: boundary_conditions.py License: MIT License | 5 votes |
def get_fixed_nodes(self): # Return a list of fixed nodes for the problem dofs = np.arange(2 * (self.nelx + 1) * (self.nely + 1)) fixed = np.union1d(dofs[0:2 * (self.nely + 1):2], np.array([2 * (self.nelx + 1) * (self.nely + 1) - 1])) return fixed
Example 22
Project: fenics-topopt Author: zfergus File: problem.py License: MIT License | 5 votes |
def __init__(self, nelx, nely, penal, bc): # Problem size self.nelx = nelx self.nely = nely # Max and min stiffness self.Emin = 1e-9 self.Emax = 1.0 # SIMP penalty self.penal = penal # dofs: self.ndof = 2 * (nelx + 1) * (nely + 1) # FE: Build the index vectors for the for coo matrix format. self.build_indices(nelx, nely) # BC's and support (half MBB-beam) dofs = np.arange(2 * (nelx + 1) * (nely + 1)) self.fixed = bc.get_fixed_nodes() self.free = np.setdiff1d(dofs, self.fixed) # Solution and RHS vectors self.f = bc.get_forces() self.u = np.zeros(self.f.shape) # Per element compliance self.ce = np.zeros(nely * nelx)
Example 23
Project: fenics-topopt Author: zfergus File: tower.py License: MIT License | 5 votes |
def get_forces(self): # Return the force vector for the problem topx_to_id = np.vectorize( lambda x: xy_to_id(x, 0, self.nelx, self.nely)) topx = 2 * topx_to_id(np.arange((self.nelx + 1) // 2)) + 1 f = np.zeros((2 * (self.nelx + 1) * (self.nely + 1), 1)) f[topx, 0] = -100 return f
Example 24
Project: fenics-topopt Author: zfergus File: boundary_conditions.py License: MIT License | 5 votes |
def get_fixed_nodes(self): # Return a list of fixed nodes for the problem dofs = np.arange(2 * (self.nelx + 1) * (self.nely + 1)) fixed = np.union1d(dofs[0:2 * (self.nely + 1):2], np.array([2 * (self.nelx + 1) * (self.nely + 1) - 1])) return fixed
Example 25
Project: fenics-topopt Author: zfergus File: problem.py License: MIT License | 5 votes |
def __init__(self, nelx, nely, penal, bc): # Problem size self.nelx = nelx self.nely = nely # Max and min stiffness self.Emin = 1e-9 self.Emax = 1.0 # SIMP penalty self.penal = penal # dofs: self.ndof = 2 * (nelx + 1) * (nely + 1) # FE: Build the index vectors for the for coo matrix format. self.build_indices(nelx, nely) # BC's and support (half MBB-beam) dofs = np.arange(2 * (nelx + 1) * (nely + 1)) self.fixed = bc.get_fixed_nodes() self.free = np.setdiff1d(dofs, self.fixed) # Solution and RHS vectors self.f = bc.get_forces() self.u = np.zeros(self.f.shape) # Per element compliance self.ce = np.zeros(nely * nelx)
Example 26
Project: fenics-topopt Author: zfergus File: L_bracket.py License: MIT License | 5 votes |
def get_fixed_nodes(self): """ Return a list of fixed nodes for the problem. """ x = np.arange(self.passive_min_x) topx_to_id = np.vectorize( lambda x: xy_to_id(x, 0, self.nelx, self.nely)) ids = topx_to_id(x) fixed = np.union1d(2 * ids, 2 * ids + 1) return fixed
Example 27
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: layer.py License: MIT License | 5 votes |
def _shuffle_roidb_inds(self): """Randomly permute the training roidb.""" # If the random flag is set, # then the database is shuffled according to system time # Useful for the validation set if self._random: st0 = np.random.get_state() millis = int(round(time.time() * 1000)) % 4294967295 np.random.seed(millis) if cfg.TRAIN.ASPECT_GROUPING: raise NotImplementedError ''' widths = np.array([r['width'] for r in self._roidb]) heights = np.array([r['height'] for r in self._roidb]) horz = (widths >= heights) vert = np.logical_not(horz) horz_inds = np.where(horz)[0] vert_inds = np.where(vert)[0] inds = np.hstack(( np.random.permutation(horz_inds), np.random.permutation(vert_inds))) inds = np.reshape(inds, (-1, 2)) row_perm = np.random.permutation(np.arange(inds.shape[0])) inds = np.reshape(inds[row_perm, :], (-1,)) self._perm = inds ''' else: self._perm = np.random.permutation(np.arange(len(self._roidb))) # Restore the random state if self._random: np.random.set_state(st0) self._cur = 0
Example 28
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: voc_eval.py License: MIT License | 5 votes |
def voc_ap(rec, prec, use_07_metric=False): """ ap = voc_ap(rec, prec, [use_07_metric]) Compute VOC AP given precision and recall. If use_07_metric is true, uses the VOC 07 11 point method (default:False). """ if use_07_metric: # 11 point metric ap = 0. for t in np.arange(0., 1.1, 0.1): if np.sum(rec >= t) == 0: p = 0 else: p = np.max(prec[rec >= t]) ap = ap + p / 11. else: # correct AP calculation # first append sentinel values at the end mrec = np.concatenate(([0.], rec, [1.])) mpre = np.concatenate(([0.], prec, [0.])) # compute the precision envelope for i in range(mpre.size - 1, 0, -1): mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i]) # to calculate area under PR curve, look for points # where X axis (recall) changes value i = np.where(mrec[1:] != mrec[:-1])[0] # and sum (\Delta recall) * prec ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) return ap
Example 29
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: generate_anchors.py License: MIT License | 5 votes |
def generate_anchors(base_size=16, ratios=[0.5, 1, 2], scales=2 ** np.arange(3, 6)): """ Generate anchor (reference) windows by enumerating aspect ratios X scales wrt a reference (0, 0, 15, 15) window. """ base_anchor = np.array([1, 1, base_size, base_size]) - 1 ratio_anchors = _ratio_enum(base_anchor, ratios) anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales) for i in range(ratio_anchors.shape[0])]) return anchors
Example 30
Project: controllable-text-attribute-transfer Author: Nrgeup File: model2.py License: Apache License 2.0 | 5 votes |
def __init__(self, d_model, dropout, max_len=5000): super(PositionalEncoding, self).__init__() self.dropout = nn.Dropout(p=dropout) # Compute the positional encodings once in log space. pe = torch.zeros(max_len, d_model) position = torch.arange(0, max_len).unsqueeze(1) div_term = torch.exp(torch.arange(0, d_model, 2) * -(math.log(10000.0) / d_model)) pe[:, 0::2] = torch.sin(position * div_term) pe[:, 1::2] = torch.cos(position * div_term) pe = pe.unsqueeze(0) self.register_buffer('pe', pe)