Skip to content

๐ŸŒธ SeasonLayer

๐ŸŒธ SeasonLayer

๐ŸŸข Beginner โœ… Stable ๐Ÿ”ฅ Popular

๐ŸŽฏ Overview

The SeasonLayer adds seasonal information based on the month, encoding it as a one-hot vector for the four seasons: Winter, Spring, Summer, and Fall. This layer is essential for temporal feature engineering where seasonal patterns are important.

This layer takes date components and adds seasonal information, making it perfect for time series analysis, weather forecasting, and any application where seasonal patterns matter.

๐Ÿ” How It Works

The SeasonLayer processes date components through seasonal encoding:

  1. Month Extraction: Extracts month from date components
  2. Season Classification: Classifies months into four seasons:
  3. Winter: December (12), January (1), February (2)
  4. Spring: March (3), April (4), May (5)
  5. Summer: June (6), July (7), August (8)
  6. Fall: September (9), October (10), November (11)
  7. One-Hot Encoding: Creates one-hot vectors for each season
  8. Feature Combination: Combines original date components with seasonal features
  9. Output Generation: Produces 8-dimensional feature vector
graph TD
    A[Date Components: year, month, day, day_of_week] --> B[Extract Month]
    B --> C[Season Classification]

    C --> D[Winter: Dec, Jan, Feb]
    C --> E[Spring: Mar, Apr, May]
    C --> F[Summer: Jun, Jul, Aug]
    C --> G[Fall: Sep, Oct, Nov]

    D --> H[One-Hot Encoding]
    E --> H
    F --> H
    G --> H

    A --> I[Original Components]
    H --> I
    I --> J[Combined Features: 8 dimensions]

    style A fill:#e6f3ff,stroke:#4a86e8
    style J fill:#e8f5e9,stroke:#66bb6a
    style B fill:#fff9e6,stroke:#ffb74d
    style C fill:#f3e5f5,stroke:#9c27b0
    style H fill:#e1f5fe,stroke:#03a9f4

๐Ÿ’ก Why Use This Layer?

Challenge Traditional Approach SeasonLayer's Solution
Seasonal Patterns Manual season calculation ๐ŸŽฏ Automatic season classification and encoding
Temporal Features Separate season processing โšก Integrated seasonal information with date components
One-Hot Encoding Manual one-hot encoding ๐Ÿง  Built-in one-hot encoding for seasons
Feature Engineering Complex season extraction ๐Ÿ”— Simple seasonal feature addition

๐Ÿ“Š Use Cases

  • Weather Forecasting: Adding seasonal context to weather predictions
  • Sales Analysis: Analyzing seasonal sales patterns
  • Agricultural Planning: Incorporating seasonal information for crop planning
  • Energy Consumption: Predicting energy usage based on seasons
  • Event Planning: Considering seasonal factors in event planning

๐Ÿš€ Quick Start

Basic Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import keras
from kerasfactory.layers import SeasonLayer

# Create sample date components [year, month, day, day_of_week]
date_components = keras.ops.convert_to_tensor([
    [2023, 1, 15, 6],   # Winter (January)
    [2023, 4, 15, 5],   # Spring (April)
    [2023, 7, 15, 5],   # Summer (July)
    [2023, 10, 15, 6]   # Fall (October)
], dtype="float32")

# Apply seasonal encoding
season_layer = SeasonLayer()
seasonal_features = season_layer(date_components)

print(f"Input shape: {date_components.shape}")    # (4, 4)
print(f"Output shape: {seasonal_features.shape}") # (4, 8)
print(f"Seasonal features: {seasonal_features}")
# Output: [year, month, day, day_of_week, winter, spring, summer, fall]

In a Sequential Model

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import keras
from kerasfactory.layers import SeasonLayer

