Compare commits

...

2 Commits

Author SHA1 Message Date
Colin 2ad977a072 Update show. 2024-10-31 00:01:57 +08:00
Colin 59449df047 Refine minist unsuper. 2024-10-28 18:52:28 +08:00
2 changed files with 5 additions and 5 deletions

View File

@ -78,11 +78,12 @@ def DumpTensorToImage(tensor, name, forceSquare=False, scale=1.0, Contrast=None,
def DumpTensorToLog(tensor, name="log"): def DumpTensorToLog(tensor, name="log"):
shape = tensor.shape
tensor_mean = torch.mean(tensor).cpu().detach().numpy() tensor_mean = torch.mean(tensor).cpu().detach().numpy()
tensor_abs_mean = torch.mean(torch.abs(tensor)).cpu().detach().numpy()
tensor_range = (torch.max(tensor) - torch.min(tensor)).cpu().detach().numpy() tensor_range = (torch.max(tensor) - torch.min(tensor)).cpu().detach().numpy()
f = open(name, "w") f = open(name, "w")
f.writelines("tensor mean: %s" % tensor_mean + os.linesep) f.writelines("tensor mean: %s" % tensor_mean + os.linesep)
f.writelines("tensor abs mean: %s" % tensor_abs_mean + os.linesep)
f.writelines("tensor range: %s" % tensor_range + os.linesep) f.writelines("tensor range: %s" % tensor_range + os.linesep)
data = tensor.reshape([-1]).float().cpu().detach().numpy().tolist() data = tensor.reshape([-1]).float().cpu().detach().numpy().tolist()
for d in data: for d in data:

View File

@ -20,8 +20,8 @@ np.random.seed(seed)
random.seed(seed) random.seed(seed)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# device = torch.device("cpu") device = torch.device("cpu")
# device = torch.device("mps") # device = torch.device("mps")
num_epochs = 1 num_epochs = 1
@ -128,8 +128,8 @@ for epoch in range(epochs):
all = range(0, sample.shape[0]) all = range(0, sample.shape[0])
# ratio_max = abs / mean # ratio_max = abs / mean
# ratio_nor = (max - abs) / max # ratio_nor = (max - abs) / max
ratio_nor = torch.pow(abs / mean, 4)
# ratio_nor[all, max_index] = ratio_max[all, max_index].clone() # ratio_nor[all, max_index] = ratio_max[all, max_index].clone()
ratio_nor = torch.pow(abs / mean, 4)
ratio_nor = torch.where(torch.isnan(ratio_nor), 1.0, ratio_nor) ratio_nor = torch.where(torch.isnan(ratio_nor), 1.0, ratio_nor)
label = sample * ratio_nor label = sample * ratio_nor
@ -197,7 +197,6 @@ with torch.no_grad():
labels = labels.to(device) labels = labels.to(device)
outputs = model(images) outputs = model(images)
# max returns (value ,index)
_, predicted = torch.max(outputs.data, 1) _, predicted = torch.max(outputs.data, 1)
n_samples += labels.size(0) n_samples += labels.size(0)
n_correct += (predicted == labels).sum().item() n_correct += (predicted == labels).sum().item()