2017-10-05 66 views
0

我试图在TF网站上基于MNIST教程实现CNN模型。 这里是我的代码TF错误:两张张的形状匹配

import tensorflow as tf 
import numpy as np 
from tensorflow.contrib import learn 
from tensorflow.contrib.learn.python.learn.estimators import model_fn as model_fn_lib 

def cnn_model_fn(features, labels, mode): 
    """Model function for CNN.""" 
    # Input Layer 
    # Reshape X to 4-D tensor: [batch_size, width, height, channels] 
    # breaKHis images are 32x32 pixels, and have three color channel 
    input_layer = tf.reshape(features, [-1, 32, 32, 3]) 

    # Convolutional Layer #1 
    # Computes 32 features using a 5x5 filter with ReLU activation. 
    # Padding is added to preserve width and height. 
    # Input Tensor Shape: [batch_size, 32, 32, 1] 
    # Output Tensor Shape: [batch_size, 32, 32, 32] 
    conv1 = tf.layers.conv2d(
     inputs=input_layer, 
     filters=32, 
     kernel_size=[5, 5], 
     padding="same", 
     activation=tf.nn.relu) 
    #print conv1.get_shape().as_list() 
    # Pooling Layer #1 
    # First max pooling layer with a 2x2 filter and stride of 2 
    # Input Tensor Shape: [batch_size, 32, 32, 32] 
    # Output Tensor Shape: [batch_size, 16, 16, 32] 
    pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) 
    #print pool1.get_shape().as_list() 
    # Convolutional Layer #2 
    # Computes 32 features using a 5x5 filter. 
    # Padding is added to preserve width and height. 
    # Input Tensor Shape: [batch_size, 16, 16, 32] 
    # Output Tensor Shape: [batch_size, 16, 16, 32] 
    conv2 = tf.layers.conv2d(
     inputs=pool1, 
     filters=32, 
     kernel_size=[5, 5], 
     padding="same", 
     activation=tf.nn.relu) 
    #print conv2.get_shape().as_list() 
    # Pooling Layer #2 
    # Second max pooling layer with a 3x3 filter and stride of 2 
    # Input Tensor Shape: [batch_size, 16, 16, 32] 
    # Output Tensor Shape: [batch_size, 8, 8, 32] 
    pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) 
    #print pool2.get_shape().as_list() 
    # Convolutional Layer #3 
    # Computes 64 features using a 5x5 filter. 
    # Padding is added to preserve width and height. 
    # Input Tensor Shape: [batch_size, 8, 8, 32] 
    # Output Tensor Shape: [batch_size, 8, 8, 64] 
    conv3 = tf.layers.conv2d(
     inputs=pool2, 
     filters=64, 
     kernel_size=[5, 5], 
     padding="same", 
     activation=tf.nn.relu) 
    #print conv3.get_shape().as_list() 
    # Pooling Layer #3 
    # Second max pooling layer with a 3x3 filter and stride of 2 
    # Input Tensor Shape: [batch_size, 8, 8, 64] 
    # Output Tensor Shape: [batch_size, 4, 4, 64] 
    pool3 = tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2], strides=2) 

    # Flatten tensor into a batch of vectors 
    # Input Tensor Shape: [batch_size, 4, 4, 64] 
    # Output Tensor Shape: [batch_size, 4 * 4 * 64] 
    pool3Shape = pool3.get_shape().as_list() 
    #print pool3Shape 
    pool2_flat = tf.reshape(pool2, [-1, 4*4*64]) 

    # Dense Layer 
    # Densely connected layer with 64 neurons 
    # Input Tensor Shape: [batch_size, 4 * 4 * 64] 
    # Output Tensor Shape: [batch_size, 64] 
    dense = tf.layers.dense(inputs=pool2_flat, units=64, activation=tf.nn.relu) 

    # Add dropout operation; 0.6 probability that element will be kept 
    dropout = tf.layers.dropout(
     inputs=dense, rate=0.4, training=mode == learn.ModeKeys.TRAIN) 

    # Logits layer 
    # Input Tensor Shape: [batch_size, 64] 
    # Output Tensor Shape: [batch_size, 2] 
    logits = tf.layers.dense(inputs=dropout, units=2) 

    loss = None 
    train_op = None 

    # Calculate Loss (for both TRAIN and EVAL modes) 
    if mode != learn.ModeKeys.INFER: 
    onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=2) 
    loss = tf.losses.softmax_cross_entropy(
     onehot_labels=onehot_labels, logits=logits) 

    # Configure the Training Op (for TRAIN mode) 
    if mode == learn.ModeKeys.TRAIN: 
    train_op = tf.contrib.layers.optimize_loss(
     loss=loss, 
     global_step=tf.contrib.framework.get_global_step(), 
     learning_rate=0.001, 
     optimizer="SGD") 

    # Generate Predictions 
    predictions = { 
     "classes": tf.argmax(
      input=logits, axis=1), 
     "probabilities": tf.nn.softmax(
      logits, name="softmax_tensor") 
    } 

    # Return a ModelFnOps object 
    return model_fn_lib.ModelFnOps(
     mode=mode, predictions=predictions, loss=loss, train_op=train_op) 

而且它抛出我的错误:InvalidArgumentError(见上文回溯):分配既需要张量相匹配的形状。 lhs shape = [1024,64] rhs shape = [2048,64]

我认为在最后一个FC层应该有些问题,但不知道它在哪里。

+0

相关代码丢失。你的剧本什么都没做!数据来自哪里? – Patwie

回答

相关问题