Skip to main content
 首页 » 编程设计

python之TensorFlow 中的 MatMul 问题

2025年02月15日34hnrainll

我正在尝试使用 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) 

第二个错误是:

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]. 

代码是:

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) 

请您参考如下方法:

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

我猜这里 nSamples = n_inputs = 1500,和 nFeatures = 2*10000*5。在这种情况下,请注意 h1 的形状必须是 [nFeatures, n_hidden_​​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])) 
} 

...

def multilayer_perceptron(x): 
    x_reshaped = tf.reshape(x, [None, n_features]) 
    layer_1 = tf.add(tf.matmul(x_reshaped, weights['h1']), biases['b1']) 

顺便说一句,为了更容易调试,你应该使用不同的 n_inputs、n_hidden、n_classes(你可能别无选择 n_classes 但你可以改变其他的),这样你会更容易理解整形错误(而在这里当你看到 1500 的形状时,你不会立即知道它来自哪里,所以它更容易混淆,甚至可能出于不好的原因出现在那里,并在以后造成麻烦。

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

tf.nn.softmax_cross_entropy_with_logits期望 logits 和标签具有相同的 [n_samples, n_classes] 形状,标签像 logits 一样被单热编码(除了它们通常只包含一个 1 和零)。如果 Y 的形状为 [n_samples, 1],那么我希望它只包含每个样本的类索引。在这种情况下,您应该使用 sparse_softmax_cross_entropy_with_logits相反,Y 的形状应该仅为 [n_samples],而不是 [n_samples, 1]。