错误:输入 0 与 flatten_1 层不兼容:预期 min_ndim=3,发现 ndim=2

数据挖掘 喀拉斯
2021-10-10 06:58:14

我的代码:

    from __future__ import absolute_import, division, print_function, unicode_literals
from gensim.corpora import Dictionary
from tensorflow import keras


dictionary = Dictionary.load_from_text('diccionario_gensim.txt')

import spacy
import tensorflow as tf
import keras

import tensorflow as tf
import pandas as pd
import numpy as np
import os

def clean_up(text):
    removal=['ADV','PRON','CCONJ','PUNCT','PART','DET','ADP','SPACE']
    text_out = []
    doc= nlp(text)
    for token in doc:
        if token.is_stop == False and token.is_alpha and len(token)>1 and token.pos_ not in removal:
            lemma = token.lemma_
            text_out.append(lemma)
    return text_out

def procesarString (s, s2):
    text =  [dictionary.doc2idx(clean_up(s)), dictionary.doc2idx(clean_up(s2))]
    train_data = keras.preprocessing.sequence.pad_sequences(text,
                                                        value=0,
                                                        padding='post',
                                                        maxlen=512*2)
    return train_data

nlp = spacy.load("es_core_news_sm")

def create_model( ):
    m = keras.Sequential()
    m.add(keras.layers.Embedding(len (dictionary), 16))
    m.add(keras.layers.GlobalAveragePooling1D())
    m.add(keras.layers.Dense(16, activation=tf.nn.relu))
    m.add(keras.layers.Flatten(input_shape=(1024, )))
    m.add(keras.layers.Dense(25, activation='softmax'))
    m.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['acc'])
    return m

def predict(text):
    clean_up(text)
    procesarString(text, text)
    model2 = create_model()
    checkpoint_path = "training_1/cp.ckpt"
    checkpoint_dir = os.path.dirname(checkpoint_path)
    model2.load_weights(checkpoint_path)
    tags = ['Kit Cocina', 'Gastos notariales y de documentación', 'Prorroga Alojamiento temporal - Arriendo',
            'Kit Dormitorio', 'Gastos de atención en salud', 'Egreso de hotel', 'Visitas para arrendamiento',
            'Unidades de redención Alimentación - Aseo', 'Vestuario', 'Servicios funerarios',
            'Kit de vivienda saludable ', 'Transporte emergencia', 'Remisión albergue', 'Remision de hotel',
            'Orientación oferta distrital', 'Arriendo', 'Prórroga de hotel', 'Alojamiento temporal - Arriendo',
            'Remisión Alojamiento temporal - Albergue', 'Prórroga de arriendo',
            'Egreso Alojamiento temporal - Albergue', 'Transporte intraUrbano', 'Transporte', 'Kit Vajilla',
            'Kit de aseo personal']
    prediction = model2.predict(x[:1])
    ordered =  ( [(prediction[i], tags[i]) for i in range(len(prediction))])
    return ordered.Take(3)

我收到标题中的错误:

Input 0 is incompatible with layer flatten_1: expected min_ndim=3, found ndim=2

如果我在 Jupyter 笔记本上运行代码,它可以工作,但我将它迁移到 Docker 容器内的 Django 应用程序。

我将所有库的相同版本放在 docker 中,但无法使其工作。

这是堆栈跟踪

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/series/modelPredict

Django Version: 2.2.1
Python Version: 3.7.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'series']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/usr/src/app/series/views.py" in modelPredict
  56.     return model.predict("Que visaje la vida parce....")

File "/usr/src/app/series/model.py" in predict
  48.     model2 = create_model()

File "/usr/src/app/series/model.py" in create_model
  39.     m.add(keras.layers.Flatten(input_shape=(784, )))

File "/usr/local/lib/python3.7/site-packages/keras/engine/sequential.py" in add
  181.             output_tensor = layer(self.outputs[0])

File "/usr/local/lib/python3.7/site-packages/keras/engine/base_layer.py" in __call__
  414.                 self.assert_input_compatibility(inputs)

File "/usr/local/lib/python3.7/site-packages/keras/engine/base_layer.py" in assert_input_compatibility
  327.                                      str(K.ndim(x)))

Exception Type: ValueError at /series/modelPredict
Exception Value: Input 0 is incompatible with layer flatten_1: expected min_ndim=3, found ndim=2

任何帮助将不胜感激。


谢谢大家的回答,不幸的是该项目采用了不同的方法,所以我不能再尝试这个了,我会关闭这个问题。

1个回答

我在通过更改为 CNN 使用转移学习时解决了类似的问题

base_model= VGG16(
    weights=('imagenet', include_top=False, input_shape = (128,128,3) )

到:

base_model= VGG16(
    weights=('imagenet', include_top=False, input_shape = [128,128,3] )

所以看起来括号的类型在输入形状中也很重要。