Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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 如何在Tensorflow中加载预训练的LSTM模型权重_Python_Tensorflow_Lstm_Pre Trained Model - Fatal编程技术网

Python 如何在Tensorflow中加载预训练的LSTM模型权重

Python 如何在Tensorflow中加载预训练的LSTM模型权重,python,tensorflow,lstm,pre-trained-model,Python,Tensorflow,Lstm,Pre Trained Model,我想在Tensorflow中实现一个具有预训练权重的LSTM模型。这些重量可能来自Caffee或Torch。 我发现文件rnn\u cell.py中有LSTM单元格,例如rnn\u cell.BasicLSTMCell和rnn\u cell.MultiRNNCell。但是如何加载这些LSTM单元的预训练权重。这里有一个加载预训练Caffe模型的解决方案。请参阅中的讨论中引用的 非常感谢你的回答。这对我帮助很大。但是对于RNN,我没有找到如何初始化预训练权重。您可以使用ModelFromCaffe

我想在Tensorflow中实现一个具有预训练权重的LSTM模型。这些重量可能来自Caffee或Torch。

我发现文件
rnn\u cell.py
中有LSTM单元格,例如
rnn\u cell.BasicLSTMCell
rnn\u cell.MultiRNNCell
。但是如何加载这些LSTM单元的预训练权重。

这里有一个加载预训练Caffe模型的解决方案。请参阅中的讨论中引用的


非常感谢你的回答。这对我帮助很大。但是对于RNN,我没有找到如何初始化预训练权重。您可以使用ModelFromCaffe类创建一个变量,例如
fc6\u W=tf.variable(m.get\u fc\u weight(“fc6”),name=“fc6\u W”)
请参阅。
net_caffe = caffe.Net(prototxt, caffemodel, caffe.TEST)
caffe_layers = {}

for i, layer in enumerate(net_caffe.layers):
    layer_name = net_caffe._layer_names[i]
    caffe_layers[layer_name] = layer

def caffe_weights(layer_name):
    layer = caffe_layers[layer_name]
    return layer.blobs[0].data

def caffe_bias(layer_name):
    layer = caffe_layers[layer_name]
    return layer.blobs[1].data

#tensorflow uses [filter_height, filter_width, in_channels, out_channels] 2-3-1-0 
#caffe uses [out_channels, in_channels, filter_height, filter_width] 0-1-2-3
def caffe2tf_filter(name):
    f = caffe_weights(name)
    return f.transpose((2, 3, 1, 0))

class ModelFromCaffe():
    def get_conv_filter(self, name):
        w = caffe2tf_filter(name)
        return tf.constant(w, dtype=tf.float32, name="filter")

    def get_bias(self, name):
        b = caffe_bias(name)
        return tf.constant(b, dtype=tf.float32, name="bias")

    def get_fc_weight(self, name):
        cw = caffe_weights(name)
        if name == "fc6":
            assert cw.shape == (4096, 25088)
            cw = cw.reshape((4096, 512, 7, 7)) 
            cw = cw.transpose((2, 3, 1, 0))
            cw = cw.reshape(25088, 4096)
        else:
            cw = cw.transpose((1, 0))

        return tf.constant(cw, dtype=tf.float32, name="weight")

images = tf.placeholder("float", [None, 224, 224, 3], name="images")
m = ModelFromCaffe()

with tf.Session() as sess:
  sess.run(tf.initialize_all_variables())
  batch = cat.reshape((1, 224, 224, 3))
  out = sess.run([m.prob, m.relu1_1, m.pool5, m.fc6], feed_dict={ images: batch })
...