【问题标题】:How to dense and reshape a [3751,4] dataset to [1,6] dataset during training in Tensorflow如何在 Tensorflow 训练期间将 [3751,4] 数据集密集和重塑为 [1,6] 数据集
【发布时间】:2018-05-22 22:25:58
【问题描述】:

我正在训练一个特征形状为 [3751,4] 的模型,我想使用 Tensorflow 中内置的 reshape 和 layer dense 函数来使输出标签的形状为 [1,6]。

现在我的模型中有两个隐藏层,它们将执行以下操作:

input_layer = tf.reshape(features["x"], [-1,11,11,31,4])
first_hidden_layer = tf.layers.dense(input_layer, 4, activation=tf.nn.relu)
second_hidden_layer = tf.layers.dense(first_hidden_layer, 5, activation=tf.nn.relu)
output_layer = tf.layers.dense(second_hidden_layer, 6)

所以现在我可以拥有 [?,11,11,31,6] 的 output_layer 形状。

如何进一步塑造训练节点集,以便最终将节点连接到形状 [1,6]?

【问题讨论】:

    标签: python tensorflow machine-learning tensorflow-datasets


    【解决方案1】:

    形状 [3751, 4] 不能直接重新整形为 [-1,11,11,31,4],因为 3751*4 = 15004 不能被 11*11*31*4 = 14964 整除。


    在 OP 发表评论后编辑

    您可以展平数据集并将其作为单个示例提供。见下文

    假设tf.shape(input_feat)==[3751, 4]:

    input_layer = tf.reshape(input_feat, [1,-1])
    first_hidden_layer = tf.layers.dense(input_layer, 4, activation=tf.nn.relu)
    second_hidden_layer = tf.layers.dense(first_hidden_layer, 5, activation=tf.nn.relu)
    output_layer = tf.layers.dense(second_hidden_layer, 6)
    

    原答案

    由于您使用的是密集层,因此在网络开始时不对输入特征进行整形也可以正常工作并提供类似的结果。唯一的区别是图层中的权重会移动位置,但这不会影响您的结果。

    如果我们假设tf.shape(input_feat) == [3751, 4],下面的代码 sn-p 应该可以正常工作

    input_layer = tf.identity(input_feat)
    first_hidden_layer = tf.layers.dense(input_layer, 4, activation=tf.nn.relu)
    second_hidden_layer = tf.layers.dense(first_hidden_layer, 5, activation=tf.nn.relu)
    output_layer = tf.layers.dense(second_hidden_layer, 6)
    

    【讨论】:

    • 感谢您的回复。 3751 * 4 = 15004。此外,您提供的 sn-p 最终会给出形状 [3751,6] 而我实际上想要 [1,6]。
    • 我明白了。因此,应将整个数据集视为单个输入示例:您可以通过将其重塑为 [1, -1](参见编辑)来实现。
    猜你喜欢
    • 2019-05-01
    • 2017-09-30
    • 2016-09-13
    • 2021-10-30
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2018-05-01
    相关资源
    最近更新 更多