Skip to content

Commit

Permalink
Add Ultralytics actions workflow in format.yml (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: glenn-jocher <glenn.jocher@ultralytics.com>
  • Loading branch information
UltralyticsAssistant and glenn-jocher committed Jan 1, 2024
1 parent 41bca9e commit 0c96305
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 135 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Ultralytics 🚀, AGPL-3.0 license
# Ultralytics Format Workflow
# This workflow automatically formats code and documentation in pull requests and pushes to main branch

name: Ultralytics Actions

on:
push:
branches: [main,master]
pull_request:
branches: [main,master]

jobs:
format:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Run Ultralytics Formatting Actions
uses: ultralytics/actions@main
103 changes: 62 additions & 41 deletions gcp/wave_pytorch_gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,41 @@

# set printoptions
torch.set_printoptions(linewidth=320, precision=8)
np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format}) # format short g, %precision=5
np.set_printoptions(linewidth=320, formatter={"float_kind": "{:11.5g}".format}) # format short g, %precision=5

pathd = 'data/'
pathr = 'results/'
pathd = "data/"
pathr = "results/"
torch.manual_seed(1)


def runexample(H, model, str, lr=0.001, amsgrad=False):
epochs = 100000
validations = 5000
printInterval = 1000
# batch_size = 10000
data = 'wavedata25ns.mat'
data = "wavedata25ns.mat"

cuda = torch.cuda.is_available()
os.makedirs(pathr + 'models', exist_ok=True)
name = (data[:-4] + '%s%glr%s' % (H[:], lr, str)).replace(', ', '.').replace('[', '_').replace(']', '_')
os.makedirs(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 ''))
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("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)]
x = mat["inputs"] # inputs (nx512) [waveform1 waveform2]
y = mat["outputs"][:, 1:2] # outputs (nx4) [position(mm), time(ns), PE, E(MeV)]
nz, nx = x.shape
ny = y.shape[1]

x, _, _ = normalize(x, 1) # normalize each input row
y, ymu, ys = normalize(y, 0) # normalize each output column
x, y = torch.Tensor(x), torch.Tensor(y)
x, y, xv, yv, xt, yt = splitdata(x, y, train=0.70, validate=0.15, test=0.15, shuffle=True)
labels = ['train', 'validate', 'test']
labels = ["train", "validate", "test"]

print(model)
if cuda:
Expand All @@ -57,7 +57,7 @@ def runexample(H, model, str, lr=0.001, amsgrad=False):

ticb = time.time()
L = np.full((epochs, 3), np.nan)
best = (0, 1E6, model.state_dict()) # best (epoch, validation loss, model)
best = (0, 1e6, model.state_dict()) # best (epoch, validation loss, model)
for i in range(epochs):
y_pred = model(x)
y_predv = model(xv)
Expand All @@ -72,30 +72,30 @@ def runexample(H, model, str, lr=0.001, amsgrad=False):
if L[i, 1] < best[1]:
best = (i, L[i, 1], copy.deepcopy(model.state_dict()))
if (i - best[0]) > validations:
print('\n%g validation checks exceeded at epoch %g.' % (validations, i))
print("\n%g validation checks exceeded at epoch %g." % (validations, i))
break

if i % printInterval == 0: # print and save progress
# scipy.io.savemat(pathr + name + '.mat', dict(bestepoch=best[0], loss=L[best[0]], L=L, name=name))
_, std = stdpt(y_predv - yv, ys)
print('%.3fs' % (time.time() - ticb), i, L[i], std)
print("%.3fs" % (time.time() - ticb), i, L[i], std)
ticb = time.time()

# Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
loss.backward()
optimizer.step()
else:
print('WARNING: Validation loss still decreasing after %g epochs (train longer).' % (i + 1))
#torch.save(best[2], pathr + 'models/' + name + '.pt')
print("WARNING: Validation loss still decreasing after %g epochs (train longer)." % (i + 1))
# torch.save(best[2], pathr + 'models/' + name + '.pt')
model.load_state_dict(best[2])
dt = time.time() - tica

print('\nFinished %g epochs in %.3fs (%.3f epochs/s)\nBest results from epoch %g:' % (i + 1, dt, i / dt, best[0]))
print("\nFinished %g epochs in %.3fs (%.3f epochs/s)\nBest results from epoch %g:" % (i + 1, dt, i / dt, best[0]))
loss, std = np.zeros(3), np.zeros((3, ny))
for i, (xi, yi) in enumerate(((x, y), (xv, yv), (xt, yt))):
loss[i], std[i] = stdpt(model(xi) - yi, ys)
print('%.5f %s %s' % (loss[i], std[i, :], labels[i]))
print("%.5f %s %s" % (loss[i], std[i, :], labels[i]))
# scipy.io.savemat(pathr + name + '.mat', dict(bestepoch=best[0], loss=loss, std=std, L=L, name=name))
# files.download(pathr + name + '.mat')

Expand All @@ -111,14 +111,17 @@ def runexample(H, model, str, lr=0.001, amsgrad=False):


H = [512, 64, 8, 1]


class LinearAct(torch.nn.Module):
def __init__(self, nx, ny):
super(LinearAct, self).__init__()
self.Linear1 = torch.nn.Linear(nx, ny)
self.act = torch.nn.Tanh()

def forward(self, x):
return self.act(self.Linear1(x))
return self.act(self.Linear1(x))


class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
Expand All @@ -133,16 +136,17 @@ def forward(self, x):

def tsact(): # TS activation function
H = [512, 64, 8, 1]
tsv = ['Sigmoid']#['Tanh', 'LogSigmoid', 'Softsign', 'ELU']
tsv = ["Sigmoid"] # ['Tanh', 'LogSigmoid', 'Softsign', 'ELU']
# tsv = np.logspace(-4,-2,11)
tsy = []

for a in tsv:

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("torch.nn." + a + "()")

def forward(self, x):
return self.act(self.Linear1(x))
Expand All @@ -158,16 +162,18 @@ 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)))
tsy.append(runexample(H, model=WAVE(H), str=("." + a)))
scipy.io.savemat(pathr + "TS.sigmoid" + ".mat", dict(tsv=tsv, tsy=np.array(tsy)))


