如何在一个会话中加载多个模型?附言模型具有相同的变量名称,但尺寸不同

时间:2018-07-18 04:39:17

标签: tensorflow model load

在张量流中加载多个模型时,我遇到了一个严重的问题。由于我的目标是微调联合训练模型,因此我必须在一个会话中加载预先训练的模型并开始训练。但是,存在一个严重的问题,因为两个模型都具有相同的变量名称,所以在加载第二个模型时存在问题,因为该模型已经加载了具有相同名称的变量。

有什么解决方案可以加载多个模型而不冲突相同的名称吗?

import tensorflow as tf
import random
import os
import numpy as np
import time
import random
import csv
from random import shuffle

np.random.seed(1117)

## parameters
learning_rate = 1E-5 * 5

batch_size_SE = 500
batch_size_STOI = 10

spl = 5
frames = 50
con_frame = 50

feature_dim = 256

nb_epoch = 50
layer_width_SE = 2048
layer_width_STOI = 1024

training_length = 500
validation_length = 50

keep_prob = tf.placeholder(tf.float32)

## SE_input/target placeholders
X = tf.placeholder(tf.float32, [batch_size_SE, feature_dim*(2*spl+1)])
Y = tf.placeholder(tf.float32, [batch_size_SE, feature_dim])

## STOI_target placeholders
STOI_target = tf.placeholder(tf.float32, [batch_size_STOI, 1])


######################## DNN #1 #########################

## SE_1st Hidden layer
W11 = tf.get_variable("W11", shape=[(2*spl+1)*feature_dim,layer_width_SE], initializer=tf.contrib.layers.xavier_initializer())
b11 = tf.Variable(tf.random_normal([layer_width_SE]), name = "b11")
L11 = tf.nn.relu(tf.matmul(X, W11) + b11)
L11 = tf.nn.dropout(L11, keep_prob=keep_prob)

## SE_2nd Hidden layer  
W12 = tf.get_variable("W12", shape=[layer_width_SE,layer_width_SE], initializer=tf.contrib.layers.xavier_initializer())
b12 = tf.Variable(tf.random_normal([layer_width_SE]), name = "b12")
L12 = tf.nn.relu(tf.matmul(L11, W12)+ b12)
L12 = tf.nn.dropout(L12, keep_prob=keep_prob)

## SE_3rd Hidden layer
W13 = tf.get_variable("W13", shape=[layer_width_SE, layer_width_SE], initializer=tf.contrib.layers.xavier_initializer())
b13 = tf.Variable(tf.random_normal([layer_width_SE]), name = "b13")
L13 = tf.nn.relu(tf.matmul(L12, W13) + b13)
L13 = tf.nn.dropout(L13, keep_prob=keep_prob)

## SE_4th Hidden layer
W14 = tf.get_variable("W14", shape=[layer_width_SE,layer_width_SE], initializer=tf.contrib.layers.xavier_initializer()) 
b14 = tf.Variable(tf.random_normal([layer_width_SE]), name = "b14")
L14 = tf.nn.relu(tf.matmul(L13, W14)+ b14)
L14 = tf.nn.dropout(L14, keep_prob=keep_prob)

## enhanced_speech_output layer
W15 = tf.get_variable("W15", shape=[layer_width_SE,feature_dim], initializer=tf.contrib.layers.xavier_initializer())    
b15 = tf.Variable(tf.random_normal([feature_dim]), name = "b15")
SE_hypothesis = tf.matmul(L14, W15) + b15


######################## DNN #2 #########################

SE_hypothesis_append = tf.reshape(SE_hypothesis, [(batch_size_SE/frames), (feature_dim*frames)])
Y_append = tf.reshape(Y, [(batch_size_SE/frames), (feature_dim*frames)])

feature = tf.concat([SE_hypothesis_append, Y_append],axis=1)

## STOI_1st Hidden layer
W21 = tf.get_variable("W21", shape=[feature_dim*frames*2,layer_width_STOI], initializer=tf.contrib.layers.xavier_initializer())
b21 = tf.Variable(tf.random_normal([layer_width_STOI]), name = "b21")
L21 = tf.nn.relu(tf.matmul(feature, W21) + b21)
L21 = tf.nn.dropout(L21, keep_prob=keep_prob)

## STOI_2nd Hidden layer    
W22 = tf.get_variable("W22", shape=[layer_width_STOI,layer_width_STOI/2], initializer=tf.contrib.layers.xavier_initializer())
b22 = tf.Variable(tf.random_normal([layer_width_STOI/2]), name = "b22")
L22 = tf.nn.relu(tf.matmul(L21, W22)+ b22)
L22 = tf.nn.dropout(L22, keep_prob=keep_prob)

## STOI_3rd Hidden layer
W23 = tf.get_variable("W23", shape=[layer_width_STOI/2, layer_width_STOI/4], initializer=tf.contrib.layers.xavier_initializer())
b23 = tf.Variable(tf.random_normal([layer_width_STOI/4]), name = "b23")
L23 = tf.nn.relu(tf.matmul(L22, W23) + b23)
L23 = tf.nn.dropout(L23, keep_prob=keep_prob)

## STOI_4th Hidden layer
W24 = tf.get_variable("W24", shape=[layer_width_STOI/4,layer_width_STOI/8], initializer=tf.contrib.layers.xavier_initializer()) 
b24 = tf.Variable(tf.random_normal([layer_width_STOI/8]), name = "b24")
L24 = tf.nn.relu(tf.matmul(L23, W24)+ b24)
L24 = tf.nn.dropout(L24, keep_prob=keep_prob)

