'Tensor'对象没有属性'_keras_history'Keras没有Tensorflow张量

时间:2018-10-13 20:33:06

标签: python tensorflow keras deep-learning

此代码:

a = Input(ish)
for i in range(a.shape[1]):
  x=Conv2D(filters=50, kernel_size=3, padding='same', activation=rl)(a[:,i])
  x=MaxPooling2D(pool_size=2)(x)
  x=Dropout(0.5)(x)
  x=Conv2D(filters=100, kernel_size=5, padding='same', activation=rl)(x)
  x=MaxPooling2D(pool_size=2)(x)
  x=Dropout(0.5)(x)
  x=Conv2D(filters=200, kernel_size=7, padding='same', activation=rl)(x)
  x=MaxPooling2D(pool_size=2)(x)
t=Flatten()(x)
t=Dropout(0.7)(t)
b=Dense(num_classes, activation='softmax')(t)

model = Model(inputs=a, outputs=b)

在最后一行给出此错误:

AttributeError: 'Tensor' object has no attribute '_keras_history'

有什么想法会导致问题以及如何解决?

1 个答案:

答案 0 :(得分:1)

为了保留Keras元数据,您必须在Lambda层内进行索引编制:

a = Input(ish)
for i in range(a.shape[1]):
  x=Lambda(lambda x: x[:, i])(a)
  x=Conv2D(filters=50, kernel_size=3, padding='same', activation=rl)(x)
  x=MaxPooling2D(pool_size=2)(x)
  x=Dropout(0.5)(x)
  x=Conv2D(filters=100, kernel_size=5, padding='same', activation=rl)(x)
  x=MaxPooling2D(pool_size=2)(x)
  x=Dropout(0.5)(x)
  x=Conv2D(filters=200, kernel_size=7, padding='same', activation=rl)(x)
  x=MaxPooling2D(pool_size=2)(x)
t=Flatten()(x)
t=Dropout(0.7)(t)
b=Dense(num_classes, activation='softmax')(t)

model = Model(inputs=a, outputs=b)