gpt4 book ai didi

python - 无法构建模型,因为 backend.squeeze 没有层

转载 作者:行者123 更新时间:2023-11-28 22:16:10 25 4
gpt4 key购买 nike

我正在尝试构建一个模型,其中我有一个张量,必须对其进行压缩,然后将其输入 LSTM。

模型无法编译,因为压缩张量没有层属性。

Using TensorFlow backend.
Traceback (most recent call last):
File "C:/workspace/keras_test/src/testing.py", line 10, in <module>
model = Model(inputs=model_in, outputs=output)
File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 93, in __init__
self._init_graph_network(*args, **kwargs)
File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 237, in _init_graph_network
self.inputs, self.outputs)
File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 1353, in _map_graph_network
tensor_index=tensor_index)
File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 1340, in build_map
node_index, tensor_index)
File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 1340, in build_map
node_index, tensor_index)
File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 1312, in build_map
node = layer._inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

有关最小示例,请参见:

from keras import Input, backend, Model
from keras.layers import LSTM, Dense

input_shape = (128, 1, 1)
model_in = Input(tensor=Input(input_shape), shape=input_shape)
squeezed = backend.squeeze(model_in, 2)
hidden1 = LSTM(10)(squeezed)
output = Dense(1, activation='sigmoid')(hidden1)

model = Model(inputs=model_in, outputs=output)
model.summary()

如何在不丢失层信息的情况下删除 model_in 的一维?

最佳答案

后端操作 squeeze 没有包含在 Lambda 中层,因此生成的张量不是 Keras 张量。因此,它缺少一些属性,例如 _inbound_nodes。您可以按如下方式包装 squeeze 操作:

from keras import Input, backend, Model
from keras.layers import LSTM, Dense, Lambda

input_shape = (128, 1, 1)
model_in = Input(tensor=Input(input_shape), shape=input_shape)
squeezed = Lambda(lambda x: backend.squeeze(x, 2))(model_in)
hidden1 = LSTM(10)(squeezed)
output = Dense(1, activation='sigmoid')(hidden1)

model = Model(inputs=model_in, outputs=output)
model.summary()

关于python - 无法构建模型,因为 backend.squeeze 没有层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52365491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com