我正在使用TensorFlow开发一个自编码器。在计算损失函数时,我遇到了一个错误,提示必须找到均值的维度必须相等。因此,我显示了输入层和输出层的形状,发现两者不同。我无法分析问题出在哪里。数据集中使用的图像形状为(54,96,3)。这是我的代码
##--------------------------------------------import cv2 as cvimport numpy as npimport tensorflow as tfimport argparseimport osimport globimport matplotlibimport matplotlib.pyplot as pltfrom functools import partialdef 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## Parametersn_inputs = 96 * 54BATCH_SIZE = 150batch_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() ## Encodern_hidden_1 = 300n_hidden_2 = 150 # codings## Decodern_hidden_3 = n_hidden_1n_outputs = n_inputslearning_rate = 0.01l2_reg = 0.0001## Define the Xavier initializationxav_init = tf.contrib.layers.xavier_initializer()## Define the L2 regularizerl2_regularizer = tf.contrib.layers.l2_regularizer(l2_reg)## Create the dense layerdense_layer = partial(tf.layers.dense, activation=tf.nn.elu, kernel_initializer=xav_init, kernel_regularizer=l2_regularizer)## Make the mat mulhidden_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 functionloss = tf.reduce_mean(tf.square(outputs - features))## Optimizeloss = tf.reduce_mean(tf.square(outputs - features))optimizer = tf.train.AdamOptimizer(learning_rate)train = optimizer.minimize(loss)
输出:
$ python computery_dance.py2019-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].
回答:
形状的差异在于outputs
的形状为[?, ?, ?, 5184],而features
的形状为[?,?,?,3]。问题出在n_outputs = n_inputs
。输出的最后一个维度应该等于输入的通道数(在你的例子中是3),而不是输入两个维度的乘积(5184)。