Reputation: 1
I have trained a text classification model using tflearn and successfully performed model evaluation. I want to calculate the precision, recall and f1 using sklearn. But when i tried to perform prediction on test labels i receive an error.
#creating our training data
training = []
output = []
#create an empty array for our output
output_empty = [0] * len(classes)
#training set, bag of words for each sentence
for doc in documents:
#initialize our bag of words
bag = []
#list of tokenized words for the pattern
pattern_words = doc[0]
# stem each word
pattern_words = [stemmer.stem(word.lower()) for word in pattern_words]
# create our bag of words array
for w in words:
bag.append(1) if w in pattern_words else bag.append(0)
# output is a '0' for each tag and '1' for current tag
output_row = list(output_empty)
output_row[classes.index(doc[1])] = 1
training.append([bag, output_row])
# shuffle our features and turn into np.array
random.shuffle(training)
training = np.array(training)
# create train and test lists
train_x = list(training[:,0])
train_y = list(training[:,1])
from sklearn.model_selection import train_test_split
train_x, test_x, train_y, test_y = train_test_split(train_x, train_y, test_size=0.10, random_state=10, stratify=train_y)
train_x = np.array(train_x)
train_y = np.array(train_y)
test_x=np.array(test_x)
test_y = np.array(test_y)
net = tflearn.input_data(shape=[None, len(train_x[0])])
net = tflearn.fully_connected(net, 128)
net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')
net = tflearn.regression(net)
# Define model and setup tensorboard
model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')
model.fit(train_x, train_y, n_epoch=10, batch_size=8, show_metric=True )
model.evaluate(test_x, test_y)
I am able to perform prediction on test_x but i get the error below when i perform prediction on test_y
predict_y = model.predict(test_y)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-56-33f24cfe9a52> in <module>
----> 1 predicted_y = model.predict(test_y)
2 #y_hatclass = model.predict_classes(test_x)
3 frames
/usr/local/lib/python3.8/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1162 if (not is_tensor_handle_feed and
1163 not subfeed_t.get_shape().is_compatible_with(np_val.shape)):
-> 1164 raise ValueError(
1165 f'Cannot feed value of shape {str(np_val.shape)} for Tensor '
1166 f'{subfeed_t.name}, which has shape '
ValueError: Cannot feed value of shape (143, 7) for Tensor InputData/X:0, which has shape (?, 55)
Please anyone help me with the way to go about it.
Upvotes: 0
Views: 48