-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear-model.py
More file actions
61 lines (52 loc) · 1.46 KB
/
linear-model.py
File metadata and controls
61 lines (52 loc) · 1.46 KB
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
'''
This script build the weather prediction models
'''
'''
Import libraries
'''
import tensorflow as tf
import os
import pandas as pd
import datetime
import matplotlib.pyplot as plt
import numpy as np
from windowgenerator import WindowGenerator as wg
'''
Set Constants
'''
MAX_EPOCHS = 20
'''
Load the data
'''
train_df = pd.read_csv('train_data.csv', index_col='Date Time')
test_df = pd.read_csv('test_data.csv', index_col = 'Date Time')
'''
One-hour input linear model
'''
'We first design a one-step window generator'
window = wg(
input_width=1,
label_width=1,
shift=1,
train_df=train_df,
test_df=test_df,
label_columns=['T (degC)']
)
'''
We then build a linear deep feedforward network. If we do not define a activaion function, the
defauls is linear.
'''
linear = tf.keras.Sequential()
linear.add(tf.keras.layers.Dense(units=1))
linear.compile(loss=tf.keras.losses.MeanSquaredError(),
optimizer=tf.keras.optimizers.Adam(),
metrics=[tf.keras.metrics.MeanAbsoluteError()])
history = linear.fit(window.train, epochs=MAX_EPOCHS,
validation_data=window.test,)
'Lets visualize the model'
tf.keras.utils.plot_model(linear, to_file="linear_model.png", show_shapes=True)
'Save the training history'
history_df = pd.DataFrame(history.history)
history_df.to_csv('linear-model-training-history.csv')
'Save the model'
linear.save('trained-linear-model')