Tensorflow numeric_column似乎拉平了张量尺寸

时间:2018-10-06 01:42:32

标签: python tensorflow tensorflow-estimator

我正在尝试在python中构建一个Tensorflow自定义估算器API。我遵循了在这里几乎逐字记录的示例代码:https://github.com/tensorflow/models/blob/master/samples/core/get_started/custom_estimator.py

但是,我正在尝试实现CNN,因此我的模型函数具有以下结构,并通过params []传递了一些额外的参数:

############### Define the Graph Structure
net = tf.feature_column.input_layer(features, params['feature_columns'])

for filters,window in params['CNN']:
    net = tf.layers.conv1d(inputs=net, filters=filters, kernel_size=window, activation=tf.nn.relu)

    net = tf.layers.max_pooling1d(inputs=net,pool_size=params['POOL'])

for nodes in params['DENSE']:
    net = tf.layers.dense(inputs=net,units=nodes,activation=tf.nn.relu)

output = tf.layers.dense(inputs=net,units=params['OUT'],activation=tf.nn.relu)

我已将要素列定义为五个长度为32的序列。尺寸为(32,5)。

the_feature_column = []
for key in feattraindatanorm.keys():
the_feature_column.append(tf.feature_column.numeric_column(key = key, shape=(32,5)))

我按如下方式创建随机数据:

testtestdata = dict({'sequences':np.random.rand(100,32,5)})
testtestlabel = dict({'labels':np.random.rand(100)})
traintestdata = dict({'sequences':np.random.rand(100,32,5)})
traintestlabel = dict({'labels':np.random.rand(100)})

我的输入函数看起来像这样(还是上面链接文件的一个副本):

def input_functor(datanorm,labelsnorm,batch_size):
    long = int(datanorm['sequences'].shape[0])
    dataset = tf.data.Dataset.from_tensor_slices((datanorm,labelsnorm))
    dataset = dataset.shuffle(long).repeat().batch(batch_size)
    return dataset.make_one_shot_iterator().get_next()

Tensorflow构造模型没有错误。但是,当我尝试训练时,出现以下错误:

~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\layers\base.py in _assert_input_compatibility(self, inputs)
   1184                            'expected ndim=' + str(spec.ndim) + ', found ndim=' +
   1185                            str(ndim) + '. Full shape received: ' +
-> 1186                            str(x.get_shape().as_list()))
   1187       if spec.max_ndim is not None:
   1188         ndim = x.get_shape().ndims

ValueError: Input 0 of layer conv1d_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 160]

看来我的要素列尺寸(batch,32,5)已经以某种方式展平到(batch,160),并且与CNN第一层不兼容。

我已经尽力想尽一切办法,但是找不到输入为什么不能保持正确尺寸的原因。

0 个答案:

没有答案