【问题标题】:How can I merge two different models and train in tensorflow?如何合并两个不同的模型并在 tensorflow 中训练?
【发布时间】:2018-12-20 07:22:12
【问题描述】:

我一直在寻找使用 Tensorflow 实现深度学习模型的信息,最后我在这里问,因为我无法挖掘出来。这可能是一个非常基本的问题,但我会感谢您的友好回复。

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)    
# for reproduct

# parameters
learning_rate = 1E-5 * 5

batch_size_SE = 2500
batch_size_STOI = 50

spl = 5
frames = 50
con_frame = 50

feature_dim = 256

nb_epoch = 50
layer_width = 2048

training_length = 500
validation_length = 50

keep_prob = tf.placeholder(tf.float32)

# SE_input/output 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_input/output placeholders
STOI_feature = tf.placeholder(tf.float32, [batch_size_STOI, feature_dim*frames*2])
STOI_target = tf.placeholder(tf.float32, [batch_size_STOI, 1])



#########################Speech enhancement DNN#########################

# SE_1st Hidden layer
W11 = tf.get_variable("W11", shape=[(2*spl+1)*feature_dim,layer_width], initializer=tf.contrib.layers.xavier_initializer())
b11 = tf.Variable(tf.random_normal([layer_width]))
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,layer_width], initializer=tf.contrib.layers.xavier_initializer())
b12 = tf.Variable(tf.random_normal([layer_width]))
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("W23", shape=[layer_width, layer_width], initializer=tf.contrib.layers.xavier_initializer())
b13 = tf.Variable(tf.random_normal([layer_width]))
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,layer_width], initializer=tf.contrib.layers.xavier_initializer())   
b14 = tf.Variable(tf.random_normal([layer_width]))
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,feature_dim], initializer=tf.contrib.layers.xavier_initializer())   
b15 = tf.Variable(tf.random_normal([feature_dim]))
SE_hypothesis = tf.matmul(L14, W15) + b15



#########################STOI estimation DNN#########################

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

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

# STOI_3rd Hidden layer
W23 = tf.get_variable("W23", shape=[layer_width, layer_width], initializer=tf.contrib.layers.xavier_initializer())
b23 = tf.Variable(tf.random_normal([layer_width]))
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,layer_width], initializer=tf.contrib.layers.xavier_initializer())   
b24 = tf.Variable(tf.random_normal([layer_width]))
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,1], initializer=tf.contrib.layers.xavier_initializer()) 
b25 = tf.Variable(tf.random_normal([1]))
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)

saver = tf.train.Saver()

现在,我想使用一种方法来训练两个神经工作模型。 DNN1,这是我要训练的模型,DNN2 是已经训练好的模型。我尝试用tensorflow构建这两个模型的框架。但是,它有一条错误消息“尺寸必须相等,但为 2816 和 25600”。我觉得我不应该像传统的 tensorflow DNN 那样训练,所以请问我可以用什么样的方法来修改代码。

【问题讨论】:

    标签: tensorflow deep-learning


    【解决方案1】:

    你问了一个非常广泛的问题。弹出的错误意味着您正在尝试将值提供给具有不同维度的张量。

    我将坚持使用您在图片中发布的 DNN,尽管您开始后似乎很难,但看起来更容易,我不会给您代码,但我会给您说明您应该做什么:

    import tensorflow as tf
    INT = tf.int32
    
    def graph_add():
        g = tf.Graph()
        with g.as_default() as g:
            a = tf.placeholder(INT, [], name='a')
            b = tf.placeholder(INT, [], name='b')
            c = tf.add(a, b, name='c')
        return g.as_graph_def()
    
    def graph_pow():
         g = tf.Graph()
        with g.as_default() as g:
            d = tf.placeholder(INT, [], name='d')
            e = tf.pow(d, 2, name='e')
        return g.as_graph_def()
    
    tf.reset_default_graph()
    
    # input of the main graph
    a = tf.placeholder(INT, [], name='a')
    b = tf.placeholder(INT, [], name='b')
    
    # connect a, b to graph_add, output is g1_c
    [g1_c] = tf.import_graph_def(
        graph_add(), input_map={'a': a,
                                'b': b}, return_elements=['c:0'])
    
    # connect output of graph add, g1_c, as input of graph_pow, output is g2_e
    [g2_e] = tf.import_graph_def(
        graph_pow(), input_map={'d': g1_c}, return_elements=['e:0'])
    
    # get results of g1_c and g2_e
    with tf.Session() as sess:
        c, e = sess.run([g1_c, g2_e], feed_dict={a: 10, b: 20})
        print('a + b =', c)
        print('(a + b)^2 =', e)
    

    此代码来自here,它是合并两个不同图形的关键。您需要做的是为您的目的在第二个图中加载您训练的模型,它应该可以工作。我挑战你尝试自己做,你会学到很多东西。如果你是 TF 的初学者,我建议你先尝试一些更简单的东西,也许是 MNIST?

    希望对你有所帮助!

    PD:我只是好奇,你能告诉我你在训练什么吗?

    【讨论】:

    • 感谢您的帮助。非常感谢。我希望这段代码能帮助我的硕士学位实验。如果我需要任何信息,我会再问你一次。 PD:我正在训练语音增强深度神经网络。它有高维输入,所以我有几个问题要处理。
    • 希望如此。
    • 我正在尝试修复此代码以匹配我的模型,但实际上此代码可以链接,因为第一个模型的输出和第二个模型的输入具有相同的维度。
    • 是的,但请确保您已“打开”(已定义)中间的入口点,以便您可以在需要时通过那里提供数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多