无法挤压dim[1],预期尺寸为1,输入形状为[?,4] 的'metrics/accuracy/Squeeze'(操作:'Squeeze')为4 [英] Can not squeeze dim[1], expected a dimension of 1, got 4 for 'metrics/accuracy/Squeeze' (op: 'Squeeze') with input shapes: [?,4]
问题描述
张量流 - 2.1.0
Tensorflow - 2.1.0
Python - 3.6
Python - 3.6
我已经在 stackoverflow 上搜索过这个问题,但找不到解决方案.
I have searched this issue on stackoverflow but could not find solution.
我正在尝试使用 tensorflow 创建一个聊天机器人.这是错误:
I am trying to create a chatbot using tensorflow. This is error:
不能挤压dim[1],期望维度为1,得到4个'metrics/accuracy/Squeeze'(操作:'Squeeze'),输入形状:[?,4].
Can not squeeze dim[1], expected a dimension of 1, got 4 for 'metrics/accuracy/Squeeze' (op: 'Squeeze') with input shapes: [?,4].
这是代码:
words = []
classes = []
documents = []
ignore_words = ['?', '!']
data_file = open('fil.json').read()
intents = json.loads(data_file)
for intent in intents['intents']:
for pattern in intent['question']:
w = nltk.word_tokenize(pattern)
words.extend(w)
documents.append((w, intent['tag']))
if intent['tag'] not in classes:
classes.append(intent['tag'])
words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words]
words = sorted(list(set(words)))
classes = sorted(list(set(classes)))
pickle.dump(words, open('words.pkl', 'wb'))
pickle.dump(classes, open('classes.pkl', 'wb'))
training = []
output_empty = [0] * len(classes)
for doc in documents:
bag = []
pattern_words = doc[0]
pattern_words = [lemmatizer.lemmatize(word.lower()) for word in pattern_words]
for w in words:
bag.append(1) if w in pattern_words else bag.append(0)
output_row = list(output_empty)
output_row[classes.index(doc[1])] = 1
training.append([bag, output_row])
random.shuffle(training)
training = np.array(training)
train_x = list(training[:, 0])
train_y = list(training[:, 1])
print("Training data created")
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(128, input_shape=(len(train_x[0]),), activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(len(train_y[0]), activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
hist = model.fit(train_x, train_y, epochs=5)
model.save('chatbot_model.h5', hist)
print("model created")
推荐答案
确实,由于您的模型使用 softmax 密集层作为输出,您应该将其损失设置为 categorical_crossentropy
.之后您得到的错误是已知问题.有几个选项可以帮助您解决此问题:
Indeed, since your model is using softmax dense layer as output, you should set its loss to categorical_crossentropy
. The error you get after that is a known issue. There's a few options that could help you to fix it:
- 检查您的 Python 环境.确保您没有与 TensorFlow 一起安装 Keras,并且您的
Keras-Applications
和Keras-Preprocessing
包是最新的. - 在
model.fit()
中调用集合workers=0
. - 尝试将您的 TensorFlow 软件包降级到 2.0.1.
- Check your Python environment. Ensure that you don't have Keras installed along with TensorFlow and that your
Keras-Applications
andKeras-Preprocessing
packages are up to date. - In
model.fit()
call setworkers=0
. - Try to downgrade your TensorFlow package to 2.0.1.
这篇关于无法挤压dim[1],预期尺寸为1,输入形状为[?,4] 的'metrics/accuracy/Squeeze'(操作:'Squeeze')为4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!