tensorflow v2.1.0的等效StringLookup
我正在尝试构建一个类似于此的推荐模型。但是这个例子使用的是Tensorflow v2.4.0,在我的工作中,我需要使用v2.1.0。似乎v2.1.0中不存在tensorflow v2.1.0的等效StringLookup,tensorflow,tensorflow2.0,Tensorflow,Tensorflow2.0,我正在尝试构建一个类似于此的推荐模型。但是这个例子使用的是Tensorflow v2.4.0,在我的工作中,我需要使用v2.1.0。似乎v2.1.0中不存在StringLookUp层。在2.1.0中是否有任何等效的方法来实现完全相同的目标?我需要在这样的模型中使用它: user_model = tf.keras.Sequential([ tf.keras.layers.experimental.preprocessing.StringLookup( vocabulary=uniq
StringLookUp
层。在2.1.0中是否有任何等效的方法来实现完全相同的目标?我需要在这样的模型中使用它:
user_model = tf.keras.Sequential([
tf.keras.layers.experimental.preprocessing.StringLookup(
vocabulary=unique_user_ids, mask_token=None),
tf.keras.layers.Embedding(len(unique_user_ids) + 1, embedding_dimension)
])
只要不关心映射顺序,就可以使用将字符串散列到索引
示例:
import tensorflow as tf
print(tf.__version__)
# 2.1.0
NUM_BUCKETS = 6
EMB_DIM = 128
# five unique user ids
user_ids = tf.constant([u'463', u'112', u'666', u'932', u'878', u'[UNK]'])
# hash to numbers 0-5; 5 numbers for the known unique ids and one
# to account for unknown or empty strings
idxs = tf.strings.to_hash_bucket_strong(
input=user_ids,
num_buckets=NUM_BUCKETS,
key=[1, 2])
print(idxs)
# <tf.Tensor: shape=(6,), dtype=int64, numpy=array([2, 3, 4, 0, 5, 1])>
# And now you can apply your embeddings to the indices you've generated
emb = tf.keras.layers.Embedding(
input_dim=NUM_BUCKETS,
output_dim=EMB_DIM)
assert emb(idxs).shape == (6, 128)
将tensorflow导入为tf
打印(tf.\U版本\U)
# 2.1.0
NUM_bucket=6
EMB_DIM=128
#五个唯一的用户ID
用户ID=tf.常数([u'463',u'112',u'666',u'932',u'878',u'[UNK]']))
#散列到数字0-5;已知唯一ID的5个编号和一个
#说明未知或空字符串的步骤
idxs=tf.strings.to_hash_bucket_strong(
输入=用户ID,
num_bucket=num_bucket,
键=[1,2])
打印(idxs)
#
#现在,您可以将嵌入应用于生成的索引
emb=tf.keras.layers.emb(
输入尺寸=桶数,
输出尺寸=EMB尺寸)
断言emb(idxs).shape==(6128)