时间分布(密集)与密集和时间分布(Conv2D)

时间:2021-06-22 18:55:17

标签: python tensorflow keras deep-learning lstm

我正在尝试了解 TimeDitributed() 层在 keras 中的工作原理!!我知道当我们将 Conv2D 层包裹在 TimeDitributed() 中时,它将相同的 Conv2D 层应用于视频的所有时间事件(或视频序列中存在的不同帧)。如此处所述https://www.tensorflow.org/api_docs/python/tf/keras/layers/TimeDistributed

为了我的项目的目的,我正在尝试构建一个 LSTM 模型,如下所示:

class Lstm_model_1(tf.keras.Model):

    def __init__(self, num_classes):
        super(Lstm_model_1, self).__init__()   
        self.Lstm1 = tf.keras.layers.LSTM(32,return_sequences=True)
        self.Lstm2 = tf.keras.layers.LSTM(32,return_sequences=True) 
        self.classifier = tf.keras.layers.Dense(num_classes, activation='softmax')
        self.TimeDistributed=tf.keras.layers.TimeDistributed(self.classifier)

    def call(self, inputs):
        input_A=inputs
        x = self.Lstm1(input_A)
        x = self.Lstm2(x)
        output = self.TimeDistributed(x)
        
        return  output
lstm_1 = Lstm_model_1(3)
lstm_1.compile(optimizer='adam', loss=tf.keras.losses.CategoricalCrossentropy())
lstm_1.fit(X_train,Y_train, epochs=3,validation_data=(X_test,Y_test))
lstm_1.summary()
Model: "lstm_model_1_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_9 (LSTM)                multiple                  55552     
_________________________________________________________________
lstm_10 (LSTM)               multiple                  8320      
_________________________________________________________________
dense_6 (Dense)              multiple                  99        
_________________________________________________________________
time_distributed (TimeDistri multiple                  99        
=================================================================
Total params: 63,971
Trainable params: 63,971
Non-trainable params: 0
_________________________________________________________________

这里我在 TimeDistributed() 层获得了 99 个参数。

现在当我不使用 TimeDistributed() 层时,我得到相同数量的参数,即 99。

我已阅读以下帖子:-

<块引用>

如果 return_sequences=True,则 Dense 层用于像 TimeDistributedDense 一样在每个时间步应用。


<块引用>

附带说明:这使得 TimeDistributed(Dense(...)) 和 Dense(...) 彼此等效。


<块引用>

另一个注意事项:请注意,这会产生共享权重的影响。""


  1. TimeDistributed(Dense) vs Dense in seq2seq
  2. Keras Dense layer's input is not flattened

现在据我所知,在 LSTM return_sequences=True 上应用的密集层应该对所有时间戳具有相同的权重。但我有几个问题会在下面提到。

  1. TimeDitributed() 包裹的 Dense() 是多余的吗?我们可以直接使用 Dense() 吗?
  2. 如果假设我不想使用与序列输出相对应的共享权重,那我该怎么办?我希望我的网络在 return_sequences=True
  3. 的情况下学习与每个输出相对应的不同权重集
  4. 如果我们的 Dense() 层在时间序列中共享权重,为什么我们仍然将 TimeDitributed() 层包装在 TimeDitributed() 层中?我在这里看到了 RepeatedVector() 层与 Dense() 层的用法https://datascience.stackexchange.com/questions/46491/what-is-the-job-of-repeatvector-and-timedistributed
  5. 是只有在 TimeDitribued() 的情况下 Conv2D 是多余的还是 import pandas as pd import csv # 1.) Create the .csv file super_secret_dict = {'super_secret_information':'foobar'} with open('super_secret_csv.csv','w') as f: w = csv.DictWriter(f,super_secret_dict.keys()) w.writeheader() w.writerow(super_secret_dict) # 2.) Now encrypt the .csv file with a very safe encryption method and generate # a password/token that can be shared with people that should have access to the # encrypted .csv file # ... # ... # 3.) Everytime a user wants to read in the .csv file (e.g. using pd.read_csv()) # the script should ask the user to type in the password, then read in # the .csv file and then continue running the rest of the script super_secret_df = pd.read_csv('./super_secret_csv.csv') 层也是如此?

0 个答案:

没有答案