Keras TypeError:预期为float32,而不是类型为'Tensor'的< tf.Tensor ..> [英] Keras TypeError: Expected float32, got <tf.Tensor ..>of type 'Tensor' instead

查看:931
本文介绍了Keras TypeError:预期为float32,而不是类型为'Tensor'的< tf.Tensor ..>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Keras模型,会给我错误

I have a Keras model which gives me error

TypeError: Expected float32, got <tf.Tensor 'recommender_dnn_25/strided_slice_5:0' shape=(None, 1) dtype=float32> of type 'Tensor' instead.

对于我的keras模型,我正在发送类型为 numpy的火车/验证数据.ndarray 。这来自 movielens 数据集,值分别为 movie_id user_id 邮政编码年龄性别。下面的示例行:

To my keras model, I am sending train/validation data of type numpy.ndarray. This is from movielens dataset and the values are movie_id, user_id, zip_code, age, gender. A sample row below:

x_train[0]
array(['195', '241', 415, 3, 1], dtype=object)

第1个输入和第2个输入一起被训练为嵌入模型训练过程。在合并所有五个功能之前,最后三个(邮政编码,年龄,性别)经过以下转换。

The 1st two inputs are trained to an embedding along with the model training process. The last three (zip_code, age, gender) goes through below conversion before all five of the features are concatenated.


  1. 转换为浮动

  2. 重塑为(None,1)

  3. 使用 zip_code = K.constant(zip_code)转换为张量,如果没有此步骤,我将看到错误 ValueError:无法转换NumPy数组到张量(不受支持的对象类型int)

  1. converted to float
  2. reshaped to (None,1)
  3. converted to tensor using zip_code = K.constant(zip_code), Without this step I see error ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int)

现在运行此模型时-我收到错误 TypeError:预期为float32,得到< tf.Tensor'recommender_dnn_25 / strided_slice_5:0'shape =(None,1)dtype = float32>

Now when I run this model - I get error TypeError: Expected float32, got <tf.Tensor 'recommender_dnn_25/strided_slice_5:0' shape=(None, 1) dtype=float32> of type 'Tensor' instead.

错误发生在 zip_code = K.constant(zip_code)甚至在进入连接阶段之前。

The error is happening at zip_code = K.constant(zip_code) even before it gets into the concatenate phase.

下面的模型代码:

x_train.shape
(90000, 5)

EMBEDDING_SIZE = 50
NUM_USERS =movielens['user_id'].nunique()
NUM_MOVIES = movielens['movie_id'].nunique()

class RecommenderDNN(keras.Model):
    def __init__(self, num_users, num_movies, embedding_size, **kwargs):
        super(RecommenderDNN, self).__init__(**kwargs)
        self.num_users = num_users
        self.num_movies = num_movies
        self.embedding_size = embedding_size
        self.user_embedding = layers.Embedding(
            num_users,
            embedding_size,
            embeddings_initializer="he_normal",
            embeddings_regularizer=keras.regularizers.l2(1e-6),
        )
        self.movie_embedding = layers.Embedding(
            num_movies,
            embedding_size,
            embeddings_initializer="he_normal",
            embeddings_regularizer=keras.regularizers.l2(1e-6),
        )


    def call(self, inputs):
        user_vector = self.user_embedding(inputs[:, 0])

        movie_vector = self.movie_embedding(inputs[:, 1])

        zip_code = float(inputs[:, 2])
        age = float(inputs[:, 3])
        gender = float(inputs[:,4])

        zip_code = zip_code[: ,None]
        age = age[: ,None]
        gender = gender[: ,None]

        zip_code = K.constant(zip_code)
        age = K.constant(age)
        gender = K.constant(gender)


        print(user_vector.shape)
        print(movie_vector.shape)
        print(zip_code.shape)
        print(age.shape)
        print(gender.shape)


        concat = layers.concatenate([user_vector, movie_vector, zip_code, age, gender], axis=1)
        concat_dropout = layers.Dropout(0.2)(concat)
        # rest of the layers ...
        result = layers.Dense(1, activation='softmax',name='Activation')(dense_4)
        return result


model = RecommenderDNN(NUM_USERS, NUM_MOVIES, EMBEDDING_SIZE)
model.compile(
    loss=keras.losses.BinaryCrossentropy(), optimizer=keras.optimizers.Adam(lr=0.001)
)

请提出建议。

推荐答案

我当时是什么这样做有一些根本的问题。我正在将嵌入层与分类输入连接在一起。嵌入层输出3D张量(尽管打印出 .shape 仅显示2D,但不确定为什么)。尽管没有其他任何层,但将其与输入类别组合而不通过类别却没有任何意义。因此,在连接它们之前,我展平了嵌入层的输出,并通过 Dense 层对输入进行了分类。

What I was doing had some fundamental problems. I was concatenating the embedding layers with the categorical input. The embedding layer outputs a 3D tensor (although printing out .shape only shows 2D, not sure why). Combining it with the input categorical without passing the categorical though any other layer does not really make sense. So I flattened the embedding layer output and passed the input categorical through a Dense layer before concatenating them.

为简单起见,假设我们具有2个功能- user_id age 。我们希望通过培训过程为 user_id 生成嵌入。 age 是在 LabelEncoding 之后作为模型输入传递的分类变量。

For simplicity, lets assume we have 2 features - user_id and age. We want to generate embedding for the user_id through the training process. And age is the categorical variable that is passed as the model input after LabelEncoding.

下面解决该问题的代码:

Below code that resolved the issue:

row_count = train.shape[0]
EMBEDDING_SIZE = 50
NUM_USERS = movielens['user_id'].nunique()

user_input = keras.Input(shape=(1,), name='user')
age_input = keras.Input(shape=(1,), name='age')
user_emb = layers.Embedding(output_dim=EMBEDDING_SIZE, input_dim=NUM_USERS+1, input_length=row_count, name='user_emb')(user_input)
user_vec = layers.Flatten(name='FlattenUser')(user_emb)
dense_1 = layers.Dense(20, activation='relu')(age_input)
concat = layers.concatenate([user_vec, dense_1], axis=1)
dense = layers.Dense(10,name='FullyConnected')(concat)
outputs = layers.Dense(10, activation="softmax") (dense)
adam = keras.optimizers.Adam(lr=0.005)
model = keras.Model([user_input, age_input], outputs)
model.compile(optimizer=adam,loss= 'mean_absolute_error')

这篇关于Keras TypeError:预期为float32,而不是类型为'Tensor'的&lt; tf.Tensor ..&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