Reputation: 33
I've been searching for information to implement a deep learning model with Tensorflow, and finally I ask here because I just couldn't dig it out. It may be a very basic question, but I would appreciate your kind reply.
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()
Now, I want to use a method of training two neural work models. DNN1, which is a model that I want to train for, and DNN2 is a model that has been trained. I tried to build the framework of the two models with tensorflow. However, it has an error message "Dimensions must be equal, but are 2816 and 25600". I don't think I should train like a traditional tensorflow DNN, so I ask what kind of approach I can use to modify the code.
Upvotes: 2
Views: 2559
Reputation: 2206
You are asking a very wide question. The error that pops out means that you are trying to feed a value to a tensor that has different dimension.
I am going to stick with the DNN you posted in the pic, although it seems hard since you are starting, it is easier that in looks, I wont give you the code but I will give you instructions of what you should do:
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)
This code comes from here and it is the key to merge two different graphs. What you will need to do is load your trained model in the second graph for your purposes and it should work. I challenge you to try to do it by yourself, you will learn a lot. And if you are a beginner with TF I recoomend you to try something easier first, maybe MNIST?
I hope I helped you!
PD: I am just curious, could you tell me what you are training for?
Upvotes: 1