From 0b794d1008ad422241b93fbbb20a1a44af50a9c7 Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Fri, 5 May 2023 13:16:39 +0200 Subject: [PATCH] added rnn --- teng-ml/main.py | 82 ++++++++++++------ teng-ml/rnn.py | 2 +- .../__pycache__/transform.cpython-310.pyc | Bin 2143 -> 2191 bytes teng-ml/util/data_loader.py | 18 ++-- teng-ml/util/transform.py | 20 +++-- 5 files changed, 82 insertions(+), 40 deletions(-) diff --git a/teng-ml/main.py b/teng-ml/main.py index 3857553..ae71f5f 100644 --- a/teng-ml/main.py +++ b/teng-ml/main.py @@ -11,10 +11,10 @@ import matplotlib.pyplot as plt import pandas as pd import torch import torch.nn as nn +import torch.nn.utils.rnn as rnn_utils from torch.utils.data import DataLoader - -from .util.transform import ConstantInterval +from .util.transform import ConstantInterval, Normalize from .util.data_loader import load_datasets, LabelConverter def test_interpol(): @@ -24,7 +24,7 @@ def test_interpol(): array = df.to_numpy() print(ConstantInterval.get_average_interval(array[:,0])) transformer = ConstantInterval(0.05) - interp_array = transformer(array[:,0], array[:,2]) + interp_array = transformer(array[:,[0,2]]) fig1, ax1 = plt.subplots() ax1.plot(interp_array[:,0], interp_array[:,1], color="r", label="Interpolated") @@ -42,15 +42,22 @@ if __name__ == "__main__": ) print(f"Using device: {device}") - labels = LabelConverter(["foam", "glass", "kapton", "foil"]) - train_set, test_set = load_datasets("/home/matth/data", labels, voltage=8.2) + labels = LabelConverter(["foam", "glass", "kapton", "foil", "cloth", "rigid_foam"]) + t_const_int = ConstantInterval(0.01) + t_norm = Normalize(0, 1) + train_set, test_set = load_datasets("/home/matth/Uni/TENG/testdata", labels, voltage=8.2, transforms=[t_const_int], train_to_test_ratio=0.7, random_state=42) # train_loader = iter(DataLoader(train_set)) # test_loader = iter(DataLoader(test_set)) - # sample = next(train_loader) - # print(sample) - train_loader = iter(DataLoader(train_set)) - test_loader = iter(DataLoader(test_set)) + train_loader = iter(DataLoader(train_set, batch_size=3, shuffle=True)) + test_loader = iter(DataLoader(test_set, batch_size=3, shuffle=True)) + + sample = next(train_loader) + print(sample) + + feature_count = 1 + + class RNN(nn.Module): def __init__(self, input_size, hidden_size, num_layers, num_classes, if_bidirectional): super(RNN, self).__init__() @@ -58,6 +65,7 @@ if __name__ == "__main__": self.hidden_size = hidden_size self.if_bidirectional = if_bidirectional self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=if_bidirectional) + # x = (batch_size, sequence, feature) if if_bidirectional == True: self.fc = nn.Linear(hidden_size * 2, num_classes) @@ -66,14 +74,21 @@ if __name__ == "__main__": def forward(self, x): + print(f"forward pass") D = 2 if self.if_bidirectional == True else 1 - Batch = x.batch_sizes[0] - h0 = torch.zeros(D * self.num_layers, Batch, self.hidden_size).to(device) - c0 = torch.zeros(D * self.num_layers, Batch, self.hidden_size).to(device) + print(f"x({x.shape})={x}") + batch_size = x.shape[1] + print(f"batch_size={batch_size}") + + h0 = torch.zeros(D * self.num_layers, batch_size, self.hidden_size).to(device) + print(f"h0={h0}") + c0 = torch.zeros(D * self.num_layers, batch_size, self.hidden_size).to(device) x.to(device) _, (h_n, _) = self.lstm(x, (h0, c0)) - final_state = h_n.view(self.num_layers, D, Batch, self.hidden_size)[-1] # num_layers, num_directions, batch, hidden_size + print(f"h_n={h_n}") + final_state = h_n.view(self.num_layers, D, batch_size, self.hidden_size)[-1] # num_layers, num_directions, batch, hidden_size + print(f"final_state={final_state}") if D == 1: X = final_state.squeeze() @@ -81,12 +96,14 @@ if __name__ == "__main__": h_1, h_2 = final_state[0], final_state[1] # forward & backward pass #X = h_1 + h_2 # Add both states X = torch.cat((h_1, h_2), 1) # Concatenate both states, X-size: (Batch, hidden_size * 2) - + else: + raise ValueError("D must be 1 or 2") output = self.fc(X) # fully-connected layer + print(f"out={output}") return output - model = RNN(input_size = 1, hidden_size = 8, num_layers = 3, num_classes = 18, if_bidirectional = True).to(device) + model=RNN(input_size=1, hidden_size=8, num_layers=3, num_classes=18, if_bidirectional=True).to(device) loss_func = torch.nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.02) scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.95) @@ -99,34 +116,47 @@ if __name__ == "__main__": train_total = 0 val_correct = 0 val_total = 0 - for (x, y), length in train_loader: + for data, y in train_loader: + # data = batch, seq, features + print(ep, "Train") + print(f"data({data.shape})={data}") + x = data[:,:,2] # select voltage data + print(f"x({x.shape})={x}") + length = torch.tensor([x.shape[1] for _ in range(x.shape[0])], dtype=torch.int64) + print(f"length({length.shape})={length}") batch_size = x.shape[0] - v = x.view(batch_size, -1, nFeatrue) - data = rnn_utils.pack_padded_sequence(v.type(torch.FloatTensor), length, batch_first=True).to(device) + print(f"batch_size={batch_size}") + v = x.view(batch_size, -1, feature_count) + data = rnn_utils.pack_padded_sequence(v.type(torch.FloatTensor), length, batch_first=True).to(device)[0] # print(data.batch_sizes[0]) # print(data) out = model(data) - loss = loss_func(out, y) + loss = loss_func(out, y) # print(loss) optimizer.zero_grad() # clear gradients for next train loss.backward() # backpropagation, compute gradients optimizer.step() # apply gradients - + predicted = torch.max(torch.nn.functional.softmax(out), 1)[1] train_total += y.size(0) train_correct += (predicted == y).sum().item() - scheduler.step() - - for (x, y), length in test_loader: + + for data, y in test_loader: + print(ep, "Test") + x = data[:,2] + print(f"x({x.shape})={x}") + length = torch.tensor(x.shape[0], dtype=torch.int64) + print(f"length={length}") batch_size = x.shape[0] - v = x.view(batch_size, -1, nFeatrue) + print(f"batch_size={batch_size}") + v = x.view(batch_size, -1, feature_count) data = rnn_utils.pack_padded_sequence(v.type(torch.FloatTensor), length, batch_first=True).to(device) out = model(data) - loss = loss_func(out, y) - + loss = loss_func(out, y) + predicted = torch.max(torch.nn.functional.softmax(out), 1)[1] val_total += y.size(0) val_correct += (predicted == y).sum().item() diff --git a/teng-ml/rnn.py b/teng-ml/rnn.py index 2733d4b..f8425c4 100644 --- a/teng-ml/rnn.py +++ b/teng-ml/rnn.py @@ -31,7 +31,7 @@ class RNN(nn.Module): X = final_state.squeeze() elif D == 2: h_1, h_2 = final_state[0], final_state[1] # forward & backward pass - #X = h_1 + h_2 # Add both states + # X = h_1 + h_2 # Add both states X = torch.cat((h_1, h_2), 1) # Concatenate both states, X-size: (Batch, hidden_size * 2) output = self.fc(X) # fully-connected layer diff --git a/teng-ml/util/__pycache__/transform.cpython-310.pyc b/teng-ml/util/__pycache__/transform.cpython-310.pyc index 9ccaebd778c0d2794cf8f8e52b6b6353722d9c6b..6a44ccc45486faf36b710a8d294283a2d732c595 100644 GIT binary patch delta 610 zcmYL{zi-qq6vy*y$4RfG9M^I`T3U`u3@5mY=l~K-6-Y241WZL$6{EIiFlMKHHh!X@lfn25f}YTC6qsU9BnQquHExHT)<~7|n zx7hk>6ZPcGF`T}fMJ%Hi1W_@Ee%*Y9`MnTl9R0dWDTIa;pXVec+oa`{M0syP}vag%0{A4$!JnsY^RAzE*%YPy~K(_i>Bsf;{QgEC0W`QOU*At)mlI zDuuF;S)S!e$cg#vFP^F4hBkUpMO0Z67%V^?LZ|}A27jE9=P>aOa z-Miubw8+9D5n?}lQs&{_&hA5X3SaGr6?qv)N|Kl0si)408qfwls@L?@VCT5X#-hIG zi@eBckrY)u^7Awi$;dy{gSy2YtoN`Izw!^dGG9RS(QATm)xheV0gfr4lCQnUOLHNK>-QnI+ko@y3}07?Mgh zhnb1zF)Phy}Y%V3xdGm`n~)rqO}s9 zl+QD++iT5==cQ;_oi|x)9ikTF7cJ}s7I*Zr`(L1zX<=Fmw`aVHT)aEh(vhlGKg_kB zhx-TUwbsdnW!M0Rc&N|^yM_E6@V@$Nt-_}IX+>UZUcRBF2U!^qT#D@#*ic7ye_^Qa zV2E|9&h4$WMGX0MjU|m`4gCw;Q{U{(n_UcVpx3%L$6(-?4kh+A!n