Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x tf.global_variables_initializer()不起作用_Python 3.x_Machine Learning_Tensorflow - Fatal编程技术网

Python 3.x tf.global_variables_initializer()不起作用

Python 3.x tf.global_variables_initializer()不起作用,python-3.x,machine-learning,tensorflow,Python 3.x,Machine Learning,Tensorflow,您好Tensorflow用户/开发人员 尽管我调用了初始化器函数,但reporter告诉我,我的变量都没有初始化。我使用tf.get_variable()创建了它们。以下是创建我的会话和图形对象的位置: with tf.Graph().as_default(): # Store all scores (each score is a loss-per-episode) init = tf.global_variables_initializer() all_scores,

您好Tensorflow用户/开发人员

尽管我调用了初始化器函数,但reporter告诉我,我的变量都没有初始化。我使用tf.get_variable()创建了它们。以下是创建我的会话和图形对象的位置:

with tf.Graph().as_default():
    # Store all scores (each score is a loss-per-episode)
    init = tf.global_variables_initializer()
    all_scores, scores = [], []
    # Build common tensors used throughout entire session
    nn.build(seq_len)
    # Generate inference and loss models
    [loss, train_op] = nn.generate_models()
    with tf.Session() as sess:
        try:
            st = time.time()
            # Initialize all variables (Note that! not operation tensors; but variable tensors)
            print('Initializing variables...')
            sess.run(init)
            print('Training starts...')
            for e, (input_, target) in sample_generator:
                feed_dict = nn.prepare_dict(input_, target)
                # Run one step of the model.  The return values are the activations
                # from the `train_op` (which is discarded) and the `loss` Op.
                x = sess.run(tf.report_uninitialized_variables(tf.global_variables()))
                print(x)
                _, score = sess.run([train_op, loss],
                                    feed_dict=feed_dict)
                all_scores.append(score)
                scores.append(score)
                # Asses your predictions against target
                if e > 0 and not (e%100):
                    print('Episode %05d: %.6f' % (e, np.mean(scores).tolist()[0]))
                    scores.clear()
        except KeyboardInterrupt:
            print('Elapsed time: %ld' % (time.time()-st))
            pass
我已经调用这个方法数百万次了,它工作得非常好;但现在它让我陷入困境。你认为原因可能是什么?任何建议都将不胜感激

另外,我试着调用tf.local\u variables\u initializer();尽管记者告诉我,你们根本并没有本地的


提前感谢。

如果它不存在,我怀疑您无意中降级了Tensorflow版本。 你能试试tf.initialize所有变量吗

如果这不起作用,您可以发布您正在使用的版本吗?

谢谢您的回复

我已经弄明白了。在构建模型之前,我不应该执行以下分配指令:

init = tf.global_variables_initializer()
供大家参考:您可能会认为“当我在会话中执行此操作时,我将执行并获得名为“init”的操作的结果。因此,我在哪里执行上述指定的赋值并不重要。”


不!这不是真的。Tensorflow决定在执行此赋值指令后立即初始化哪些变量。因此,在构建整个模型后调用它。

我得到了相同的错误。但是这是我的解决方案:只需跳过init=tf.global\u variables\u initializer()

只需使用: sess=tf.Session
sess.run(init=tf.global_variables_initializer())

看起来变量是在创建
init
op之后定义的。它不包括那些初始化者;您可能只需要将其向下移动几行。