如何使用TensorFlow Keras在网络中一起使用嵌入层和其他要素列 [英] How to use embedding layer and other feature columns together in a network using tensorflow keras

查看:59
本文介绍了如何使用TensorFlow Keras在网络中一起使用嵌入层和其他要素列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑一个示例数据集,该数据集包含6列和10行.

Let’s consider a example data set contains 6 columns and 10 rows.

在这3列中为数字,其余3列为类别变量.

In these 3 columns are numeric and remaining 3 columns are categorical variable.

类别列将转换为大小为10x3的多热编码数组.

categorical columns are converted into multi hot encoded array of size 10x3.

我有我想预测的目标列也是分类变量,可以再次采用3个可能的值.该列是一个热编码的列.

I have target columns which i want predict is also categorical variable which can take 3 possible values again. This column is one hot encoded.

现在,我想使用此多热编码数组作为嵌入层的输入.嵌入层应输出2个单位.

Now I want to use this multi-hot-encoded array as input to embedding layer. Embedding layer should output 2 units.

然后我要使用数据集中的3个数字列和嵌入层的2个输出单位,总共5个单位作为隐藏层的输入.

Then I want to use 3 numeric columns from the dataset and 2 output units from the embedding layer total 5 unit as input to the hidden layer.

这是我被困住的地方.我不知道如何使用tensorflow keras桥接嵌入层和其他要素列,也不知道如何传递输入以嵌入层和其他2个单位.

This is the place I got stuck up. I don’t know how to bridge embedding layer and my other feature columns using tensorflow keras, also I don’t know how to pass input for embedding layer and other 2 units.

我已经用谷歌搜索了.我尝试了以下代码,但仍然出现错误. 我想tf.keras软件包中没有Merge层.

I have googled it. I tried the following code but still I get error. I guess there is no Merge layer in tf.keras package.

在此方面的任何帮助将不胜感激.

Any help on this would be greatly appreciated.

        import tensorflow as tf
        from tensorflow import keras
        import numpy as np

        num_data = np.random.random(size=(10,3))
        multi_hot_encode_data = np.random.randint(0,2, 30).reshape(10,3)
        target =  np.eye(3)[np.random.randint(0,3, 10)]

        model = keras.Sequential()
        model.add(keras.layers.Embedding(input_dim=multi_hot_encode_data.shape[1], output_dim=2))
        model.add(keras.layers.Dense(3, activation=tf.nn.relu, input_shape=(num_data.shape[1],)))
        model.add(keras.layers.Dense(3, activation=tf.nn.softmax)

        model.compile(optimizer=tf.train.RMSPropOptimizer(0.01),
                      loss=keras.losses.categorical_crossentropy,
                      metrics=[keras.metrics.categorical_accuracy])

        #model.fit([multi_hot_encode_data, num_data], target)   # I get error here 

我的网络结构将是

    multi-hot-encode-input  num_data_input 
            |                   |
            |                   |
            |                   |
        embedding_layer         |
            |                   |
            |                   | 
             \                 /        
               \              / 
              dense_hidden_layer
                     | 
                     | 
                  output_layer 

推荐答案

此合并"模式与顺序模型不兼容.我认为将功能性keras API与 keras.Model 而不是keras.Sequential(主要区别的简短说明):

This "merge" pattern incompatible with a sequential model. I think it's easier to use functional keras API with keras.Model instead of keras.Sequential (short explanation of main differences):

import tensorflow as tf
from tensorflow import keras
import numpy as np

num_data = np.random.random(size=(10,3))
multi_hot_encode_data = np.random.randint(0,2, 30).reshape(10,3)
target =  np.eye(3)[np.random.randint(0,3, 10)]

# Use Input layers, specify input shape (dimensions except first)
inp_multi_hot = keras.layers.Input(shape=(multi_hot_encode_data.shape[1],))
inp_num_data = keras.layers.Input(shape=(num_data.shape[1],))
# Bind nulti_hot to embedding layer
emb = keras.layers.Embedding(input_dim=multi_hot_encode_data.shape[1], output_dim=2)(inp_multi_hot)  
# Also you need flatten embedded output of shape (?,3,2) to (?, 6) -
# otherwise it's not possible to concatenate it with inp_num_data
flatten = keras.layers.Flatten()(emb)
# Concatenate two layers
conc = keras.layers.Concatenate()([flatten, inp_num_data])
dense1 = keras.layers.Dense(3, activation=tf.nn.relu, )(conc)
# Creating output layer
out = keras.layers.Dense(3, activation=tf.nn.softmax)(dense1)
model = keras.Model(inputs=[inp_multi_hot, inp_num_data], outputs=out)

model.compile(optimizer=tf.train.RMSPropOptimizer(0.01),
              loss=keras.losses.categorical_crossentropy,
              metrics=[keras.metrics.categorical_accuracy])

  • 您可以先将嵌入层的输出平坦化,然后再进行连接,否则,numerical_data应该具有兼容的形状并且至少具有三个维度
  • 在层之后定义功能模型.输入和输出可以是单层或可迭代的层
  • model.summary的输出:

    __________________________________________________________________________________________________
    Layer (type)                    Output Shape         Param #     Connected to                     
    ==================================================================================================
    input_5 (InputLayer)            (None, 3)            0                                            
    __________________________________________________________________________________________________
    embedding_2 (Embedding)         (None, 3, 2)         6           input_5[0][0]                    
    __________________________________________________________________________________________________
    flatten (Flatten)               (None, 6)            0           embedding_2[0][0]                
    __________________________________________________________________________________________________
    input_6 (InputLayer)            (None, 3)            0                                            
    __________________________________________________________________________________________________
    concatenate_2 (Concatenate)     (None, 9)            0           flatten[0][0]                    
                                                                     input_6[0][0]                    
    __________________________________________________________________________________________________
    dense (Dense)                   (None, 3)            30          concatenate_2[0][0]              
    __________________________________________________________________________________________________
    dense_1 (Dense)                 (None, 3)            12          dense[0][0]                      
    ==================================================================================================
    Total params: 48
    Trainable params: 48
    Non-trainable params: 0
    __________________________________________________________________________________________________
    

    也很适合:

    model.fit([multi_hot_encode_data, num_data], target)
    Epoch 1/1
    10/10 [==============================] - 0s 34ms/step - loss: 1.0623 - categorical_accuracy: 0.3000
    

    这篇关于如何使用TensorFlow Keras在网络中一起使用嵌入层和其他要素列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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