我正在训练一个模型,其特征形状为[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)
因此,现在我可以得到output_layer的形状为[?,11,11,31,6]。
如何进一步调整训练节点集,使其最终连接到形状为[1,6]的节点?
回答:
形状[3751, 4]
无法直接重塑为[-1,11,11,31,4]
,因为3751*4 = 15004
不能被11*11*31*4 = 14964
整除。
根据原始发布者的评论后的编辑
您可以将数据集展平,并将其作为单个示例输入。请看下方
假设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]
,以下代码片段应该可以正常工作
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)