Skip to content

DANN for time series prediction #131

@ceavilest

Description

@ceavilest

I was wondering if you have an example of modifying the vanilla get_task and encoder functions to handle time series data, so far this is what I have but it is not working, if you have any suggestions I would really appreciate it. It works but the predictions are all 0.

Example of my data (I have real data)

source_data = np.random.rand(118, 6, 26) # Shape (118, 6, 26)
target_data = np.random.rand(90, 6, 26) # Shape (90, 6, 26)

Labels (for example purposes, using random labels)

source_labels = np.random.randint(2, size=(118, 1))
target_labels = np.random.randint(2, size=(90, 1)) # True target labels for evaluation

import tensorflow as tf
from tensorflow.keras.layers import Input, LSTM, Dense, Dropout
from tensorflow.keras.models import Model

Define the encoder model

def create_encoder(input_shape):
input_layer = Input(shape=input_shape)
x = LSTM(64, return_sequences=True)(input_layer)
x = Dropout(0.5)(x)
x = LSTM(32)(x)
feature_output = Dense(16, activation='relu')(x)
return Model(inputs=input_layer, outputs=feature_output)

Define the task model

def create_task(input_shape):
feature_input = Input(shape=input_shape)
classifier_output = Dense(1, activation='sigmoid', name='classifier_output')(feature_input)
return Model(inputs=feature_input, outputs=classifier_output)

Define the discriminator model

def create_discriminator(input_shape):
feature_input = Input(shape=input_shape)
domain_output = Dense(1, activation='sigmoid', name='domain_output')(feature_input)
return Model(inputs=feature_input, outputs=domain_output)

input_shape = (6, 26)
encoder = create_encoder(input_shape)
task = create_task((16,))
discriminator = create_discriminator((16,))

from adapt.feature_based import DANN
from tensorflow.keras.optimizers.legacy import Adam, SGD, RMSprop, Adagrad

dann = DANN(encoder=encoder,
task=task,
discriminator=discriminator,
optimizer=Adam(learning_rate=0.001),
loss='mean_squared_error',
metrics=['accuracy'],
lambda_=1.0)

Fit the model on the source and target data

history = dann.fit(X=source_data, y=source_labels,
Xt=target_data, yt=target_labels,
epochs=100)

import pandas as pd
import matplotlib.pyplot as plt

pd.DataFrame(dann.history_).plot(figsize=(8, 5))
plt.title("Training history", fontsize=14); plt.xlabel("Epochs"); plt.ylabel("Scores")
plt.legend(ncol=2)
plt.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions