我正在尝试让嵌入层适用于字符串类别,但无法解决这个问题。你能建议如何实现 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(10,activation='sigmoid')(nn)
nn = Dropout(0.1)(nn)
nn = Dense(1)(nn)
model = Model(inputs=[i1,i2],outputs=nn)