keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.86k stars 19.44k forks source link

I am having a similiar problem, can anybody help me fix it ? My problem is a multi-class problem having 12 classes. #10467

Closed illusionic closed 3 years ago

illusionic commented 6 years ago
input = Input(shape=(max_sequence_size,))

embedding = Embedding(num_amino_acids, embedding_dims, input_length=max_sequence_size)(input)

x = Convolution1D(250, 15, activation='relu', subsample_length=1)(embedding)
x = Dropout(0.3)(x)

x = Convolution1D(100, 15, activation='relu', subsample_length=1)(x)
x = Dropout(0.3)(x)
x = GlobalAveragePooling1D()(x)
# x = Flatten()(x)

if target_function != '':
    output_nodes = max_num_functions# 2
    output_activation = 'sigmoid' # 'softmax'
else:
    output_nodes = max_num_functions
    output_activation = 'sigmoid'

x = Dense(output_nodes)(x)
x = BatchNormalization()(x) 
output = Activation(output_activation)(x)

loss_fn = 'categorical_crossentropy' model.compile(loss=loss_fn, optimizer='adam', metrics=['accuracy', 'recall', 'precision', 'fbeta_score'])

gives the following error: ValueError: Error when checking model target: expected activation_6 to have shape (None, 12) but got array with shape (7L, 1L)

maxpumperla commented 6 years ago

@illusionic impossible to help without the full model, could you please update your listing and at the very least provide info on embedding_dims,max_sequence_size etc.? Maybe create a gist with your model and your full stack trace?

illusionic commented 6 years ago

@maxpumperla

`from keras.preprocessing import sequence from keras.models import Sequential, Model from keras.layers import Dense, Dropout, Activation from keras.layers import Embedding from keras.layers import Convolution1D, MaxPooling1D, ZeroPadding1D from keras.layers.pooling import GlobalMaxPooling1D, GlobalAveragePooling1D from keras.layers.core import Flatten from keras.layers.recurrent import LSTM from keras.layers import merge, Input from keras import backend as K from keras.callbacks import CSVLogger, ModelCheckpoint from keras.layers.normalization import BatchNormalization import numpy as np import logging

embedding_dims = 23

def get_lstm_model(num_amino_acids, max_sequence_size, max_num_functions): logging.info('Building LSTM model using functional API ...') logging.debug("Embedding dims = " + str(embedding_dims))

input = Input(shape=(max_sequence_size,))

embedding = Embedding(num_amino_acids, embedding_dims, input_length=max_sequence_size, dropout=0.2)(input)
# x = BatchNormalization()(embedding)
x = LSTM(200, dropout_W=0.2, dropout_U=0.2, return_sequences=True)(embedding)
x = LSTM(200, dropout_W=0.2, dropout_U=0.2)(x)

x = Dense(max_num_functions)(x)
x = BatchNormalization()(x) # can also try to do this after the activation (didn't work)
output = Activation('sigmoid')(x)

model = Model([input], output)
model.summary()
return model

def get_cnn_model(num_amino_acids, max_sequence_size, max_num_functions, target_function): logging.info('Building CNN model using functional API ...') logging.debug("Embedding dims = " + str(embedding_dims))

# no need to mention the num_amino_acids when using functional API
input = Input(shape=(max_sequence_size,))

embedding = Embedding(num_amino_acids, embedding_dims, input_length=max_sequence_size)(input)

x = Convolution1D(250, 15, activation='relu', subsample_length=1)(embedding)
x = Dropout(0.3)(x)

x = Convolution1D(100, 15, activation='relu', subsample_length=1)(x)
x = Dropout(0.3)(x)

# x = ZeroPadding1D((0,20))(x)
#
# z = Convolution1D(100, 5, subsample_length=1)(embedding)
# z = BatchNormalization()(z)
# z = Activation('sigmoid')(z)
# z = Dropout(0.5)(z)
#
# z = Convolution1D(50, 5, subsample_length=1)(z)
# z = BatchNormalization()(z)
# z = Activation('sigmoid')(z)
# z = Dropout(0.5)(z)

# residual connection
# x = merge([x, z], mode='concat')

x = GlobalAveragePooling1D()(x)
#x = Flatten()(x)

if target_function != '':
    output_nodes = max_num_functions
    output_activation = 'softmax' # 'softmax'
else:
    output_nodes = max_num_functions
    output_activation = 'softmax'

x = Dense(output_nodes)(x)
x = BatchNormalization()(x) # can also try to do this after the activation (didn't work)
output = Activation(output_activation)(x)

model = Model([input], output)
model.summary()
return model

` Below is loss function code: loss_fn = 'categorical_crossentropy' model.compile(loss=loss_fn, optimizer='adam', metrics=['accuracy', 'recall', 'precision', 'fbeta_score'])

