gpt4 book ai didi

python - 在 get_operations 中找不到 Tensorflow 优化器操作

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:18 25 4
gpt4 key购买 nike

当我像这样定义一个类时

class SimpleGraphCreator:
"""
Some simple graph object...
"""
def __init__(self):
self.graph_obj = tf.Graph()

with self.graph_obj.as_default() as g_def:
x_input_fun = tf.placeholder(dtype=tf.float32, name='input')
y_output_fun = tf.placeholder(dtype=tf.float32, name='output')
w_weights_fun = tf.get_variable('weight_set', dtype=tf.float32, shape=(5,5))
output = tf.matmul(x_input_fun, w_weights_fun, name='pred')
loss = tf.subtract(output, y_output_fun, name='loss')
self.opti = tf.train.AdamOptimizer(loss, name='opti')
g_def.add_to_collection(tf.GraphKeys.TRAIN_OP, self.opti)
# self.saver = tf.train.Saver()

self.graph_obj = g_def

当我想访问该图的优化器节点时,我想使用

some_graph = SimpleGraphCreater()
some_graph.graph_obj.get_operation_by_name('opti')

但是,在操作列表中找不到该名称...(查看some_graph.graph_obj.get_operations())

只有当我在图中特别说明这个优化器应该添加到tf.GraphKeys.TRAIN_OP集合中时,我才能将其取回。有没有更简单的方法?

我面临的一个限制是我只能传递图形对象......所以没有单独的图形操作。

最佳答案

tf.train.AdamOptimizer不是一个op,而是一个封装优化逻辑的类。 name 参数定义了优化器使用的内部变量和操作的范围

您真正想要的是将名称命名为 minimize操作:

optimizer.minimize(loss, name='train_op')

...

some_graph.graph_obj.get_operation_by_name('train_op')

关于python - 在 get_operations 中找不到 Tensorflow 优化器操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48169759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com