title: ValueError: Error when checking target: expected activation_17 to have 2 dimensions, but got array with shape (1, 256, 256, 3)
body: <p>I was constructing a CNN to predict human age based on VGG-16, and feeding two RGB images. (filetype: jpg). I was using tensorflow backend in anaconda environment with Python 2.7.</p>
  
  <p>However, it always raises an error:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):</p>
    
    <p>File "train2.py", line 167, in 
        shuffle=True)</p>
    
    <p>File "/Users/name/anaconda/lib/python2.7/sitepackages/keras/models.py",line 973, in fit validation_steps=validation_steps)</p>
    
    <p>File "/Users/name/anaconda/lib/python2.7/sitepackages/keras/engine/training.py", line 1581, in fit batch_size=batch_size)</p>
    
    <p>File "/Users/name/anaconda/lib/python2.7/sitepackages/keras/engine/training.py", line 1418, in _standardize_user_data exception_prefix='target')</p>
    
    <p>File "/Users/name/anaconda/lib/python2.7/sitepackages/keras/engine/training.py", line 141, in _standardize_input_data str(array.shape))</p>
    
    <p>ValueError: Error when checking target: expected activation_17 to</p>
    
    <p>have 2  dimensions, but got array with shape (1, 256, 256, 3)</p>
  </blockquote>
  
  <p>How can I solve this error? Here is the code:   </p>
  
  <pre><code>import keras
  
  from keras.preprocessing.image import ImageDataGenerator
  
  from keras.models import Sequential
  
  from keras.layers import Dense
  
  from keras.layers import Dropout
  
  from keras.layers import Activation
  
  from keras.layers import Flatten
  
  from keras.layers import Conv2D
  
  from keras.layers import MaxPooling2D
  
  from keras.layers import BatchNormalization
  
  from keras.utils import np_utils
  
  import cv2
  
  from PIL import Image
  
  import numpy as np
  
  from sklearn.cross_validation import train_test_split
  
  from sklearn.model_selection import train_test_split
  
  name_path = ["pathname"]
  
  new_age_list=[45,52]
  
  img_rows=256
  
  img_cols=256
  
  img_array = 
  np.array([np.array(Image.open(i).resize((img_rows,img_cols),Image.BILINEAR)) for i in name_path[0:2]],"f")
  
  (X, y) = (img_array[0:2],new_age_list[0:2])
  
  y=np.asarray(y)
  
  X=X.reshape(2,256,256,3)
  
  # STEP 1: split X and y into training and testing sets
  
  train_data, train_label,test_data, test_label= train_test_split(X, y, 
  test_size=0.5, random_state=4)
  
  train_data = train_data.astype('float32')
  
  test_data = test_data.astype('float32')
  
  train_data = train_data / 255
  
  test_data = test_data / 255
  
  """ Model """
  
  model = Sequential()
  
  """ Block 1 """
  
  model.add(Conv2D(64, (3,3), padding='same', 
  border_mode='valid',input_shape=(256,256,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(Conv2D(64, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(MaxPooling2D(pool_size=(2,2)))
  
  """ Block 2 """
  
  model.add(Conv2D(128, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(Conv2D(128, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(MaxPooling2D(pool_size=(2,2)))
  
  """ Block 3 """
  
  model.add(Conv2D(256, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(Conv2D(256, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(Conv2D(256, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(MaxPooling2D(pool_size=(2,2)))
  
  """ Block 4 """
  model.add(Conv2D(512, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(Conv2D(512, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(Conv2D(512, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(MaxPooling2D(pool_size=(2,2)))
  
  """ Block 5 """
  
  model.add(Conv2D(512, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(Conv2D(512, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(Conv2D(512, (3,3)))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(MaxPooling2D(pool_size=(2,2)))
  
  model.add(Dropout(0.25))
  
  """ Flatten """
  
  model.add(Flatten())
  
  model.add(Dense(512))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(Dense(128))
  
  model.add(BatchNormalization())
  model.add(Activation('relu'))
  
  model.add(Dense(32))
  
  model.add(BatchNormalization())
  
  model.add(Activation('relu'))
  
  model.add(Dense(1))
  
  model.add(BatchNormalization())
  
  model.add(Activation('softmax'))
  
  """ Optimizer """
  
  opt = keras.optimizers.rmsprop(lr=config.learning_rate, 
  
  decay=config.decay)
  
  print model.summary()
  
  model.compile(loss='mean_squared_error', optimizer=opt, metrics=
  ['accuracy'])
  
  """ Fit Data """
  
  batch_size = 512
  
  epoch = 1000
  
  learning_rate = 1e-4
  
  decay = 1e-7
  
  for i in range(epoch):
  
      model.fit(train_data, train_label,
  
                batch_size=batch_size,
  
                epochs=int(epoch/epoch),
  
                validation_data=(test_data, test_label),
  
                shuffle=True)
  </code></pre>