简单示例

时间:2017-11-08 12:22:02

标签: tensorflow tensorflow-gpu

关于conv1d的两个简单示例:1维特征: ` 由[2,1,3]卷积的[1,0,2,3,0,1,1],输出为[8. 11. 7. 9. 4.],这是对的。但对于二维特征:" [[1,0,2,3,0,1,1],[1,0,2,3,0,1,1]]和#34;这由[" [[2,1,3],[2,1,3]]"应该输出" [[8. 11. 7. 9. 4.],[8。11. 7. 9. 4。]]"

`

但是程序输出:

[[  2.   1.   3.]
[ 10.   5.  15.]
[  2.   1.   3.]
[  4.   2.   6.]
[  4.   2.   6.]
[  6.   3.   9.]
[  4.   2.   6.]]

问题出在哪里?任何帮助将不胜感激。*

import tensorflow as tf
i = tf.constant([1, 0, 2, 3, 0, 1, 1], dtype=tf.float32, name='i')
print(i.shape)
ii = tf.constant([[1, 0, 2, 3, 0, 1, 1],[1, 0, 2, 3, 0, 1, 1]])
print(ii.shape)
k = tf.constant([2, 1, 3], dtype=tf.float32, name='k')
k2 = tf.constant([[2, 1, 3], [2, 1, 3]], dtype=tf.float32, name='k')
print(k2.shape)

data = tf.reshape(i, [1, int(i.shape[0]), 1], name='data')
data2 = tf.reshape(ii, [1, int(i.shape[0]), 2], name='data')

kernel = tf.reshape(k, [int(k.shape[0]), 1, 1], name='kernel')
kernel2 = tf.reshape(k2, [1, int(k2.shape[0]), 3], name='kernel')
print(kernel2)

res = tf.squeeze(tf.nn.conv1d(data, kernel, 1, 'VALID'))
res2 = tf.squeeze(tf.nn.conv1d(data2, kernel2, 1, 'VALID'))

with tf.Session() as sess:
    print(sess.run(kernel2))
    print sess.run(res)
    print sess.run(res2)

2 个答案:

答案 0 :(得分:2)

1)tf.nn.conv1d的默认输入格式是[batch,in_width,in_channels],在你的情况下它是[2,7,1](对于data2)

2)卷积内核在批次之间是相同的,因此您不需要为每个批次克隆内核,除非您想为同一输入应用不同的内核,这将导致更多输出中的通道。 (f.e。[2,7,2])

因此,如果您想获得上述结果,代码应如下所示:

k = tf.constant([2, 1, 3], dtype=tf.float32, name='k')

data = tf.reshape(i, [1, int(i.shape[0]), 1], name='data')
data2 = tf.reshape(ii, [2, int(i.shape[0]), 1], name='data2')

kernel = tf.reshape(k, [int(k.shape[0]), 1, 1], name='kernel')

res = tf.squeeze(tf.nn.conv1d(data, kernel, 1, 'VALID'))
res2 = tf.squeeze(tf.nn.conv1d(data2, kernel, 1, 'VALID'))

答案 1 :(得分:0)

我想说明一下Conv1D如何通过以下两种方式工作。

enter image description here 让我们看看如何转移Conv1D以及Conv2D问题。由于Conv1D通常用于NLP场景,我们可以在下面的NLP问题中对此进行说明。
enter image description here

首先让我们使用Conv2D方法做到这一点:

cat = [0.7, 0.4, 0.5]
sitting = [0.2, -0.1, 0.1]
there = [-0.5, 0.4, 0.1]
dog = [0.6, 0.3, 0.5]
resting = [0.3, -0.1, 0.2]
here = [-0.5, 0.4, 0.1]
sentence = tf.constant([[cat, sitting, there, dog, resting, here]]
# [batch, in_width, in_channels]: [1, 6, 3]
data = tf.reshape(sentence), (1, 1, 6, 3))
# we reshape [batch, in_width, in_channels] to [batch, 1, in_width, in_channels] according to the quote above
# each dimension in the embedding is a channel(three in_channels)
f1c1 = [0.6, 0.2]
f1c2 = [0.4, -0.1]
f1c3 = [0.5, 0.2]
# filters = tf.constant([[f1c1, f1c2, f1c3]])
# [out_channels, in_channels, filter_width]: [1, 3, 2]
# here we have also only one filter and also three channels in it. 
filter1D = tf.transpose(tf.constant([[f3c1, f3c2, f3c3]]), (2, 1, 0))
# shape: [2, 3, 1] for the conv1d example
filters = tf.reshape(filter1D, (1, 2, 3, 1))  # this should be expand_dim actually
# transpose [out_channels, in_channels, filter_width] to [filter_width, in_channels, out_channels]] and then reshape the result to [1, filter_width, in_channels, out_channels]
output = tf.squeeze(tf.nn.conv2d(data, filters, strides=(1, 1, 2, 1), padding="VALID"))
# the numbers for strides are for [batch, 1, in_width, in_channels] of the data input
# <tf.Tensor: id=119, shape=(3,), dtype=float32, numpy=array([0.9       , 0.09999999, 0.12      ], dtype=float32)>

让我们使用Conv1D(在TensorFlow中也是如此)

output = tf.squeeze(tf.nn.conv1d(sentence, filter1D, stride=2, padding="VALID"))
# <tf.Tensor: id=135, shape=(3,), dtype=float32, numpy=array([0.9       , 0.09999999, 0.12      ], dtype=float32)>
# here stride defaults to be for the in_width

希望有帮助。