Python Tensorflow未执行错误

Python Tensorflow未执行错误,python,tensorflow,exception,Python,Tensorflow,Exception,我在Tensorflow中实现了以下示例: import tensorflow as tf import numpy as np def loss_function(values, a, b): N = values.shape[0] i = tf.constant(0) values_array = tf.get_variable( "values", values.shape, initializer=tf.constant_initializer

我在Tensorflow中实现了以下示例:

import tensorflow as tf
import numpy as np


def loss_function(values, a, b):
    N = values.shape[0]
    i = tf.constant(0)
    values_array = tf.get_variable(
        "values", values.shape, initializer=tf.constant_initializer(values))
    result = tf.constant(0, dtype=tf.float32)

    def body1(i):

        op2 = tf.assign(values_array[i, 0],
                        a + b)  # Here is where it should be updated. The value being assigned is actually calculated from variable a and b.

        with tf.control_dependencies([op2]):
            return tf.identity(i + 1)

    def condition1(i): return tf.less(i, N)
    i = tf.while_loop(condition1, body1, [i])

    with tf.control_dependencies([i]):
        op1 = tf.assign(values_array[0, 0],
                        999.0)  # Here is where it should be updated

        # The final cost is calculated based on the entire values_array
        with tf.control_dependencies([op1]):
            result = result + tf.reduce_mean(values_array)
            return tf.identity(result)


# The parameters we want to calculate in the end
a = tf.Variable(tf.random_uniform([1], 0, 700), name='a')
b = tf.Variable(tf.random_uniform([1], -700, 700), name='b')

values = np.ones([2, 4], dtype=np.float32)

# cost function
cost_function = loss_function(values, a, b)

# training algorithm
optimizer = tf.train.MomentumOptimizer(
    0.1, momentum=0.9).minimize(cost_function)

# initializing the variables
init = tf.global_variables_initializer()

# starting the session session
sess = tf.Session()
sess.run(init)

training_cost = sess.run([cost_function])
_ = sess.run([optimizer])

print tf.get_collection(
    tf.GraphKeys.GLOBAL_VARIABLES, scope="values")[0].eval(session=sess)
总的来说,我想要的是cost函数根据输入numpy 2D数组和参数a和b计算一个临时2D数组。然后,从临时二维阵列计算最终成本。但是,它抛出了一个
异常:UnimplementedError
UnimplementedError()
异常

有什么帮助吗


谢谢

我明白了。它抛出
UnimplementedError(回溯见上文):切片的l值形状[]与r值形状[1]不匹配。尚未实现自动广播。
。因此,将
op2=tf.assign(values\u array[i,0],a+b)
更改为
op2=tf.assign(values\u array[i,0],(a+b)[0])
将解决这个问题。

。它抛出
UnimplementedError(回溯见上文):切片的l值形状[]与r值形状[1]不匹配。尚未实现自动广播。
。因此,将
op2=tf.assign(值数组[i,0],a+b)]
更改为
op2=tf.assign(值数组[i,0],(a+b)[0])
将解决此问题