TensorFlow文本生成仅预测[UNK]字符,求问题排查与解决
TensorFlow文本生成仅输出[UNK]字符的问题求助
我尝试用TensorFlow生成特定风格的文本,但哪怕直接复制官方教程里的代码(已经添加了防止生成[UNK]的掩码),模型仍然只预测出[UNK]字符。我推测这可能和我使用的Python版本(3.9.15)有关——毕竟连官方代码和数据集都没法正常运行。有没有其他人遇到过这个问题?
我的运行环境是搭载M1芯片的Mac,用的是最新版本的TensorFlow,这或许也是问题的诱因之一。恳请各位帮忙解决!
参考TensorFlow官方文本生成教程内容。
以下是我从教程里直接复制的代码:
class OneStep(tf.keras.Model): def __init__(self, model, chars_from_ids, ids_from_chars, temperature=1.0): super().__init__() self.temperature = temperature self.model = model self.chars_from_ids = chars_from_ids self.ids_from_chars = ids_from_chars # Create a mask to prevent "[UNK]" from being generated. skip_ids = self.ids_from_chars(['[UNK]'])[:, None] sparse_mask = tf.SparseTensor( # Put a -inf at each bad index. values=[-float('inf')]*len(skip_ids), indices=skip_ids, # Match the shape to the vocabulary dense_shape=[len(ids_from_chars.get_vocabulary())]) self.prediction_mask = tf.sparse.to_dense(sparse_mask) @tf.function def generate_one_step(self, inputs, states=None): # Convert strings to token IDs. input_chars = tf.strings.unicode_split(inputs, 'UTF-8') input_ids = self.ids_from_chars(input_chars).to_tensor() # Run the model. # predicted_logits.shape is [batch, char, next_char_logits] predicted_logits, states = self.model(inputs=input_ids, states=states, return_state=True) # Only use the last prediction. predicted_logits = predicted_logits[:, -1, :] predicted_logits = predicted_logits/self.temperature # Apply the prediction mask: prevent "[UNK]" from being generated. predicted_logits = predicted_logits + self.prediction_mask # Sample the output logits to generate token IDs. predicted_ids = tf.random.categorical(predicted_logits, num_samples=1) predicted_ids = tf.squeeze(predicted_ids, axis=-1) # Convert from token ids to characters predicted_chars = self.chars_from_ids(predicted_ids) # Return the characters and model state. return predicted_chars, states
内容的提问来源于stack exchange,提问作者Kevin Smith
相关产品推荐
相关产品推荐

