-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSin Project.py
95 lines (61 loc) · 2.08 KB
/
Sin Project.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
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 20 22:05:06 2022
@author: Mohammad Mehdi Keramati
"""
import keras
import numpy as np
import pandas as pd
import keras
from keras import layers
t = np.arange(1500)
x = np.sin(0.08 * t)
x=pd.DataFrame(x)
######## Train, Test and Valivation Split #####
num_train_samples = int(0.5 * len(x))
num_val_samples = int(0.25 * len(x))
num_test_samples = len(x) - num_train_samples - num_val_samples
################ Normaliztion ####################
sampling_rate = 1
sequence_length = 100
delay = sampling_rate * (sequence_length) ######## -1 ????
batch_size = 10
test_1= x[:-delay]
test_2=x[delay:]
################# Transormation ####################
train_dataset = keras.utils.timeseries_dataset_from_array(
x[:-delay],
targets=x[delay:],
sampling_rate=sampling_rate,
sequence_length=sequence_length,
shuffle=True,
batch_size=batch_size,
start_index=0,
end_index=num_train_samples)
val_dataset = keras.utils.timeseries_dataset_from_array(
x[:-delay],
targets=x[delay:],
sampling_rate=sampling_rate,
sequence_length=sequence_length,
shuffle=True,
batch_size=batch_size,
start_index=num_train_samples,
end_index=num_train_samples + num_val_samples)
test_dataset = keras.utils.timeseries_dataset_from_array(
x[:-delay],
targets=x[delay:],
sampling_rate=sampling_rate,
sequence_length=sequence_length,
shuffle=True,
batch_size=batch_size,
start_index=num_train_samples + num_val_samples)
######################### Model ########################################
inputs = keras.Input(shape=(sequence_length, x.shape[-1]))
x = layers.GRU(32, recurrent_dropout=0.25)(inputs)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(1)(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer="adam", loss="mse", metrics=["mae"])
history = model.fit(train_dataset,
epochs=10,
validation_data=val_dataset)