python - 在下面的情况下如何在 tensorflow 中使用占位符

标签 python tensorflow

对于下面的代码,我可以使用 n 的占位符,并通过 feed 传递 n 吗? 我对 tensorflow 还很陌生。

n = int(input("Enter an integer: "))

one=tf.constant(1)
#increase
increasing_value=tf.Variable(0,name="increasing_value")
increasing_op=tf.assign_add(increasing_value,one)

#sum
sumvalue=tf.Variable(0,name="sumvalue")
sum_op=tf.assign_add(sumvalue,increasing_value)

init=tf.global_variables_initializer()

with tf.Session() as session:
    session.run(init)
    for _ in range (n):
        session.run(increasing_op)
        session.run(sum_op)
    print(session.run(sumvalue))

最佳答案

你能试试这个吗?

n = tf.placeholder(tf.int32, name='n')

fedvalue = session.run( n , feed_dict = { n : 10 })
for _ in range ( fedvalue ):
    session.run(increasing_op)
    session.run(sum_op)

关于python - 在下面的情况下如何在 tensorflow 中使用占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52887454/

相关文章:

c++ - 用于嵌入式脚本/文本处理引擎的 Python vs Lua

linux - 关于如何托管公共(public) TensorBoard 以展示 ML 原型(prototype)的建议?

tensorflow - 将 Keras 与 TensorFlow 后端一起使用时,如何禁用 cuDNN 或强制其具有确定性行为?

python - 在 python 中运行并行请求 session

python - 为什么我的 protected 循环出错?

python - 如何使用模板(例如 Jinja2)将列值列表呈现到表中

python - “help” 通过将 2 个特征绑定(bind)在一起的决策树

python - 选择随机 Action 的 Tensorflow 代理

python - from_logits=True 和 from_logits=False 为 UNet 的 tf.losses.CategoricalCrossentropy 获得不同的训练结果

python - tensorflow 的 tables_initializer 有什么作用?