Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何正确地将pytorch LSTM转换为keras CuDNNLSTM?_Python_Tensorflow_Keras_Pytorch - Fatal编程技术网

Python 如何正确地将pytorch LSTM转换为keras CuDNNLSTM?

Python 如何正确地将pytorch LSTM转换为keras CuDNNLSTM?,python,tensorflow,keras,pytorch,Python,Tensorflow,Keras,Pytorch,我正在尝试手动将Pytorch模型转换为Tensorflow进行部署。ONNX似乎并不是从Pytorch LSTMs到Tensorflow CuDNNLSTMs的,所以这就是我手工编写它的原因 我尝试了以下代码: 这是在运行Python 2.7、Pytorch 1.0、tensorflow 1.12和cuda9的Anaconda环境中运行的。我在Pytorch层中运行它时没有任何偏差,因为它遵循batchnorm,但由于Keras不提供该选项,我只是将偏差指定为0 导入火炬 导入tensorfl

我正在尝试手动将Pytorch模型转换为Tensorflow进行部署。ONNX似乎并不是从Pytorch LSTMs到Tensorflow CuDNNLSTMs的,所以这就是我手工编写它的原因

我尝试了以下代码: 这是在运行Python 2.7、Pytorch 1.0、tensorflow 1.12和cuda9的Anaconda环境中运行的。我在Pytorch层中运行它时没有任何偏差,因为它遵循batchnorm,但由于Keras不提供该选项,我只是将偏差指定为0

导入火炬
导入tensorflow作为tf
将numpy作为np导入
从tensorflow.keras.layers导入CuDNNLSTM,双向
从tensorflow.keras.models导入顺序模型
输入大小=80
隐藏大小=512
使用手电筒。无梯度()
rnn1=torch.nn.LSTM(输入大小=输入大小,隐藏大小=隐藏大小,双向=真,偏差=假,批处理大小=真)。cuda()
模型=顺序()
add(双向(CuDNNLSTM(hidden\u size,return\u sequences=True),input\u shape=(None,input\u size),name='rnn'))
偏差大小=rnn1.重量\uhh\ul0.分离().cpu().numpy().T.shape[1]*2
keras_格式_权重=[
rnn1.weight\u ih\u l0.detach().cpu().numpy().T,
rnn1.weight\u hh\u l0.detach().cpu().numpy().T,
np.零(偏差大小,),
rnn1.weight\u ih\u l0\u reverse.detach().cpu().numpy().T,
rnn1.weight\u hh\u l0\u reverse.detach().cpu().numpy().T,
np.零(偏差大小,),
]
model.layers[0]。设置权重(keras\u格式\u权重)
随机检验=np.random.rand(1,1,80)
res1,u=rnn1.forward(torch.FloatTensor(随机测试).cuda())
res1=res1.detach().cpu().numpy()
res2=模型预测(随机_检验)
打印(np.allclose(res1、res2、atol=1e-2))
打印(res1)
打印(res2)
现在,这确实适用于通用Keras LSTM:

model = Sequential()
model.add(Bidirectional(LSTM(hidden_size, recurrent_activation='sigmoid', return_sequences=True),  input_shape=(None, input_size), name='rnn'))

bias_size = rnn1.weight_hh_l0.detach().cpu().numpy().T.shape[1]
keras_format_weights = [
                    rnn1.weight_ih_l0.detach().cpu().numpy().T,
                    rnn1.weight_hh_l0.detach().cpu().numpy().T,
                    np.zeros((bias_size,)),
                    rnn1.weight_ih_l0_reverse.detach().cpu().numpy().T,
                    rnn1.weight_hh_l0_reverse.detach().cpu().numpy().T,
                    np.zeros((bias_size,))
                  ]

但我需要CuDNNLSTM的速度优势,Pytorch无论如何都使用相同的后端。

更新:解决方案是将torch模型转换为keras基础LSTM模型,然后调用

base_lstm_model.save_weights('1.h5')
cudnn_lstm_model.load_weights('1.h5')
base_lstm_model.save_weights('1.h5')
cudnn_lstm_model.load_weights('1.h5')