如何修复 "model expected. Expected to see 2 array(s), but instead got ...." 和“'_thread._local' 对象没有属性 'value'”

How to Fix "model expected. Expected to see 2 array(s), but instead got ...." and " '_thread._local' object has no attribute 'value' "

我正在尝试使用深度学习构建矩阵分解模型并使用 Flask 进行部署。我还使用 apscheduler 从新输入中重新训练模型。这是模型。

模型有 2 个输入 cloth_ids、user_ids 和一个输出评级。输入和输出的形状都是 1D

    #tensorflow version - 2.1.0
    #keras version - 2.3.1


    user_input = Input(shape=(1,))
    cloth_input = Input(shape=(1,))

    user_embedding = Embedding(self.n_users, embedding_dimR)(user_input)
    cloth_embedding = Embedding(self.n_cloths, embedding_dimR)(cloth_input)

    user_embedding = Flatten()(user_embedding)
    cloth_embedding = Flatten()(cloth_embedding)

    x = Concatenate()([user_embedding, cloth_embedding])
    # x = Dense(denseR, activation='relu')(x)
    x = Dense(R_hidden, activation='relu', name='dense1')(x)
    x = Dense(R_hidden, activation='relu', name='dense2')(x)
    x = Dense(R_hidden, activation='relu', name='dense3')(x)
    x = Dense(R_out, activation='relu', name='dense_out')(x)

    model = Model(
        inputs=[user_input, cloth_input],
        outputs=x
        )

    self.model = model

    self.model.fit(
        x=[self.train_user_ids,self.train_cloth_ids],
        y=self.train_ratings,
        batch_size=batch_sizeR,
        epochs=num_epochsR,
        validation_data=(
            [self.test_user_ids,self.test_cloth_ids],
            self.test_ratings
            )
        )

    self.model.predict([[user_id],[cloth_id]])
    # user_id, cloth_id are integers

1) 首先,我使用 tensorflow.keras 作为导入层、模型 API 和指标。 然后我在做预测时遇到了以下错误,但是 apscheduler 工作正常

    ValueError: Error when checking model input: the list of Numpy arrays that you are passing
    to your model is not the size the model expected. Expected to see 2 array(s), for inputs 
    ['input_11', 'input_12'] but instead got the following list of 1 arrays: [array([[23],
    [ 0]], dtype=int64)]...

2) 在我使用 keras 而不是 tensorflow.keras 之后 model.predict 正常工作 但是 apscheduler 得到了以下错误

    Job "train_task (trigger: interval[0:00:20], next run at: 2020-05-08 12:22:29 +0530)" raised
    an exception
    AttributeError: '_thread._local' object has no attribute 'value'

将 keras 降级到 2.2.5 或在 app.run() 中使用 debug=False, threaded=False 无效。 请帮助我,谢谢

我能够使用以下模型代码重现您的问题。

注意-您可以从here.

下载我在模型中使用的数据集

重现问题的代码 -

%tensorflow_version 1.x
import tensorflow as tf
print(tf.__version__)
# MLP for Pima Indians Dataset saved to single file
import numpy as np
from numpy import loadtxt
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input, Concatenate

# load pima indians dataset
dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")

input1 = Input(shape=(1,))
input2 = Input(shape=(1,))

# define model
x1 = Dense(12, input_shape = (2,), activation='relu')(input1)
x2 = Dense(12, input_shape = (2,), activation='relu')(input2)
x = Concatenate()([x1, x2])
x = Dense(8, activation='relu')(x)
x = Dense(1, activation='sigmoid')(x)

model = Model(inputs=[input1, input2], outputs=x)

# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Model Summary
model.summary()

X1 = dataset[:,0]
X2 = dataset[:,1]

Y = dataset[:,8]

# Fit the model
model.fit(x=[X1,X2], y=Y, epochs=150, batch_size=10, verbose=0)

# evaluate the model
scores = model.predict([[X1,X2]], verbose=0)

输出-

