简体   繁体   中英

'Tensor' object has no attribute '_keras_history'

I wanted to extract and apply independently a Conv2D layer on the columns of my input tensors, but after adding the code:

accelerometer_input = Input(shape=(1400, 3))

for i in range(3):
    out = Lambda(lambda x: x[:,:, i:i+1])(accelerometer_input) # Extracting the ith channel
    out = K.expand_dims(out, axis=1)
    out = Conv2D(64, (30, 1), data_format="channels_first")(out)  
    branch_outputs.append(out)
out_put = K.concatenate(branch_outputs)

it gives me the error in the title. I think it is due to the Lambda layer or the extraction which is not differentiable.

But how can I do without it?

That's because you are directly applying a backend function (ie K.expand_dims() ) on a Keras Tensor (ie out ) and therefore the result would be a Tensor (and not a Keras Tensor). Actually, a Keras Tensor is an augmented version of Tensor and have additional attributes (eg _keras_history ) which helps Keras to build the model. Now, to resolve the issue, you just need to put the backend function in a Lambda layer to have a Keras Tensor as output:

out = Lambda(lambda x: K.expand_dims(x, axis=1))(out)

The same thing applies to using K.concatenate() . However in this case, there is a specific layer for it in Keras:

from keras.layers import concatenate, Concatenate

# use functional interface
out_put = concatenate(branch_outputs)

# or use layer class
out_put = Concatenate()(branch_outputs)

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