【问题标题】:How to use the dynamic shape of a tensor in tensorflow如何在张量流中使用张量的动态形状
【发布时间】:2017-05-05 18:59:28
【问题描述】:

如何为以下计算构建张量流图?我现在遇到的问题是如何使用具有可变形状大小的张量 A 的形状信息。

A = tf.placeholder(tf.float32, [None,10])
B = tf.Variable(tf.random_normal([10,20]))
C = tf.matmul(A, B)
D = tf.matmul(tf.transpose(C), A) # the value of A.shape[0]

【问题讨论】:

    标签: tensorflow tensorflow-serving


    【解决方案1】:

    您已经将张量 A 的值传递给占位符,并且当您这样做时,您已经知道它的形状。我会为您关心的形状创建另一个占位符并传递它:

    import tensorflow as tf
    import numpy as np
    
    A = tf.placeholder(tf.float32, [None,10])
    L = tf.placeholder(tf.float32, None)
    
    B = tf.Variable(tf.random_normal([10,20]))
    C = tf.matmul(A, B)
    D = tf.multiply(tf.transpose(C), L) // L is a number, matmul does not multiply matrix with a number
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        a = np.zeros((5, 10), dtype=np.float32)
        l = a.shape[0]
        sess.run(D, {A: a, L: l}) 
    

    【讨论】:

    • 谢谢。这行得通。这并不完美,因为每次你想获取 D 时,你都必须记住输入形状。
    • 实际上,有一个生成所有提要字典的助手是一个很好的做法,您可以执行以下操作:sess.run(<things>, your_dict_helper())
    • 我明白了。非常感谢!
    • 此链接显示了一种使用 TensorFlow 而不是 Python 来处理形状的方法:stackoverflow.com/questions/41668786/…
    猜你喜欢
    • 1970-01-01
    • 2018-12-13
    • 2016-12-12
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多