-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import torch | ||
from torchvision import transforms | ||
import os, glob | ||
from torch.utils.data import DataLoader | ||
from torch import nn | ||
import numpy as np | ||
from PIL import Image | ||
|
||
|
||
class train_data(nn.Module): | ||
def __init__(self, data_path, lattice): | ||
self.datapath = [] | ||
self.datapath.extend(glob.glob(os.path.join(data_path, "*.npy"))) | ||
self.L = lattice | ||
|
||
def __getitem__(self, index): | ||
img_path = self.datapath[index] | ||
data = np.load(img_path) | ||
spin_data = np.reshape(data, [-1, self.L, self.L]) | ||
if np.abs(np.sum(spin_data)) < 0.8: | ||
label = 0 | ||
else: | ||
label = 1 | ||
|
||
return spin_data, label | ||
|
||
def __len__(self): | ||
return len(self.datapath) | ||
|
||
if __name__=="__main__": | ||
path = "E:/deeplearning/depp learning for Phys/PCA for 2DIsing model/40_L" | ||
datatrain = train_data(path, 40) | ||
dataloader = DataLoader(datatrain, batch_size=8) | ||
|
||
for x, y in dataloader: | ||
print("x.shape is:", x.shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from torch.utils.data import DataLoader | ||
import torch | ||
from torch import nn, optim | ||
from model import LeNet | ||
from tqdm import tqdm | ||
from dataset import train_data | ||
|
||
model = LeNet() | ||
BZ = 16 | ||
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9) | ||
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") | ||
path = "E:/deeplearning/depp learning for Phys/PCA for 2DIsing model/40_L" | ||
data = train_data(path, 40) | ||
Data = DataLoader(data, batch_size=BZ) | ||
model.to(device) | ||
lossfunction = nn.CrossEntropyLoss() | ||
|
||
def Acc(y_pred, y_truth): | ||
pred = y_pred.argmax(dim=1) | ||
return (pred.data.cpu() == y_truth.data).sum() | ||
|
||
def train(): | ||
for epoch in range(10): | ||
torch.set_grad_enabled(True) | ||
acc = 0 | ||
loss_all = 0 | ||
step = 0 | ||
for spin_data, label in tqdm(Data): | ||
step += 1 | ||
optimizer.zero_grad() | ||
spin_data = spin_data.to(device, torch.float) | ||
out = model(spin_data) | ||
loss = lossfunction(out, label.to(device, torch.long)) | ||
loss_all += loss | ||
acc += Acc(out, label) | ||
loss.backward() | ||
optimizer.step() | ||
print("epoch is:", epoch, "loss is %.4f" % (loss_all / step), "accuracy is %.4f" % (acc / (step * BZ))) | ||
|
||
if __name__=="__main__": | ||
train() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from torch import nn | ||
import torch | ||
|
||
class LeNet(nn.Module): | ||
def __init__(self): | ||
super(LeNet, self).__init__() | ||
self.C1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, stride=1, padding=2) | ||
self.R1 = nn.ReLU() | ||
self.S2 = nn.MaxPool2d(kernel_size=2) | ||
self.C3 = nn.Conv2d(6, 16, 5, 1, 0) | ||
self.R2 = nn.ReLU() | ||
self.S4 = nn.MaxPool2d(2) | ||
self.C5 = nn.Conv2d(16, 120, 5, 1, 0) | ||
self.R3 = nn.ReLU() | ||
self.F6 = nn.Linear(in_features=1920, out_features=84) | ||
self.R4 = nn.ReLU() | ||
self.OUT = nn.Linear(84, 2) | ||
|
||
def forward(self, x): | ||
x = self.C1(x) | ||
x = self.R1(x) | ||
x = self.S2(x) | ||
x = self.C3(x) | ||
x = self.R2(x) | ||
x = self.S4(x) | ||
x = self.C5(x) | ||
x = self.R3(x) | ||
x = x.view(x.size(0), -1) | ||
x = self.F6(x) | ||
x = self.R4(x) | ||
x = self.OUT(x) | ||
return x | ||
|
||
if __name__=="__main__": | ||
model = LeNet() | ||
test = torch.ones(1,1,40,40) | ||
out = model(test) | ||
print("out.shape is:", out.shape) |