-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
66 lines (48 loc) · 2 KB
/
train.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
import os
import numpy as np
import pandas as pd
# os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
from tensorflow import keras
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import f1_score
# with tf.device('/cpu:0'):
def create_dataset(X, y, time_steps=1):
Xs, ys = [], []
for i in range(len(X) - time_steps):
v = X.iloc[i:(i + time_steps)].values
Xs.append(v)
ys.append(y.iloc[i + time_steps])
return np.array(Xs), np.array(ys)
time_step = 25
model = keras.Sequential()
model.add(keras.layers.LSTM(units=64, input_shape=(time_step, 1), return_sequences=True))
model.add(keras.layers.Dropout(rate=0.2))
model.add(keras.layers.LSTM(units=32, return_sequences=False))
model.add(keras.layers.Dropout(rate=0.2))
model.add(keras.layers.RepeatVector(n=time_step))
# model.add(keras.layers.LSTM(units=32, return_sequences=True))
# model.add(keras.layers.Dropout(rate=0.2))
model.add(keras.layers.LSTM(units=64, return_sequences=True))
model.add(keras.layers.Dropout(rate=0.2))
model.add(keras.layers.TimeDistributed(keras.layers.Dense(units=1)))
model.compile(loss='mae', optimizer='adam')
if __name__ == '__main__':
input_path = 'C:\\Users\\pc\\Desktop\\Anomaly Detection\\datasets\\selected datasets'
for file in os.listdir(input_path):
# df = pd.read_csv(f'C:\\Users\\pc\\Downloads\\Compressed\\time-series\\{file}.csv')
df = pd.read_csv(os.path.join(input_path, file))
df = df[df.label == 0]
print(f'####################On file {file} ############################')
scaler = StandardScaler()
df['value'] = scaler.fit_transform(df[['value']])
X_train, y_train = create_dataset(df[['value']], df.value,time_step)
print('Start training........ ')
history = model.fit(
X_train, y_train,
epochs=25,
batch_size=64,
validation_split=0.1,
shuffle=False
)
model.save('./models/new_model.h5')