def train_daily_model(energy, model, mode="time_series"): device = torch.device("cpu") # W energy model train_dataset = EnergySet("train", mode, energy, True) test_dataset = EnergySet("test", mode, energy, True) train_dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True, pin_memory=True) test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False, pin_memory=True) # model = NeuralNet(15, 128, 2) # model = RNN(19, 256, 3, 1) # model = ensembleNN(19, 256, 3, 1) # model = CNN(1) # torchsummary.summary(model, (24, 9)) optimizer = torch.optim.Adam(model.parameters(), lr=0.003) total_step = len(train_dataloader) for epoch in range(20): for i, (features, labels) in enumerate(train_dataloader): # print(i) # features = torch.FloatTensor(features) outputs = model(features) # loss =cv_rmse_loss(outputs, labels, 0.5, 0.5) # loss = cv_rmse(outputs, labels) loss = cv_rmse(outputs, labels) optimizer.zero_grad() loss.backward() optimizer.step() if (i + 1) % 10 == 0: print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format( epoch + 1, 30, i + 1, total_step, loss.item())) W_preds = [] W_targets = [] with torch.no_grad(): for features, labels in test_dataloader: outputs = model(features) W_preds.append(outputs.numpy()) W_targets.append(labels.numpy()) W_preds = np.array(W_preds) W_targets = np.array(W_targets) W_preds = W_preds.reshape((-1, 1)) W_targets = W_targets.reshape((-1, 1)) print(cv_rmse(torch.from_numpy(W_preds), torch.from_numpy(W_targets))) print(W_preds[:100]) torch.save(model.state_dict(), 'dailynn_' + energy + '.pth')
def train_hour_model(energy='Q'): train_dataset = hourEnergy("train", energy) test_dataset = hourEnergy("test", energy) train_dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True, pin_memory=True) test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False, pin_memory=True) # model = HourNN() model = HourRNN(4, 256, 3, 1) optimizer = torch.optim.Adam(model.parameters(), lr=0.001) total_step = len(train_dataloader) for epoch in range(15): for i, ((hour_features, daily_features), labels) in enumerate(train_dataloader): # print(i) # features = torch.FloatTensor(features) outputs = model((hour_features, daily_features)) # loss =cv_rmse_loss(outputs, labels, 0.5, 0.5) # loss = cv_rmse(outputs, labels) loss = train_loss(outputs, labels, daily_features) optimizer.zero_grad() loss.backward() optimizer.step() if (i + 1) % 10 == 0: print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format( epoch + 1, 30, i + 1, total_step, loss.item())) Q_preds = [] Q_targets = [] with torch.no_grad(): for (hour_features, daily_features), labels in test_dataloader: outputs = model((hour_features, daily_features)) Q_preds.append(outputs.numpy()) Q_targets.append(labels.numpy()) Q_preds = np.array(Q_preds) Q_targets = np.array(Q_targets) Q_preds = Q_preds.reshape((-1, 1)) Q_targets = Q_targets.reshape((-1, 1)) print(cv_rmse(torch.from_numpy(Q_preds), torch.from_numpy(Q_targets))) print(Q_preds[:24]) print(Q_targets[:24]) torch.save(model.state_dict(), 'hournn_' + energy + '.pth')
def test(daily_model, hour_model, energy, mode, building_id, year, to_csv=False): # if energy == "Q" and separate_feature: # test_dataset = EnergySet("test", "regression", energy, separate_feature=separate_feature) # test_dataset = testingBuildingSet() # else: # test_dataset = EnergySet("test", "time_series", energy, separate_feature=separate_feature) test_dataset = testingBuildingSet(energy, mode=mode, building_id=building_id, year=year) test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False, pin_memory=True) daily_preds = [] daily_targets = [] with torch.no_grad(): for features, labels in test_dataloader: outputs = daily_model(features) daily_preds.append(outputs.numpy()) daily_targets.append(labels.numpy()) daily_preds = np.array(daily_preds) daily_targets = np.array(daily_targets) daily_preds = daily_preds.reshape((-1, 1)) daily_targets = daily_targets.reshape((-1, 1)) print(daily_preds[:30]) print(daily_targets[:30]) # add code to output prediction to csv file print( cv_rmse(torch.from_numpy(daily_preds), torch.from_numpy(daily_targets))) # print(len(daily_preds)) if to_csv: df = pd.read_csv("output_" + energy + ".csv") df['prediction'] = daily_preds df.to_csv("output_" + energy + ".csv", index=False)
uni=utils.cros_validation(regression.LinearRegression(regularization_factor=1.0),X,train_label,n_folds,random_grid) random_grid = {'n_estimators': [100,200,300,400,500,600,700,720,740,760,780,800]} lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5, learning_rate=0.05, n_estimators=800, max_bin = 60, bagging_fraction = 0.8, bagging_freq = 5, feature_fraction = 0.2319, feature_fraction_seed=9, bagging_seed=9, min_data_in_leaf =6, min_sum_hessian_in_leaf = 11) model_lgb=utils.cros_validation(lgb,train.values,train_label,n_folds,random_grid) #call k-folds validation utils.scores('Lasso',utils.cv_rmse(lasso,train.values,train_label,n_folds)) utils.scores('Multivariate Linear Regression',utils.cv_rmse(multi,train.values,train_label,n_folds)) utils.scores('Univariate Linear Regression',utils.cv_rmse(uni,X,train_label,n_folds)) utils.scores('Gradient Boosting',utils.cv_rmse(model_lgb,train.values,train_label,n_folds)) #---------------------------Meta Learning ---------------------------------- sub = pd.DataFrame() sub['Id'] = test_id sub['SalePrice'] = np.exp(lasso.predict(test)*0.2+multi.predict(test)*0.2+model_lgb.predict(test)*0.6)
"Time", "BuildingID", ], axis=1) scale_feature = [ 'Stair1', 'Stair2', 'Area', 'temp', 'point_temp', 'humidity', 'pa', 'wind speed', "Record_W", "Record_Q" ] scaler = MinMaxScaler() scaler.fit(df[scale_feature]) df[scale_feature] = scaler.transform(df[scale_feature]) label_w = df["Record_W"] label_q = df["Record_Q"] df = df.drop(["Record_W", "Record_Q"], axis=1) X_train, X_test, y_train, y_test = train_test_split(df, label_w, test_size=0.2, random_state=42) # reg = LinearRegression().fit(X_train, y_train) # print(reg.coef_) # y_pred = reg.predict(X_test) # # print(mean_squared_error(y_test, y_pred)) # print(cv_rmse(y_test, y_pred)) svm_reg = RandomForestRegressor() svm_reg.fit(X_train, y_train) y_pred = svm_reg.predict(X_test) # print(mean_squared_error(y_test, y_pred)) print(cv_rmse(y_test, y_pred))
torch.nn.utils.clip_grad_norm_(model.parameters(), 1) optimizer.step() if (i + 1) % 10 == 0: print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format( epoch + 1, 10, i + 1, total_step, loss.item())) with torch.no_grad(): preds = [] targets = [] for features, labels in test_dataloader: outputs = model(features, labels) preds.append(outputs.numpy()) targets.append(labels.numpy()) # loss = cv_rmse_loss(outputs, labels) # print(loss.item()) preds = np.array(preds) preds = preds.reshape((-1, 2)) targets = np.array(targets) targets = targets.reshape((-1, 2)) print(cv_rmse(targets[:, 0], preds[:, 0]), cv_rmse(targets[:, 1], preds[:, 1])) print( cv_rmse_loss(torch.from_numpy(preds), torch.from_numpy(targets), 0.3, 0.7)) # mat = np.zeros((len(preds), 10)) # mat[:, 8] = preds[:, 0] # mat[:, 9] = preds[:, 1] print(preds)