-
Notifications
You must be signed in to change notification settings - Fork 27
/
Sioux_Falls_3hour.py
813 lines (495 loc) · 21.4 KB
/
Sioux_Falls_3hour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# coding: utf-8
# In[25]:
import numpy as np
import pandas as pd
import datetime
import glob
import os.path
from pandas.compat import StringIO
# ### NREL Bird Model implementation: for obtaining clear sky GHI
# In[26]:
import itertools
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# In[27]:
#get_ipython().magic('matplotlib inline')
sns.set_color_codes()
# In[28]:
import pvlib
from pvlib import clearsky, atmosphere
from pvlib.location import Location
# In[29]:
sif = Location(43.544,-96.73, 'US/Central', 448.086, 'Sioux Falls')
# In[30]:
times2009 = pd.DatetimeIndex(start='2009-01-01', end='2010-01-01', freq='1min',
tz=sif.tz) # 12 months of 2009 - For testing
times2010and2011 = pd.DatetimeIndex(start='2010-01-01', end='2012-01-01', freq='1min',
tz=sif.tz) # 24 months of 2010 and 2011 - For training
# In[32]:
cs_2009 = sif.get_clearsky(times2009)
cs_2010and2011 = sif.get_clearsky(times2010and2011) # ineichen with climatology table by default
#cs_2011 = bvl.get_clearsky(times2011)
# In[33]:
cs_2009.drop(['dni','dhi'],axis=1, inplace=True) #updating the same dataframe by dropping two columns
cs_2010and2011.drop(['dni','dhi'],axis=1, inplace=True) #updating the same dataframe by dropping two columns
#cs_2011.drop(['dni','dhi'],axis=1, inplace=True) #updating the same dataframe by dropping two columns
# In[34]:
cs_2009.reset_index(inplace=True)
cs_2010and2011.reset_index(inplace=True)
#cs_2011.reset_index(inplace=True)
# In[35]:
cs_2009['index']=cs_2009['index'].apply(lambda x:x.to_datetime())
cs_2009['year'] = cs_2009['index'].apply(lambda x:x.year)
cs_2009['month'] = cs_2009['index'].apply(lambda x:x.month)
cs_2009['day'] = cs_2009['index'].apply(lambda x:x.day)
cs_2009['hour'] = cs_2009['index'].apply(lambda x:x.hour)
cs_2009['min'] = cs_2009['index'].apply(lambda x:x.minute)
# In[36]:
cs_2010and2011['index']=cs_2010and2011['index'].apply(lambda x:x.to_datetime())
cs_2010and2011['year'] = cs_2010and2011['index'].apply(lambda x:x.year)
cs_2010and2011['month'] = cs_2010and2011['index'].apply(lambda x:x.month)
cs_2010and2011['day'] = cs_2010and2011['index'].apply(lambda x:x.day)
cs_2010and2011['hour'] = cs_2010and2011['index'].apply(lambda x:x.hour)
cs_2010and2011['min'] = cs_2010and2011['index'].apply(lambda x:x.minute)
# In[37]:
print(cs_2009.shape)
print(cs_2010and2011.shape)
#print(cs_2011.shape)
# In[38]:
cs_2009.drop(cs_2009.index[-1], inplace=True)
cs_2010and2011.drop(cs_2010and2011.index[-1], inplace=True)
#cs_2011.drop(cs_2011.index[-1], inplace=True)
# In[39]:
print(cs_2009.shape)
print(cs_2010and2011.shape)
#print(cs_2011.shape)
# In[40]:
cs_2010and2011.head()
# ### Import files from each year in a separate dataframe
#
# - year integer year, i.e., 1995
# - jday integer Julian day (1 through 365 [or 366])
# - month integer number of the month (1-12)
# - day integer day of the month(1-31)
# - hour integer hour of the day (0-23)
# - min integer minute of the hour (0-59)
# - dt real decimal time (hour.decimalminutes, e.g., 23.5 = 2330)
# - zen real solar zenith angle (degrees)
# - dw_solar real downwelling global solar (Watts m^-2)
# - uw_solar real upwelling global solar (Watts m^-2)
# - direct_n real direct-normal solar (Watts m^-2)
# - diffuse real downwelling diffuse solar (Watts m^-2)
# - dw_ir real downwelling thermal infrared (Watts m^-2)
# - dw_casetemp real downwelling IR case temp. (K)
# - dw_dometemp real downwelling IR dome temp. (K)
# - uw_ir real upwelling thermal infrared (Watts m^-2)
# - uw_casetemp real upwelling IR case temp. (K)
# - uw_dometemp real upwelling IR dome temp. (K)
# - uvb real global UVB (milliWatts m^-2)
# - par real photosynthetically active radiation (Watts m^-2)
# - netsolar real net solar (dw_solar - uw_solar) (Watts m^-2)
# - netir real net infrared (dw_ir - uw_ir) (Watts m^-2)
# - totalnet real net radiation (netsolar+netir) (Watts m^-2)
# - temp real 10-meter air temperature (?C)
# - rh real relative humidity (%)
# - windspd real wind speed (ms^-1)
# - winddir real wind direction (degrees, clockwise from north)
# - pressure real station pressure (mb)
#
# In[41]:
cols = ['year', 'jday', 'month', 'day','hour','min','dt','zen','dw_solar','dw_solar_QC','uw_solar',
'uw_solar_QC', 'direct_n','direct_n_QC','diffuse', 'diffuse_QC', 'dw_ir', 'dw_ir_QC', 'dw_casetemp',
'dw_casetemp_QC', 'dw_dometemp','dw_dometemp_QC','uw_ir', 'uw_ir_QC', 'uw_casetemp','uw_casetemp_QC',
'uw_dometemp','uw_dometemp_QC','uvb','uvb_QC','par','par_QC','netsolar','netsolar_QC','netir','netir_QC',
'totalnet','totalnet_QC','temp','temp_QC','rh','rh_QC','windspd','windspd_QC','winddir','winddir_QC',
'pressure','pressure_QC']
# In[42]:
path = r'./data/Sioux_Falls/Exp_1_train'
all_files = glob.glob(path + "/*.dat")
all_files.sort()
df_big_train = pd.concat([pd.read_csv(f, skipinitialspace = True, quotechar = '"',skiprows=(2),delimiter=' ',
index_col=False,header=None, names=cols) for f in all_files],ignore_index=True)
df_big_train.shape
# In[43]:
path = r'./data/Sioux_Falls/Exp_1_test'
all_files = glob.glob(path + "/*.dat")
all_files.sort()
df_big_test = pd.concat((pd.read_csv(f, skipinitialspace = True, quotechar = '"',skiprows=(2),delimiter=' ',
index_col=False,header=None, names=cols) for f in all_files),ignore_index=True)
df_big_test.shape
# In[44]:
df_big_test[df_big_test['dw_solar']==-9999.9].shape
# ### Merging Clear Sky GHI And the big dataframe
# In[45]:
df_train = pd.merge(df_big_train, cs_2010and2011, on=['year','month','day','hour','min'])
df_train.shape
# In[46]:
df_test = pd.merge(df_big_test, cs_2009, on=['year','month','day','hour','min'])
df_test.shape
# In[47]:
df_train.drop(['index'],axis=1, inplace=True) #updating the same dataframe by dropping the index columns from clear sky model
df_test.drop(['index'], axis=1, inplace=True)
# In[48]:
df_train.shape
# ### Managing missing values
# In[49]:
# Resetting index
df_train.reset_index(drop=True, inplace=True)
df_test.reset_index(drop=True, inplace=True)
# In[50]:
# Dropping rows with two or more -9999.9 values in columns
# In[51]:
# Step1: Get indices of all rows with 2 or more -999
missing_data_indices = np.where((df_train <=-9999.9).apply(sum, axis=1)>=2)[0]
# Step2: Drop those indices
df_train.drop(missing_data_indices, axis=0, inplace=True)
# Checking that the rows are dropped
df_train.shape
# In[52]:
missing_data_indices_test = np.where((df_test <= -9999.9).apply(sum, axis=1)>=2)[0]
df_test.drop(missing_data_indices_test, axis=0, inplace=True)
df_test.shape
# In[53]:
# For the rows with only one cell as -9999.9, replacing this cell with the mean of the column
# #### First resetting index after dropping rows in the previous part of the code
# In[54]:
# 2nd time - Reseting Index
df_train.reset_index(drop=True, inplace=True)
df_test.reset_index(drop=True, inplace=True)
# In[55]:
one_miss_train_idx = np.where((df_train <=-9999.9).apply(sum, axis=1)==1)[0]
# In[56]:
len(one_miss_train_idx)
# In[57]:
df_train.shape
# In[58]:
col_names = df_train.columns
from collections import defaultdict
stats = defaultdict(int)
total_single_missing_values = 0
for name in col_names:
col_mean = df_train[~(df_train[name] == -9999.9)][name].mean()
missing_indices = np.where((df_train[name] == -9999.9))
stats[name] = len(missing_indices[0])
df_train[name].loc[missing_indices] = col_mean
total_single_missing_values += sum(df_train[name] == -9999.9)
# In[59]:
df_col_min = df_train.apply(min, axis=0)
df_col_max = df_train.apply(max, axis =0)
#print(df_col_min, df_col_max)
# In[60]:
train = np.where((df_train <=-9999.9).apply(sum, axis=1)==1)[0]
# In[61]:
len(train)
# In[62]:
# doing the same thing on test dataset
# In[63]:
one_miss_test_idx = np.where((df_test <=-9999.9).apply(sum, axis=1)==1)[0]
len(one_miss_test_idx)
# In[64]:
col_names_test = df_test.columns
from collections import defaultdict
stats_test = defaultdict(int)
total_single_missing_values_test = 0
for name in col_names_test:
col_mean = df_test[~(df_test[name] == -9999.9)][name].mean()
missing_indices = np.where((df_test[name] == -9999.9))
stats_test[name] = len(missing_indices[0])
df_test[name].loc[missing_indices] = col_mean
total_single_missing_values_test += sum(df_test[name] == -9999.9)
# In[65]:
test = np.where((df_test <=-9999.9).apply(sum, axis=1)==1)[0]
# In[66]:
len(test)
# In[67]:
df_train.shape
# In[68]:
df_test.shape
# ### Exploratory Data Analysis
# In[69]:
dw_solar_everyday = df_test.groupby(['jday'])['dw_solar'].mean()
ghi_everyday = df_test.groupby(['jday'])['ghi'].mean()
j_day = df_test['jday'].unique()
# In[70]:
fig = plt.figure()
axes1 = fig.add_axes([0.1,0.1,0.8,0.8])
#axes2 = fig.add_axes([0.1,0.1,0.8,0.8])
axes1.scatter(j_day,dw_solar_everyday,label='Observed dw_solar',color='red')
axes1.scatter(j_day, ghi_everyday, label='Clear Sky GHI',color='green')
axes1.set_xlabel('Days')
axes1.set_ylabel('Solar Irradiance (Watts /m^2)')
axes1.set_title('Solar Irradiance - Test Year 2009')
axes1.legend(loc='best')
fig.savefig('./RNN Paper Results/Exp1_2/Sioux_Falls/3hour_Figure2.jpg', bbox_inches = 'tight')
# In[71]:
sns.jointplot(x=dw_solar_everyday,y=ghi_everyday,kind='reg')
#plt.title('observed dw_solar vs clear sky ghi')
plt.xlabel('Observed global downwelling solar (Watts/m^2)')
plt.ylabel('Clear Sky GHI (Watts/m^2)')
plt.savefig('./RNN Paper Results/Exp1_2/Sioux_Falls/3hour_Figure 3', bbox_inches='tight')
# ### making the Kt (clear sky index at time t) column by first removing rows with ghi==0
# In[72]:
df_train = df_train[df_train['ghi']!=0]
df_test = df_test[df_test['ghi']!=0]
df_train['Kt'] = df_train['dw_solar']/df_train['ghi']
df_test['Kt'] = df_test['dw_solar']/df_test['ghi']
# In[ ]:
df_train.reset_index(inplace=True)
df_test.reset_index(inplace=True)
# In[ ]:
print("test Kt max: "+str(df_test['Kt'].max()))
print("test Kt min: "+str(df_test['Kt'].min()))
print("test Kt mean: "+str(df_test['Kt'].mean()))
print("\n")
print("train Kt max: "+str(df_train['Kt'].max()))
print("train Kt min: "+str(df_train['Kt'].min()))
print("train Kt mean: "+str(df_train['Kt'].mean()))
# In[ ]:
plt.plot(df_train['Kt'])
# In[ ]:
plt.plot(df_test['Kt'])
# In[ ]:
df_train= df_train[df_train['Kt']< 5000]
df_train= df_train[df_train['Kt']> -1000]
df_test= df_test[df_test['Kt']< 5000]
df_test= df_test[df_test['Kt']> -1000]
# #### Group the data (train dataframe)
# In[ ]:
zen = df_train.groupby(['year','month','day','hour'])['zen'].mean()
dw_solar = df_train.groupby(['year','month','day','hour'])['dw_solar'].mean()
uw_solar = df_train.groupby(['year','month','day','hour'])['uw_solar'].mean()
direct_n = df_train.groupby(['year','month','day','hour'])['direct_n'].mean()
diffuse = df_train.groupby(['year','month','day','hour'])['diffuse'].mean()
dw_ir = df_train.groupby(['year','month','day','hour'])['dw_ir'].mean()
dw_casetemp = df_train.groupby(['year','month','day','hour'])['dw_casetemp'].mean()
dw_dometemp = df_train.groupby(['year','month','day','hour'])['dw_dometemp'].mean()
uw_ir = df_train.groupby(['year','month','day','hour'])['uw_ir'].mean()
uw_casetemp = df_train.groupby(['year','month','day','hour'])['uw_casetemp'].mean()
uw_dometemp = df_train.groupby(['year','month','day','hour'])['uw_dometemp'].mean()
uvb = df_train.groupby(['year','month','day','hour'])['uvb'].mean()
par = df_train.groupby(['year','month','day','hour'])['par'].mean()
netsolar = df_train.groupby(['year','month','day','hour'])['netsolar'].mean()
netir = df_train.groupby(['year','month','day','hour'])['netir'].mean()
totalnet = df_train.groupby(['year','month','day','hour'])['totalnet'].mean()
temp = df_train.groupby(['year','month','day','hour'])['temp'].mean()
rh = df_train.groupby(['year','month','day','hour'])['rh'].mean()
windspd = df_train.groupby(['year','month','day','hour'])['windspd'].mean()
winddir = df_train.groupby(['year','month','day','hour'])['winddir'].mean()
pressure = df_train.groupby(['year','month','day','hour'])['pressure'].mean()
ghi = df_train.groupby(['year','month','day','hour'])['ghi'].mean()
Kt = df_train.groupby(['year','month','day','hour'])['Kt'].mean()
# In[ ]:
df_new_train = pd.concat([zen,dw_solar,uw_solar,direct_n,diffuse,dw_ir,dw_casetemp,dw_dometemp,uw_ir,uw_casetemp,uw_dometemp,
uvb,par,netsolar,netir,totalnet,temp,rh,windspd,winddir,pressure,ghi,Kt], axis=1)
# In[ ]:
df_new_train.head()
# #### Groupdata - test dataframe
# In[ ]:
test_zen = df_test.groupby(['month','day','hour'])['zen'].mean()
test_dw_solar = df_test.groupby(['month','day','hour'])['dw_solar'].mean()
test_uw_solar = df_test.groupby(['month','day','hour'])['uw_solar'].mean()
test_direct_n = df_test.groupby(['month','day','hour'])['direct_n'].mean()
test_diffuse = df_test.groupby(['month','day','hour'])['diffuse'].mean()
test_dw_ir = df_test.groupby(['month','day','hour'])['dw_ir'].mean()
test_dw_casetemp = df_test.groupby(['month','day','hour'])['dw_casetemp'].mean()
test_dw_dometemp = df_test.groupby(['month','day','hour'])['dw_dometemp'].mean()
test_uw_ir = df_test.groupby(['month','day','hour'])['uw_ir'].mean()
test_uw_casetemp = df_test.groupby(['month','day','hour'])['uw_casetemp'].mean()
test_uw_dometemp = df_test.groupby(['month','day','hour'])['uw_dometemp'].mean()
test_uvb = df_test.groupby(['month','day','hour'])['uvb'].mean()
test_par = df_test.groupby(['month','day','hour'])['par'].mean()
test_netsolar = df_test.groupby(['month','day','hour'])['netsolar'].mean()
test_netir = df_test.groupby(['month','day','hour'])['netir'].mean()
test_totalnet = df_test.groupby(['month','day','hour'])['totalnet'].mean()
test_temp = df_test.groupby(['month','day','hour'])['temp'].mean()
test_rh = df_test.groupby(['month','day','hour'])['rh'].mean()
test_windspd = df_test.groupby(['month','day','hour'])['windspd'].mean()
test_winddir = df_test.groupby(['month','day','hour'])['winddir'].mean()
test_pressure = df_test.groupby(['month','day','hour'])['pressure'].mean()
test_ghi = df_test.groupby(['month','day','hour'])['ghi'].mean()
test_Kt = df_test.groupby(['month','day','hour'])['Kt'].mean()
# In[ ]:
df_new_test = pd.concat([test_zen,test_dw_solar,test_uw_solar,test_direct_n,test_diffuse,test_dw_ir,
test_dw_casetemp,test_dw_dometemp,test_uw_ir,test_uw_casetemp,test_uw_dometemp,
test_uvb,test_par,test_netsolar,test_netir,test_totalnet,test_temp,test_rh,
test_windspd,test_winddir,test_pressure,test_ghi,test_Kt], axis=1)
# In[ ]:
df_new_test.loc[2].xs(17,level='day')
# ### Shifting Kt values to make 1 hour ahead forecast
# #### Train dataset
# In[ ]:
levels_index= []
for m in df_new_train.index.levels:
levels_index.append(m)
# In[ ]:
for i in levels_index[0]:
for j in levels_index[1]:
df_new_train.loc[i].loc[j]['Kt'] = df_new_train.loc[i].loc[j]['Kt'].shift(-3)
# In[ ]:
df_new_train = df_new_train[~(df_new_train['Kt'].isnull())]
# #### Test dataset
# In[ ]:
levels_index2= []
for m in df_new_test.index.levels:
levels_index2.append(m)
# In[ ]:
for i in levels_index2[0]:
for j in levels_index2[1]:
df_new_test.loc[i].loc[j]['Kt'] = df_new_test.loc[i].loc[j]['Kt'].shift(-3)
# In[ ]:
df_new_test = df_new_test[~(df_new_test['Kt'].isnull())]
# In[ ]:
df_new_test[df_new_test['Kt']==df_new_test['Kt'].max()]
# ### Normalize train and test dataframe
# In[ ]:
train_norm = (df_new_train - df_new_train.mean()) / (df_new_train.max() - df_new_train.min())
test_norm = (df_new_test - df_new_test.mean()) / (df_new_test.max() - df_new_test.min())
# In[ ]:
train_norm.reset_index(inplace=True,drop=True)
test_norm.reset_index(inplace=True,drop=True)
# ### Making train and test sets with train_norm and test_norm
# #### finding the gcf (greatest common factor) of train and test dataset's length and chop off the extra rows to make it divisible with the batchsize
# In[ ]:
from fractions import gcd
gcd(train_norm.shape[0],test_norm.shape[0])
# In[ ]:
import math
def roundup(x):
return int(math.ceil(x / 100.0)) * 100
# In[ ]:
train_lim = roundup(train_norm.shape[0])
test_lim = roundup(test_norm.shape[0])
train_random = train_norm.sample(train_lim-train_norm.shape[0])
test_random = test_norm.sample(test_lim-test_norm.shape[0])
train_norm = train_norm.append(train_random)
test_norm = test_norm.append(test_random)
# In[ ]:
X1 = train_norm.drop('Kt',axis=1)
y1 = train_norm['Kt']
X2 = test_norm.drop('Kt',axis=1)
y2 = test_norm['Kt']
# In[ ]:
print("X1_train shape is {}".format(X1.shape))
print("y1_train shape is {}".format(y1.shape))
print("X2_test shape is {}".format(X2.shape))
print("y2_test shape is {}".format(y2.shape))
# In[ ]:
X_train = np.array(X1)
y_train = np.array(y1)
X_test = np.array(X2)
y_test = np.array(y2)
# ### start of RNN
# In[ ]:
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision.datasets as dsets
from torch.autograd import Variable
# In[ ]:
class RNNModel(nn.Module):
def __init__(self, input_dim, hidden_dim, layer_dim, output_dim):
super(RNNModel, self).__init__()
#Hidden Dimension
self.hidden_dim = hidden_dim
# Number of hidden layers
self.layer_dim = layer_dim
#Building the RNN
self.rnn = nn.RNN(input_dim, hidden_dim, layer_dim, batch_first=True, nonlinearity='relu')
# Readout layer
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
# Initializing the hidden state with zeros
# (layer_dim, batch_size, hidden_dim)
h0 = Variable(torch.zeros(self.layer_dim, x.size(0), self.hidden_dim))
#One time step (the last one perhaps?)
out, hn = self.rnn(x, h0)
# Indexing hidden state of the last time step
# out.size() --> ??
#out[:,-1,:] --> is it going to be 100,100
out = self.fc(out[:,-1,:])
# out.size() --> 100,1
return out
# In[ ]:
# Instantiating Model Class
input_dim = 22
hidden_dim = 15
layer_dim = 1
output_dim = 1
batch_size = 100
model = RNNModel(input_dim, hidden_dim, layer_dim, output_dim)
# Instantiating Loss Class
criterion = nn.MSELoss()
# Instantiate Optimizer Class
learning_rate = 0.001
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# converting numpy array to torch tensor
X_train = torch.from_numpy(X_train)
y_train = torch.from_numpy(y_train)
X_test = torch.from_numpy(X_test)
y_test = torch.from_numpy(y_test)
# initializing lists to store losses over epochs:
train_loss = []
test_loss = []
train_iter = []
test_iter = []
# In[ ]:
# Training the model
seq_dim = 1
n_iter =0
num_samples = len(X_train)
test_samples = len(X_test)
batch_size = 100
num_epochs = 1000
feat_dim = X_train.shape[1]
X_train = X_train.type(torch.FloatTensor)
y_train = y_train.type(torch.FloatTensor)
X_test = X_test.type(torch.FloatTensor)
y_test = y_test.type(torch.FloatTensor)
for epoch in range(num_epochs):
for i in range(0, int(num_samples/batch_size -1)):
features = Variable(X_train[i*batch_size:(i+1)*batch_size, :]).view(-1, seq_dim, feat_dim)
Kt_value = Variable(y_train[i*batch_size:(i+1)*batch_size])
#print("Kt_value={}".format(Kt_value))
optimizer.zero_grad()
outputs = model(features)
#print("outputs ={}".format(outputs))
loss = criterion(outputs, Kt_value)
train_loss.append(loss.data[0])
train_iter.append(n_iter)
#print("loss = {}".format(loss))
loss.backward()
optimizer.step()
n_iter += 1
if n_iter%100 == 0:
for i in range(0,int(test_samples/batch_size -1)):
features = Variable(X_test[i*batch_size:(i+1)*batch_size, :]).view(-1, seq_dim, feat_dim)
Kt_test = Variable(y_test[i*batch_size:(i+1)*batch_size])
outputs = model(features)
mse = np.sqrt(np.mean((Kt_test.data.numpy() - outputs.data.numpy().squeeze())**2)/num_samples)
test_iter.append(n_iter)
test_loss.append(mse)
print('Epoch: {} Iteration: {}. Train_MSE: {}. Test_MSE: {}'.format(epoch, n_iter, loss.data[0], mse))
# In[ ]:
print(len(test_loss))
#plt.plot(test_loss)
plt.plot(train_loss,'-')
#plt.ylim([0.000,0.99])
# In[ ]:
plt.plot(test_loss,'r')
# #### Demornamization
# In[ ]:
rmse = np.sqrt(mse)
# In[ ]:
rmse_denorm = (rmse * (df_new_test['Kt'].max() - df_new_test['Kt'].min()))+ df_new_test['Kt'].mean()
# In[ ]:
print("rmse_denorm=",rmse_denorm)
# In[ ]:
print(df_new_test['Kt'].describe())
# ### Saving train and test losses to a csv
# In[ ]:
df_trainLoss = pd.DataFrame(data={'Train Loss':train_loss, 'iteration':train_iter}, columns=['Train Loss','iteration'])
df_trainLoss.to_csv('./RNN Paper Results/Exp1_2/Sioux_Falls/3hour_SiouxFalls_TrainLoss.csv')
df_testLoss = pd.DataFrame(data={'Test Loss':test_loss, 'iteration':test_iter}, columns=['Test Loss','iteration'])
df_testLoss.to_csv('./RNN Paper Results/Exp1_2/Sioux_Falls/3hour_TestLoss.csv')
# In[ ]: