Tensorflow和Keras之间的卷积LSTM输出不同

时间:2019-07-13 03:23:38

标签: python-3.x tensorflow keras

我正在尝试将Tensorflow中卷积LSTM 2D的输出复制到Keras。下面是带有玩具示例的代码。 out_1out_1lstm_keras的结果应与我的理解相同,但我有所不同。我怀疑要么我错过了Keras函数的某些参数,要么我在Keras中的初始权重设置在Tensorflow中是不同的。有人对此有想法吗?谢谢。

Tensorflow版本:1.12.0,Keras版本:2.2.4,numpy版本:1.16.0,python版本:3.6.8

import tensorflow as tf
import numpy as np
from keras.layers import ConvLSTM2D, Input
from keras.models import Model

data_input_test = np.ones((1, 5, 30, 30, 32))

# Tensorflow implementation

input_data = tf.placeholder(tf.float32, [1, 5, 30, 30, 32])
layer_conv_lstm = tf.contrib.rnn.ConvLSTMCell(
            conv_ndims = 2,
            input_shape = [30, 30, 32],
            output_channels = 32,
            kernel_shape = [2, 2])
output_1, _ = tf.nn.dynamic_rnn(layer_conv_lstm, input_data, dtype = input_data.dtype)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
feed_dict = {input_data: data_input_test}
weight_conv_lstm = sess.run(tf.trainable_variables()[0])
bias_conv_lstm = sess.run(tf.trainable_variables()[1])
out_1 = sess.run(output_1, feed_dict = feed_dict) 

# Keras implementation

input_init = Input(shape = (5, 30, 30, 32))
outputs_1, _, _ = ConvLSTM2D(filters = 32 , 
                             kernel_size = (2, 2), 
                             strides = (1, 1), 
                             padding = 'same', 
                             activation = 'tanh', 
                             return_sequences = True, 
                             return_state = True,
                             recurrent_activation='sigmoid')(input_init)
model = Model(inputs = input_init, outputs = outputs_1)
model.layers[1].set_weights([weight_conv_lstm[:,:,0:32,:,], weight_conv_lstm[:,:,32:64,:], bias_conv_lstm])
out_1lstm_keras = model.predict(data_input_test)

0 个答案:

没有答案