## enhanced_speech_output layer
W25 = tf.get_variable("W25", shape=[layer_width_STOI/8,1], initializer=tf.contrib.layers.xavier_initializer())  
b25 = tf.Variable(tf.random_normal([1]), name = "b25")
STOI_hypothesis = tf.matmul(L24, W25) + b25

########################Cost function and optimizer#########################

SE_var_list = [W11, W12, W13, W14, W15, b11, b12, b13, b14, b15]

cost = tf.reduce_mean(tf.square(STOI_target - STOI_hypothesis))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost, var_list = SE_var_list)

config = tf.ConfigProto(gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.1))

with tf.Session(config = config) as sess:

    sess.run(tf.global_variables_initializer())

    cost_list = np.array([])
    v_cost_list = np.array([])

    STOI_maximum = np.zeros([batch_size_STOI, 1])
    STOI_maximum = STOI_maximum+ 1

    x = input("Do you have saved model : 1 - yes / 2 - no ")

    #### TRAINING&VALIDATION ####   
    if x == 2:

        saver_SE = tf.train.import_meta_graph("/home/jhkim17/test_tensorflow/model_batchsize_500/model.meta")
        saver_SE.restore(sess, "/home/jhkim17/test_tensorflow/model_batchsize_500/model")

        graph1 = tf.get_default_graph()

        W11 = graph1.get_tensor_by_name("W1:0")
        W12 = graph1.get_tensor_by_name("W2:0")
        W13 = graph1.get_tensor_by_name("W3:0")
        W14 = graph1.get_tensor_by_name("W4:0")
        W15 = graph1.get_tensor_by_name("W5:0")

        b11 = graph1.get_tensor_by_name("Variable:0")
        b12 = graph1.get_tensor_by_name("Variable_1:0")
        b13 = graph1.get_tensor_by_name("Variable_2:0")
        b14 = graph1.get_tensor_by_name("Variable_3:0")
        b15 = graph1.get_tensor_by_name("Variable_4:0")

        saver_STOI = tf.train.import_meta_graph("/home/jhkim17/test_tensorflow/STOI_model_batchsize_10_linear/model.meta")
        saver_STOI.restore(sess, "/home/jhkim17/test_tensorflow/STOI_model_batchsize_10_linear/model")

        graph2 = tf.get_default_graph()

        W21 = graph2.get_tensor_by_name("W1:0")
        W22 = graph2.get_tensor_by_name("W2:0")
        W23 = graph2.get_tensor_by_name("W3:0")
        W24 = graph2.get_tensor_by_name("W4:0")
        W25 = graph2.get_tensor_by_name("W5:0")

        b21 = graph2.get_tensor_by_name("Variable:0")
        b22 = graph2.get_tensor_by_name("Variable_1:0")
        b23 = graph2.get_tensor_by_name("Variable_2:0")
        b24 = graph2.get_tensor_by_name("Variable_3:0")
        b25 = graph2.get_tensor_by_name("Variable_4:0")

这是我调试此系统时的错误消息。恢复saver_STOI时会发生

Traceback (most recent call last):
  File "/home/jhkim17/.local/lib/python2.7/site-packages/pudb/__init__.py", line 93, in runscript
    dbg._runscript(mainpyfile)
  File "/home/jhkim17/.local/lib/python2.7/site-packages/pudb/debugger.py", line 432, in _runscript
    self.run(statement, globals=globals_, locals=locals_)
  File "/usr/lib/python2.7/bdb.py", line 400, in run
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "SE_test.py", line 154, in <module>
    saver_STOI.restore(sess, "/home/jhkim17/test_tensorflow/STOI_model_batchsize_10_linear/model")
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1560, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 895, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1124, in _run
    feed_dict_tensor, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1321, in _do_run
    options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1340, in _do_call
    raise type(e)(node_def, op, message)
InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [2048] rhs shape= [512]
     [[Node: save/Assign_3 = Assign[T=DT_FLOAT, _class=["loc:@Variable_1"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/gpu:0"](Variable_1, save/RestoreV2_3/_57)]]

Caused by op u'save/Assign_3', defined at:
  File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/jhkim17/.local/lib/python2.7/site-packages/pudb/__main__.py", line 6, in <module>
    main()
  File "/home/jhkim17/.local/lib/python2.7/site-packages/pudb/run.py", line 34, in main
    steal_output=options.steal_output)
  File "/home/jhkim17/.local/lib/python2.7/site-packages/pudb/__init__.py", line 93, in runscript
    dbg._runscript(mainpyfile)
  File "/home/jhkim17/.local/lib/python2.7/site-packages/pudb/debugger.py", line 432, in _runscript
    self.run(statement, globals=globals_, locals=locals_)
  File "/usr/lib/python2.7/bdb.py", line 400, in run
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "SE_test.py", line 136, in <module>
    saver_SE = tf.train.import_meta_graph("/home/jhkim17/test_tensorflow/model_batchsize_500/model.meta")
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1698, in import_meta_graph
    **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/meta_graph.py", line 656, in import_scoped_meta_graph
    producer_op_list=producer_op_list)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py", line 313, in import_graph_def
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [2048] rhs shape= [512]
     [[Node: save/Assign_3 = Assign[T=DT_FLOAT, _class=["loc:@Variable_1"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/gpu:0"](Variable_1, save/RestoreV2_3/_57)]]

0 个答案:

没有答案