Python 如何修复 TensorFlow 中的维度错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34092850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:27:37  来源:igfitidea点击:

How do I fix a dimension error in TensorFlow?

pythontensorflow

提问by Ravaal

I'm trying to apply the expert portion of the tutorial to my own data but I keep running into dimension errors. Here's the code leading up to the error.

我正在尝试将本教程的专家部分应用于我自己的数据,但我一直遇到维度错误。这是导致错误的代码。

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1], padding='SAME')

W_conv1 = weight_variable([1, 8, 1, 4])
b_conv1 = bias_variable([4])

x_image = tf.reshape(tf_in, [-1,2,8,1])

h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

And then when I try to run this command:

然后当我尝试运行此命令时:

W_conv2 = weight_variable([1, 4, 4, 8])
b_conv2 = bias_variable([8])

h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

I get the following errors:

我收到以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-41-7ab0d7765f8c> in <module>()
      3 
      4 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
----> 5 h_pool2 = max_pool_2x2(h_conv2)

ValueError: ('filter must not be larger than the input: ', 'Filter: [', Dimension(2), 'x', Dimension(2), '] ', 'Input: [', Dimension(1), 'x', Dimension(4), '] ')

Just for some background information, the data that I'm dealing with is a CSV file where each row contains 10 features and 1 empty column that can be a 1 or a 0. What I'm trying to get is a probability in the empty column that the column will equal a 1.

只是为了一些背景信息,我正在处理的数据是一个 CSV 文件,其中每行包含 10 个特征和 1 个可以是 1 或 0 的空列。我想要得到的是空的概率列将等于 1。

采纳答案by Ravaal

You have to shape the input so it is compatible with both the training tensor and the output. If you input is length 1, your output should be length 1 (length is substituted for dimension).

您必须对输入进行整形,使其与训练张量和输出兼容。如果输入长度为 1,则输出应为长度 1(长度替换尺寸)。

When you're dealing with-

当你在处理——

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 1, 1, 1],
                    strides=[1, 1, 1, 1], padding='SAME')

Notice how I changed the strides and the ksize to [1, 1, 1, 1]. This will match an output to a 1 dimensional input and prevent errors down the road.

请注意我如何将 strides 和 ksize 更改为[1, 1, 1, 1]. 这将使输出与一维输入相匹配,并防止出现错误。

When you're defining your weight variable (see code below)-

当您定义权重变量时(请参阅下面的代码)-

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

you're going to have to make the first 2 numbers conform to the feature tensor that you are using to train your model, the last two numbers will be the dimension of the predicted output (same as the dimension of the input).

您必须使前两个数字符合用于训练模型的特征张量,最后两个数字将是预测输出的维度(与输入的维度相同)。

W_conv1 = weight_variable([1, 10, 1, 1])
b_conv1 = bias_variable([1])

Notice the [1, 10,in the beginning which signifies that the feature tensor is going to be a 1x10 feature tensor; the last two numbers 1, 1]correspond to the dimensions of the input and output tensors/predictors.

注意[1, 10,开头的 表示特征张量将是 1x10 特征张量;最后两个数字1, 1]对应于输入和输出张量/预测器的维度。

When you reshape your x_foo tensor (I call it x_ [x prime]), you, for whatever reason, have to define it like so-

当你重塑你的 x_foo 张量(我称之为 x_ [x prime])时,无论出于何种原因,你都必须像这样定义它-

x_ = tf.reshape(x, [-1,1,10,1])

Notice the 1 and 10 in the middle- ...1,10,.... Once again, these numbers correspond to the dimension of your feature tensor.

注意中间的 1 和 10 - ...1,10,...。同样,这些数字对应于特征张量的维度。

For every bias variable, you choose the final number of the previously defined variable. For example, if W_conv1 = weight_variable([1, 10, 1, 1])appears like so, you take the final number and put that into your bias variable so it can match the dimensions of the input. This is done like so- b_conv1 = bias_variable([1]).

对于每个偏差变量,您选择先前定义的变量的最终数字。例如,如果W_conv1 = weight_variable([1, 10, 1, 1])看起来像这样,您将最终数字放入偏差变量中,以便它可以匹配输入的维度。这是这样做的 - b_conv1 = bias_variable([1])

If you need any more explanation please comment below.

如果您需要更多解释,请在下面评论。

回答by erickrf

The dimensions you are using for the filter are not matching the output of the hidden layer.

您用于过滤器的维度与隐藏层的输出不匹配。

Let me see if I understood you: your input is composed of 8 features, and you want to reshape it into a 2x4 matrix, right?

让我看看我是否理解您:您的输入由 8 个特征组成,您想将其重塑为 2x4 矩阵,对吗?

The weights you created with weight_variable([1, 8, 1, 4])expect a 1x8 input, in one channel, and produce a 1x8 output in 4 channels (or hidden units). The filter you are using sweeps the input in 2x2 squares. However, since the result of the weights is 1x8, they won't match.

您创建的权weight_variable([1, 8, 1, 4])重在一个通道中期望 1x8 输入,并在 4 个通道(或隐藏单元)中产生 1x8 输出。您使用的过滤器以 2x2 的正方形扫描输入。但是,由于权重的结果是 1x8,因此它们不会匹配。

You should reshape the input as

您应该将输入重塑为

x_image = tf.reshape(tf_in, [-1,2,4,1])

Now, your input is actually 2x4 instead of 1x8. Then you need to change the weight shape to (2, 4, 1, hidden_units)to deal with a 2x4 output. It will also produce a 2x4 output, and the 2x2 filter now can be applied.

现在,您的输入实际上是 2x4 而不是 1x8。然后您需要将权重形状更改为(2, 4, 1, hidden_units)以处理 2x4 输出。它还将产生 2x4 输出,现在可以应用 2x2 过滤器。

After that, the filter will match the output of the weights. Also note that you will have to change the shape of your second weight matrix to weight_variable([2, 4, hidden_units, hidden2_units])

之后,过滤器将匹配权重的输出。另请注意,您必须将第二个权重矩阵的形状更改为weight_variable([2, 4, hidden_units, hidden2_units])