few fixes

This commit is contained in:
matthias@arch 2023-05-05 15:52:25 +02:00
parent 1570d47213
commit c56f51b8e5
2 changed files with 29 additions and 26 deletions

View File

@ -51,7 +51,7 @@ if __name__ == "__main__":
# test_loader = iter(DataLoader(test_set)) # test_loader = iter(DataLoader(test_set))
train_loader = iter(DataLoader(train_set, batch_size=3, shuffle=True)) train_loader = iter(DataLoader(train_set, batch_size=3, shuffle=True))
test_loader = iter(DataLoader(test_set, batch_size=3, shuffle=True)) test_loader = iter(DataLoader(test_set, batch_size=3, shuffle=True))
# , dtype=torch.float32
sample = next(train_loader) sample = next(train_loader)
print(sample) print(sample)
@ -74,21 +74,22 @@ if __name__ == "__main__":
def forward(self, x): def forward(self, x):
# x: batches, length, features
print(f"forward pass") print(f"forward pass")
D = 2 if self.if_bidirectional == True else 1 D = 2 if self.if_bidirectional == True else 1
print(f"x({x.shape})={x}") print(f"x({x.shape})=...")
batch_size = x.shape[1] batch_size = x.shape[0]
print(f"batch_size={batch_size}") print(f"batch_size={batch_size}")
h0 = torch.zeros(D * self.num_layers, batch_size, self.hidden_size).to(device) h0 = torch.zeros(D * self.num_layers, batch_size, self.hidden_size).to(device)
print(f"h0={h0}") print(f"h0({h0.shape})=...")
c0 = torch.zeros(D * self.num_layers, batch_size, self.hidden_size).to(device) c0 = torch.zeros(D * self.num_layers, batch_size, self.hidden_size).to(device)
x.to(device) x.to(device)
_, (h_n, _) = self.lstm(x, (h0, c0)) _, (h_n, _) = self.lstm(x, (h0, c0))
print(f"h_n={h_n}") print(f"h_n({h_n.shape})=...")
final_state = h_n.view(self.num_layers, D, batch_size, self.hidden_size)[-1] # num_layers, num_directions, batch, hidden_size 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}") print(f"final_state({final_state.shape})=...")
if D == 1: if D == 1:
X = final_state.squeeze() X = final_state.squeeze()
@ -99,7 +100,7 @@ if __name__ == "__main__":
else: else:
raise ValueError("D must be 1 or 2") raise ValueError("D must be 1 or 2")
output = self.fc(X) # fully-connected layer output = self.fc(X) # fully-connected layer
print(f"out={output}") print(f"out({output.shape})={output}")
return output return output
@ -119,18 +120,20 @@ if __name__ == "__main__":
for data, y in train_loader: for data, y in train_loader:
# data = batch, seq, features # data = batch, seq, features
print(ep, "Train") print(ep, "Train")
print(f"data({data.shape})={data}") # print(f"data({data.shape})={data}")
x = data[:,:,2] # select voltage data x = data[:,:,[2]].float() # select voltage data
print(f"x({x.shape})={x}") print(f"x({x.shape}, {x.dtype})=...")
length = torch.tensor([x.shape[1] for _ in range(x.shape[0])], dtype=torch.int64) print(f"y({y.shape}, {y.dtype})=...")
print(f"length({length.shape})={length}") # length = torch.tensor([x.shape[1] for _ in range(x.shape[0])], dtype=torch.int64)
batch_size = x.shape[0] # print(f"length({length.shape})={length}")
print(f"batch_size={batch_size}") # batch_size = x.shape[0]
v = x.view(batch_size, -1, feature_count) # print(f"batch_size={batch_size}")
data = rnn_utils.pack_padded_sequence(v.type(torch.FloatTensor), length, batch_first=True).to(device)[0] # 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(f"data({data.shape})={data}")
# print(data.batch_sizes[0]) # print(data.batch_sizes[0])
# print(data) # print(data)
out = model(data) out = model(x)
loss = loss_func(out, y) loss = loss_func(out, y)
# print(loss) # print(loss)
@ -146,15 +149,15 @@ if __name__ == "__main__":
for data, y in test_loader: for data, y in test_loader:
print(ep, "Test") print(ep, "Test")
x = data[:,2] x = data[:,:,[2]]
print(f"x({x.shape})={x}") print(f"x({x.shape})={x}")
length = torch.tensor(x.shape[0], dtype=torch.int64) # length = torch.tensor(x.shape[1], dtype=torch.int64)
print(f"length={length}") # print(f"length={length}")
batch_size = x.shape[0] # batch_size = x.shape[0]
print(f"batch_size={batch_size}") # print(f"batch_size={batch_size}")
v = x.view(batch_size, -1, feature_count) # v = x.view(batch_size, -1, feature_count)
data = rnn_utils.pack_padded_sequence(v.type(torch.FloatTensor), length, batch_first=True).to(device) # data = rnn_utils.pack_padded_sequence(v.type(torch.FloatTensor), length, batch_first=True).to(device)
out = model(data) out = model(x)
loss = loss_func(out, y) loss = loss_func(out, y)
predicted = torch.max(torch.nn.functional.softmax(out), 1)[1] predicted = torch.max(torch.nn.functional.softmax(out), 1)[1]

View File

@ -48,7 +48,7 @@ class Datasample:
def _load_data(self): def _load_data(self):
df = pd.read_csv(self.datapath) df = pd.read_csv(self.datapath)
self.data = df.to_numpy() self.data = df.to_numpy(dtype=np.float32)
def get_data(self): def get_data(self):
"""[[timestamps, idata, vdata]]""" """[[timestamps, idata, vdata]]"""