我也有一个类似的问题,但我找不到bug。在
这是我的代码:
# create a data structure to hold user context
context = {}
ERROR_THRESHOLD = 0.25
def classify(sentence):
# generate probabilities from the model
results = model.predict([bow(sentence, words)])
# filter out predictions below a threshold
results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD]
# sort by strength of probability
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append((classes[r[0]], r[1]))
# return tuple of intent and probability
return return_list
def response(sentence, userID='123', show_details=False):
results = classify(sentence)
# if we have a classification then find the matching intent tag
if results:
# loop as long as there are matches to process
while results:
for i in intents['intents']:
# find a tag matching the first result
if i['tag'] == results[0][0]:
# set context for this intent if necessary
if 'context_set' in i:
if show_details: print ('context:', i['context_set'])
context[userID] = i['context_set']
# check if this intent is contextual and applies to this user's conversation
if not 'context_filter' in i or \
(userID in context and 'context_filter' in i and i['context_filter'] == context[userID]):
if show_details: print ('tag:', i['tag'])
# a random response from the intent
return
print(random.choice(i['responses']))
results.pop(0)
classify("today")
但我得到了一个错误:
ValueError Traceback (most recent call last) in () ----> 1 classify(" today")
in classify(sentence) 5 def classify(sentence): 6 # generate probabilities from the model ----> 7 results = model.predict([bow(sentence, words)]) 8 # filter out predictions below a threshold 9 results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD]
/Library/Python/2.7/site-packages/tflearn/models/dnn.pyc in predict(self, X) 255 """ 256 feed_dict = feed_dict_builder(X, None, self.inputs, None) --> 257 return self.predictor.predict(feed_dict) 258 259 def predict_label(self, X):
/Library/Python/2.7/site-packages/tflearn/helpers/evaluator.pyc in predict(self, feed_dict) 67 prediction = [] 68 if len(self.tensors) == 1: ---> 69 return self.session.run(self.tensors[0], feed_dict=feed_dict) 70 else: 71 for output in self.tensors:
/Library/Python/2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata) 776 try: 777 result = self._run(None, fetches, feed_dict, options_ptr, --> 778 run_metadata_ptr) 779 if run_metadata: 780 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/Library/Python/2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata) 959 'Cannot feed value of shape %r for Tensor %r, ' 960 'which has shape %r' --> 961 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape()))) 962 if not self.graph.is_feedable(subfeed_t): 963 raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Cannot feed value of shape (48,) for Tensor u'InputData/X:0', which has shape '(?, 48)'
有人能告诉我为什么它的形状不合适吗?在
你已经给你的模型提供了一个形状为
(48,)
你必须把它重塑成(?,48)
相关问题 更多 >
编程相关推荐