我认为我可以满足我的需求。代码的注释/详细信息在代码上:
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
task_name = 'task_MNIST_flat_auto_encoder'
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
X_train, Y_train = mnist.train.images, mnist.train.labels # N x D
X_cv, Y_cv = mnist.validation.images, mnist.validation.labels
X_test, Y_test = mnist.test.images, mnist.test.labels
# data shape is "[batch, in_height, in_width, in_channels]",
# X_train = N x D
N, D = X_train.shape
# think of it as N images with height 1 and width D.
X_train = X_train.reshape(N,1,D,1)
x = tf.placeholder(tf.float32, shape=[None,1,D,1], name='x-input')
#x = tf.Variable( X_train , name='x-input')
# filter shape is "[filter_height, filter_width, in_channels, out_channels]"
filter_size, nb_filters = 10, 12 # filter_size , number of hidden units/units
# think of it as having nb_filters number of filters, each of size filter_size
W = tf.Variable( tf.truncated_normal(shape=[1, filter_size, 1,nb_filters], stddev=0.1) )
stride_convd1 = 2 # controls the stride for 1D convolution
conv = tf.nn.conv2d(input=x, filter=W, strides=[1, 1, stride_convd1, 1], padding="SAME", name="conv")
with tf.Session() as sess:
sess.run( tf.initialize_all_variables() )
sess.run(fetches=conv, feed_dict={x:X_train})
感谢Olivier的帮助(有关进一步的说明,请参见他的评论中的讨论)。
手动检查:
X_train_org = np.array([[0,1,2,3]])
N, D = X_train_org.shape
X_train_1d = X_train_org.reshape(N,1,D,1)
#X_train = tf.constant( X_train_org )
# think of it as N images with height 1 and width D.
xx = tf.placeholder(tf.float32, shape=[None,1,D,1], name='xx-input')
#x = tf.Variable( X_train , name='x-input')
# filter shape is "[filter_height, filter_width, in_channels, out_channels]"
filter_size, nb_filters = 2, 2 # filter_size , number of hidden units/units
# think of it as having nb_filters number of filters, each of size filter_size
filter_w = np.array([[1,3],[2,4]]).reshape(1,filter_size,1,nb_filters)
#W = tf.Variable( tf.truncated_normal(shape=[1,filter_size,1,nb_filters], stddev=0.1) )
W = tf.Variable( tf.constant(filter_w, dtype=tf.float32) )
stride_convd1 = 2 # controls the stride for 1D convolution
conv = tf.nn.conv2d(input=xx, filter=W, strides=[1, 1, stride_convd1, 1], padding="SAME", name="conv")
#C = tf.constant( (np.array([[4,3,2,1]]).T).reshape(1,1,1,4) , dtype=tf.float32 ) #
#tf.reshape( conv , [])
#y_tf = tf.matmul(conv, C)
##
x = tf.placeholder(tf.float32, shape=[None,D], name='x-input') # N x 4
W1 = tf.Variable( tf.constant( np.array([[1,2,0,0],[3,4,0,0]]).T, dtype=tf.float32 ) ) # 2 x 4
y1 = tf.matmul(x,W1) # N x 2 = N x 4 x 4 x 2
W2 = tf.Variable( tf.constant( np.array([[0,0,1,2],[0,0,3,4]]).T, dtype=tf.float32 ))
y2 = tf.matmul(x,W2) # N x 2 = N x 4 x 4 x 2
C1 = tf.constant( np.array([[4,3]]).T, dtype=tf.float32 ) # 1 x 2
C2 = tf.constant( np.array([[2,1]]).T, dtype=tf.float32 )
p1 = tf.matmul(y1,C1)
p2 = tf.matmul(y2,C2)
y = p1 + p2
with tf.Session() as sess:
sess.run( tf.initialize_all_variables() )
print 'manual conv'
print sess.run(fetches=y1, feed_dict={x:X_train_org})
print sess.run(fetches=y2, feed_dict={x:X_train_org})
#print sess.run(fetches=y, feed_dict={x:X_train_org})
print 'tf conv'
print sess.run(fetches=conv, feed_dict={xx:X_train_1d})
#print sess.run(fetches=y_tf, feed_dict={xx:X_train_1d})
输出:
manual conv
[[ 2. 4.]]
[[ 8. 18.]]
tf conv
[[[[ 2. 4.]
[ 8. 18.]]]]
0
好的,我想在Tensorflow中对时间序列数据进行一维卷积。根据这些 票证和手册 ,显然可以使用
tf.nn.conv2d
支持此操作 。唯一的要求是设置strides=[1,1,1,1]
。听起来很简单!但是,即使在非常小的测试用例中,我也无法弄清楚该如何做。我究竟做错了什么?
让我们进行设置。
好的,现在在两个小的数组上生成基本的卷积测试。我将通过使用1的批量大小来简化该操作,并且由于时间序列是一维的,因此我的“图像高度”将为1。并且由于它是单变量时间序列,因此显然“通道”的数量也是1,所以这很简单,对吧?
繁荣。错误。
好的,首先,我不知道在任何维度上都应该发生这种情况,因为我已经指定要在卷积OP中填充参数。
但是很好,也许对此有限制。我一定对文档感到困惑,并且在张量的错误轴上设置了这种卷积。我将尝试所有可能的排列:
结果:
嗯好的,看来现在有两个问题。首先,我猜
ValueError
是关于沿错误的轴应用过滤器的,尽管有两种形式。但是,随后我可以沿其应用过滤器的轴也令人困惑-请注意,它实际上构造了具有输入形状(5,1,1,1,1)和过滤器形状(1,1,1,3)的图。根据文档中的AFAICT,这应该是一个过滤器,以批次为例,一个“像素”和一个“通道”,输出3个“通道”。那么,当其他人不起作用时,为什么一个人起作用呢?
无论如何,有时在构建图形时它不会失败。有时它会构造图;然后我们得到
tensorflow.python.framework.errors.InvalidArgumentError
。从一些令人困惑的github票证中,我收集到这可能是由于我在CPU而不是GPU上运行的事实,反之亦然卷积运算仅针对32位浮点数而不是64位浮点数进行定义的事实。如果任何人都可以扔在哪个轴我应该对准什么上,为了与卷积内核时间序列一些轻,我会非常感激。