Closed illusionic closed 3 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?
@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).
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.
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
@maxpumperla
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
mpl.use('Agg') Experiment ID: 00001 Traceback (most recent call last):
File "
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
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)
@maxpumperla what is your opinion on fixing this error ? code files are here at :https://github.com/recluze/deepseq/tree/master/src
@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.
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)