【问题标题】:Shape of tensor after tf.layers.Dense?tf.layers.Dense 后张量的形状?
【发布时间】:2018-08-23 00:39:38
【问题描述】:

我正在制作一个解决回归问题的 DNN。

首先我加载一个预训练的 VGG16 网络,然后创建几个全连接的隐藏层。最后一层有一个节点输出一个标量。

我认为输出的形状应该类似于 [batch_size] 或 [batch_size, 1]。

但是当我打电话时......

...

fc5 = tf.layers.dense(inputs=fc4, units=1)

print(tf.shape(fc5))
print(fc5.get_shape())

...我明白了:

> Tensor("Shape:0", shape=(4,), dtype=int32)

> (?, ?, ?, 1)

有人可以解释一下吗?为什么形状有前三个维度,tf.layers.dense 不应该将其设为标量或标量列表吗?


编辑:

vgg_layer7_out 形状:

> Tensor("Shape:0", shape=(4,), dtype=int32)

> (?, ?, ?, 4096)

fc1 形状:

> Tensor("Shape:0", shape=(4,), dtype=int32)

> (?, ?, ?, 1024)

...

fc4 形状:

> Tensor("Shape:0", shape=(4,), dtype=int32)

> (?, ?, ?, 10)


fc 层代码:

fc1 = tf.layers.dense(inputs=vgg_layer7_out, units=1024, activation=tf.nn.elu, bias_initializer=init, kernel_initializer=init, kernel_regularizer=reg, bias_regularizer=reg)

drop1 = tf.nn.dropout(fc1, keep_prob)

fc2 = tf.contrib.layers.fully_connected(drop1, 128, activation_fn=tf.nn.elu, weights_initializer=init, weights_regularizer=reg)

drop2 = tf.nn.dropout(fc2, keep_prob)

fc3 = tf.contrib.layers.fully_connected(drop2, 50, activation_fn=tf.nn.elu, weights_initializer=init, weights_regularizer=reg)

drop3 = tf.nn.dropout(fc3, keep_prob)

fc4 = tf.contrib.layers.fully_connected(drop3, 10, activation_fn=tf.nn.elu, weights_initializer=init, weights_regularizer=reg)

drop2 = tf.nn.dropout(fc4, keep_prob)

fc5 = tf.layers.dense(inputs=drop2, units=1, activation=None)

【问题讨论】:

  • fc4的形状是什么?
  • 谢谢,现在在编辑中添加。
  • 我认为原因是 fc1 是从 4d vgg_layer7_out 而不是它的扁平版本 flatt 计算出来的。
  • 你是对的,它必须被展平。贴出答案。当形状为 (?,?,?,1) 时,我从来没有出错,我怎么知道现在一切正常,损失正在完美减少。

标签: python arrays tensorflow deep-learning regression


【解决方案1】:

正如 Dimitiry 所说,我忘了把它展平......你也可以在调用 loss func laster 之前使用 tf.squeeze 函数。

【讨论】:

    猜你喜欢
    • 2019-09-01
    • 2018-12-13
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    相关资源
    最近更新 更多