tensorflow - 关于 tensorflow 教程“model()”与“model.predict()”的问题
问题描述
为什么我将“predictions = model(input_eval)”更改为“predictions = model.predict(input_eval)”时出现错误:
CancelledError: [Op:StatefulPartitionedCall]
这是代码
def generate_text(model, start_string):
# Evaluation step (generating text using the learned model)
# Number of characters to generate
num_generate = 1000
# Converting our start string to numbers (vectorizing)
input_eval = [char2idx[s] for s in start_string]
input_eval = tf.expand_dims(input_eval, 0)
# Empty string to store our results
text_generated = []
# Low temperatures results in more predictable text.
# Higher temperatures results in more surprising text.
# Experiment to find the best setting.
temperature = 1.0
# Here batch size == 1
model.reset_states()
for i in range(num_generate):
predictions = model(input_eval)
# remove the batch dimension
predictions = tf.squeeze(predictions, 0)
# using a multinomial distribution to predict the word returned by the model
predictions = predictions / temperature
predicted_id = tf.multinomial(predictions, num_samples=1)[-1,0].numpy()
# We pass the predicted word as the next input to the model
# along with the previous hidden state
input_eval = tf.expand_dims([predicted_id], 0)
text_generated.append(idx2char[predicted_id])
return (start_string + ''.join(text_generated))
链接:https ://colab.research.google.com/drive/1hoEHMCQrZYFJnNoWzRNTmPjQUFrPvTWy
解决方案
推荐阅读
- curl - PlaylistItems: 列表返回 playlistNotFound
- java - Spring缓存@CacheEvict匹配列表中的键?
- roblox - 动画脚本在 ReplicatedStorager 中不起作用
- virtualhost - 不能从浏览器访问端口 8080,即使它在 0.0.0.0 打开
- kdb - kdb+/q Pivot Table 将多列变成单列?
- python - 多行输入中的 IPython 控制台校正
- java - 使用 Homebrew 安装 OpenCV 时选择 Java 版本
- amazon-web-services - Lambda@Edge 和 AWS EC2 延迟比较
- amazon-web-services - VPC中的Web服务器,如何公开访问?
- python - 将文本列表转成 json 格式列表