简体   繁体   English

如何修复“预期的模型。 预计会看到 2 个数组,但得到了……”和“'_thread._local' object 没有属性 'value'”

[英]How to Fix “model expected. Expected to see 2 array(s), but instead got …” and “ '_thread._local' object has no attribute 'value' ”

I'm trying to build Matrix factorization model with deep learning and deploy it using flask.我正在尝试使用深度学习构建矩阵分解 model 并使用 flask 部署它。 I also use apscheduler to retrain the model from new inputs.我还使用 apscheduler 从新输入中重新训练 model。 Here is the model.这是 model。

Model has 2 inputs cloth_ids, user_ids and one outputs ratings. Model 有 2 个输入cloth_ids、user_ids 和一个输出评级。 both inputs and the output has the shape of 1D两个输入和 output 具有一维的形状

    #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) First I used tensorflow.keras for import layer, model APIs and metrics. 1) 首先,我使用tensorflow.keras作为导入层,model API 和指标。 Then I got following error while do predictions but apscheduler worked properly然后我在做预测时出现以下错误,但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) After I used keras instead of tensorflow.keras then model.predict worked properly but the apscheduler got the following error 2)在我使用keras 而不是 tensorflow.keras然后Z20F35E630DAF44DBFA4C3F68FZ9 正常工作之后,预测错误但 apschedulC 得到了以下错误。

    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'

Downgrading keras to 2.2.5 or using debug=False, threaded=False inside app.run() not working.将 keras 降级到 2.2.5 或在 app.run() 中使用debug=False, threaded=False不起作用。 Please Help Me, Thanks请帮助我,谢谢

I was able to recreate your issue using the below code for a model.我能够使用 model 的以下代码重新创建您的问题。

Note - You can download the dataset I am using in the model from here .注意 -您可以从这里下载我在 model 中使用的数据集。

Code to recreate the issue -重新创建问题的代码 -

%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)

Output - Output -

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.]])]...

Solution - The issue is in the bracket's for the data passed in model.predict() .解决方案 -问题在于model.predict()中传递的数据的括号中。 It has to be similar way as data is passed in model.fit() .它必须与在model.fit()中传递数据的方式相似。 So I changed the model.predict([[X1,X2]], verbose=0) to model.predict([X1,X2], verbose=0) in my code and it worked fine.所以我在我的model.predict([[X1,X2]], verbose=0)更改为model.predict([X1,X2], verbose=0)并且效果很好。 So in your case, you have to change model.predict([[user_id],[cloth_id]]) to model.predict([user_id,cloth_id]) and it should work fine.因此,在您的情况下,您必须将model.predict([[user_id],[cloth_id]])更改为model.predict([user_id,cloth_id]) ,它应该可以正常工作。

Fixed Code -固定代码 -

%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)

Output - Output -

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
__________________________________________________________________________________________________

Hope this answers your question.希望这能回答你的问题。 Happy Learning.快乐学习。

I just reshape the user_id and cloth_id as follows and it works.我只是按如下方式重塑 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())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask 和 Keras model 错误 ''_thread._local' ZA8CFDE6331BD59EB2AC966F8'1'值''B4 没有? - Flask and Keras model Error ''_thread._local' object has no attribute 'value''? 在 Django 中运行 Keras LSTM 模型返回“thread._local”对象没有属性“value” - Running Keras LSTM Model In Django Returns 'thread._local' object has no attribute 'value' &#39;thread._local&#39; 对象没有属性 - 'thread._local' object has no attribute 如何解决“预期看到2个阵列,但得到以下1个阵列的列表” - How to fix 'Expected to see 2 array(s), but instead got the following list of 1 arrays' Python:'thread._local对象没有属性'todo' - Python: 'thread._local object has no attribute 'todo' AttributeError: &#39;_thread._local&#39; 对象没有属性 &#39;token&#39; - AttributeError: '_thread._local' object has no attribute 'token' AttributeError:“ thread._local”对象没有属性“浏览器” - AttributeError: 'thread._local' object has no attribute 'browser' 您传递给模型的Numpy数组列表不是模型期望的大小。 预计会看到2个阵列 - 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) Dask 延迟错误 - AttributeError: '_thread._local' object 没有属性 'value' - Dask Delayed Error - AttributeError: '_thread._local' object has no attribute 'value' flask 应用程序中的 Deepface。 出现错误:'_thread._local' object 没有属性'值' - Deepface in flask application. Getting error: '_thread._local' object has no attribute 'value'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM