Skip to content

Commit

Permalink
Code Refactor for Speed and Readability (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
  • Loading branch information
3 people authored Jun 9, 2024
1 parent cbd9b9a commit 44d68ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
44 changes: 19 additions & 25 deletions gcp/wave_pytorch_gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def runexample(H, model, str, lr=0.001, amsgrad=False):
data = "wavedata25ns.mat"

cuda = torch.cuda.is_available()
os.makedirs(pathr + "models", exist_ok=True)
os.makedirs(f"{pathr}models", exist_ok=True)
name = (data[:-4] + "%s%glr%s" % (H[:], lr, str)).replace(", ", ".").replace("[", "_").replace("]", "_")

tica = time.time()
device = torch.device("cuda:0" if cuda else "cpu")
print("Running %s on %s\n%s" % (name, device.type, torch.cuda.get_device_properties(0) if cuda else ""))

if not os.path.isfile(pathd + data):
os.system("wget -P data/ https://storage.googleapis.com/ultralytics/" + data)
os.system(f"wget -P data/ https://storage.googleapis.com/ultralytics/{data}")
mat = scipy.io.loadmat(pathd + data)
x = mat["inputs"] # inputs (nx512) [waveform1 waveform2]
y = mat["outputs"][:, 1:2] # outputs (nx4) [position(mm), time(ns), PE, E(MeV)]
Expand Down Expand Up @@ -146,7 +146,7 @@ class LinearAct(torch.nn.Module):
def __init__(self, nx, ny):
super(LinearAct, self).__init__()
self.Linear1 = torch.nn.Linear(nx, ny)
self.act = eval("torch.nn." + a + "()")
self.act = eval(f"torch.nn.{a}()")

def forward(self, x):
return self.act(self.Linear1(x))
Expand All @@ -161,9 +161,9 @@ def __init__(self, n): # n = [512, 108, 23, 5, 1]
def forward(self, x):
return self.fc2(self.fc1(self.fc0(x)))

for i in range(10):
tsy.append(runexample(H, model=WAVE(H), str=("." + a)))
scipy.io.savemat(pathr + "TS.sigmoid" + ".mat", dict(tsv=tsv, tsy=np.array(tsy)))
for _ in range(10):
tsy.append(runexample(H, model=WAVE(H), str=f".{a}"))
scipy.io.savemat(f"{pathr}TS.sigmoid.mat", dict(tsv=tsv, tsy=np.array(tsy)))


def tsnoact(): # TS activation function
Expand All @@ -184,27 +184,25 @@ def __init__(self, n): # n = [512, 108, 23, 5, 1]
def forward(self, x):
return self.fc2(self.fc1(self.fc0(x)))

for i in range(10):
tsy.append(runexample(H, model=WAVE(H), str=("." + a)))
scipy.io.savemat(pathr + "TS.noact" + ".mat", dict(tsv=tsv, tsy=np.array(tsy)))
for _ in range(10):
tsy.append(runexample(H, model=WAVE(H), str=f".{a}"))
scipy.io.savemat(f"{pathr}TS.noact.mat", dict(tsv=tsv, tsy=np.array(tsy)))


def tslr(): # TS learning rate
tsv = np.logspace(-5, -2, 13)
tsy = []
for a in tsv:
for i in range(10):
tsy.append(runexample(H, model=WAVE(H), str=("." + "Tanh"), lr=a))
scipy.io.savemat(pathr + "TS.lr" + ".mat", dict(tsv=tsv, tsy=np.array(tsy)))
tsy.extend(runexample(H, model=WAVE(H), str=("." + "Tanh"), lr=a) for _ in range(10))
scipy.io.savemat(f"{pathr}TS.lr.mat", dict(tsv=tsv, tsy=np.array(tsy)))


def tsams(): # TS AMSgrad
tsv = [False, True]
tsy = []
for a in tsv:
for i in range(3):
tsy.append(runexample(H, model=WAVE(H), str=(".TanhAMS" + str(a)), amsgrad=a))
scipy.io.savemat(pathr + "TS.AMSgrad" + ".mat", dict(tsv=tsv, tsy=np.array(tsy)))
tsy.extend(runexample(H, model=WAVE(H), str=f".TanhAMS{str(a)}", amsgrad=a) for _ in range(3))
scipy.io.savemat(f"{pathr}TS.AMSgrad.mat", dict(tsv=tsv, tsy=np.array(tsy)))


def tsshape(): # TS network shape
Expand All @@ -223,8 +221,6 @@ def tsshape(): # TS network shape
# tsv = ['Tanh', 'LogSigmoid', 'Softsign', 'ELU']
# tsv = np.logspace(-4, -2, 11)
tsv = [[512, 23, 1], [512, 64, 8, 1], [512, 108, 23, 5, 1], [512, 147, 42, 12, 3, 1], [512, 181, 64, 23, 8, 3, 1]]
tsy = []

H = tsv[0]

class WAVE(torch.nn.Module):
Expand All @@ -236,9 +232,7 @@ def __init__(self, n): # n = [512, 108, 23, 5, 1]
def forward(self, x):
return self.fc1(self.fc0(x))

for i in range(10):
tsy.append(runexample(H, model=WAVE(H), str=("." + "Tanh")))

tsy = [runexample(H, model=WAVE(H), str=("." + "Tanh")) for _ in range(10)]
H = tsv[1]

class WAVE(torch.nn.Module):
Expand All @@ -251,7 +245,7 @@ def __init__(self, n): # n = [512, 108, 23, 5, 1]
def forward(self, x):
return self.fc2(self.fc1(self.fc0(x)))

for i in range(10):
for _ in range(10):
tsy.append(runexample(H, model=WAVE(H), str=("." + "Tanh")))

H = tsv[2]
Expand All @@ -267,7 +261,7 @@ def __init__(self, n): # n = [512, 108, 23, 5, 1]
def forward(self, x):
return self.fc3(self.fc2(self.fc1(self.fc0(x))))

for i in range(10):
for _ in range(10):
tsy.append(runexample(H, model=WAVE(H), str=("." + "Tanh")))

H = tsv[3]
Expand All @@ -284,7 +278,7 @@ def __init__(self, n): # n = [512, 108, 23, 5, 1]
def forward(self, x):
return self.fc4(self.fc3(self.fc2(self.fc1(self.fc0(x)))))

for i in range(10):
for _ in range(10):
tsy.append(runexample(H, model=WAVE(H), str=("." + "Tanh")))

H = tsv[4]
Expand All @@ -302,9 +296,9 @@ def __init__(self, n): # n = [512, 108, 23, 5, 1]
def forward(self, x):
return self.fc5(self.fc4(self.fc3(self.fc2(self.fc1(self.fc0(x))))))

for i in range(10):
for _ in range(10):
tsy.append(runexample(H, model=WAVE(H), str=("." + "Tanh")))
scipy.io.savemat(pathr + "TS.shape" + ".mat", dict(tsv=tsv, tsy=np.array(tsy)))
scipy.io.savemat(f"{pathr}TS.shape.mat", dict(tsv=tsv, tsy=np.array(tsy)))


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def train(H, model, str, lr=0.001):
data = "wavedata25ns.mat"

cuda = torch.cuda.is_available()
os.makedirs(pathr + "models", exist_ok=True)
os.makedirs(f"{pathr}models", exist_ok=True)
name = (data[:-4] + "%s%glr%s" % (H[:], lr, str)).replace(", ", ".").replace("[", "_").replace("]", "_")
print("Running " + name)
print(f"Running {name}")

device = select_device()

if not os.path.isfile(pathd + data):
os.system("wget -P data/ https://storage.googleapis.com/ultralytics/" + data)
os.system(f"wget -P data/ https://storage.googleapis.com/ultralytics/{data}")
mat = scipy.io.loadmat(pathd + data)
x = mat["inputs"][:] # inputs (nx512) [waveform1 waveform2]
y = mat["outputs"][:, 0:2] # outputs (nx4) [position(mm), time(ns), PE, E(MeV)]
Expand Down
8 changes: 4 additions & 4 deletions train_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def runexample(H, model, str):
cuda = tf.test.is_gpu_available()
tf.set_random_seed(1)
path = "data/"
os.makedirs(path + "models", exist_ok=True)
os.makedirs(f"{path}models", exist_ok=True)
name = (data[:-4] + "%s%glr%geps%s" % (H[:], lr, eps, str)).replace(", ", "_").replace("[", "_").replace("]", "_")

tica = time.time()
device = "/gpu:0" if cuda else "/cpu:0"
print("Running %s on %s" % (name, device))
print(f"Running {name} on {device}")

if not os.path.isfile(path + data):
os.system("wget -P data/ https://storage.googleapis.com/ultralytics/" + data)
os.system(f"wget -P data/ https://storage.googleapis.com/ultralytics/{data}")
mat = scipy.io.loadmat(path + data)
x = mat["inputs"] # inputs (nx512) [waveform1 waveform2]
y = mat["outputs"][:, 0:2] # outputs (nx4) [position(mm), time(ns), PE, E(MeV)]
Expand Down Expand Up @@ -128,4 +128,4 @@ def criteria(y_pred, y): # MSE
if __name__ == "__main__":
H = [128, 32, 8]
for i in range(1):
runexample(H, None, "." + str(i))
runexample(H, None, f".{str(i)}")

0 comments on commit 44d68ff

Please sign in to comment.