Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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 tf.while_循环->;主体的输入和输出数量必须匹配循环变量:1,2_Python_Tensorflow - Fatal编程技术网

Python tf.while_循环->;主体的输入和输出数量必须匹配循环变量:1,2

Python tf.while_循环->;主体的输入和输出数量必须匹配循环变量:1,2,python,tensorflow,Python,Tensorflow,我需要使用tf.while_循环,但我得到以下消息: ValueError: Number of inputs and outputs of body must match loop_vars: 1, 2 总损耗,i=list(),tf.常数(0) def采样\u softmax \u体(i,总损耗): loss=tf.nn.sampled\u softmax\u loss(权重=logits\u权重,偏差=logits\u偏差,标签=target,输入=h2\u解码器\u输出[:,i,:],

我需要使用tf.while_循环,但我得到以下消息:

ValueError: Number of inputs and outputs of body must match loop_vars: 1, 2
总损耗,i=list(),tf.常数(0) def采样\u softmax \u体(i,总损耗): loss=tf.nn.sampled\u softmax\u loss(权重=logits\u权重,偏差=logits\u偏差,标签=target,输入=h2\u解码器\u输出[:,i,:],num\u sampled=n\u sampled\u softmax,num\u classes=n\u fra\u words,partition\u strategy=“div”) 总损失。追加(损失) i=i+1 返回i,总损失 def状况(i,总损耗): 返回True,每个句子的单词数<最大值[“fra”] tf.while\u循环(条件,采样的\u softmax\u体,[i,总损耗])
我改变了我的策略,开始工作:

total_loss, i = list(), tf.constant(0)
def sampled_softmax_body(i):
    loss = tf.nn.sampled_softmax_loss(weights=logits_weights, biases=logits_biases, labels=target, inputs=h2_decoder_outputs[:,i,:], num_sampled=n_sampled_softmax, num_classes=n_fra_words, partition_strategy="div")     
    total_loss.append(loss)
    return tf.add(i, 1)
def condition(i):
    return i < max_words_per_sentence["fra"]
tf.while_loop(condition, sampled_softmax_body, [i])

我的目标是对不熟悉tensorflow的人使用反向传播(使用
total_loss

),即传递一个列表但返回一对看起来可疑。你是什么意思?我的输出参数与输入参数相同。除了总损失增加之外。
[i,总损失]
(i,总损失)
是不同的形状。更改它,使您可以从
(i,总损失)
开始,或者从正文返回
[i,总损失]
。谢谢,这很有帮助。这并没有完全解决问题,但起到了帮助作用。
total_loss, i = list(), tf.constant(0)
def sampled_softmax_body(i):
    loss = tf.nn.sampled_softmax_loss(weights=logits_weights, biases=logits_biases, labels=target, inputs=h2_decoder_outputs[:,i,:], num_sampled=n_sampled_softmax, num_classes=n_fra_words, partition_strategy="div")     
    total_loss.append(loss)
    return tf.add(i, 1)
def condition(i):
    return i < max_words_per_sentence["fra"]
tf.while_loop(condition, sampled_softmax_body, [i])
In [8]: total_loss
Out[8]: [<tf.Tensor 'while/softmax_cross_entropy_with_logits/Reshape_2:0' shape=(?,) dtype=float32>]