简体   繁体   中英

Keras Error when checking input

I want to explore and intermediate layer on a tensorflow model defined with Keras:

  input_dim = 30
  input_layer = Input(shape=(input_dim, ))

  encoder = Dense(encoding_dim, activation="tanh", 
            activity_regularizer=regularizers.l1(10e-5))(input_layer)
  encoder = Dense(int(encoding_dim / 2), activation="relu")(encoder)

  decoder = Dense(int(encoding_dim / 2), activation='tanh')(encoder)
  decoder = Dense(input_dim, activation='relu')(decoder)

  autoencoder = Model(inputs=input_layer, outputs=decoder)

  ####TRAINING....

  #inspect layer 1
  intermediate_layer_model = Model(inputs=autoencoder.layers[0].input,
                             outputs=autoencoder.layers[1].output)
  xtest = #array of dim (30,)
  intermediate_output = intermediate_layer_model.predict(xtest)
  print(intermediate_output)

However I got the error on dimension when I inspect:

/usr/local/lib/python2.7/site-packages/keras/engine/training_utils.pyc in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    134                             ': expected ' + names[i] + ' to have shape ' +
    135                             str(shape) + ' but got array with shape ' +
--> 136                             str(data_shape))
    137     return data
    138 

ValueError: Error when checking input: expected input_4 to have shape (30,) but got array with shape (1,)

Any help appreciated

From the Keras docs :

shape: A shape tuple (integer), not including the batch size. For instance, shape=(32,) indicates that the expected input will be batches of 32-dimensional vectors.

When specifying the model, you do not need to provide a batch dimension. model.predict() expects your array to be shaped as such however.

Reshape your xtest to contain a batch dimension: xtest = np.reshape(xtest, (1, -1)) and set the batch_size argument of model.predict() to 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM