首页 文章

Tensorflow tf.squared_difference输出意想不到的形状

提问于
浏览
0

新手在这里,我很抱歉,如果这个问题很愚蠢,但我在网上找不到任何关于它的信息 . 我为 tf.squared_difference 的输出获得了意想不到的形状 . 我希望获得具有 shape=(100, ?) 形状的Tensor作为以下片段的损失

[print("Logits",logits,"#Labels",labels,"LOSS",tf.squared_difference(labels,logits)) for logits, labels in zip(logits_series,labels_series)]

但它会产生 (100,100) 损失

Logits Tensor(“add_185:0”,shape =(100,1),dtype = float32)#Labels Tensor(“unstack_29:0”,shape =(100,),dtype = float32)LOSS Tensor(“SquaredDifference_94:0 “,shape =(100,100),dtype = float32)Logits Tensor(”add_186:0“,shape =(100,1),dtype = float32)#Labels Tensor(”unstack_29:1“,shape =(100, ),dtype = float32)LOSS Tensor(“SquaredDifference_95:0”,shape =(100,100),dtype = float32)

我用下面的代码测试了另一个例子并给出了预期的输出形状 .

myTESTX = tf.placeholder(tf.float32, [100, None])
myTESTY = tf.placeholder(tf.float32, [100, 1])

print("Test diff X-Y",tf.squared_difference(myTESTX,myTESTY) )
print("Test diff Y-X",tf.squared_difference(myTESTY,myTESTX) )

测试diff X-Y Tensor(“SquaredDifference_92:0”,shape =(100,?),dtype = float32)测试diff Y-X Tensor(“SquaredDifference_93:0”,shape =(100,?),dtype = float32)

I am having issue why these two snippets produce different output shape

1 回答

  • 2

    第一个示例(使用 logitslabels )和第二个示例(使用 myTESTXmyTESTY )之间存在细微差别 . logitsmyTESTY(100, 1) 具有相同的形状 . 但是, labels 的形状为 (100,) (不是动态形状),但 myTESTX 的形状为 (100, ?) .

    在第一种情况下( logitslabels ),输入形状为 (100,)(100,1) ,张量流使用广播 . 输入形状都不是动态的,因此您的输出形状是静态的: (100, 100) 由于广播 .

    在第二种情况下( myTESTXmyTESTY ),输入形状为 (100, ?)(100, 1) . 第一个输入形状是动态的,因此输出形状是动态的: (100, ?) .

    作为numpy中的一个更简单的说明性示例(使用相同的广播),请考虑以下情况:

    import numpy as np
    x = np.arange(10)                # Shape: (10,)
    y = np.arange(10).reshape(10,1)  # Shape: (10, 1)
    difference = x-y                 # Shape: (10, 10)
    

相关问题