model = keras.Sequential([
    SeasonLayer(),
    keras.layers.Dense(32, activation='relu'),
    keras.layers.Dense(16, activation='relu'),
    keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

In a Functional Model

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import keras
from kerasfactory.layers import SeasonLayer

# Define inputs
inputs = keras.Input(shape=(4,))  # [year, month, day, day_of_week]

# Apply seasonal encoding
x = SeasonLayer()(inputs)

# Continue processing
x = keras.layers.Dense(32, activation='relu')(x)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Dense(16, activation='relu')(x)
outputs = keras.layers.Dense(1, activation='sigmoid')(x)

model = keras.Model(inputs, outputs)

Advanced Configuration

 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
# Advanced configuration with seasonal analysis
def create_seasonal_analysis_model():
    # Input for date components
    date_input = keras.Input(shape=(4,))  # [year, month, day, day_of_week]

    # Apply seasonal encoding
    seasonal_features = SeasonLayer()(date_input)

    # Process seasonal features
    x = keras.layers.Dense(64, activation='relu')(seasonal_features)
    x = keras.layers.BatchNormalization()(x)
    x = keras.layers.Dropout(0.2)(x)
    x = keras.layers.Dense(32, activation='relu')(x)

    # Multi-task output
    temperature = keras.layers.Dense(1, name='temperature')(x)
    humidity = keras.layers.Dense(1, name='humidity')(x)
    season = keras.layers.Dense(4, activation='softmax', name='season')(x)

    return keras.Model(date_input, [temperature, humidity, season])

model = create_seasonal_analysis_model()
model.compile(
    optimizer='adam',
    loss={'temperature': 'mse', 'humidity': 'mse', 'season': 'categorical_crossentropy'},
    loss_weights={'temperature': 1.0, 'humidity': 0.5, 'season': 0.3}
)

๐Ÿ“– API Reference

kerasfactory.layers.SeasonLayer

SeasonLayer for adding seasonal information based on month.

This layer adds seasonal information based on the month, encoding it as a one-hot vector for the four seasons: Winter, Spring, Summer, and Fall.

Classes

SeasonLayer
1
SeasonLayer(**kwargs)

Layer for adding seasonal information based on month.

This layer adds seasonal information based on the month, encoding it as a one-hot vector for the four seasons: Winter, Spring, Summer, and Fall.

Parameters:

Name Type Description Default
**kwargs

Additional layer arguments

{}
Input shape

Tensor with shape: (..., 4) containing [year, month, day, day_of_week]

Output shape

Tensor with shape: (..., 8) containing the original 4 components plus 4 one-hot encoded season values

Initialize the layer.

Source code in kerasfactory/layers/SeasonLayer.py
30
31
32
def __init__(self, **kwargs):
    """Initialize the layer."""
    super().__init__(**kwargs)
Functions
compute_output_shape
1
2
3
compute_output_shape(
    input_shape,
) -> tuple[tuple[int, ...], tuple[int, ...]]

Compute the output shape of the layer.

Parameters:

Name Type Description Default
input_shape

Shape of the input tensor

required

Returns:

Type Description
tuple[tuple[int, ...], tuple[int, ...]]

Output shape

Source code in kerasfactory/layers/SeasonLayer.py
106
107
108
109
110
111
112
113
114
115
116
117
118
def compute_output_shape(
    self,
    input_shape,
) -> tuple[tuple[int, ...], tuple[int, ...]]:
    """Compute the output shape of the layer.

    Args:
        input_shape: Shape of the input tensor

    Returns:
        Output shape
    """
    return input_shape[:-1] + (input_shape[-1] + 4,)

๐Ÿ”ง Parameters Deep Dive

No Parameters

  • Purpose: This layer has no configurable parameters
  • Behavior: Automatically classifies months into four seasons
  • Output: Always produces 8-dimensional output (4 original + 4 seasonal)

๐Ÿ“ˆ Performance Characteristics

  • Speed: โšกโšกโšกโšก Very fast - simple conditional logic
  • Memory: ๐Ÿ’พ Low memory usage - no additional parameters
  • Accuracy: ๐ŸŽฏ๐ŸŽฏ๐ŸŽฏ๐ŸŽฏ Excellent for seasonal feature extraction
  • Best For: Temporal data requiring seasonal information

๐ŸŽจ Examples

Example 1: Weather Prediction with Seasons

 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
import keras
import numpy as np
from kerasfactory.layers import SeasonLayer

# Create weather prediction model with seasonal information
def create_weather_model():
    # Input for date components
    date_input = keras.Input(shape=(4,))  # [year, month, day, day_of_week]

    # Apply seasonal encoding
    seasonal_features = SeasonLayer()(date_input)

    # Process seasonal features
    x = keras.layers.Dense(128, activation='relu')(seasonal_features)
    x = keras.layers.BatchNormalization()(x)
    x = keras.layers.Dropout(0.3)(x)
    x = keras.layers.Dense(64, activation='relu')(x)
    x = keras.layers.Dropout(0.2)(x)
    x = keras.layers.Dense(32, activation='relu')(x)

    # Weather predictions
    temperature = keras.layers.Dense(1, name='temperature')(x)
    humidity = keras.layers.Dense(1, name='humidity')(x)
    precipitation = keras.layers.Dense(1, name='precipitation')(x)
    wind_speed = keras.layers.Dense(1, name='wind_speed')(x)

    return keras.Model(date_input, [temperature, humidity, precipitation, wind_speed])

model = create_weather_model()
model.compile(
    optimizer='adam',
    loss={'temperature': 'mse', 'humidity': 'mse', 'precipitation': 'mse', 'wind_speed': 'mse'},
    loss_weights={'temperature': 1.0, 'humidity': 0.5, 'precipitation': 0.3, 'wind_speed': 0.2}
)

# Test with sample data
sample_dates = keras.ops.convert_to_tensor([
    [2023, 1, 15, 6],   # Winter Sunday
    [2023, 4, 15, 5],   # Spring Saturday
    [2023, 7, 15, 5],   # Summer Saturday
    [2023, 10, 15, 6]   # Fall Sunday
], dtype="float32")

predictions = model(sample_dates)
print(f"Weather predictions: {[p.shape for p in predictions]}")

Example 2: Sales Analysis with Seasonal Patterns

 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
# Analyze sales patterns with seasonal information
def create_sales_analysis_model():
    # Input for date components
    date_input = keras.Input(shape=(4,))  # [year, month, day, day_of_week]

    # Apply seasonal encoding
    seasonal_features = SeasonLayer()(date_input)

    # Process seasonal features
    x = keras.layers.Dense(64, activation='relu')(seasonal_features)
    x = keras.layers.BatchNormalization()(x)
    x = keras.layers.Dropout(0.2)(x)
    x = keras.layers.Dense(32, activation='relu')(x)

    # Sales predictions
    sales_volume = keras.layers.Dense(1, name='sales_volume')(x)
    revenue = keras.layers.Dense(1, name='revenue')(x)
    is_peak_season = keras.layers.Dense(1, activation='sigmoid', name='is_peak_season')(x)

    return keras.Model(date_input, [sales_volume, revenue, is_peak_season])

model = create_sales_analysis_model()
model.compile(
    optimizer='adam',
    loss={'sales_volume': 'mse', 'revenue': 'mse', 'is_peak_season': 'binary_crossentropy'},
    loss_weights={'sales_volume': 1.0, 'revenue': 0.5, 'is_peak_season': 0.3}
)

Example 3: Seasonal Feature Analysis

 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
# Analyze seasonal features produced by the layer
def analyze_seasonal_features():
    # Create sample date components for each season
    dates = keras.ops.convert_to_tensor([
        [2023, 1, 15, 6],   # Winter (January)
        [2023, 4, 15, 5],   # Spring (April)
        [2023, 7, 15, 5],   # Summer (July)
        [2023, 10, 15, 6]   # Fall (October)
    ], dtype="float32")

    # Apply seasonal encoding
    season_layer = SeasonLayer()
    seasonal_features = season_layer(dates)

    # Analyze seasonal patterns
    print("Seasonal Feature Analysis:")
    print("=" * 60)
    print("Date\t\tMonth\tWinter\tSpring\tSummer\tFall")
    print("-" * 60)

    for i, date in enumerate(dates):
        year, month, day, dow = date.numpy()
        year, month, day, dow, winter, spring, summer, fall = seasonal_features[i].numpy()

        print(f"{int(year)}-{int(month):02d}-{int(day):02d}\t{int(month)}\t"
              f"{int(winter)}\t{int(spring)}\t{int(summer)}\t{int(fall)}")

    return seasonal_features

# Analyze seasonal features
# seasonal_data = analyze_seasonal_features()

๐Ÿ’ก Tips & Best Practices

  • Input Format: Input must be [year, month, day, day_of_week] format
  • Season Classification: Automatically classifies months into four seasons
  • One-Hot Encoding: Produces one-hot encoded seasonal features
  • Feature Combination: Combines original date components with seasonal features
  • Neural Networks: Works well with neural networks for seasonal patterns
  • Integration: Combines well with other temporal processing layers

โš ๏ธ Common Pitfalls

  • Input Shape: Must be (..., 4) tensor with date components
  • Component Order: Must be [year, month, day, day_of_week] in that order
  • Data Type: Input should be float32 tensor
  • Missing Values: Doesn't handle missing values - preprocess first
  • Season Definition: Uses standard Northern Hemisphere season definitions

๐Ÿ“š Further Reading