TensorFlow优化器抗议一些指数



i具有以下图形网络:

initia = tf.zeros_initializer
I = tf.placeholder(tf.float32, shape=[None,1], name='I') # input
W = tf.get_variable('W', shape=[1,DEPTH], initializer=initia, dtype=tf.float32) # weights
b = tf.get_variable('b', shape=[DEPTH], initializer=initia, dtype=tf.float32) # biases
O = tf.nn.relu(tf.matmul(I, W) + b, name='O')
O_0 = tf.gather_nd(O, [0,0])
W_1 = tf.gather_nd(W, [0,1])
distance = tf.square( O_0 - W_1 )
train_op = tf.train.GradientDescentOptimizer(1e-4).minimize(distance)

这将失败此错误:

valueerror:形状必须至少排名1,但是等级0 带有输入 形状:[2],[],[2]。

我尝试将距离标量嵌入到1 dim张量中,但它给出了类似的错误:

train_op = tf.train.GradientDescentOptimizer(1e-4).minimize(tf.stack([distance]))

任何想法上面的图是怎么了?

如果修改了 indices参数,以便它是单个元素位置的列表

O_0 = tf.gather_nd(O, [[0, 0]]) # if DEPTH is 2
W_1 = tf.gather_nd(W, [[0, 1]])

它可以正常工作:

DEPTH=2
initia = tf.zeros_initializer
I = tf.placeholder(tf.float32, shape=[None,1], name='I') # input
W = tf.get_variable('W', shape=[1,DEPTH], initializer=initia, dtype=tf.float32) # weights
b = tf.get_variable('b', shape=[DEPTH], initializer=initia, dtype=tf.float32) # biases
O = tf.nn.relu(tf.matmul(I, W) + b, name='O')
O_0 = tf.gather_nd(O, [[0,0]])
W_1 = tf.gather_nd(W, [[0, 1]])
distance = tf.square( O_0 - W_1 )
train_op = tf.train.GradientDescentOptimizer(1e-4).minimize(distance)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(train_op, {I: [[1], [2], [3]]}))

您将在文档中找到有关indices参数的更多信息:

将切片从params收集到带有indices指定的形状的张量。

indices是k维整数张量,最好将其视为 (K-1( - 索引的维度张量为params,其中每个元素都定义了 params的切片:

  output[i_0, ..., i_{K-2}] = params[indices[i0, ..., i_{K-2}]]

而在 @{tf.gather} indices中将切片定义为第一个 params的维度,在tf.gather_nd中,indices将切片定义为 params的第一个N尺寸,其中N = indices.shape[-1]

indices的最后一个维度最多可以是 params

  indices.shape[-1] <= params.rank

indices的最后一个维度对应于元素 (如果indices.shape[-1] == params.rank(或切片 (如果indices.shape[-1] < params.rank(沿

最新更新