Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DANN for time series prediction #131

Open
ceavilest opened this issue May 30, 2024 · 0 comments
Open

DANN for time series prediction #131

ceavilest opened this issue May 30, 2024 · 0 comments

Comments

@ceavilest
Copy link

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant