def compute_weight(inputs, sigma, similarity_type='Gauss', use_gpu=torch.cuda.is_available()): dist = torch.sum(torch.pow(inputs - inputs[:, :, 0].unsqueeze(2), 2), dim=1) / sigma if similarity_type == 'Gauss': Gauss_simi = torch.exp(-dist) Gauss_simi[:, 0] = torch.sum(Gauss_simi[:, 1:], dim=1) simi = torch.div(Gauss_simi, torch.sum(Gauss_simi, dim=1, keepdim=True)) elif similarity_type == 'Student-t': t_simi = torch(1, 1 + dist) t_simi[:, 0] = torch.sum(t_simi[:, 1:], dim=1) simi = torch.div(t_simi, torch.sum(t_simi, dim=1, keepdim=True)) elif similarity_type == 'inv-Guass': Gauss_simi = torch.exp(dist) Gauss_simi[:, 0] = torch.sum(Gauss_simi[:, 1:], dim=1) simi = torch.div(Gauss_simi, torch.sum(Gauss_simi, dim=1, keepdim=True)) simi[:, 0] = simi[:, 0] elif similarity_type == 'No_inform': N = inputs.size(-1) simi = torch.ones(1, N) / (N - 1) simi[0, 0] = 1 simi = torch.mul(torch.ones(inputs.size(0), 1), simi) simi = torch.div(simi, torch.sum(simi, dim=1, keepdim=True)) return simi.cuda()
def create_vectors(self, i_list, o_list, atomtypes, optypes): i_vector = torch(len(self.optypes)) o_vector = torch((len(self.atomtypes) * 2)) for line in i_list: y = line.split() if y[0] in self.optypes: i_vector[self.optypes.index(y[0])] = round(float(y[2]), 10) elif y[0] not in self.optypes: print(y[0] + " is not present in the optype list") else: i_vector[self.optypes.index(y[0])] = numpy.NaN print(y[0] + "does not exist in the list of optypes") for line in o_list: y = line.split() if (y[-1] == "atomtype"): if y[0] in self.atomtypes: o_vector[self.atomtypes.index(y[0])] = round( float(y[5]), 10) elif y[0] not in self.atomtypes: print(y[0] + " is not present in the atomtype list") else: print( y[0] + "does not exist in the list of atomtypes: ATOMTYPE secion" ) elif (y[-1] == "atom"): if y[1] in self.atomtypes: o_vector[self.atomtypes.index(y[1]) + len(self.atomtypes)] = round(float(y[6]), 5) else: print( y[1] + "does not exist in the list of atomtypes: ATOM secion") else: # print("Check the entry " + line) o_vector[self.atomtypes.index(y[0])] = numpy.NaN o_vector[self.atomtypes.index(y[0]) + len(self.atomtypes)] = numpy.NaN return (i_vector, o_vector)
def encode_torch(boxes, anchors): """ :param boxes: (N, 7 + ?) x, y, z, w, l, h, r, custom values, z is the box center in z-axis :param anchors: (N, 7 + ?) :return: """ box_ndim = anchors.shape[-1] # xa, ya, za, wa, la, ha, ra, *cas = np.split(anchors, box_ndim, axis=-1) # xg, yg, zg, wg, lg, hg, rg, *cgs = np.split(boxes, box_ndim, axis=-1) zg = boxes[:, 2] hg = boxes[:, 5] za = anchors[:, 2] ha = anchors[:, 5] gt = boxes gt_reverse_angle = boxes.clone() gt_reverse_angle[:, 6] = gt_reverse_angle[:, 6] + np.pi gt_corners = boxes3d_to_corners2d_lidar_torch(gt, ry_flag=True) gt_reverse_corners = boxes3d_to_corners2d_lidar_torch(gt_reverse_angle, ry_flag=True) anchor_corners = boxes3d_to_corners2d_lidar_torch(anchors, ry_flag=False) gt_anchor_dist = torch.norm(gt_corners - anchor_corners) gt_reverse_anchor_dist = torch(gt_reverse_angle - anchor_corners) if gt_anchor_dist <= gt_reverse_anchor_dist: corner_target = gt_corners - anchor_corners else: corner_target = gt_reverse_corners - anchor_corners #calculate z center # need to convert boxes to z-center format if cfg.MODEL.RPN.RPN_HEAD.TARGET_CONFIG.DIRECT_H_AND_Z: h_target = hg z_target = zg else: zg = zg + hg / 2 za = za + ha / 2 z_target = zg - za h_target = hg - ha return torch.cat([ corner_target, z_target.unsqueeze(dim=1), h_target.unsqueeze(dim=1) ], axis=-1)
def input_diversity(input_tensor): image_resize = 500 image_width = 128 image_height = 128 prob = 0.5 rnd = int((image_resize - image_width)*torch(())+image_width) rescaled = nn.functional.interpolate(input_tensor,size=rnd,mode='nearest') h_rem = image_resize - rnd w_rem = image_resize - rnd pad_top = int(h_rem*torch.rand(())) pad_bottom = h_rem - pad_top pad_left = int(w_rem*torch.rand(())) pad_right = w_rem-pad_left padded = nn.functional.pad(rescaled,(pad_left,pad_right,pad_top,pad_bottom),mode='constant',value=0.) padded = padded.view((input_tensor.shape[0],image_resize,image_resize,3)).permute(0,3,1,2) ret = padded if torch.rand((1))[0] > prob else input_tensor ret = nn.functional.interpolate(ret,size=image_height,mode='nearest')
return results_d, op_sets def mk_dirs(path): if not os.path.exists(path): os.makedirs(path) return True return False if __name__ == '__main__': test_models = ['resnet50', 'inception_v3', 'shufflenet', 'fcn', 'lstm'] model_path = './../models' img_path = os.path.join('./', 'elephant.jpg') # mkdirs mk_dirs(os.path.join(model_path, 'IR')) mk_dirs(os.path.join(model_path, 'tensorflow')) mk_dirs(os.path.join(model_path, 'PyTorch')) tf_err, tf_op_sets = tensorflow(test_models, model_path, img_path) torch_err, torch_op_sets = torch(test_models, model_path, img_path) for model in test_models: print('Model: {}'.format(model)) print('- Error: tf ({}) | torch ({})'.format(tf_err[model], torch_err[model])) print('- TF Ops: {}'.format(tf_op_sets[model])) print('- Torch Ops: {}'.format(torch_op_sets[model])) print('\n')
# 维度相等的就不说了 #tensor为空,导致不能广播 x1 = torch.empty((0, )) y1 = torch.empty(2, 2) z1 = x1 + y1 #维度不相同,但是维度存在且不相同,导致不能广播 x2 = torch.empty(2, 2) y2 = torch.empty(3, 2) z2 = x2 + y2 #可以广播的情况 x3 = torch.empty(5, 2, 2, 2) y3 = torch.empty(1, 2, 2) z3 = x2 + y2 #广播后的size是两个tensor中最大的值,z3的size是(5,2,2,2) #在本地操作是,例如add_ 操作,不能用广播改变其tensor大小 x4 = torch.empty(5, 2, 2, 2) y4 = torch.empty(1, 2, 2) x4.add_(y4) #可以执行,并没有改变x4的维度 y4.add_(x4) #就会报错,因为改变的是y4的维度 #BP中的广播 torch.add(torch.ones(4, 1), torch(4)) #会先产生一个(4,1) ,最后产生的是(4,4) #这种情况会导致BP出现错误,可以设置torch.utils.backcompat.broadcast_warning.enabled=True,会产生python警告 torch.utils.backcompat.broadcast_warning.enabled = True torch.add(torch.ones(4, 1), torch(4))
use_normalized = args.use_normalized print("training sample size: {}".format(N_Samples_Training)) print("testing sample size: {}".format(N_Samples_Testing)) print("use normalized:", use_normalized) prior_type = args.prior_type if prior_type == "scale_mixture": mixture_sigma1 = torch.tensor([math.exp(args.scale_mixture_log_sigma1)]) mixture_sigma2 = torch.tensor([math.exp(args.scale_mixture_log_sigma2)]) if use_cuda: mixture_sigma1 = mixture_sigma1.cuda() mixture_sigma2 = mixture_sigma2.cuda() mixture_pi = args.scale_mixture_pi print("prior distribution: scale mixture ({}, {}, {})".format(mixture_pi, mixture_sigma1, mixture_sigma2)) elif prior_type == "gaussian": gaussian_mean = torch.tensor([args.gaussian_mean]) gaussian_sigma = torch([math.exp(args.gaussian_log_sigma)]) if use_cuda: gaussian_mean = gaussian_mean.cuda() gaussian_sigma = gaussian_sigma.cuda() print("prior distribution: gaussian (mean: {}, sigma: {})".format(gaussian_mean, gaussian_sigma)) if experiment_name is None: experiment_name = "mnist_num_units_{}_optim_{}".format(N_units, optimizer_type) if network_type == 'standard': if use_dropout: experiment_name += "_standard_dropout_{}".format(dropout_rate) else: experiment_name += "_standard_SGD" elif network_type == 'bayesian': # maybe also add prior distribution parameters to the file name? experiment_name += "_bayesian_{}_num_samples_testing_{}".format(prior_type, N_Samples_Testing)
def train(self,v0,vk,ph0,phk): #Update Tensor of weights self.W += torch.mm(v0.t(),ph0) - torch(vk.t(),phk) self.b += torch.sum({v0 - vk},0) self.a += torch.sum({ph0 - phk},0)
def _initHidden(self, batch_size): return torch(4, batch_size, self.hidden_size).to(device)
def preprocess(image): device = torch('cuda') image = PIL.Image.fromarray(image) image = transforms.functional.to_tensor(image).to(device) image.sub_(mean[:, None, None]).div_(std[:, None, None]) return image[None, ...]
def initHidden(self): result = Variable(torch(1, 1, self.hidden_size)) if use_cuda: result = result.cuda() return result
labels_flat = labels.flatten() for label in np.unique(labels_flat): y_preds = preds_flat[labels_flat == label] y_true = labels_flat[labels_flat == label] print(f"Class: {label_dict_inverse[label]}") print(f"Accuracy: {len(y_preds[y_preds==label])}/{len(y_true)}\n") seed_val = 17 random.seed(seed_val) np.random.seed(seed_val) torch.manual_seed(seed_val) torch.cuda.manual_seed_all(seed_val) device = torch('cpu') model.to(device) for epoch in tqdm(range(1, epochs + 1)): model.train() loss_train_total = 0 progress_bar = tqdm(dataloader_train, des='Epoch {:1d}'.format(epoch), leave=False, disable=False) for batch in progress_bar:
return F.sigmoid(out) data = pd.read_csv('data/ex2data1.txt', header=None) X = Variable(torch.from_numpy(data.iloc[:, :2].as_matrix()).float()) Y = Variable( np.reshape(torch.from_numpy(data.iloc[:, 2].as_matrix()).float(), (-1, 1))) input_size = 2 num_class = 1 total_epoch = 250000 learning_rate = 0.0001 model = LogisticRegression(input_size, num_class) criterion = nn.BCELoss() optimizer = torch(model.parameters(), lr=learning_rate) # optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) loss_history = [] for epoch in range(total_epoch): output = model(X) optimizer.zero_grad() loss = criterion(output, Y) loss.backward() optimizer.step() if (epoch + 1) % 5 == 0: print('Epoch: [%d/%d], Loss: %.4f' % (epoch + 1, total_epoch, loss.data[0])) loss_history.append(loss.data[0])
def model(x): theta = sample('theta', dist.Uniform(torch(0.0), torch(1.0))) sample('x', dist.Bernoulli(theta), obs=x)