Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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 通过张量迭代的张量流_Python_Tensorflow - Fatal编程技术网

Python 通过张量迭代的张量流

Python 通过张量迭代的张量流,python,tensorflow,Python,Tensorflow,我用的是Tensorflow 1.13。然而,我得到一个错误,我不能迭代通过张量,除非我在渴望模式。有没有一种方法可以在不进入渴望模式的情况下实现这一点 with tf.Session(config=config) as sess: context = tf.placeholder(tf.int32, [args.batch_size, None]) mask = tf.placeholder(tf.int32, [args.batch_size, 2]) output

我用的是Tensorflow 1.13。然而,我得到一个错误,我不能迭代通过张量,除非我在渴望模式。有没有一种方法可以在不进入渴望模式的情况下实现这一点

with tf.Session(config=config) as sess:
    context = tf.placeholder(tf.int32, [args.batch_size, None])
    mask = tf.placeholder(tf.int32, [args.batch_size, 2])
    output = model.model(hparams=hparams, X=context)



    for batch_index in range(args.batch_size):
        start = mask[batch_index][0]
        end   = mask[batch_index][1]

        for i in range(start, end+1):
            output['logits'][batch_index, i , context[batch_index,i]].assign(math.inf)

    loss = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=context[:, 1:],  logits=output['logits'][:, :-1]))
你能试试使用吗?您可以尝试下面的代码片段(可能对代码做了一些小的修改),看看它是否有效

import tensorflow as tf
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
    context = tf.placeholder(tf.int32, [args.batch_size, None])
    mask = tf.placeholder(tf.int32, [args.batch_size, 2])
    output = model.model(hparams=hparams, X=context)



    for batch_index in [0,1,2,3]: #I have assumed a dummy list cz we can't iterate through a 'Dimension'
        start = mask[batch_index][0]
        end   = mask[batch_index][1]

        i = tf.constant(0)
        while_condition = lambda i: (tf.less(i, end)) & (tf.math.greater_equal(i,start))

        def body(i):
            return output['logits'][batch, i , context[batch,i]].assign(math.inf)

        r = tf.while_loop(while_condition, body, [i])

        # for i in range(start, end+1):
        #     output['logits'][batch, i , context[batch,i]].assign(math.inf)

    loss = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=context[:, 1:],  logits=output['logits'][:, :-1]))

你能发布一个输入样本和预期的输出吗?嗨@gorjan这很难做到。你能看到Achintha建议中的注释吗?当我运行
sess.run(loss,feed\u dict={…})
时,如何确保执行
循环时?我希望这是有意义的?是的,这是有意义的。您能否尝试一些示例输入并使用
loss\u r,r\u r=sess.run([loss,r],feed\u dict={…})
运行会话?查看您是否获得了预期的培训绩效?这是一个不同的错误,发生在以下行中:
labels=context[:,1:,logits=output['logits'][:,:-1])
对吗?看看这里。原来错误来自
assign
部分。请看这里: