使用梯度带求LSTM模型的雅可比矩阵-Python
我正在使用LSTM构建一个序列到一个模型预测。我的数据有4个输入变量和1个需要预测的输出变量。该数据是一个时间序列数据。数据的总长度为38265(总时间步数)。总数据位于大小为38265*5的数据帧中 我想使用4个输入变量的前20个时间步长数据来预测我的输出变量。为此,我使用以下代码使用梯度带求LSTM模型的雅可比矩阵-Python,python,tensorflow,tensorflow2.0,gradient,gradienttape,Python,Tensorflow,Tensorflow2.0,Gradient,Gradienttape,我正在使用LSTM构建一个序列到一个模型预测。我的数据有4个输入变量和1个需要预测的输出变量。该数据是一个时间序列数据。数据的总长度为38265(总时间步数)。总数据位于大小为38265*5的数据帧中 我想使用4个输入变量的前20个时间步长数据来预测我的输出变量。为此,我使用以下代码 model = Sequential() model.add(LSTM(units = 120, activation ='relu', return_sequences = False,input_shape
model = Sequential()
model.add(LSTM(units = 120, activation ='relu', return_sequences = False,input_shape =
(train_in.shape[1],5)))
model.add(Dense(100,activation='relu'))
model.add(Dense(50,activation='relu'))
model.add(Dense(1))
我想用tf.梯度带计算输出变量w.r.t的雅可比矩阵。。有人能帮我解决这个问题吗?将输出的雅可比矩阵与LSTM输入分离的解决方案如下:
#获取每个持久梯度求值的雅可比矩阵
model=tf.keras.Sequential()
model.add(tf.keras.layers.Dense(2,activation='relu'))
model.add(tf.keras.layers.Dense(2,activation='relu'))
x=tf.常数([[5,6,3.]]
使用tf.GradientTape(persistent=True,watch\u accessed\u variables=True)作为磁带:
#前传
磁带。手表(x)
y=型号(x)
损失=tf.减少平均值(y**2)
打印('渐变\n')
jacobian_wrt_损耗=磁带。jacobian(损耗,x)
打印(f'{jacobian_wrt_loss}\n')
雅可比(y,x)
打印(f'{jacobian_wrt_y}\n')
#获取各层输出雅可比矩阵的通用语法
将numpy作为np导入
导入tensorflow作为tf
tf.急切地执行_()
x=tf.常数([[15,60,32.]]
x_inp=tf.keras.layers.Input(张量=tf.constant([15,60,32.]))
model=tf.keras.Sequential()
model.add(tf.keras.layers.Dense(2,activation='relu',name='Dense_1'))
model.add(tf.keras.layers.Dense(2,activation='relu',name='Dense_2'))
aux_model=tf.keras.Sequential()
aux_model.add(tf.keras.layers.densed(2,activation='relu',name='densed_1'))
#compile(loss='sparse'\u categorical'\u crossentropy',optimizer='adam',metrics=['accurity'])
使用tf.GradientTape(persistent=True,watch\u accessed\u variables=True)作为磁带:
#前传
磁带。手表(x)
x_y=模型(x)
act_y=辅助_模型(x)
打印(x_-y,键入(x_-y))
ops=[model.layers中层的layer.output]
#ops=[model.layers中层的layer.output]
#inps=[model.layers中图层的layer.input]
打印('完整FFNN的雅可比矩阵\n')
雅可比矩阵=磁带雅可比矩阵(x_y,x)
打印(f'{jacobian[0]}\n')
print('FFNN的雅可比矩阵,仅第一个稠密\n')
雅可比矩阵=磁带雅可比矩阵(act_y,x)
打印(f'{jacobian[0]}\n')
在这里,我使用了一个由两个密集层组成的简单FFNN,但我想计算第一个密集层的输出w.r.t。因此,我创建了一个只有一个密集层的辅助模型,并从中确定了雅可比矩阵的输出
可以找到详细信息。将输出的雅可比矩阵与LSTM输入分离的解决方案如下:
#获取每个持久梯度求值的雅可比矩阵
model=tf.keras.Sequential()
model.add(tf.keras.layers.Dense(2,activation='relu'))
model.add(tf.keras.layers.Dense(2,activation='relu'))
x=tf.常数([[5,6,3.]]
使用tf.GradientTape(persistent=True,watch\u accessed\u variables=True)作为磁带:
#前传
磁带。手表(x)
y=型号(x)
损失=tf.减少平均值(y**2)
打印('渐变\n')
jacobian_wrt_损耗=磁带。jacobian(损耗,x)
打印(f'{jacobian_wrt_loss}\n')
雅可比(y,x)
打印(f'{jacobian_wrt_y}\n')
#获取各层输出雅可比矩阵的通用语法
将numpy作为np导入
导入tensorflow作为tf
tf.急切地执行_()
x=tf.常数([[15,60,32.]]
x_inp=tf.keras.layers.Input(张量=tf.constant([15,60,32.]))
model=tf.keras.Sequential()
model.add(tf.keras.layers.Dense(2,activation='relu',name='Dense_1'))
model.add(tf.keras.layers.Dense(2,activation='relu',name='Dense_2'))
aux_model=tf.keras.Sequential()
aux_model.add(tf.keras.layers.densed(2,activation='relu',name='densed_1'))
#m
tf.executing_eagerly() #check if eager execution is enabled or not. Should give "True"
data = pd.read_excel("FileName or Location ")
#My data is in the from of dataframe with 127549 rows and 5 columns(127549*5)
a = data[:20] #shape is (20,5)
b = data[50:70] # shape is (20,5)
A = [a,b] # making a list
A = np.array(A) # convert into array size (2,20,5)
At = tf.convert_to_tensor(A, np.float32) #convert into tensor
At.shape # TensorShape([Dimension(2), Dimension(20), Dimension(5)])
model = load_model('EKF-LSTM-1.h5') # Load the trained model
# I have a trained model which is shown in the question above.
# Output of this model is a single value
with tf.GradientTape(persistent=True,watch_accessed_variables=True) as tape:
tape.watch(At)
y1 = model(At) #defining your output as a function of input variables
print(y1,type(y1)
#output
tf.Tensor([[0.04251503],[0.04634088]], shape=(2, 1), dtype=float32) <class
'tensorflow.python.framework.ops.EagerTensor'>
jacobian=tape.jacobian(y1,At) #jacobian of output w.r.t both inputs
jacobian.shape
TensorShape([Dimension(2), Dimension(1), Dimension(2), Dimension(20), Dimension(5)])
jacobian=tape.jacobian(y1,At[0]) #jacobian of output w.r.t only 1st input in 'At'
jacobian.shape
TensorShape([Dimension(1), Dimension(1), Dimension(1), Dimension(20), Dimension(5)])