首页 > 解决方案 > 自动编码器输出尺寸与输入尺寸不同

问题描述

我正在使用张量流开发自动编码器。在计算损失函数时,我遇到了一个错误,说维度必须相等才能找到平均值。所以我显示了输入层和输出层的形状,两者都不一样。我无法分析问题出在哪里。数据集中使用的图像的形状是 (54,96,3) 这是我的代码

##--------------------------------------------
import cv2 as cv
import numpy as np
import tensorflow as tf
import argparse
import os
import glob
import matplotlib
import matplotlib.pyplot as plt
from functools import partial

def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
    img = cv.imread(os.path.join(folder,filename))
    if img is not None:
        images.append(img)
return np.asarray(images)

def plot_image(image, cmap = "Greys_r"):
    plt.imshow(image.reshape([54, 96, 3]).astype(np.uint8), 
               cmap=cmap,interpolation="nearest")
    plt.axis("off")

def _parse_function(filename):
    image_string = tf.read_file(filename)
    image_decoded = tf.image.decode_jpeg(image_string, channels=3)
    image = tf.cast(image_decoded, tf.float32)
    return image

## Parameters
n_inputs = 96 * 54
BATCH_SIZE = 150
batch_size = tf.placeholder(tf.int64)

files = list(glob.glob(os.path.join('danceVideoFrame1','*.*')))
dataset = tf.data.Dataset.from_tensor_slices((files))

dataset = dataset.map(_parse_function)
dataset = dataset.batch(BATCH_SIZE)

iterator = dataset.make_one_shot_iterator()
features = iterator.get_next()

with tf.Session() as sess:
    #print(sess.run(features).shape)
    #plot_image(sess.run(features), cmap = "Greys_r")
    #plt.show()

    ## Encoder
n_hidden_1 = 300
n_hidden_2 = 150  # codings

## Decoder
n_hidden_3 = n_hidden_1
n_outputs = n_inputs

learning_rate = 0.01
l2_reg = 0.0001

## Define the Xavier initialization
xav_init =  tf.contrib.layers.xavier_initializer()
## Define the L2 regularizer
l2_regularizer = tf.contrib.layers.l2_regularizer(l2_reg)

## Create the dense layer
dense_layer = partial(tf.layers.dense,
                     activation=tf.nn.elu,
                     kernel_initializer=xav_init,
                     kernel_regularizer=l2_regularizer)
## Make the mat mul
hidden_1 = dense_layer(features, n_hidden_1)
hidden_2 = dense_layer(hidden_1, n_hidden_2)
hidden_3 = dense_layer(hidden_2, n_hidden_3)
outputs = dense_layer(hidden_3, n_outputs, activation=None)

print (outputs.shape)
print (features.shape)
#loss function
loss = tf.reduce_mean(tf.square(outputs - features))
## Optimize
loss = tf.reduce_mean(tf.square(outputs - features))
optimizer = tf.train.AdamOptimizer(learning_rate)
train  = optimizer.minimize(loss)

输出:

$ python computery_dance.py
2019-01-11 03:11:14.446355: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
(?, ?, ?, 5184)
(?, ?, ?, 3)
Traceback (most recent call last):
  File "C:\Users\J MANOJ\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1628, in _create_c_op
    c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 5184 and 3 for 'sub' (op: 'Sub') with input shapes: [?,?,?,5184], [?,?,?,3].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "computery_dance.py", line 88, in <module>
    loss = tf.reduce_mean(tf.square(outputs - features))
  File "C:\Users\J MANOJ\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py", line 866, in binary_op_wrapper
    return func(x, y, name=name)
  File "C:\Users\J MANOJ\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 8912, in sub
    "Sub", x=x, y=y, name=name)
  File "C:\Users\J MANOJ\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\J MANOJ\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "C:\Users\J MANOJ\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3274, in create_op
    op_def=op_def)
  File "C:\Users\J MANOJ\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1792, in __init__
    control_input_ops)
  File "C:\Users\J MANOJ\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1631, in _create_c_op
    raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 5184 and 3 for 'sub' (op: 'Sub') with input shapes: [?,?,?,5184], [?,?,?,3].

标签: pythontensorflowmachine-learningartificial-intelligence

解决方案


形状的区别在于形状为 outputs[?, ?, ?, 5184] 的形状与features形状为 [?,?,?,3] 的形状不同。问题出在n_outputs = n_inputs. 输出的最后一个维度应等于输入的通道数(在您的情况下为 3),而不是两个输入维度的乘积(5184)。


推荐阅读