From 5b2cd4da61f0d2ab19f8a18393edabe1eb19e593 Mon Sep 17 00:00:00 2001 From: Colin Date: Mon, 28 Oct 2024 16:31:42 +0800 Subject: [PATCH] Add model.conv1.weight normal after update grad. --- unsuper/.gitignore | 4 +++- unsuper/minist.py | 22 ++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/unsuper/.gitignore b/unsuper/.gitignore index 4809819..e25ce62 100644 --- a/unsuper/.gitignore +++ b/unsuper/.gitignore @@ -1,2 +1,4 @@ dump1 -dump2 \ No newline at end of file +dump2 +*.png +*.log \ No newline at end of file diff --git a/unsuper/minist.py b/unsuper/minist.py index 8be7062..024fa69 100644 --- a/unsuper/minist.py +++ b/unsuper/minist.py @@ -5,15 +5,23 @@ import torch.nn as nn import torch.nn.functional as F # Add this line import torchvision import torchvision.transforms as transforms +import numpy as np +import random sys.path.append("..") from tools import show -seed = 4321 +seed = 42 torch.manual_seed(seed) -torch.cuda.manual_seed_all(seed) +if torch.cuda.is_available(): + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) +np.random.seed(seed) +random.seed(seed) + device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +# device = torch.device("cpu") # device = torch.device("mps") num_epochs = 1 @@ -60,15 +68,12 @@ class ConvNet(nn.Module): def printFector(self, x, label, dir=""): show.DumpTensorToImage(x.view(-1, x.shape[2], x.shape[3]), dir + "/input_image.png", Contrast=[0, 1.0]) - # show.DumpTensorToLog(x, "input_image.log") w = self.normal_conv1_weight() x = torch.conv2d(x, w) show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]), dir + "/conv1_weight.png") - # show.DumpTensorToLog(w, "conv1_weight.log") show.DumpTensorToImage(x.view(-1, x.shape[2], x.shape[3]), dir + "/conv1_output.png") - # show.DumpTensorToLog(x, "conv1_output.png") x = self.pool(x) x = self.conv2(x) @@ -132,16 +137,17 @@ for epoch in range(epochs): model.conv1.weight.grad = None loss.backward() - model.conv1.weight.data = model.conv1.weight.data - model.conv1.weight.grad * 10000 + model.conv1.weight.data = model.conv1.weight.data - model.conv1.weight.grad * 1000 + model.conv1.weight.data = model.normal_conv1_weight() if (i + 1) % 100 == 0: print(f"Epoch [{epoch+1}/{epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.8f}") show.DumpTensorToImage(images.view(-1, images.shape[2], images.shape[3]), "input_image.png", Contrast=[0, 1.0]) g = model.conv1.weight.grad -show.DumpTensorToImage(g.view(-1, g.shape[2], g.shape[3]).cpu(), "conv1_weight_grad.png") +show.DumpTensorToImage(g.view(-1, g.shape[2], g.shape[3]).cpu(), "conv1_weight_grad.png", Value2Log=True) w = model.conv1.weight.data -show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]), "conv1_weight_update.png") +show.DumpTensorToImage(w.view(-1, w.shape[2], w.shape[3]), "conv1_weight_update.png", Value2Log=True) # model.conv1.weight.data = torch.rand(model.conv1.weight.data.shape, device=device)