Python 如何使用tf.keras.layers.Embedding layer处理增加的训练?

Python 如何使用tf.keras.layers.Embedding layer处理增加的训练?,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,我有一个模型,在训练过程中将项目id编码到嵌入向量中。我正在使用tf.keras.layers.Embedding来实现这一点。但是,在培训模型时,需要根据当前状态进行更新,这意味着需要满足两个要求: 以前项目的嵌入权重应根据其当前值进行更新 对于新项目,随机创建其嵌入权重,并在训练过程中更新它们 我的想法是将新项目的嵌入权重添加到先前训练的tf.keras.layers.embedding对象中,并使用这个新的嵌入层,它可以处理所有项目id,对所有项目进行编码,然后执行训练过程。但我不知道怎么

我有一个模型,在训练过程中将项目id编码到嵌入向量中。我正在使用
tf.keras.layers.Embedding
来实现这一点。但是,在培训模型时,需要根据当前状态进行更新,这意味着需要满足两个要求:

  • 以前项目的嵌入权重应根据其当前值进行更新
  • 对于新项目,随机创建其嵌入权重,并在训练过程中更新它们
  • 我的想法是将新项目的嵌入权重添加到先前训练的
    tf.keras.layers.embedding
    对象中,并使用这个新的嵌入层,它可以处理所有项目id,对所有项目进行编码,然后执行训练过程。但我不知道怎么做。谁能给我举个例子吗?谢谢

    import tensorflow as tf
    
    # Assume this is the previous trained embedding layer:
    e1 = tf.keras.layers.Embedding(3, 2)
    
    # So these are the previous trained weights for item 0, 1, 2
    print(e1(0))
    print(e1(1))
    print(e1(2))
    
    # Here comes new item 3 and 4 and their initial weights
    w3 = tf.Variable([0.01142126, 0.43289672])
    w4 = tf.Variable([-0.03306466, 0.08222979])
    
    # How can I create a trainable embedding layer e2 that gives the following?
    #  e2(0) -> e1(0)
    #  e2(1) -> e1(1)
    #  e2(2) -> e1(2)
    #  e2(3) -> w3
    #  e2(4) -> w4