1.15.2
Model: "model_23"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_38 (InputLayer)           [(None, 1)]          0                                            
__________________________________________________________________________________________________
input_39 (InputLayer)           [(None, 1)]          0                                            
__________________________________________________________________________________________________
dense_92 (Dense)                (None, 12)           24          input_38[0][0]                   
__________________________________________________________________________________________________
dense_93 (Dense)                (None, 12)           24          input_39[0][0]                   
__________________________________________________________________________________________________
concatenate_12 (Concatenate)    (None, 24)           0           dense_92[0][0]                   
                                                                 dense_93[0][0]                   
__________________________________________________________________________________________________
dense_94 (Dense)                (None, 8)            200         concatenate_12[0][0]             
__________________________________________________________________________________________________
dense_95 (Dense)                (None, 1)            9           dense_94[0][0]                   
==================================================================================================
Total params: 257
Trainable params: 257
Non-trainable params: 0
__________________________________________________________________________________________________
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-d6b7d46777c6> in <module>()
     38 
     39 # evaluate the model
---> 40 scores = model.predict([[X1,X2]], verbose=0)

3 frames
/tensorflow-1.15.2/python3.6/tensorflow_core/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    527                        'Expected to see ' + str(len(names)) + ' array(s), '
    528                        'but instead got the following list of ' +
--> 529                        str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
    530     elif len(names) > 1:
    531       raise ValueError('Error when checking model ' + exception_prefix +

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[  6.,   1.,   8., ...,   5.,   1.,   1.],
       [148.,  85., 183., ..., 121., 126.,  93.]])]...

解决方案-问题出在model.predict()中传递的数据的括号中。它必须与在 model.fit() 中传递数据的方式类似。所以我在我的代码中将 model.predict([[X1,X2]], verbose=0) 更改为 model.predict([X1,X2], verbose=0) 并且它工作正常。所以在你的情况下,你必须将 model.predict([[user_id],[cloth_id]]) 更改为 model.predict([user_id,cloth_id]) 并且它应该可以正常工作。

固定代码-

%tensorflow_version 1.x
import tensorflow as tf
print(tf.__version__)
# MLP for Pima Indians Dataset saved to single file
import numpy as np
from numpy import loadtxt
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input, Concatenate

# load pima indians dataset
dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")

input1 = Input(shape=(1,))
input2 = Input(shape=(1,))

# define model
x1 = Dense(12, input_shape = (2,), activation='relu')(input1)
x2 = Dense(12, input_shape = (2,), activation='relu')(input2)
x = Concatenate()([x1, x2])
x = Dense(8, activation='relu')(x)
x = Dense(1, activation='sigmoid')(x)

model = Model(inputs=[input1, input2], outputs=x)

# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Model Summary
model.summary()

X1 = dataset[:,0]
X2 = dataset[:,1]

Y = dataset[:,8]

# Fit the model
model.fit(x=[X1,X2], y=Y, epochs=150, batch_size=10, verbose=0)

# evaluate the model
scores = model.predict([X1,X2], verbose=0)

输出-

1.15.2
Model: "model_24"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_40 (InputLayer)           [(None, 1)]          0                                            
__________________________________________________________________________________________________
input_41 (InputLayer)           [(None, 1)]          0                                            
__________________________________________________________________________________________________
dense_96 (Dense)                (None, 12)           24          input_40[0][0]                   
__________________________________________________________________________________________________
dense_97 (Dense)                (None, 12)           24          input_41[0][0]                   
__________________________________________________________________________________________________
concatenate_13 (Concatenate)    (None, 24)           0           dense_96[0][0]                   
                                                                 dense_97[0][0]                   
__________________________________________________________________________________________________
dense_98 (Dense)                (None, 8)            200         concatenate_13[0][0]             
__________________________________________________________________________________________________
dense_99 (Dense)                (None, 1)            9           dense_98[0][0]                   
==================================================================================================
Total params: 257
Trainable params: 257
Non-trainable params: 0
__________________________________________________________________________________________________

希望这能回答您的问题。快乐学习。

我只是按如下方式重塑了 user_id 和 cloth_id,它起作用了。

  u =  np.array([user_id]).reshape(-1,1)
  c =  np.array([cloth_id]).reshape(-1,1)
  rating = float(self.model.predict([u,c]).squeeze())