无法挤压尺寸为1的dim [1],其输入形状为[?,4]的“度量/精度/挤压”(运算符:“挤压”)得到4。

时间:2020-02-10 09:28:56

标签: python tensorflow

Tensorflow-2.1.0

Python-3.6

我已经在stackoverflow上搜索了此问题,但是找不到解决方案。

我正在尝试使用张量流创建一个聊天机器人。这是错误:

无法挤压dim [1],预期尺寸为1,为4 输入形状为[?,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")

1 个答案:

答案 0 :(得分:1)

实际上,由于模型使用softmax密集层作为输出,因此应将其损失设置为categorical_crossentropy。之后出现的错误是known issue。有一些选项可以帮助您解决问题:

  • 检查您的Python环境。确保没有随TensorFlow一起安装Keras,并且您的Keras-ApplicationsKeras-Preprocessing软件包是最新的。
  • model.fit()呼叫集中workers=0中。
  • 尝试将TensorFlow软件包降级到2.0.1。