Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

python - Tensorflow Cannot feed value Text Classifier

I try to build a tensorflow text classifier. I am new to tf. I get an error "Cannot feed value of shape (41,) for Tensor 'InputData/X:0', which has shape '(?, 41)'"

There a several questions at stack but I dont find any answer with some background information.

This is my code for my model:

from preprocessV2 import PP
from settings import *
import tensorflow as tf
import tflearn
import numpy

class tflModel:
    def __init__(self):
        tf.compat.v1.reset_default_graph()
        #len(PP.training[0])) --> 41
        net = tflearn.input_data(shape=[None, len(PP.training[0])])
        net = tflearn.fully_connected(net, 8)
        net = tflearn.fully_connected(net, 8)
        #len(PP.output[0])) --> 7
        net = tflearn.fully_connected(
        net, len(PP.output[0]), activation="softmax")
        net = tflearn.regression(net)
        self.model = tflearn.DNN(net)
        try:
            self.model.load("model.tflearn")
        except:
            self.create_model()

    def create_model(self):
        self.model.fit(PP.training, PP.output, n_epoch=1000, batch_size=8, show_metric=True)
        self.model.save("model.tflearn")

    def do_predict(self, inp):
        bag = PP.bag_of_words(inp)
        #len(bag) --> 41
        result = self.model.predict(bag) #Error occurs here "Cannot feed value of shape (41,) for Tensor 'InputData/X:0', which has shape '(?, 41)'"
        if settings.DEBUG == True: print(result)
        results_index = numpy.argmax(results)
        results_val = numpy.amax(results)
        tag = labels[results_index]
        r = {
            "tag": tag,
            "accuracy": results_val
        }
        if settings.DEBUG == True: print(r)
        return r

I wanted to train the model with texts with different lengths. The length of the training bag is 41. The length of the output bag is 7. What is this error message about? How do I change the model to make it work?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...