我有一个类似的问题,我无法找到错误。
这是我的代码:
# 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(最近一次调用last)in()----> 1分类(" 今天&#34)
分类中的(句子)5 def classify(句子):6#generate 来自模型的概率----> 7结果= model.predict([bow(sentence,words)])8#过滤下面的预测 阈值9结果= [[i,r]表示i,r表示枚举(结果)if R> ERROR_THRESHOLD]
/Library/Python/2.7/site-packages/tflearn/models/dnn.pyc in 预测(自我,X)255""" 256 feed_dict = feed_dict_builder(X,无, self.inputs,None) - > 257返回self.predictor.predict(feed_dict) 258 259 def predict_label(self,X):
/Library/Python/2.7/site-packages/tflearn/helpers/evaluator.pyc in 如果len(self.tensors)==,则预测(self,feed_dict)67 prediction = [] 68 1:---> 69返回self.session.run(self.tensors [0], feed_dict = feed_dict)70 else:71表示self.tensors中的输出:
/Library/Python/2.7/site-packages/tensorflow/python/client/session.pyc 在运行中(self,fetches,feed_dict,options,run_metadata)776尝试:777 result = self._run(None,fetches,feed_dict,options_ptr, - > 778 run_metadata_ptr)779如果run_metadata:780 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/Library/Python/2.7/site-packages/tensorflow/python/client/session.pyc 在_run中(self,handle,fetches,feed_dict,options,run_metadata)959 '无法为Tensor%r提供形状%r的值,' 960'有形状 %R' - > 961%(np_val.shape,subfeed_t.name, str(subfeed_t.get_shape())))962如果没有 self.graph.is_feedable(subfeed_t):963引发ValueError(' Tensor%s可能 没有被喂食。' %subfeed_t)
ValueError:无法为Tensor u' InputData / X:0'提供形状值(48,),其形状为'(?,48)'
任何人都可以告诉我为什么它的形状不合适?
答案 0 :(得分:0)
您已向模型提供数据,其形状为(48,)
,您必须将其重新整形为(?,48)
import numpy as np
data = np.zeros(48)
print(data.shape)
#>>>(48,)
data =np.array([data])
print(data.shape)
#>>>(1, 48)