Embedding_dims=23 ,max_sequence_size is 2000, max_num_functions=12. The error i am having is: ValueError: Error when checking model target: expected activation_8 to have shape (None, 12) but got array with shape (94L, 1L).

illusionic commented 6 years ago

I am trying to do multiclass classification having 12 classes e:g class1 is represented as '0005524'. Dont have idea on how to fix the shape mismatch error on output node.

maxpumperla commented 6 years ago

sorry, still very difficult to understand. Can you make sure this renders properly or use a gist.

Maybe add your model summary (so that I know what activation_8 is etc.) and your full stack trace? thanks

illusionic commented 6 years ago

@maxpumperla

00001-console.txt

The problem is when i do max_num_functions = 1 , the code compiles successfully but when i change max_num_functions = 5 the code stops execution with the following error :

Using Theano backend. visualization.py:2: UserWarning: This call to matplotlib.use() has no effect because the backend has already been chosen; matplotlib.use() must be called before pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time.

The backend was originally set to 'module://ipykernel.pylab.backend_inline' by the following code: File "C:\Users\Illusion\Anaconda2\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 268, in main() File "C:\Users\Illusion\Anaconda2\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 252, in main kernel.initialize() File "", line 2, in initialize File "C:\Users\Illusion\Anaconda2\lib\site-packages\traitlets\config\application.py", line 87, in catch_config_error return method(app, *args, kwargs) File "C:\Users\Illusion\Anaconda2\lib\site-packages\ipykernel\kernelapp.py", line 472, in initialize self.init_code() File "C:\Users\Illusion\Anaconda2\lib\site-packages\IPython\core\shellapp.py", line 266, in init_code self._run_exec_lines() File "C:\Users\Illusion\Anaconda2\lib\site-packages\IPython\core\shellapp.py", line 292, in _run_exec_lines self.shell.run_cell(line, store_history=False) File "C:\Users\Illusion\Anaconda2\lib\site-packages\ipykernel\zmqshell.py", line 537, in run_cell return super(ZMQInteractiveShell, self).run_cell(*args, *kwargs) File "C:\Users\Illusion\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2724, in run_cell self.events.trigger('post_run_cell') File "C:\Users\Illusion\Anaconda2\lib\site-packages\IPython\core\events.py", line 74, in trigger func(args, kwargs) File "C:\Users\Illusion\Anaconda2\lib\site-packages\ipykernel\pylab\backend_inline.py", line 160, in configure_once activate_matplotlib(backend) File "C:\Users\Illusion\Anaconda2\lib\site-packages\IPython\core\pylabtools.py", line 315, in activate_matplotlib matplotlib.pyplot.switch_backend(backend) File "C:\Users\Illusion\Anaconda2\lib\site-packages\matplotlib\pyplot.py", line 231, in switch_backend matplotlib.use(newbackend, warn=False, force=True) File "C:\Users\Illusion\Anaconda2\lib\site-packages\matplotlib__init.py", line 1410, in use reload(sys.modules['matplotlib.backends']) File "C:\Users\Illusion\Anaconda2\lib\site-packages\matplotlib\backends\init__.py", line 16, in line for line in traceback.format_stack()

mpl.use('Agg') Experiment ID: 00001 Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/Illusion/.spyder/src/train.py', wdir='C:/Users/Illusion/.spyder/src')

File "C:\Users\Illusion\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)

File "C:\Users\Illusion\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/Illusion/.spyder/src/train.py", line 345, in (model, best_fbeta_score) = train(X_train, y_train, X_test, y_test, target_function)

File "C:/Users/Illusion/.spyder/src/train.py", line 218, in train verbose=1)

File "C:\Users\Illusion\Anaconda2\lib\site-packages\keras\engine\training.py", line 1068, in fit batch_size=batch_size)

File "C:\Users\Illusion\Anaconda2\lib\site-packages\keras\engine\training.py", line 985, in _standardize_user_data exception_prefix='model target')

File "C:\Users\Illusion\Anaconda2\lib\site-packages\keras\engine\training.py", line 113, in standardize_input_data str(array.shape))

ValueError: Error when checking model target: expected activation_1 to have shape (None, 5) but got array with shape (3204L, 1L)

illusionic commented 6 years ago

@maxpumperla what is your opinion on fixing this error ? code files are here at :https://github.com/recluze/deepseq/tree/master/src

tomastheod-ITI commented 6 years ago

@illusionic What are the dimensions of your target variable y_train? It seems that max_num_functions = 5 changes what your model outputs, not the dimension of target variable y_train. You have to change y_train to have 5 features as well in this case.