def tsnoact(): # TS activation function
H = [512, 64, 8, 1]
tsv = ['NoAct']#['Tanh', 'LogSigmoid', 'Softsign', 'ELU']
tsv = ["NoAct"] # ['Tanh', 'LogSigmoid', 'Softsign', 'ELU']
# tsv = np.logspace(-4,-2,11)
tsy = []

for a in tsv:

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
Expand All @@ -179,26 +185,26 @@ 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)))
tsy.append(runexample(H, model=WAVE(H), str=("." + a)))
scipy.io.savemat(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.append(runexample(H, model=WAVE(H), str=("." + "Tanh"), lr=a))
scipy.io.savemat(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.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)))


def tsshape(): # TS network shape
Expand All @@ -220,42 +226,52 @@ def tsshape(): # TS network shape
tsy = []

H = tsv[0]

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
self.fc0 = LinearAct(n[0], n[1])
self.fc1 = torch.nn.Linear(n[1], n[2])

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.append(runexample(H, model=WAVE(H), str=("." + "Tanh")))

H = tsv[1]

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
self.fc0 = LinearAct(n[0], n[1])
self.fc1 = LinearAct(n[1], n[2])
self.fc2 = torch.nn.Linear(n[2], n[3])

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=('.' + 'Tanh')))
tsy.append(runexample(H, model=WAVE(H), str=("." + "Tanh")))

H = tsv[2]

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
self.fc0 = LinearAct(n[0], n[1])
self.fc1 = LinearAct(n[1], n[2])
self.fc2 = LinearAct(n[2], n[3])
self.fc3 = torch.nn.Linear(n[3], n[4])

def forward(self, x):
return self.fc3(self.fc2(self.fc1(self.fc0(x))))

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

H = tsv[3]

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
Expand All @@ -264,12 +280,15 @@ def __init__(self, n): # n = [512, 108, 23, 5, 1]
self.fc2 = LinearAct(n[2], n[3])
self.fc3 = LinearAct(n[3], n[4])
self.fc4 = torch.nn.Linear(n[4], n[5])

def forward(self, x):
return self.fc4(self.fc3(self.fc2(self.fc1(self.fc0(x)))))

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

H = tsv[4]

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
Expand All @@ -279,13 +298,15 @@ def __init__(self, n): # n = [512, 108, 23, 5, 1]
self.fc3 = LinearAct(n[3], n[4])
self.fc4 = LinearAct(n[4], n[5])
self.fc5 = torch.nn.Linear(n[5], n[6])

def forward(self, x):
return self.fc5(self.fc4(self.fc3(self.fc2(self.fc1(self.fc0(x))))))

for i 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)))
tsy.append(runexample(H, model=WAVE(H), str=("." + "Tanh")))
scipy.io.savemat(pathr + "TS.shape" + ".mat", dict(tsv=tsv, tsy=np.array(tsy)))


if __name__ == '__main__':
#tsnoact()
tsact()
if __name__ == "__main__":
# tsnoact()
tsact()
Loading

0 comments on commit 0c96305

Please sign in to comment.