def predict(self, X, verbose=False): '''Runs the entire prediction procedure, updating internal tracking of wl_preds and Yhat, and returns the randomly chosen Yhat Args: X (list): A list of the covariates of the current data point. Float for numerical, string for categorical. Categorical data must have been in initial dataset Returns: Yhat (string): The final class prediction ''' self.X = np.array(X) # Initialize values expert_votes = np.zeros(self.num_classes) expert_votes_mat = np.empty([self.num_wls, self.num_classes]) for i in xrange(self.num_wls): data_indices = self.data_indices[i] pred_inst = self.make_cov_instance(self.X[data_indices]) # Get our new week learner prediction and our new expert prediction pred_probs = \ self.weaklearners[i].distribution_for_instance(pred_inst) pred_probs = np.array(pred_probs) # if self.loss == 'zero_one': # _max = max(pred_probs) # tmp = [i for i in range(self.num_classes) if \ # pred_probs[i] > _max-0.001] # label = np.random.choice(tmp, 1)[0] # pred_probs = np.zeros(self.num_classes) # pred_probs[label] = 1 self.wl_preds[i, :] = pred_probs if verbose is True: print i, pred_probs expert_votes += self.wl_weights[i] * pred_probs expert_votes_mat[i, :] = expert_votes if self.loss == 'zero_one': pred_index = -1 else: tmp = self.expert_weights / sum(self.expert_weights) pred_index = np.random.choice(range(self.num_wls), p=tmp) self.Yhat = expert_votes_mat[pred_index, :] self.topkYhat = utils.topk(self.Yhat, self.k) self.expert_votes_mat = expert_votes_mat self.Ytilde = self.generate_random_ranking(self.Yhat, self.topkYhat) self.topkYtilde = utils.topk(self.Ytilde, self.k) return self.Ytilde
def benchmark_gtopk_sparse_allreduce(): logger.setLevel(logging.DEBUG) np.set_printoptions(precision=3) comm = MPI.COMM_WORLD rank = comm.rank #np.random.seed(rank) size = 8 ratio = 0.5 tensor = np.random.rand(size).astype(np.float32) k = int(tensor.size * ratio) indexes, values = utils.topk(tensor, k) #indexes, values = topk(tensor, k) #logger.info('topk[%d]%s', rank, values) tmp = tensor[indexes] tensor.fill(0.) tensor[indexes] = tmp logger.debug('[%d]%s', rank, tensor) storage = {} #t = gtopk_sparse_allreduce(comm, tmp, storage=storage, indexes=indexes) result, global_indexes, included_indexes = gtopk_sparse_recursive_allreduce( comm, tmp, storage=storage, indexes=indexes, dtype=np.float32) iteration = 10 stime = time.time() logger.debug('[%d]value: %s, \n indexes: %s, \n included_indexes: %s', rank, result, global_indexes, included_indexes) #for i in range(iteration): # t,_ = gtopk_sparse_allreduce(comm, tmp, storage=storage, indexes=indexes) total_time = time.time() - stime
def test_pair_prob(): data_source = 'mediamill_reduced' k = 97 rho = 0.5 d = 2 A = TopkAdaBoostOLM(data_source=data_source, k=k, rho=rho, d=d) A.topkYhat = set(range(k)) A.Yhat = np.flip(np.arange(101)) label_a = 98 label_b = 99 M = 5000000 counts = 0.0 expected = A.pair_prob(label_a, label_b) print('beginning monte-carlo') for i in np.arange(M): rand_ranking = A.generate_random_ranking(A.Yhat, A.topkYhat) topk_rand_ranking = utils.topk(rand_ranking, k) if label_a in topk_rand_ranking and label_b in topk_rand_ranking: counts += 1.0 result = counts / M print('num_classes: ', A.num_classes) print('estimation: ', result) print('calculated: ', expected)
def update_A(self, sess, init=False): if init: A_values = np.ones(self.dataset.kg.shape[0] + self.dataset.train_edgelist.shape[0]) else: feed_dict = { self.h: self.dataset.kg[:, 0], self.r: self.dataset.kg[:, 1], self.pos_t: self.dataset.kg[:, 2], self.users: self.dataset.train_edgelist[:, 0], self.pos_items: self.dataset.train_edgelist[:, 1], self.dropout: 0.0, } if self.args.kg_ui: feed_dict = { self.h: self.dataset.fkg[:, 0], self.r: self.dataset.fkg[:, 1], self.pos_t: self.dataset.fkg[:, 2], self.dropout: 0.0, } A_values = np.hstack(sess.run(self.kg_pi, feed_dict=feed_dict)) else: A_values = np.hstack( sess.run([self.kg_pi, self.pos_score_e], feed_dict=feed_dict)) self.a_tensor = sess.run(self.A_norm, feed_dict={self.A_values: A_values}) self.A_in = self.sptensor2mat(self.a_tensor) if self.model_type in ['EKGCN', 'EKGCN_g']: self.adjlist, self.datalist = topk(self.A_in, self.args.max_degree)
def benchmark_gtopk_sparse_allreduce(): logger.setLevel(logging.INFO) comm = MPI.COMM_WORLD rank = comm.rank #np.random.seed(rank) size = 25 * 1024 * 1024 ratio = 0.001 tensor = np.random.rand(size).astype(np.float32) k = int(tensor.size * ratio) indexes, values = utils.topk(tensor, k) #indexes, values = topk(tensor, k) #logger.info('topk[%d]%s', rank, values) tmp = tensor[indexes] tensor.fill(0.) tensor[indexes] = tmp logger.debug('[%d]%s', rank, tensor) storage = {} t = gtopk_sparse_allreduce(comm, tensor, storage=storage, indexes=indexes) iteration = 10 stime = time.time() for i in range(iteration): t, _ = gtopk_sparse_allreduce(comm, tensor, storage=storage, indexes=indexes) total_time = time.time() - stime logger.info('average time: %f', total_time / iteration)
def forward(self, graph: dgl.DGLGraph, feature: torch.Tensor): score = self.score_layer(graph, feature).squeeze() perm, next_batch_num_nodes = topk( score, self.ratio, get_batch_id(graph.batch_num_nodes()), graph.batch_num_nodes()) feature = feature[perm] * self.non_linearity(score[perm]).view(-1, 1) graph = dgl.node_subgraph(graph, perm) # node_subgraph currently does not support batch-graph, # the 'batch_num_nodes' of the result subgraph is None. # So we manually set the 'batch_num_nodes' here. # Since global pooling has nothing to do with 'batch_num_edges', # we can leave it to be None or unchanged. graph.set_batch_num_nodes(next_batch_num_nodes) return graph, feature, perm
def run_exp(exp_info, data): af = data['all_feature'] qf = data['query_feature'] an = data['all_name'] qn = data['query_name'] metric_name, metric = exp_info['metric'] distance = metric(af, qf) closest = topk(distance, K, axis=0) n_query = qf.shape[0] precision = [] for i in range(n_query): index = closest[:, i] matches = [] p = Precision(qn[i]) for idx in index: matches.append((an[idx], distance[idx, i])) p.check(an[idx]) save_match_result(qn[i], matches, exp_info) precision.append((qn[i], p.get_precision())) avg_precision = sum([p[1] for p in precision]) / len(precision) save_precision_result(precision, avg_precision, exp_info)
def topk_sparse_allreduce(comm, sparse_tensor, storage, indexes=None, dtype=np.float32): tensor = sparse_tensor if indexes is None: k = int(tensor.size * 0.01) indexes, values = utils.topk(tensor, k) else: if not (type(indexes) is np.ndarray): indexes = indexes.cpu().numpy().astype(np.uint32) k = len(indexes) values = tensor #[indexes] num_workers = comm.size if storage is not None and 'values_1d' in storage: values_1d = storage['values_1d'] indexes_1d = storage['indexes_1d'] result = storage['result'] else: values_1d = np.zeros(k * num_workers, dtype=np.float32) indexes_1d = np.zeros(k * num_workers, dtype=np.uint32) result = np.zeros_like(tensor) storage['values_1d'] = values_1d storage['indexes_1d'] = indexes_1d storage['result'] = result if dtype != np.float32: values_1d = values_1d.astype(dtype) result.fill(0) if len(indexes) == 0: return result, None nnz = k comm.Allgather(values, values_1d[:num_workers * nnz]) comm.Allgather(indexes, indexes_1d[:num_workers * nnz]) return values_1d, indexes_1d, None #result, None
def val_step(x_val, y_val, step, writer=None): feed_dict = { network.inputs: x_val, network.labels: y_val, network.dropout_keep_prob: 1.0 } step, summaries, loss, scores = sess.run( [global_step, val_summary_op, network.loss, network.scores], feed_dict) time_str = datetime.datetime.now().isoformat() if step % FLAGS.checkpoint_every == 0: prec_rec = topk(sorted_like_preds=list( np.array(y_val)[np.argsort(scores)]), sorted_like_groundtrouth=list(np.sort(y_val)), step=step, res_dir=RESULTS_DIR) else: prec_rec = '' mAP, acc = rank2(scores, y_val, step=step, res_dir=RESULTS_DIR) print("{0}: step {1}, loss {2}, rank2 {3}, acc: {4}, prec_rec:{5}".format( time_str, step, loss, mAP, acc, prec_rec)) if writer: writer.add_summary(summaries, step)
def gtopk_sparse_allreduce(comm, sparse_tensor, storage=None, indexes=None, dtype=np.float32): """ 0: 0(0) <- 1(1), 2(2) <- 3(3), 4(4) <- 5(5), 6(6) <- 7(7) 1: 0(0) <- 2(1), 4(2) <- 6(3) 2: 0(0) <- 4(1) 0 -> 1 0 -> 2, 1 -> 3 0 -> 4, 1 -> 5, 2 -> 6, 3 -> 7 """ num_workers = comm.size rank = comm.rank tensor = sparse_tensor if indexes is None: k = int(tensor.size * 0.001) indexes, values = utils.topk(tensor, k) else: if not (type(indexes) is np.ndarray): indexes = indexes.cpu().numpy() k = len(indexes) values = tensor original_indexes = indexes send_values = np.concatenate((indexes, values)) send_values[0:k] = indexes.astype(np.uint32) send_values[k:2 * k] = values.astype(np.float32) if storage is not None and 'result_v2' in storage: recv_values = storage['result_v2'] if recv_values.size < k * 2: recv_values = np.zeros_like(send_values) if storage: storage['result_v2'] = recv_values recv_values = recv_values[0:k * 2] else: recv_values = np.zeros_like(send_values) if storage: storage['result_v2'] = recv_values num_round = int(np.log2(num_workers)) local_rank = rank exist_workers = num_workers step = 1 participate_ranks = range(0, num_workers, step) for i in range(num_round): if rank in participate_ranks: local_rank = participate_ranks.index(rank) if local_rank % 2 == 0: source = participate_ranks[local_rank + 1] comm.Recv([recv_values, MPI.FLOAT], source=source) tmp_indexes = recv_values[0:k].astype(np.int) tmp_values = recv_values[k:2 * k] cv, c1, c2 = np.intersect1d(indexes, tmp_indexes, assume_unique=False, return_indices=True) values[c1] += tmp_values[c2] tmp_values[c2] = 0.0 tmp_c = np.concatenate((values, tmp_values)) tmp_topki, tmp_topkv = utils.topk(tmp_c, k) first_array_indexes = tmp_topki[tmp_topki < k] second_array_indexes = tmp_topki[tmp_topki >= k] - k indexes = np.concatenate((indexes[first_array_indexes], tmp_indexes[second_array_indexes])) values = np.concatenate((values[first_array_indexes], tmp_values[second_array_indexes])) send_values = np.concatenate((indexes, values)) send_values[0:k] = indexes.astype(np.uint32) send_values[k:2 * k] = values.astype(np.float32) else: target = participate_ranks[local_rank - 1] logger.debug('[round:%d], %d(%d)->%d(%d)', i, rank, local_rank, target, local_rank - 1) comm.Send([send_values, MPI.FLOAT], dest=target) exist_workers //= 2 step *= 2 participate_ranks = range(0, num_workers, step) comm.Barrier() if rank == 0: send_values = np.concatenate((indexes, values)) indexes = indexes.astype(np.uint32) values = values.astype(np.float32) send_values[0:k] = indexes send_values[k:2 * k] = values else: send_values = recv_values[0:2 * k] comm.Bcast(send_values, root=0) tensor.fill(0.) if rank != 0: tmp_indexes = send_values[0:k].astype(np.uint32) tmp_values = send_values[k:2 * k].astype(np.float32) values = tmp_values indexes = tmp_indexes cv, c1, c2 = np.intersect1d(original_indexes, indexes, assume_unique=False, return_indices=True) included_indexes = c1 return values, indexes, included_indexes # final selected values and indexes
def gtopk_sparse_recursive_allreduce(comm, sparse_tensor, storage=None, indexes=None, dtype=np.float32): """ 0: 0(0) <-> 1(1), 2(2) <-> 3(3), 4(4) <-> 5(5), 6(6) <-> 7(7) 1: 0 <-> 2, 1 <-> 3, 4 <-> 6, 5 <-> 7 2: 0 <-> 4, 1 <-> 5, 2 <-> 6, 3 <-> 7 """ num_workers = comm.size rank = comm.rank tensor = sparse_tensor if indexes is None: k = int(tensor.size * 0.001) indexes, values = utils.topk(tensor, k) else: if not (type(indexes) is np.ndarray): indexes = indexes.cpu().numpy() k = len(indexes) values = tensor original_indexes = indexes send_values = np.concatenate((indexes, values)) send_values[0:k] = indexes.astype(np.uint32) send_values[k:2 * k] = values.astype(np.float32) original_indexes = indexes original_order_indexes = np.arange(0, k) mask = np.ones(k, dtype=np.int) if storage is not None and 'result_v2' in storage: recv_values = storage['result_v2'] if recv_values.size < k * 2: recv_values = np.zeros_like(send_values) if storage: storage['result_v2'] = recv_values recv_values = recv_values[0:k * 2] else: recv_values = np.zeros_like(send_values) if storage: storage['result_v2'] = recv_values num_round = int(np.log2(num_workers)) peer_masks = np.zeros(num_workers, dtype=np.int) for i in range(num_round): peer_distance = 2**i for j in range(num_workers): local_rank = j % (2 * peer_distance) if local_rank < peer_distance: peer_masks[j] = 1 else: peer_masks[j] = -1 peer = peer_masks[rank] * peer_distance + rank logger.debug('Start round: %d [%d]<->[%d]', i, rank, peer) recv_req = comm.Irecv([recv_values, MPI.FLOAT], source=peer) send_req = comm.Isend([send_values, MPI.FLOAT], dest=peer) send_req.Wait() recv_req.Wait() if rank < peer: first_indexes = indexes first_values = values second_indexes = recv_values[0:k].astype(np.int) second_values = recv_values[k:2 * k] else: first_indexes = recv_values[0:k].astype(np.int) first_values = recv_values[k:2 * k] second_indexes = indexes second_values = values cv, c1, c2 = np.intersect1d(first_indexes, second_indexes, assume_unique=True, return_indices=True) first_values[c1] += second_values[c2] second_values[c2] = 0.0 tmp_c = np.concatenate((first_values, second_values)) tmp_topki, _ = utils.topk(tmp_c, k) first_array_indexes = tmp_topki[tmp_topki < k] second_array_indexes = tmp_topki[tmp_topki >= k] - k indexes = np.concatenate((first_indexes[first_array_indexes], second_indexes[second_array_indexes])) values = np.concatenate((first_values[first_array_indexes], second_values[second_array_indexes])) cv, c1, c2 = np.intersect1d(original_indexes, indexes, assume_unique=True, return_indices=True) diff = np.setdiff1d(original_order_indexes, c1, assume_unique=True) mask[diff] = 0 send_values = np.concatenate((indexes, values)) send_values[0:k] = indexes.astype(np.uint32) send_values[k:2 * k] = values.astype(np.float32) comm.Barrier() logger.debug('End round: %d: %s', rank, send_values) included_indexes = np.nonzero(mask)[0] values = values.astype(np.float32) indexes = indexes.astype(np.uint32) return values, indexes, included_indexes # final selected values and indexes
min_conf=0.01, max_conf=0.9, min_weight=3, max_weight=10) model.verbose = False start = time.time() error_history = [] history_counter = 0 total_unnorm_error = 0.0 for row in (train_rows * loops): X = row[:class_index] Y = row[class_index:] pred = model.predict(X) topk = utils.topk(pred, k) Yset = utils.label_array_to_set(Y) topkRel = topk.intersection(Yset) model.update(topkRel) total_unnorm_error += utils.unnormalized_rank_loss(pred, Yset) history_counter += 1.0 error_history.append(total_unnorm_error / history_counter) mid = time.time() cum_error = 0 cum_unnorm_error = 0 model.verbose = True for row in test_rows:
def get_bboxes_single(cls_score_list, bbox_pred_list, mlvl_anchors, img_shape, scale_factor, cfg, rescale=False): assert len(cls_score_list) == len(bbox_pred_list) == len(mlvl_anchors) mlvl_bboxes = [] mlvl_scores = [] # ############# add ############# use_sigmoid_cls = False cls_out_channels = 2 target_means = (.0, .0, .0, .0) target_stds = (0.1, 0.1, 0.2, 0.2) # ############# add ############# for cls_score, bbox_pred, anchors in zip(cls_score_list, bbox_pred_list, mlvl_anchors): assert cls_score.shape[-2:] == bbox_pred.shape[-2:] cls_score = np.transpose(cls_score, (1, 2, 0)).reshape(-1, cls_out_channels) if use_sigmoid_cls: scores = sigmoid(cls_score) else: scores = softmax(cls_score) bbox_pred = np.transpose(bbox_pred, (1, 2, 0)).reshape(-1, 4) nms_pre = cfg.get('nms_pre', -1) if 0 < nms_pre < scores.shape[0]: # Get maximum scores for foreground classes. if use_sigmoid_cls: max_scores = scores.max(axis=1) else: max_scores, _ = scores[:, 1:].max(axis=1) topk_inds = topk(max_scores, nms_pre, axis=1) anchors = anchors[topk_inds, :] bbox_pred = bbox_pred[topk_inds, :] scores = scores[topk_inds, :] bboxes = delta2bbox(anchors, bbox_pred, target_means, target_stds, img_shape) mlvl_bboxes.append(bboxes) mlvl_scores.append(scores) mlvl_bboxes = np.concatenate(mlvl_bboxes) mlvl_scores = np.concatenate(mlvl_scores) if use_sigmoid_cls: # Add a dummy background class to the front when using sigmoid padding = np.zeros((mlvl_scores.shape[0], 1), dtype=mlvl_scores.dtype) mlvl_scores = np.concatenate([padding, mlvl_scores], axis=1) from bbox_nms import multiclass_nms det_bboxes, det_labels = multiclass_nms(mlvl_bboxes, mlvl_scores, cfg.score_thr, cfg.nms, cfg.max_per_img) if rescale: det_bboxes[:, 0] *= scale_factor[0] det_bboxes[:, 1] *= scale_factor[1] det_bboxes[:, 2] *= scale_factor[0] det_bboxes[:, 3] *= scale_factor[1] return det_bboxes, det_labels
def forward(self, graph: DGLGraph, feat: Tensor, e_feat=None): # top-k pool first if e_feat is None: e_feat = torch.ones((graph.number_of_edges(), ), dtype=feat.dtype, device=feat.device) batch_num_nodes = graph.batch_num_nodes() x_score = self.calc_info_score(graph, feat, e_feat) perm, next_batch_num_nodes = topk(x_score, self.ratio, get_batch_id(batch_num_nodes), batch_num_nodes) feat = feat[perm] pool_graph = None if not self.sample or not self.sl: # pool graph graph.edata["e"] = e_feat pool_graph = dgl.node_subgraph(graph, perm) e_feat = pool_graph.edata.pop("e") pool_graph.set_batch_num_nodes(next_batch_num_nodes) # no structure learning layer, directly return. if not self.sl: return pool_graph, feat, e_feat, perm # Structure Learning if self.sample: # A fast mode for large graphs. # In large graphs, learning the possible edge weights between each # pair of nodes is time consuming. To accelerate this process, # we sample it's K-Hop neighbors for each node and then learn the # edge weights between them. # first build multi-hop graph row, col = graph.all_edges() num_nodes = graph.num_nodes() scipy_adj = scipy.sparse.coo_matrix( (e_feat.detach().cpu(), (row.detach().cpu(), col.detach().cpu())), shape=(num_nodes, num_nodes)) for _ in range(self.k_hop): two_hop = scipy_adj**2 two_hop = two_hop * (1e-5 / two_hop.max()) scipy_adj = two_hop + scipy_adj row, col = scipy_adj.nonzero() row = torch.tensor(row, dtype=torch.long, device=graph.device) col = torch.tensor(col, dtype=torch.long, device=graph.device) e_feat = torch.tensor(scipy_adj.data, dtype=torch.float, device=feat.device) # perform pooling on multi-hop graph mask = perm.new_full((num_nodes, ), -1) i = torch.arange(perm.size(0), dtype=torch.long, device=perm.device) mask[perm] = i row, col = mask[row], mask[col] mask = (row >= 0) & (col >= 0) row, col = row[mask], col[mask] e_feat = e_feat[mask] # add remaining self loops mask = row != col num_nodes = perm.size(0) # num nodes after pool loop_index = torch.arange(0, num_nodes, dtype=row.dtype, device=row.device) inv_mask = ~mask loop_weight = torch.full((num_nodes, ), 0, dtype=e_feat.dtype, device=e_feat.device) remaining_e_feat = e_feat[inv_mask] if remaining_e_feat.numel() > 0: loop_weight[row[inv_mask]] = remaining_e_feat e_feat = torch.cat([e_feat[mask], loop_weight], dim=0) row, col = row[mask], col[mask] row = torch.cat([row, loop_index], dim=0) col = torch.cat([col, loop_index], dim=0) # attention scores weights = (torch.cat([feat[row], feat[col]], dim=1) * self.att).sum(dim=-1) weights = F.leaky_relu(weights, self.negative_slop) + e_feat * self.lamb # sl and normalization sl_graph = dgl.graph((row, col)) if self.sparse: weights = edge_sparsemax(sl_graph, weights) else: weights = edge_softmax(sl_graph, weights) # get final graph mask = torch.abs(weights) > 0 row, col, weights = row[mask], col[mask], weights[mask] pool_graph = dgl.graph((row, col)) pool_graph.set_batch_num_nodes(next_batch_num_nodes) e_feat = weights else: # Learning the possible edge weights between each pair of # nodes in the pooled subgraph, relative slower. # construct complete graphs for all graph in the batch # use dense to build, then transform to sparse. # maybe there's more efficient way? batch_num_nodes = next_batch_num_nodes block_begin_idx = torch.cat([ batch_num_nodes.new_zeros(1), batch_num_nodes.cumsum(dim=0)[:-1] ], dim=0) block_end_idx = batch_num_nodes.cumsum(dim=0) dense_adj = torch.zeros( (pool_graph.num_nodes(), pool_graph.num_nodes()), dtype=torch.float, device=feat.device) for idx_b, idx_e in zip(block_begin_idx, block_end_idx): dense_adj[idx_b:idx_e, idx_b:idx_e] = 1. row, col = torch.nonzero(dense_adj).t().contiguous() # compute weights for node-pairs weights = (torch.cat([feat[row], feat[col]], dim=1) * self.att).sum(dim=-1) weights = F.leaky_relu(weights, self.negative_slop) dense_adj[row, col] = weights # add pooled graph structure to weight matrix pool_row, pool_col = pool_graph.all_edges() dense_adj[pool_row, pool_col] += self.lamb * e_feat weights = dense_adj[row, col] del dense_adj torch.cuda.empty_cache() # edge softmax/sparsemax complete_graph = dgl.graph((row, col)) if self.sparse: weights = edge_sparsemax(complete_graph, weights) else: weights = edge_softmax(complete_graph, weights) # get new e_feat and graph structure, clean up. mask = torch.abs(weights) > 1e-9 row, col, weights = row[mask], col[mask], weights[mask] e_feat = weights pool_graph = dgl.graph((row, col)) pool_graph.set_batch_num_nodes(next_batch_num_nodes) return pool_graph, feat, e_feat, perm
assert (np.allclose(addcmul(px, pw, dx), torch.addcmul(px_t, 1, pw_t, dx_t))) ndarray = np.random.rand(12, 15, 18) tensor = torch.tensor(ndarray) assert (np.array_equal( np.transpose(ndarray, (1, 0, 2)).reshape((-1, 12)), tensor.permute(1, 0, 2).reshape(-1, 12))) assert (np.allclose(np.max(ndarray, axis=1), tensor.max(dim=1)[0].numpy())) dists = np.random.permutation(np.arange(30)).reshape(6, 5) tensor = torch.tensor(dists) _, idx = tensor.topk(2) assert (np.array_equal(dists[topk(dists, 2, axis=1)], tensor[idx].numpy())) ndarray = np.random.rand(12, 15, 18, 21) tensor = torch.tensor(ndarray) assert (np.array_equal( tensor.new_tensor([0, 0, 0, 0]).repeat(1, tensor.size(1) // 4).numpy(), np.tile(np.array([0, 0, 0, 0], dtype=ndarray.dtype), (1, tensor.size(1) // 4)))) assert (np.array_equal( tensor.unsqueeze(1).numpy(), np.expand_dims(ndarray, axis=1))) assert (np.array_equal(ndarray.reshape(ndarray.shape), tensor.expand_as(tensor).numpy()))