Skip to content

Commit

Permalink
Models: Update models to keras 3
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianolorenti committed May 4, 2024
1 parent 68474a4 commit d5e594e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 61 deletions.
9 changes: 5 additions & 4 deletions ceruleo/models/keras/catalog/MSWRLRCN.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Tuple
import tensorflow as tf
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling1D, MaxPool1D
from ceruleo.models.keras.layers import ExpandDimension, ResidualShrinkageBlock
from tensorflow.keras import Input, Model, Sequential
from tensorflow.keras.layers import (
from keras import Input, Model, Sequential
from keras.layers import (

BatchNormalization,

Expand All @@ -14,6 +13,8 @@
LSTM,
Bidirectional,
Concatenate,
GlobalAveragePooling1D,
MaxPooling1D

)
from typing import Tuple
Expand All @@ -32,7 +33,7 @@ def ConvBlock(n_filters: int, kernel_size: int):
Conv1D(n_filters, kernel_size, padding='same'),
BatchNormalization(),
ReLU(),
MaxPool1D(2),
MaxPooling1D(2),
]
)

Expand Down
6 changes: 3 additions & 3 deletions ceruleo/models/keras/catalog/XCM.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import tensorflow as tf

from ceruleo.models.keras.layers import ExpandDimension, RemoveDimension
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import (
from keras import Input, Model
from keras.layers import (
Activation,
BatchNormalization,
Concatenate,
Expand Down Expand Up @@ -96,7 +96,7 @@ def XCM(input_shape: Tuple[int, int], *, n_filters: int = 128, filter_window: in

model = Model(
inputs=[model_input],
outputs=[output],
outputs=output,
)
return model, (model_fisrt_conv1d, model_fisrt_conv2d, model_regression)

Expand Down
89 changes: 45 additions & 44 deletions ceruleo/models/keras/layers.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import typing
import warnings
from typing import Tuple


import numpy as np
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras import backend as K
from tensorflow.keras import regularizers
from tensorflow.keras.layers import (Activation, BatchNormalization, Conv2D,
Dense, Flatten, Lambda, Layer, Permute,
Reshape)
import keras
from keras import Sequential
from keras import backend as K
from keras import regularizers
from keras.layers import (
Activation,
BatchNormalization,
Conv2D,
GlobalAveragePooling1D,
Dense,
Flatten,
Lambda,
Layer,
Permute,
maximum,
Reshape,
)
from tensorflow.python.framework import tensor_shape
from tensorflow.python.keras.layers.pooling import GlobalAveragePooling1D


def ExpandDimension(dim: int = -1):
return Lambda(lambda x: K.expand_dims(x, dim))
return Lambda(lambda x: keras.ops.expand_dims(x, dim))


def RemoveDimension(axis=0):
return Lambda(lambda x: K.squeeze(x, axis=axis))
return Lambda(lambda x: keras.ops.squeeze(x, axis=axis))


class ConcreteDropout(tf.keras.layers.Layer):
class ConcreteDropout(Layer):
"""
Concrete Dropout layer class from https://arxiv.org/abs/1705.07832.
Dropout Feature Ranking for Deep Learning Models
Expand Down Expand Up @@ -50,9 +58,8 @@ def __init__(
init_max=0.9,
name=None,
training=True,
**kwargs
**kwargs,
):

super(ConcreteDropout, self).__init__(name=name, **kwargs)
assert init_min <= init_max, "init_min must be lower or equal to init_max."

Expand Down Expand Up @@ -96,7 +103,6 @@ def concrete_dropout(self, p, x):
return x

def call(self, inputs, training=True):

p = K.sigmoid(self.p_logit)

dropout_regularizer = p * K.log(p)
Expand All @@ -110,11 +116,12 @@ def call(self, inputs, training=True):
return x


class ResidualShrinkageBlock(tf.keras.layers.Layer):
class ResidualShrinkageBlock(Layer):
"""
ResidualShrinkageBlock
"""

def build(self, input_shape):
self.blocks = []
for i in range(2):
Expand Down Expand Up @@ -171,63 +178,57 @@ def call(self, inputs):

zeros = sub - sub

n_sub = tf.keras.layers.maximum([sub, zeros])
n_sub = maximum([sub, zeros])

residual = tf.keras.backend.sign(residual) * n_sub
residual = keras.ops.sign(residual) * n_sub
residual = RemoveDimension(3)(residual)
return residual + inputs




class ZeroWeights(tf.keras.constraints.Constraint):


def __init__(self, l1:float):
self.l1 = l1
def __init__(self, l1: float):
self.l1 = l1

def __call__(self, w):

return (tf.math.multiply(w, tf.cast(tf.abs(w) > self.l1, tf.float32)) )
def __call__(self, w):
return tf.math.multiply(w, tf.cast(tf.abs(w) > self.l1, tf.float32))

def get_config(self):
return {'l1': self.l1}
def get_config(self):
return {"l1": self.l1}


class LASSOLayer(Layer):
"""
"""
LASSO Layer
Parameters:
l1: L1 regularization parameter
"""
def __init__(self, l1:float):

def __init__(self, l1: float):
super(LASSOLayer, self).__init__()
self.l1 = l1
self.kernel_regularizer = regularizers.L1(l1)


def build(self, input_shape):
W_size = np.prod(input_shape[1:])
self.w = self.add_weight(
shape=(W_size, ),
shape=(W_size,),
initializer="random_normal",
trainable=True,
regularizer=self.kernel_regularizer,
constraint=ZeroWeights(self.l1)
constraint=ZeroWeights(self.l1),
)



self.input_reshape = Reshape((W_size,))
self.output_reshape = Reshape(input_shape[1:])



def call(self, inputs):
x = self.input_reshape(inputs)


x = tf.math.multiply(self.w, x)

self.add_metric(tf.math.reduce_sum(tf.cast(tf.abs(self.w) > 0, tf.float32)), name="Number of features")

x = tf.math.multiply(self.w, x)

self.add_metric(
tf.math.reduce_sum(tf.cast(tf.abs(self.w) > 0, tf.float32)),
name="Number of features",
)
return self.output_reshape(x)
2 changes: 1 addition & 1 deletion docs/dataset/analysis/Sensor Validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Homepage = "https://github.com/lucianolorenti/ceruleo"
Documentation = "https://lucianolorenti.github.io/ceruleo/"

[project.optional-dependencies]
tensorflow = ["tensorflow >= 2.5", "keras < 3"]
tensorflow = ["tensorflow >= 2.5"]

test = [
"pytest",
Expand All @@ -62,4 +62,4 @@ doc = [
"sphinxcontrib.bibtex",
"mkdocs-matplotlib",

]
]
14 changes: 7 additions & 7 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import numpy as np
import pandas as pd
import tensorflow as tf
from numpy.random import seed
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Dense, Flatten
from xgboost import XGBRegressor

from ceruleo.dataset.ts_dataset import AbstractPDMDataset
from ceruleo.iterators.iterators import WindowedDatasetIterator
from ceruleo.iterators.shufflers import AllShuffled
Expand All @@ -12,7 +19,6 @@
from ceruleo.models.keras.catalog.CNLSTM import CNLSTM
from ceruleo.models.keras.catalog.InceptionTime import InceptionTime
from ceruleo.models.keras.catalog.MSWRLRCN import MSWRLRCN
from ceruleo.models.keras.catalog.MVCNN import MVCNN
from ceruleo.models.keras.catalog.MultiScaleConvolutional import (
MultiScaleConvolutionalModel,
)
Expand All @@ -36,12 +42,6 @@
from ceruleo.transformation import Transformer
from ceruleo.transformation.features.scalers import MinMaxScaler
from ceruleo.transformation.features.selection import ByNameFeatureSelector
from numpy.random import seed
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Dense, Flatten
from xgboost import XGBRegressor

seed(1)

Expand Down

0 comments on commit d5e594e

Please sign in to comment.