我正在尝试在Tensorflow中实现AutoEncoder。 我是Python和StackOverflow的初学者。 这两个是我的编码器和解码器。我的train_data.shape是(42000,28,28,1)(mnist数据集)。
def Network(Input):
with tf.name_scope("Encoder"):
#encoder starts here
conv1 = tf.layers.conv2d(Input, filters = 64, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv1')
conv2 = tf.layers.conv2d(conv1, filters = 64, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv2')
pool1 = tf.layers.max_pooling2d(conv2, pool_size = 2, strides = 2, name = 'pool1')
conv3 = tf.layers.conv2d(pool1, filters = 128, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv3')
conv4 = tf.layers.conv2d(conv3, filters = 128, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv4')
pool2 = tf.layers.max_pooling2d(conv4, pool_size = 2, strides = 2, name = 'pool2')
conv5 = tf.layers.conv2d(pool2, filters = 256, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv5')
conv6 = tf.layers.conv2d(conv5, filters = 256, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv6')
conv7 = tf.layers.conv2d(conv6, filters = 256, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv7')
pool3 = tf.layers.max_pooling2d(conv7, pool_size = 2, strides = 2, name = 'pool3')
conv8 = tf.layers.conv2d(pool3, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv8')
conv9 = tf.layers.conv2d(conv8, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv9')
conv10 = tf.layers.conv2d(conv9, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv10')
pool4 = tf.layers.max_pooling2d(conv10, pool_size = 2, strides = 2, name = 'pool4')
conv11 = tf.layers.conv2d(pool4, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv11')
conv12 = tf.layers.conv2d(conv11, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv12')
conv13 = tf.layers.conv2d(conv12, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'conv13')
pool5 = tf.layers.max_pooling2d(conv13, pool_size = 2, strides = 2, name = 'pool5')
return pool5
...
def Decoder(pool5):
with tf.name_scope("Decoder"):
deconv1=tf.layers.conv2d_transpose(pool5, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv1')
deconv2=tf.layers.conv2d_transpose(deconv1, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv2')
deconv3=tf.layers.conv2d_transpose(deconv2, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv3')
pool6=tf.layers.max_pooling2d(deconv3, pool_size = 2, strides = 2, name = 'pool6')
deconv4 = tf.layers.conv2d_transpose(pool6, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv4')
deconv5 = tf.layers.conv2d_transpose(deconv4, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv5')
deconv6 = tf.layers.conv2d_transpose(deconv5, filters = 512, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv6')
pool7 = tf.layers.max_pooling2d(deconv6, pool_size = 2, strides = 2, name = 'pool7')
deconv7 = tf.layers.conv2d_transpose(pool7, filters = 256, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv7')
deconv8 = tf.layers.conv2d_transpose(deconv7, filters = 256, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv8')
deconv9 = tf.layers.conv2d_transpose(deconv8, filters = 256, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv9')
pool8 = tf.layers.max_pooling2d(deconv9, pool_size = 2, strides = 2, name = 'pool8')
deconv10 = tf.layers.conv2d_transpose(pool8, filters = 128, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv10')
deconv11 = tf.layers.conv2d_transpose(deconv10, filters = 128, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv11')
pool9= tf.layers.max_pooling2d(deconv11, pool_size = 2, strides = 2, name = 'pool9')
deconv12 = tf.layers.conv2d_transpose(pool9, filters = 64, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv12')
deconv13 = tf.layers.conv2d_transpose(deconv12, filters = 64, kernel_size = 3, strides = 1, activation = tf.nn.relu, name = 'deconv13')
flat = tf.contrib.layers.flatten(deconv13)
fc1 = tf.layers.dense(flat, units = 1024, activation = tf.nn.relu, name = 'fc1')
fc2 = tf.layers.dense(fc1, units = 10, activation = None, name = 'fc2')
return fc2
我遇到了这个错误:
----------------------------------------------------------------------
ValueError Traceback (most recent call last)
ValueError: Negative dimension size caused by subtracting 3 from 2 for 'Encoder/conv6/Conv2D' (op: 'Conv2D') with input shapes: [?,2,2,256], [3,3,256,256].
我觉得我的输入中有一些错误。请建议在每次转换操作后如何可视化张量的形状。
Input = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28, 1])
答案 0 :(得分:1)
要显示张量t
的形状,请使用t.get_shape()
。该错误明确表示您尝试在太小的输入上使用太大的内核。从Stanford's CS231n class notes开始,您可以根据特定的内核大小,步幅和填充来计算数据的确切形状您应该减小内核大小,增加步幅,或者获得更大的输入能够执行那么多卷积。请注意,您的max_pooling
也会缩小输入,因为该图层中的池大小会是您的数据量的最大值,因此如果它是2,那么您的数据将缩小2倍