Я начал с извлечения данных обучения и тестирования из кадра данных и масштабировал их:
Код: Выделить всё
#training data
Qp_values = my_df['Set UF Permeate Flowrate (GPM)'].values
TMP_values = my_df['UF-TMP (psi)'].values
backwash_states = my_df['Backwash (0=Off, 1=On)'].values
elapsed_time = my_df['Elapsed Time (hr)'].values
scaler = MinMaxScaler()
Qp_values = scaler.fit_transform(Qp_values.reshape(-1, 1)).flatten()
TMP_values = scaler.fit_transform(TMP_values.reshape(-1, 1)).flatten()
#testing data
Qp_values_test = my_df['Set UF Permeate Flowrate (GPM)'].values
TMP_values_test = my_df['UF-TMP (psi)'].values
backwash_states_test = my_df['Backwash (0=Off, 1=On)'].values
Qp_values_test = scaler.fit_transform(Qp_values_test.reshape(-1, 1)).flatten()
TMP_values_test = scaler.fit_transform(TMP_values_test.reshape(-1, 1)).flatten()
Код: Выделить всё
#Identify the filtration cycles from change in backwash states
def find_filtration_cycles(stats):
cycles = [] #stored list of tuples including start and end indices of filtrtion cycles
start = 0 # starting index fof a filtration cycle, set to 0 as filtration cycle starts from beginning
in_cycle = True # boolean flag to state if filtration phase is in a cycle, True, as it is
for i in range(1, len(stats)):
if in_cycle and stats[i] == 1 and stats[i-1] == 0:
cycles.append((start, i))
in_cycle = False
elif not in_cycle and stats[i] == 0 and stats[i-1] == 1:
start = i
in_cycle = True
# Handle case where the cycle extends to the end of the list
if in_cycle:
cycles.append((start, len(stats)-1))
return cycles
filtration_cycles = find_filtration_cycles(backwash_states)
Код: Выделить всё
# Inputs: Qp_values (k) and TMP(k)
# Output: TMP(k+1) where k is the data point
class TMP_Prediction_Model(nn.Module):
def __init__(self, in_features=2, h1=10, h2=8, h3=8, h4=8, out_features=1):
super().__init__()
self.fc1 = nn.Linear(in_features, h1)
self.fc2 = nn.Linear(h1, h2)
self.fc3 = nn.Linear(h2, h3)
self.fc4 = nn.Linear(h3, h4)
self.out = nn.Linear(h4, out_features)
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.relu(self.fc4(x))
x = self.out(x)
return x
torch.manual_seed(41)
# Instantiate the model, optimizer, and loss function
model = TMP_Prediction_Model()
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
Подробнее здесь: https://stackoverflow.com/questions/789 ... ing-window