尺寸必须相等,但对于输入形状为'MatMul_15'(op:'MatMul')的尺寸为1024和3136:[100,1024],[3136,100]

时间:2017-07-03 03:55:24

标签: tensorflow python-3.5

我是TensorFlow的新手。我的数据集中的每个图像都有16x16的大小。 这是我的代码,我收到了这个错误。我提供了每个卷积层形状的代码和更多信息。

我很感激你的帮助。

def my_conv_net(input_data):
    
    batch_size = 100
    learning_rate = 0.005
    evaluation_size = 500
#    image_width = ttrainData[0].shape[0]
#    image_height = ttrainData[0].shape[1]
    image_width = 16
    image_height = 16
#    target_size = max(ttrainLabels) + 1
    num_channels = 1
    generations = 500
    eval_every = 5
    conv1_features = 32
    conv2_features = 64
    max_pool_size1 = 2
    max_pool_size2 = 2
    fully_connected_size1 = 100
    dropout_prob = 0.75
    
    # First Conv-ReLU-MaxPool Layer
    conv1 = tf.nn.conv2d(input_data, conv1_weight, strides=[1, 1, 1, 1], padding='SAME')
    relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_bias))
    max_pool1 = tf.nn.max_pool(relu1, ksize=[1, max_pool_size1, max_pool_size1, 1], strides=[1, max_pool_size1, max_pool_size1, 1], padding='SAME')
    
    print("input_data>>>: ", input_data)
    print("conv1_weight>>>: ", conv1_weight)
    print("conv1_weight get shape: ", conv1_weight.get_shape())
    print("conv1_bias>>>: ", conv1_bias)
    print("conv1>>>: ", conv1)
    print("relu1>>>: ", relu1)
    print("max_pool1>>>: ", max_pool1)
    print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n")
    
    # Second Conv-ReLU-MaxPool Layer
    conv2 = tf.nn.conv2d(max_pool1, conv2_weight, strides=[1, 1, 1, 1], padding='SAME')
    relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_bias))
    max_pool2 = tf.nn.max_pool(relu2, ksize=[1, max_pool_size2, max_pool_size2, 1], strides=[1, max_pool_size2, max_pool_size2, 1], padding='SAME')
    
    print("conv2_weight>>>: ", conv2_weight)
    print("conv2_bias>>>: ", conv2_bias)
    print("conv2>>>: ", conv2)
    print("relu2>>>: ", relu2)
    print("max_pool2>>>: ", max_pool2)
    print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n")
    
    # Transform Output into a 1xN layer for next fully connected layer
    final_conv_shape = max_pool2.get_shape().as_list()
    final_shape = final_conv_shape[1] * final_conv_shape[2] * final_conv_shape[3]
    flat_output = tf.reshape(max_pool2, [final_conv_shape[0], final_shape])
    
    # First Fully Connected Layer
    print("flat_output ***: ", type(flat_output))
    print("flat_output ***: ", flat_output)
    print("full1_weight ***: ", type(full1_weight))
    print("full1_weight ***: ", full1_weight)
    print("full1_bias ***: ", type(full1_bias))
    print("full1_bias ***: ", full1_bias)
    
    fully_connected1 = tf.nn.relu(tf.add(tf.matmul(flat_output, full1_weight), full1_bias))
    
    # Second Fully Connected Layer
    final_model = tf.add(tf.matmul(fully_connected1, full2_weight), full2_bias)
    
    # Add dropout
    final_model_output = tf.nn.dropout(final_model, dropout)
    
    return(final_model_output)

这就是我得到的。

ttrainData type: <class 'numpy.ndarray'>
ttrainData shape: (279, 16, 16)
train_data type: <class 'numpy.ndarray'>
teval_dataArray shape: (120, 16, 16)
ttrainData[0]:  [[ 31.  30.  29. ...,  34.  41.  43.]
 [ 31.  31.  30. ...,  33.  38.  40.]
 [ 31.  32.  32. ...,  32.  37.  39.]
 ..., 
 [ 26.  30.  37. ...,  48.  52.  54.]
 [ 32.  34.  39. ...,  45.  46.  45.]
 [ 42.  41.  42. ...,  45.  41.  37.]]
ttrainData[0]:  (16, 16)
ttestData[0]:  [[ 116.  101.   86. ...,   96.   89.   82.]
 [ 125.  111.   96. ...,   91.   86.   81.]
 [ 130.  119.  106. ...,   88.   87.   85.]
 ..., 
 [ 120.  117.  116. ...,  107.  107.  111.]
 [ 123.  120.  120. ...,  115.  114.  118.]
 [ 126.  122.  122. ...,  121.  119.  123.]]
ttestData[0]:  (16, 16)
$$$$$$$$$$$$$$$$$ checking1 $$$$$$$$$$$$$$$$$

image_width:  16
image_height:  16
Type of fully_connected_size1:  <class 'int'>
fully_connected_size1 is:  100
Type of target_size:  <class 'numpy.float64'>
target_size is:  2.0
*********convert float to int 1st way:  2
*********convert float to int:  <class 'int'>
$$$$$$$$$$$$$$$$ checking2 $$$$$$$$$$$$$$$$$$$

checking3
checking4
checking5
input_data>>>:  Tensor("Placeholder_31:0", shape=(100, 16, 16, 1), dtype=float32)
conv1_weight>>>:  Tensor("Variable/read:0", shape=(4, 4, 1, 32), dtype=float32)
conv1_weight get shape:  (4, 4, 1, 32)
conv1_bias>>>:  Tensor("Variable_1/read:0", shape=(32,), dtype=float32)
conv1>>>:  Tensor("Conv2D_14:0", shape=(100, 16, 16, 32), dtype=float32)
relu1>>>:  Tensor("Relu_20:0", shape=(100, 16, 16, 32), dtype=float32)
max_pool1>>>:  Tensor("MaxPool_14:0", shape=(100, 8, 8, 32), dtype=float32)
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

conv2_weight>>>:  Tensor("Variable_2/read:0", shape=(4, 4, 32, 64), dtype=float32)
conv2_bias>>>:  Tensor("Variable_3/read:0", shape=(64,), dtype=float32)
conv2>>>:  Tensor("Conv2D_15:0", shape=(100, 8, 8, 64), dtype=float32)
relu2>>>:  Tensor("Relu_21:0", shape=(100, 8, 8, 64), dtype=float32)
max_pool2>>>:  Tensor("MaxPool_15:0", shape=(100, 4, 4, 64), dtype=float32)
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

flat_output ***:  <class 'tensorflow.python.framework.ops.Tensor'>
flat_output ***:  Tensor("Reshape_13:0", shape=(100, 1024), dtype=float32)
full1_weight ***:  <class 'tensorflow.python.ops.variables.Variable'>
full1_weight ***:  Tensor("Variable_4/read:0", shape=(3136, 100), dtype=float32)
full1_bias ***:  <class 'tensorflow.python.ops.variables.Variable'>
full1_bias ***:  Tensor("Variable_5/read:0", shape=(100,), dtype=float32)
Traceback (most recent call last):

  File "<ipython-input-22-1db506e814de>", line 1, in <module>

  
ValueError: Dimensions must be equal, but are 1024 and 3136 for 'MatMul_15' (op: 'MatMul') with input shapes: [100,1024], [3136,100].

1 个答案:

答案 0 :(得分:2)

你必须将shape =(100,16,16,1)更改为shape =( - 1,16,16,1) &#34;对于批量大小指示-1,它指定应根据输入中的输入值数量动态计算此维度&#34;