Python TensorFlow中的MatMul问题

Python TensorFlow中的MatMul问题,python,tensorflow,neural-network,Python,Tensorflow,Neural Network,我正在尝试用4D numpy阵列数据在TensorFlow中实现多层感知器 我在MatMul函数上得到了这个问题。 我希望有人能在这里帮助我,非常感谢你 ValueError: Shape must be rank 2 but is rank 4 for 'MatMul' (op: 'MatMul') with input shapes: [1500,2,10000,5], [1500,1500]. 我的代码是: # Network Parameters n_hidden_1 = 1500 #

我正在尝试用4D numpy阵列数据在TensorFlow中实现多层感知器 我在MatMul函数上得到了这个问题。 我希望有人能在这里帮助我,非常感谢你

ValueError: Shape must be rank 2 but is rank 4 for 'MatMul' (op: 'MatMul') with input shapes: [1500,2,10000,5], [1500,1500].
我的代码是:

# Network Parameters
n_hidden_1 = 1500 # 1st layer number of neurons
n_hidden_2 = 1500 # 2nd layer number of neurons
n_input = 1500 
n_classes = 1500

# tf Graph input
X = tf.placeholder("float", [1500,2,10000,5])
Y = tf.placeholder("float", [1500,1])

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}


# Create model
def multilayer_perceptron(x):

    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])

    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])

    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

    # Construct model
    logits = multilayer_perceptron(X)
p_keep_input = tf.placeholder("float", name="p_keep_input")
p_keep_hidden = tf.placeholder("float", name="p_keep_hidden")
py_x = model(X, w_h, w_h2, w_o, p_keep_input, p_keep_hidden)

with tf.name_scope("cost"):
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
    train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
第二个错误是:

ValueError: Dimension 1 in both shapes must be equal, but are 1500 and 1 for 'cost/SoftmaxCrossEntropyWithLogits' (op: 'SoftmaxCrossEntropyWithLogits') with input shapes: [1500,1500], [1500,1].
代码是:

# Network Parameters
n_hidden_1 = 1500 # 1st layer number of neurons
n_hidden_2 = 1500 # 2nd layer number of neurons
n_input = 1500 
n_classes = 1500

# tf Graph input
X = tf.placeholder("float", [1500,2,10000,5])
Y = tf.placeholder("float", [1500,1])

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}


# Create model
def multilayer_perceptron(x):

    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])

    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])

    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

    # Construct model
    logits = multilayer_perceptron(X)
p_keep_input = tf.placeholder("float", name="p_keep_input")
p_keep_hidden = tf.placeholder("float", name="p_keep_hidden")
py_x = model(X, w_h, w_h2, w_o, p_keep_input, p_keep_hidden)

with tf.name_scope("cost"):
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
    train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)

对于密集层,您通常首先将输入数据重塑为每个样本一行,这样就形成了一个
[nSamples,nFeatures]
矩阵(具有2维,而不是4维),因为您不会使用该结构。只有这样,MatMul才能正确发生(现在是两个2D矩阵的乘法)

我猜这里是
nSamples=n_inputs=1500
,以及
nFeatures=2*10000*5
。在这种情况下,请小心
h1
的形状
[n特征,n\u隐藏1]

n_features = 2*10000*5
weights = {
    'h1': tf.Variable(tf.random_normal([n_features, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}

顺便说一句,为了便于调试,您应该使用不同的n_输入、n_隐藏、n_类(您可能没有n_类的选择,但可以更改其他类),这样您就可以更容易地理解成形错误(而在这里,当你看到一个1500的形状时,你不知道它是从哪里来的,所以它更令人困惑,它甚至可能因为不好的原因出现在那里,并在以后引起麻烦)

第二个问题的答案(编辑):


需要具有相同[n_samples,n_classes]形状的logit和标签,标签与logit一样是一个热编码的(除非它们通常只包含1和0)。如果Y是形状[n_samples,1],那么我希望它只包含每个示例的类索引。在这种情况下,您应该改为使用,Y的形状应仅为
[n\u samples]
,而不是
[n\u samples,1]

我遇到了另一个错误。我将把它添加到我的问题中。我已经回答了。以后请在另一个帖子中发布新的arrising问题,因为它们是不同的问题,这样你就可以接受第一个答案并对新的错误给予更多的关注。而且你不应该在任何地方都使用相同的数字(您的1500用于n_样本、n_类、n_隐藏1、n_隐藏2)。即使您使用1500、1499、1498、1497,它的行为也几乎完全相同,但如果您的形状符合要求,您会更快地知道。再次感谢您的回答!作为机器学习和TensorFlow的初学者,这对我来说是一个很好的练习。这对我很有帮助。很高兴我能帮助您!请接受answ呃,如果问题解决了,对于我的代表和未来可能会看到这一点的人来说,都面临着同样的问题。当尝试使用
tf.reformate(x,[None,n_features])
时,我得到
TypeError:无法将类型的对象转换为张量
n_features = 2*10000*5
weights = {
    'h1': tf.Variable(tf.random_normal([n_features, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}