'Error performing RandomSearchCV on 3 dimensional target variable : ValueError: Invalid shape for y:
How do I pass a 3dimentional target vector to RandomsearchCV?
Hello I am trying to build a NLP NER recognition model using Deep Learning.
When I fit the model alone i have no issues however when I try to fit the model using Randomsearch cv I get an error
ValueError: Invalid shape for y:
Here's my model
model = Sequential()
model.add(Embedding(input_dim=total_unique_words, output_dim=40,input_length=sentence_length))
model.add(SpatialDropout1D(0.1))
model.add(Bidirectional(LSTM(units=100, return_sequences=True, recurrent_dropout=0.1)))
model.add(TimeDistributed(Dense(18, activation='softmax')))
model.compile(optimizer=Adam(learning_rate=0.001) ,loss='categorical_crossentropy', metrics=['accuracy'])
when I fit the model
history = model.fit(np.array(X), np.array(y), batch_size=32, epochs=5, validation_split=0.2, verbose=1)
this works with no issues
for reference here's the dimention
np.array(X).shape
np.array(y).shape
np.array(X).ndim
np.array(y).ndim
o/p
(47959, 40)
(47959, 40, 18)
2
3
Now for the same model I am trying to tune hyper parameter below is my code
from tensorflow.keras.optimizers import Adam
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import RandomizedSearchCV
def get_model(output_dim,activation,bilstm_neurons,lr):
model = Sequential()
model.add(Embedding(input_dim=total_unique_words, output_dim=output_dim, input_length=sentence_length))
model.add(SpatialDropout1D(0.1))
model.add(Bidirectional(LSTM(units=bilstm_neurons, return_sequences=True, recurrent_dropout=0.1)))
model.add(TimeDistributed(Dense(18, activation='softmax')))
model.compile(optimizer=Adam(learning_rate=lr) ,loss='categorical_crossentropy', metrics=['accuracy'])
from sklearn.metrics import f1_score, make_scorer
f1 = make_scorer(f1_score , average='macro')
and when i try to fit using the below code
model = KerasClassifier(build_fn = get_model)
params = {'bilstm_neurons' : [100],
'output_dim' : [40],
'activation' : ['softmax'],
'lr' : [0.001],
'batch_size' : [32]
}
random_search = RandomizedSearchCV(model, param_distributions=params,cv=4)
random_search.fit(np.array(X),np.array(y))
I get an error stating
ValueError: Invalid shape for y: (47959, 40, 18)
Can someone please tell me how to fix this. I believe the RandomSearchCV is not capable of a 3 dimensional target. I might be completely wrong or is there a way to specify or am i looking at it completely wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
