我正在尝试使用具有自己数据集的lstm创建一个简单的聊天机器人 而且我正在用一袋词的概念来提问。
我收到此错误
__init__.py
我的数据集:
InvalidArgumentError: logits and labels must have the same first dimension, got logits shape
[12,32] and labels shape [72] [[{{node
loss_23/lstm_72_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]
我的代码
{"intents": [
{"tag": "greeting",
"patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day"],
"responses": ["Hello, thanks for visiting", "Good to see you again", "Hi there, how can I help?"],
"context_set": ""
},
{"tag": "goodbye",
"patterns": ["Bye", "See you later", "Goodbye"],
"responses": ["See you later, thanks for visiting", "Have a nice day", "Bye! Come back again soon."]
},
{"tag": "thanks",
"patterns": ["Thanks", "Thank you", "That's helpful"],
"responses": ["Happy to help!", "Any time!", "My pleasure"]
},
{"tag": "hours",
"patterns": ["What hours are you open?", "What are your hours?", "When are you open?" ],
"responses": ["We're open every day 9am-9pm", "Our hours are 9am-9pm every day"]
},
{"tag": "payments",
"patterns": ["Do you take credit cards?", "Do you accept Mastercard?", "Are you cash only?" ],
"responses": ["We accept VISA, Mastercard and AMEX", "We accept most major credit cards"]
},
{"tag": "opentoday",
"patterns": ["Are you open today?", "When do you open today?", "What are your hours today?"],
"responses": ["We're open every day from 9am-9pm", "Our hours are 9am-9pm every day"]
}
]
}
from tensorflow.keras.layers import LSTM,Dense,Flatten
from tensorflow.keras import Sequential
import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
import numpy
import tflearn
import tensorflow
import random
import json
import pickle
with open("C:\\Users\\hp\\Desktop\\intents.json") as file:
data = json.load(file)
#bag of words
here
words = []
labels = []
docs_x = []
docs_y = []
print('array')
for intent in data["intents"]:
for pattern in intent["patterns"]:
wrds = nltk.word_tokenize(pattern)
words.extend(wrds)
docs_x.append(wrds)
docs_y.append(intent["tag"])
print('pattern')
if intent["tag"] not in labels:
labels.append(intent["tag"])
words = [stemmer.stem(w.lower()) for w in words if w != "?"]
print(words)
words = sorted(list(set(words)))
labels = sorted(labels)
training = []
output = []
out_empty = [0 for _ in range(len(labels))]
for x, doc in enumerate(docs_x):
bag = []
wrds = [stemmer.stem(w.lower()) for w in doc]
for w in words:
if w in wrds:
bag.append(1)
else:
bag.append(0)
output_row = out_empty
output_row[labels.index(docs_y[x])] = 1
print(output_row)
training.append(bag)
output.append(output_row)
training = numpy.array(training,dtype=numpy.float32).reshape(20,1,32)
output=numpy.array(output,dtype=numpy.float32).reshape(20,6,1)
model=Sequential()
model.add(LSTM(32,input_shape=(1,32),activation='relu',return_sequences=True))
model.add(LSTM(64,activation='relu',return_sequences=True))
model.add(LSTM(32,activation='softmax',return_sequences=True))
model.compile(optimizer='adam',metrics=['accuracy'],loss='sparse_categorical_crossentropy')
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(training,output,random_state=1,test_size=0.4)
model.fit(x_train,y_train,epochs=10,validation_data=(x_test,y_test))
1460 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
InvalidArgumentError Traceback (most recent call last)
<ipython-input-272-fae79c4d9f04> in <module>
----> 1 model.fit(x_train,y_train,epochs=10,validation_data=(x_test,y_test))
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y,
batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle,
class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq,
max_queue_size, workers, use_multiprocessing, **kwargs)
778 validation_steps=validation_steps,
779 validation_freq=validation_freq,
--> 780 steps_name='steps_per_epoch')
781
782 def evaluate(self,
\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py in
model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks,
val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch,
validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset,
steps_name, **kwargs)
361
362 # Get outputs.
--> 363 batch_outs = f(ins_batch)
364 if not isinstance(batch_outs, list):
365 batch_outs = [batch_outs]
~\Anaconda3\lib\site-packages\tensorflow\python\keras\backend.py in __call__(self, inputs)
3290
3291 fetched = self._callable_fn(*array_vals,
-> 3292 run_metadata=self.run_metadata)
3293 self._call_fetch_callbacks(fetched[-len(self._fetches):])
3294 output_structure = nest.pack_sequence_as(
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in __call__(self, *args,
**kwargs)
1456 ret = tf_session.TF_SessionRunCallable(self._session._session,
1457 self._handle, args,
-> 1458
1459 if run_metadata: