-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved GRU and Skip GRU to skip_gru.py. Implemented SkipGRU. Added SkipGRU and ResNet to LSTNet.
- Loading branch information
1 parent
67b6726
commit d5f9674
Showing
2 changed files
with
53 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,45 @@ | ||
import keras | ||
from keras.saving import register_keras_serializable | ||
from bayesflow.experimental.types import Tensor | ||
from keras import layers, Sequential | ||
# from bayesflow.experimental.types import Tensor | ||
from tensorflow import Tensor | ||
|
||
@register_keras_serializable(package="bayesflow.networks") | ||
@register_keras_serializable(package="bayesflow.networks.skip_gru") | ||
class SkipGRU(keras.Model): | ||
# TODO | ||
pass | ||
def __init__(self, gru_out: int, skip_steps: list[int], **kwargs): | ||
super().__init__(**kwargs) | ||
self.gru_out = gru_out | ||
self.skip_steps = skip_steps | ||
self.gru = layers.GRU(gru_out) | ||
self.skip_grus = [layers.GRU(gru_out) for _ in range(len(self.skip_steps))] | ||
|
||
def call(self, x: Tensor) -> Tensor: | ||
# Standard GRU | ||
# In: (batch, reduced time steps, cnn_out) | ||
gru = self.gru(x) # -> (batch, gru_out) | ||
|
||
# x = C, gru = R | ||
|
||
# Skip GRU | ||
batch_size = x.shape[0] | ||
reduced_steps = x.shape[1] | ||
for i, skip_step in enumerate(self.skip_steps): | ||
# Reshape, remove skipped time points | ||
skip_length = reduced_steps // skip_step | ||
s = x[:, -skip_length * skip_step:, :] # -> (batch, shrinked time steps, cnn_out) | ||
s1 = keras.ops.reshape(s, (s.shape[0], s.shape[2], skip_length, skip_step)) # -> (batch, cnn_out, skip_length, skip_step) | ||
s2 = keras.ops.transpose(s1, [0, 3, 2, 1]) # -> (batch, skip step, skip_length, cnn_out) | ||
s3 = keras.ops.reshape(s2, (s2.shape[0] * s2.shape[1], s2.shape[2], s2.shape[3])) # -> (batch * skip step, skip_length, cnn_out) | ||
|
||
# GRU on remaining data | ||
s4 = self.skip_grus[i](s3) # -> (batch * skip step, gru_out) | ||
s5 = keras.ops.reshape(s4, (batch_size, skip_step * s4.shape[1])) # -> (batch, skip step * gru_out) | ||
|
||
# Concat | ||
gru = keras.ops.concatenate([gru, s5], axis=1) # -> (batch, gru_out * skip step * 2) | ||
|
||
return gru | ||
|
||
def build(self, input_shape): | ||
super().build(input_shape) | ||
self(keras.KerasTensor(input_shape)) |