【问题标题】:Layer conv2d_3 was called with an input that isn't a symbolic tensor使用不是符号张量的输入调用层 conv2d_3
【发布时间】:2018-05-29 01:29:53
【问题描述】:

您好,我正在为一类分类构建图像分类器,在该分类器中我在运行此模型时使用了自动编码器我收到此错误(ValueError: Layer conv2d_3 was called with an input that is not a symbolic tensor. Received类型:。完整输入:[(128, 128, 3)]。层的所有输入都应该是张量。)

num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,),dtype='int64')



labels[0:376]=0 
names = ['cat']

Y = np_utils.to_categorical(labels, num_class)
input_shape=img_data[0].shape

x,y = shuffle(img_data,Y, random_state=2)

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_shape)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# at this point the representation is (4, 4, 8) i.e. 128-dimensional

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_shape, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')


autoencoder.fit(X_train, X_train,
            epochs=50,
            batch_size=32,
            shuffle=True,
            validation_data=(X_test, X_test),
            callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

【问题讨论】:

  • 请分享完整的代码和完整的错误堆栈跟踪。复制行为的示例数据也会有所帮助。
  • 这就够了吗?

标签: keras image-recognition autoencoder


【解决方案1】:

这里:

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_shape)

形状不是张量。

这样做:

from keras.layers import *
inputTensor = Input(input_shape)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(inputTensor)

关于自动编码器的提示

您应该将编码器和解码器作为单独的模型分开。稍后您可能只想使用其中之一。

编码器:

inputTensor = Input(input_shape)
x = ....
encodedData = MaxPooling2D((2, 2), padding='same')(x)

encoderModel = Model(inputTensor,encodedData)

解码器:

encodedInput = Input((4,4,8))
x = ....
decodedData = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

decoderModel = Model(encodedInput,decodedData)

自动编码器:

autoencoderInput = Input(input_shape)
encoded = encoderModel(autoencoderInput)
decoded = decoderModel(encoded)

autoencoderModel = Model(autoencoderInput,decoded)

【讨论】:

  • inputTensor = Input(input_shape) 错误:- 输入名称未定义
  • from keras.layers import *
  • 再说一遍,不是input_shape,而是autoencoderInput
  • autoencoder_model.fit error-ValueError: 检查目标时出错:预期 model_2 有 4 个维度,但得到的数组形状为 (300, 1) autoencoder_model.fit 错误-
  • 自动编码器的输出等于输入,你不应该在那里使用类或 Y。只有x_trainx_train
猜你喜欢
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 2020-11-30
相关资源
最近更新 更多