Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按名称访问TensorFlow优化器_Python_Tensorflow - Fatal编程技术网

Python 按名称访问TensorFlow优化器

Python 按名称访问TensorFlow优化器,python,tensorflow,Python,Tensorflow,希望有人能帮助我。我没有找到一个答案,我的问题使用谷歌或这个网站 我正在尝试按名称访问TesorFlow优化器。下面是一些简单的示例代码: 导入系统 将numpy作为np导入 导入tensorflow作为tf print( "Python version: {0}".format(sys.version) ) print( "Tensorflow version: {0}".format(tf.__version__) ) print('') l_input = tf.placeholder(

希望有人能帮助我。我没有找到一个答案,我的问题使用谷歌或这个网站

我正在尝试按名称访问TesorFlow优化器。下面是一些简单的示例代码: 导入系统 将numpy作为np导入 导入tensorflow作为tf

print( "Python version: {0}".format(sys.version) )
print( "Tensorflow version: {0}".format(tf.__version__) )
print('')

l_input = tf.placeholder(tf.float32, shape=(None, 2), name='input')
l_dense = tf.layers.dense(l_input, units=1, activation=None)
l_output = tf.identity(l_dense, name='output')
l_true = tf.placeholder(tf.float32, shape=(None, 2), name='true')
cost = tf.reduce_sum(tf.square(l_output - l_true),name='cost')
train_step = tf.train.AdamOptimizer(0.001,name='train_step').minimize(cost)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    print( "cost" )
    print( cost )
    print( "cost:0" )
    print( tf.get_default_graph().get_tensor_by_name("cost:0") )
    print('')
    print( "train_step" )
    print( train_step )
    print( "type(train_step)" )
    print( type(train_step) )
    print( "collection train_step:0" )
    print( tf.get_default_graph().get_collection("train_step:0") )
    print( "operation train_step:0" )
    print( tf.get_default_graph().get_operation_by_name("train_step:0") )
    print( "tensor train_step:0" )
    print( tf.get_default_graph().get_tensor_by_name("train_step:0") )
具有以下输出:

Python version: 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
Tensorflow version: 1.8.0

cost
Tensor("cost:0", shape=(), dtype=float32)
cost:0
Tensor("cost:0", shape=(), dtype=float32)

train_step
name: "train_step"
op: "NoOp"
....
type(train_step)
<class 'tensorflow.python.framework.ops.Operation'>
collection train_step:0
[]
operation train_step:0
对它进行注释并使用get_tensor_by_name,我得到以下异常

....
ValueError: Name 'train_step:0' appears to refer to a Tensor, not a Operation.
....
KeyError: "The name 'train_step:0' refers to a Tensor which does not exist. The operation, 'train_step', exists but only has 0 outputs."
我想加载保存的图形,并通过调用train\u步骤上的sess.run()继续训练。但是,为此,我确实需要以某种方式访问train_step操作。我找到了使用get_tensor_by_name的旧示例,但这些示例已停止使用相同的异常


非常感谢您的帮助。

训练步骤
是一个操作,而不是张量,并且没有输出。因此,
train\u步骤:0
不表示任何内容,因为它试图指向操作的第一个输出。试一试

print( tf.get_default_graph().get_operation_by_name( "train_step" ) )

非常感谢你的帮助。我相信我尝试过在任何地方都忽略“:0”,但显然我在这一点上没有尝试。现在可以工作了。当然,很乐意帮忙!:)我知道这种感觉,过一段时间你可能会感到困惑……:)