简体   繁体   English

检查输入时出现Keras错误

[英]Keras Error when checking input

I want to explore and intermediate layer on a tensorflow model defined with Keras: 我想探索用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 : 从Keras 文档

shape: A shape tuple (integer), not including the batch size. shape:形状元组(整数),不包括批次大小。 For instance, shape=(32,) indicates that the expected input will be batches of 32-dimensional vectors. 例如,shape =(32,)表示预期的输入将是32维向量的批次。

When specifying the model, you do not need to provide a batch dimension. 指定模型时,不需要提供批次尺寸。 model.predict() expects your array to be shaped as such however. model.predict()希望您的数组具有这种形状。

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. 重塑你xtest含有批处理尺寸: xtest = np.reshape(xtest, (1, -1))并设置batch_size的参数model.predict()为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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