为什么keras中的共享层使得构建图形的速度非常慢(tensorflow后端)

为什么keras中的共享层使得构建图形的速度非常慢(tensorflow后端),tensorflow,keras,Tensorflow,Keras,我正在构建一个图形,其中输入被拆分为长度为30的张量列表。然后在列表的每个元素上使用一个共享RNN层 编译模型大约需要1分钟。是不是一定要这样(为什么?),还是我做错了什么 代码: 在LSTM和shared\u dense上也尝试TimeDistributed如何?LSTM采用的是序列,而不是时间步长(切片),因此我不明白这有什么帮助。如果你链接到一个代码片段,也许我能理解。 shared_lstm = keras.layers.LSTM(4, return_sequences=True) sh

我正在构建一个图形,其中输入被拆分为长度为30的张量列表。然后在列表的每个元素上使用一个共享RNN层

编译模型大约需要1分钟。是不是一定要这样(为什么?),还是我做错了什么

代码:


LSTM
shared\u dense
上也尝试
TimeDistributed
如何?LSTM采用的是序列,而不是时间步长(切片),因此我不明白这有什么帮助。如果你链接到一个代码片段,也许我能理解。
shared_lstm = keras.layers.LSTM(4, return_sequences=True)
shared_dense = TimeDistributed(keras.layers.Dense(1, activation='sigmoid'))

inp_train = keras.layers.Input([None, se.action_space, 3])

#  Split each possible measured label into a list:
inputs_train = [ keras.layers.Lambda(lambda x: x[:, :, i, :])(inp_train) for i in range(se.action_space) ]

# Apply the shared weights on each tensor:
lstm_out_train = [shared_lstm(x) for x in inputs_train]
dense_out_train = [(shared_dense(x)) for x in lstm_out_train]

# Merge the tensors again:
out_train = keras.layers.Lambda(lambda x: K.stack(x, axis=2))(dense_out_train)

# "Pick" the unique element along where the inp_train tensor is == 1.0 (along axis=2, in the next time step, of the first dimension of axis=3)
# (please disregard this line if it seems too complex)
shift_and_pick_layer = keras.layers.Lambda(lambda x: K.sum(x[0][:, :-1, :, 0] * x[1][:, 1:, :, 0], axis=2))

out_train = shift_and_pick_layer([out_train, inp_train])


m_train = keras.models.Model(inp_train, out_train)