TensorFlow保存模型predict的错误

TensorFlow保存模型predict的错误
问题相关代码

使用的是movielen-20m数据
class Recommender_Model(Model):
def init(self, K, uNum, mNum):
super(Recommender_Model, self).init()
self.K = K
self.uNum = uNum
self.mNum = mNum

    u = Input(shape=(1,))
    m = Input(shape=(1,))
    uEmb = Embedding(self.uNum, self.K, embeddings_initializer="he_normal", embeddings_regularizer=l2(1e-6))(u)
    mEmb = Embedding(self.mNum, self.K, embeddings_initializer="he_normal", embeddings_regularizer=l2(1e-6))(m)

    uFlat = Flatten()(uEmb)
    mFlat = Flatten()(mEmb)

    x = Concatenate()([uFlat, mFlat])
    # x = layers.dot(inputs=[uflat, mflat], axis=1)
    x = Dense(256, activation="relu", kernel_initializer="he_normal")(x)
    x = Dense(512, activation="relu", kernel_initializer="he_normal")(x)
    x = Dense(1)(x)

    model = Model(inputs=[u, m], outputs=x)
    self.model = model

    return

def call(self, x, training=None):
    m = self.model(x, training=training)

    return m

def recommend(user_id):
user2user_encoded = {user_ids[i]: i for i in range(len(user_ids))}
user_encoded2user = {i: user_ids[i] for i in range(len(user_ids))}
movie2movie_encoded = {movie_ids[i]: i for i in range(len(movie_ids))}
movie_encoded2movie = {i: movie_ids[i] for i in range(len(movie_ids))}
unwatched_movie_index = [[movie2movie_encoded[x]] for x in unwatched_movies_ids]
user_encoder = user2user_encoded.get(user_id)

user_movie_array = np.hstack(([[user_encoder]] * len(unwatched_movies_ids), unwatched_movie_index)) 
user = tf.constant(user_movie_array[:, 0], dtype=tf.int32)
movie = tf.constant(user_movie_array[:, 1], dtype=tf.int32)

predicted_ratings = model.predict([user, movie]).flatten()
top_N_rating_indices = predicted_ratings.argsort()[top_N:][::-1]
recommended_movie_ids = [movie_encoded2movie.get(unwatched_movie_index[x][0]) for x in top_N_rating_indices]
recommended_movies = [movies[i] for i in recommended_movie_ids]

print(recommended_movies)
return recommended_movies
运行结果及报错内容

ValueError: Exception encountered when calling layer "recommender__model" (type Recommender_Model).

Could not find matching concrete function to call loaded from the SavedModel. Got:
  Positional arguments (2 total):
    * (<tf.Tensor 'x:0' shape=(None,) dtype=int32>,
 <tf.Tensor 'x_1:0' shape=(None,) dtype=int32>)
    * False
  Keyword arguments: {}

 Expected these arguments to match one of the following 4 option(s):

Option 1:
  Positional arguments (2 total):
    * (TensorSpec(shape=(None, 1), dtype=tf.int32, name='input_1'),
 TensorSpec(shape=(None, 1), dtype=tf.int16, name='input_2'))
    * False
  Keyword arguments: {}

Option 2:
  Positional arguments (2 total):
    * (TensorSpec(shape=(None, 1), dtype=tf.int32, name='input_1'),
 TensorSpec(shape=(None, 1), dtype=tf.int16, name='input_2'))
    * True
  Keyword arguments: {}

Option 3:
  Positional arguments (2 total):
    * (TensorSpec(shape=(None, 1), dtype=tf.int32, name='x/0'),
 TensorSpec(shape=(None, 1), dtype=tf.int16, name='x/1'))
    * False
  Keyword arguments: {}

Option 4:
  Positional arguments (2 total):
    * (TensorSpec(shape=(None, 1), dtype=tf.int32, name='x/0'),
 TensorSpec(shape=(None, 1), dtype=tf.int16, name='x/1'))
    * True
  Keyword arguments: {}

Call arguments received by layer "recommender__model" (type Recommender_Model):
  • args=(('tf.Tensor(shape=(None,), dtype=int32)', 'tf.Tensor(shape=(None,), dtype=int32)'),)
  • kwargs={'training': 'False'}
尝试过的方法

将int list 转换成 tf.int32 list

想要达到的结果

模型返回预测值

img


这层的输入输出是int,不能转成tf.int32

朋友,你这个跑过open cv嘛?就是也会出现你这种情况