繁体   English   中英

将分类数据输入到具有多个输入的 keras 模型中的嵌入层

[英]Input categorical data to embedding layer in keras model with multiple input

我试图让嵌入层为字符串类别工作,但无法解决这个问题。 你能建议如何实现 i2 输入吗? 这个想法是使用邮政编码作为一个输入。 在 i1 中将有所有其他输入。

from tensorflow.keras import optimizers
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import Concatenate, Dense, LSTM, Input, concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.layers.experimental import preprocessing

# tfa.metrics.r_square.RSquare(dtype=np.float32,y_shape=(1,))
# initializer = tf.keras.initializers.RandomNormal(mean=0., stddev=10.)

vocabulary = postcodes

batch_size = 128
# determine the number of input features
n_features = X_train.shape[1]

i1 = Input(shape=(n_features,))
nn1 = Dense(2000, activation='relu')(i1)
nn1 = Dropout(0.1)(nn1)
nn1 = Dense(500, activation='relu')(i1)
nn1 = Dropout(0.1)(nn1)
nn1 = Model(inputs=i1, outputs=nn1)

i2 = Input(shape=(1,))

## 
lookup = preprocessing.StringLookup(vocabulary=vocabulary, mask_token=None, num_oov_indices=0, output_mode="int")

# Convert the string input values into integer indices.
encoded_feature = lookup(i2)
embedding_dims = int(math.sqrt(len(vocabulary)))
embedding = layers.Embedding(input_dim=len(vocabulary), output_dim=embedding_dims)

#nn2 = preprocessing.StringLookup(vocabulary=postcodes)(i2)
nn2 = Dense(2000, activation='relu')(embedding)
nn2 = Dropout(0.1)(nn2)
nn2 = Model(inputs=i2, outputs=nn2)

nn = concatenate([nn1.output, nn2.output])

nn = Dense(500, activation='relu')(nn)
nn = Dropout(0.1)(nn)
nn = Dense(500, activation='relu')(nn)
nn = Dropout(0.1)(nn)
nn = Dense(500, activation='relu')(nn)
nn = Dropout(0.1)(nn)
nn = Dense(10, activation='sigmoid')(nn)
nn = Dropout(0.1)(nn)
nn = Dense(1)(nn)

model = Model(inputs=[i1, i2], outputs=nn)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM