【问题标题】:TensorFlow - Slicing tensor results in: ValueError: Shape (16491,) must have rank 3TensorFlow - 切片张量导致:ValueError: Shape (16491,) must have rank 3
【发布时间】:2016-05-06 12:49:29
【问题描述】:

我想通过索引列表对张量进行切片以获取特定的张量,例如:

word_weight   = tf.get_variable("word_weight", [20])
a= word_weight[ [1,6,5] ]

(我想得到word_weight[1], word_weight[6], word_weight[5]

但是当我运行代码时出现以下错误:

ValueError: Shape (16491,) must have rank 3

【问题讨论】:

    标签: python tensorflow deep-learning


    【解决方案1】:

    首先,首先评估张量。然后,您可以索引它们:

    import tensorflow as tf
    
    word_weight = tf.get_variable("word_weight", [20])
    
    with tf.Session() as sess:   
        tf.initialize_all_variables().run()
        x = sess.run(word_weight)
        print(x[[1,6,5]])
        # Or evaluete like this
        print(sess.run([word_weight[1],word_weight[6],word_weight[5]]))
    

    这个输出:

    [ 1.61491954  0.66727936 -0.73491937]
    

    【讨论】:

    • 我可以在索引张量后评估张量吗?即 X= word_weight[ [1,6,5] ], y=sess.run(X)
    • @NilsCao 这可能有效:x = sess.run([word_weight[1],word_weight[6],word_weight[5]]) print(x)。我在答案中添加了。
    • x=sess.run(word_weight[[1,6,5]]) 也可以工作吗?很抱歉我现在不在电脑附近。
    • @NilsCao, sess.run(word_weight[[1,6,5]]) ooes 不起作用。
    • 这太有线了。非常感谢。我正在做神经网络算法。我将张量变量定义和传播算法放在类的init函数中,并在创建类的对象后评估张量。看来我不应该将传播算法放在类的 init 函数中,以便我可以解决我的问题。
    猜你喜欢
    • 1970-01-01
    • 2018-06-01
    • 2021-12-29
    • 2018-10-20
    • 2017-02-04
    • 2022-07-16
    • 1970-01-01
    • 2017-04-11
    • 2021-02-08
    相关资源
    最近更新 更多