好的,我想在Tensorflow中对时间序列数据进行一维卷积。tf.nn.conv2d
根据这些 票证和手册,显然可以支持此操作。唯一的要求是设置strides=[1,1,1,1]
。听起来很简单!
但是,即使在非常小的测试用例中,我也无法弄清楚如何做到这一点。我究竟做错了什么?
让我们进行设置。
import tensorflow as tf
import numpy as np
print(tf.__version__)
>>> 0.9.0
好的,现在在两个小的数组上生成一个基本的卷积测试。我将通过使用1的批处理大小来简化该操作,并且由于时间序列是一维的,所以我的“图像高度”将为1。并且由于它是单变量时间序列,因此显然“通道”的数量也是1,所以这很简单,对吧?
g = tf.Graph()
with g.as_default():
# data shape is "[batch, in_height, in_width, in_channels]",
x = tf.Variable(np.array([0.0, 0.0, 0.0, 0.0, 1.0]).reshape(1,1,-1,1), name="x")
# filter shape is "[filter_height, filter_width, in_channels, out_channels]"
phi = tf.Variable(np.array([0.0, 0.5, 1.0]).reshape(1,-1,1,1), name="phi")
conv = tf.nn.conv2d(
phi,
x,
strides=[1, 1, 1, 1],
padding="SAME",
name="conv")
繁荣。错误。
ValueError: Dimensions 1 and 5 are not compatible
好的,首先,我不知道在任何维度上该如何发生,因为我已经指定要在卷积OP中填充参数。
但是很好,也许对此有限制。我一定对文档感到困惑,并在张量的错误轴上设置了这种卷积。我将尝试所有可能的排列:
for i in range(4):
for j in range(4):
shape1 = [1,1,1,1]
shape1[i] = -1
shape2 = [1,1,1,1]
shape2[j] = -1
x_array = np.array([0.0, 0.0, 0.0, 0.0, 1.0]).reshape(*shape1)
phi_array = np.array([0.0, 0.5, 1.0]).reshape(*shape2)
try:
g = tf.Graph()
with g.as_default():
x = tf.Variable(x_array, name="x")
phi = tf.Variable(phi_array, name="phi")
conv = tf.nn.conv2d(
x,
phi,
strides=[1, 1, 1, 1],
padding="SAME",
name="conv")
init_op = tf.initialize_all_variables()
sess = tf.Session(graph=g)
sess.run(init_op)
print("SUCCEEDED!", x_array.shape, phi_array.shape, conv.eval(session=sess))
sess.close()
except Exception as e:
print("FAILED!", x_array.shape, phi_array.shape, type(e), e.args or e._message)
结果:
FAILED! (5, 1, 1, 1) (3, 1, 1, 1) <class 'ValueError'> ('Filter must not be larger than the input: Filter: (3, 1) Input: (1, 1)',)
FAILED! (5, 1, 1, 1) (1, 3, 1, 1) <class 'ValueError'> ('Filter must not be larger than the input: Filter: (1, 3) Input: (1, 1)',)
FAILED! (5, 1, 1, 1) (1, 1, 3, 1) <class 'ValueError'> ('Dimensions 1 and 3 are not compatible',)
FAILED! (5, 1, 1, 1) (1, 1, 1, 3) <class 'tensorflow.python.framework.errors.InvalidArgumentError'> No OpKernel was registered to support Op 'Conv2D' with these attrs
[[Node: conv = Conv2D[T=DT_DOUBLE, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](x/read, phi/read)]]
FAILED! (1, 5, 1, 1) (3, 1, 1, 1) <class 'tensorflow.python.framework.errors.InvalidArgumentError'> No OpKernel was registered to support Op 'Conv2D' with these attrs
[[Node: conv = Conv2D[T=DT_DOUBLE, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](x/read, phi/read)]]
FAILED! (1, 5, 1, 1) (1, 3, 1, 1) <class 'ValueError'> ('Filter must not be larger than the input: Filter: (1, 3) Input: (5, 1)',)
FAILED! (1, 5, 1, 1) (1, 1, 3, 1) <class 'ValueError'> ('Dimensions 1 and 3 are not compatible',)
FAILED! (1, 5, 1, 1) (1, 1, 1, 3) <class 'tensorflow.python.framework.errors.InvalidArgumentError'> No OpKernel was registered to support Op 'Conv2D' with these attrs
[[Node: conv = Conv2D[T=DT_DOUBLE, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](x/read, phi/read)]]
FAILED! (1, 1, 5, 1) (3, 1, 1, 1) <class 'ValueError'> ('Filter must not be larger than the input: Filter: (3, 1) Input: (1, 5)',)
FAILED! (1, 1, 5, 1) (1, 3, 1, 1) <class 'tensorflow.python.framework.errors.InvalidArgumentError'> No OpKernel was registered to support Op 'Conv2D' with these attrs
[[Node: conv = Conv2D[T=DT_DOUBLE, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](x/read, phi/read)]]
FAILED! (1, 1, 5, 1) (1, 1, 3, 1) <class 'ValueError'> ('Dimensions 1 and 3 are not compatible',)
FAILED! (1, 1, 5, 1) (1, 1, 1, 3) <class 'tensorflow.python.framework.errors.InvalidArgumentError'> No OpKernel was registered to support Op 'Conv2D' with these attrs
[[Node: conv = Conv2D[T=DT_DOUBLE, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](x/read, phi/read)]]
FAILED! (1, 1, 1, 5) (3, 1, 1, 1) <class 'ValueError'> ('Dimensions 5 and 1 are not compatible',)
FAILED! (1, 1, 1, 5) (1, 3, 1, 1) <class 'ValueError'> ('Dimensions 5 and 1 are not compatible',)
FAILED! (1, 1, 1, 5) (1, 1, 3, 1) <class 'ValueError'> ('Dimensions 5 and 3 are not compatible',)
FAILED! (1, 1, 1, 5) (1, 1, 1, 3) <class 'ValueError'> ('Dimensions 5 and 1 are not compatible',)
唔。好的,看来现在有两个问题。首先,ValueError
我猜想是关于沿错误的轴应用滤镜,尽管有两种形式。
但是,随后我可以沿其应用过滤器的轴也令人困惑-请注意,它实际上构造了具有输入形状(5、1、1、1、1)和过滤器形状(1、1、1、1、3)的图形。根据文档中的AFAICT,这应该是一个过滤器,该过滤器查看批次中的示例,一个“像素”和一个“通道”,并输出3个“通道”。那么,当其他人不起作用时,为什么一个人起作用呢?
无论如何,有时在构建图形时它不会失败。有时它会构造图;然后我们得到了tensorflow.python.framework.errors.InvalidArgumentError
。从一些令人困惑的github票证中,我收集到这可能是由于我在CPU而不是GPU上运行的事实,反之亦然卷积运算仅针对32位浮点数而不是64位浮点数进行定义的事实。如果任何人都可能会引发一些光其轴我应该对准什么上,为了与卷积内核时间序列,我会非常感激。
不好意思地说,但是您的第一个代码几乎是正确的。你只是倒置x
和phi
在tf.nn.conv2d
:
g = tf.Graph()
with g.as_default():
# data shape is "[batch, in_height, in_width, in_channels]",
x = tf.Variable(np.array([0.0, 0.0, 0.0, 0.0, 1.0]).reshape(1, 1, 5, 1), name="x")
# filter shape is "[filter_height, filter_width, in_channels, out_channels]"
phi = tf.Variable(np.array([0.0, 0.5, 1.0]).reshape(1, 3, 1, 1), name="phi")
conv = tf.nn.conv2d(
x,
phi,
strides=[1, 1, 1, 1],
padding="SAME",
name="conv")
更新:从版本r0.11开始,TensorFlow现在支持使用1D卷积tf.nn.conv1d
。我以前在粘贴到这里的stackoverflow文档中(现在已绝种)做了一个使用它们的指南:
考虑一个基本示例,输入的长度10
和尺寸为16
。批处理大小为32
。因此,我们有一个带有输入shape的占位符[batch_size, 10, 16]
。
batch_size = 32
x = tf.placeholder(tf.float32, [batch_size, 10, 16])
然后,我们创建一个宽度为3的过滤器,并以16
通道作为输入,并同时输出16
通道。
filter = tf.zeros([3, 16, 16]) # these should be real values, not 0
最后,我们应用tf.nn.conv1d
了跨距和填充:-跨距:整数s
-填充:这与2D相似,您可以在SAME
和之间进行选择VALID
。SAME
将输出相同的输入长度,而VALID
不会添加零填充。
在我们的示例中,跨度为2,有效填充为空白。
output = tf.nn.conv1d(x, filter, stride=2, padding="VALID")
输出形状应为[batch_size, 4, 16]
。
有了padding="SAME"
,我们的输出形状为[batch_size, 5, 16]
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句