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 class giving error for multiclass classification problems #116

Closed
sunilnag opened this issue Nov 8, 2023 · 2 comments
Closed

DANN class giving error for multiclass classification problems #116

sunilnag opened this issue Nov 8, 2023 · 2 comments

Comments

@sunilnag
Copy link

sunilnag commented Nov 8, 2023

when i am executing the following code in colab following error is popping up (as given below

def my_feature_ext(input_shape=(1000,1)):
model = Sequential()
model.add(Conv1D(10, kernel_size=3,input_shape=input_shape))
model.add(Dropout(0.5))
model.add(Conv1D(10, kernel_size=3))
model.add(Dropout(0.5))
model.add(Conv1D(10, kernel_size=3))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
return model

def my_classifier():
model = Sequential()
model.add(Dense(256, activation='relu'))
model.add(Dense(4, activation='softmax'))
return model

def my_discriminator():
model = Sequential()
model.add(Dense(512, activation='relu'))
model.add(Dense(2, activation='softmax'))
return model

model_dann = DANN(my_feature_ext(), my_classifier(), my_discriminator(), metrics=["acc"],optimizer=Adam(0.001),lambda_=0., random_state=0)
model_dann.fit(data_source, label_source, data_target, epochs=100,verbose=1);
%%%%%%%%%%%%%%%%ERROR messages%%%%%%%%%%%%%
Epoch 1/100

InvalidArgumentError Traceback (most recent call last)
in <cell line: 2>()
1 model_dann = DANN(my_feature_ext(), my_classifier(), my_discriminator(), metrics=["acc"],optimizer=Adam(0.001),lambda_=0., random_state=0)
----> 2 model_dann.fit(data_src_fft, label_src, data_tgt_fft_up, epochs=100,verbose=1);

2 frames
/usr/local/lib/python3.10/dist-packages/adapt/base.py in fit(self, X, y, Xt, yt, domains, **fit_params)
1156 self.pretrain_ = False
1157
-> 1158 hist = super().fit(dataset, validation_data=validation_data, **fit_params)
1159
1160 for k, v in hist.history.items():

/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb

/usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
50 try:
51 ctx.ensure_initialized()
---> 52 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
53 inputs, attrs, num_outputs)
54 except core._NotOkStatusException as e:

InvalidArgumentError: Graph execution error:

Detected at node 'Reshape' defined at (most recent call last):
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.10/dist-packages/colab_kernel_launcher.py", line 37, in
ColabKernelApp.launch_instance()
File "/usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 992, in launch_instance
app.start()
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 619, in start
self.io_loop.start()
File "/usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 195, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
self._run_once()
File "/usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
handle._run()
File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 685, in
lambda f: self._run_callback(functools.partial(callback, future))
File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 738, in _run_callback
ret = callback()
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 825, in inner
self.ctx_run(self.run)
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 786, in run
yielded = self.gen.send(value)
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 361, in process_one
yield gen.maybe_future(dispatch(*args))
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
yielded = ctx_run(next, result)
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 261, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
yielded = ctx_run(next, result)
File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 539, in execute_request
self.do_execute(
File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
yielded = ctx_run(next, result)
File "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 302, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 539, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 2975, in run_cell
result = self._run_cell(
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3030, in _run_cell
return runner(coro)
File "/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 78, in pseudo_sync_runner
coro.send(None)
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3257, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3473, in run_ast_nodes
if (await self.run_code(code, result, async
=asy)):
File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 2, in <cell line: 2>
model_dann.fit(data_src_fft, label_src, data_tgt_fft_up, epochs=100,verbose=1);
File "/usr/local/lib/python3.10/dist-packages/adapt/base.py", line 1158, in fit
hist = super().fit(dataset, validation_data=validation_data, **fit_params)
File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1650, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in train_function
return step_function(self, iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1233, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1222, in run_step
outputs = model.train_step(data)
File "/usr/local/lib/python3.10/dist-packages/adapt/feature_based/_dann.py", line 140, in train_step
ys_pred = tf.reshape(ys_pred, tf.shape(ys))
Node: 'Reshape'
Input to reshape is a tensor with 128 values, but the requested shape has 32
[[{{node Reshape}}]] [Op:__inference_train_function_9015]

@antoinedemathelin
Copy link
Collaborator

Hi @sunilnag,
Thank you for your interest in the Adapt library!

I think your issue comes from the fact that label_source in your case is a 1D vector of shape (n,) with n the number of data. You should use tf.one_hot on it to provide a "Onehot" encoded matrix of shape (n, 4).

Moreover, I notice that you use the softmax activation for the discriminator. However, you should use the sigmoid activation with 1 neuron for the output layer (as the discriminator is always a binary classifier).

Here is an example which runs fine on colab:

!pip install adapt

from adapt.feature_based import DANN
import numpy as np
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv1D, Dropout, Flatten, Dense

data_source = np.random.randn(300, 1000, 1)
data_target = np.random.randn(300, 1000, 1)
label_source = tf.one_hot(np.random.choice(4, 300), 4).numpy()

def my_feature_ext(input_shape=(1000,1)):
  model = Sequential()
  model.add(Conv1D(10, kernel_size=3,input_shape=input_shape))
  model.add(Dropout(0.5))
  model.add(Conv1D(10, kernel_size=3))
  model.add(Dropout(0.5))
  model.add(Conv1D(10, kernel_size=3))
  model.add(Dropout(0.5))
  model.add(Flatten())
  model.add(Dense(256, activation='relu'))
  model.add(Dropout(0.5))
  return model

def my_classifier():
  model = Sequential()
  model.add(Dense(256, activation='relu'))
  model.add(Dense(4, activation='softmax'))
  return model

def my_discriminator():
  model = Sequential()
  model.add(Dense(512, activation='relu'))
  model.add(Dense(1, activation='sigmoid'))
  return model

model_dann = DANN(my_feature_ext(), my_classifier(), my_discriminator(), metrics=["acc"],optimizer=tf.keras.optimizers.legacy.Adam(0.001),lambda_=0., random_state=0)
model_dann.fit(data_source, label_source, data_target, epochs=100,verbose=1);

(By the way, from my own experience, the Adam optimizer provides worse performance than SGD for DANN in general...)

I hope it helps...
Best,

@sunilnag
Copy link
Author

sunilnag commented Nov 8, 2023

thank you for the prompt reply it worked nicely

@sunilnag sunilnag closed this as completed Nov 8, 2023
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

2 participants