def build_model(self): print("Start building model...") print('CNN:') print(self.__dict__) # feed self.input_data = tf.placeholder( tf.float32, [None, self.img_shape[0] * self.img_shape[1] * self.channels[0] ]) # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder( tf.float32, [None, self.channels[-1]]) # N等于batch_size(训练)或_num_examples(测试) self.keep_prob = tf.placeholder(tf.float32) # reshape X X = tf.reshape( self.input_data, shape=[-1, self.img_shape[0], self.img_shape[1], self.channels[0]]) # 构建训练步 self.logits, self.pred = self.transform(X) self.build_train_step() # Tensorboard if self.tbd: for i in range(len(self.W)): Summaries.scalars_histogram('_W' + str(i + 1), self.W[i]) Summaries.scalars_histogram('_b' + str(i + 1), self.b[i]) tf.summary.scalar('loss', self.loss) tf.summary.scalar('accuracy', self.accuracy) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, 'CNN'))
def build_model(self): print("Start building model...") print('DBN:') print(self.__dict__) """ Pre-training """ # 构建dbm self.pt_model = DBM(rbm_v_type=self.rbm_v_type, dbm_struct=self.dbm_struct, rbm_epochs=self.rbm_epochs, batch_size=self.batch_size, cd_k=self.cd_k, rbm_lr=self.rbm_lr) """ Fine-tuning """ with tf.name_scope('DBN'): # feed 变量 self.input_data = tf.placeholder(tf.float32, [None, self.struct[0]]) # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder(tf.float32, [None, self.struct[-1]]) # N等于batch_size(训练)或_num_examples(测试) self.keep_prob = tf.placeholder(tf.float32) # 权值 变量(初始化) self.out_W = tf.Variable(tf.truncated_normal(shape=[self.struct[-2], self.struct[-1]], stddev=np.sqrt(2 / (self.struct[-2] + self.struct[-1]))), name='W_out') self.out_b = tf.Variable(tf.constant(0.0,shape=[self.struct[-1]]),name='b_out') # 构建dbn # 构建权值列表(dbn结构) self.parameter_list = list() for pt in self.pt_model.pt_list: self.parameter_list.append(pt.W) self.parameter_list.append(pt.bh) self.parameter_list.append(self.out_W) self.parameter_list.append(self.out_b) # 构建训练步 self.pred=self.transform(self.input_data) self.build_train_step() #****************** 记录 ****************** cnt=2 for i in range(len(self.parameter_list)): if i%cnt!=0:continue k=int(i/cnt+1) Summaries.scalars_histogram('_W'+str(k),self.parameter_list[i]) Summaries.scalars_histogram('_b'+str(k),self.parameter_list[i+1]) tf.summary.scalar('loss',self.loss) tf.summary.scalar('accuracy',self.accuracy) self.merge = tf.summary.merge(tf.get_collection(tf.GraphKeys.SUMMARIES,'DBN'))
def build_model(self): print("Start building model...") print('RNN:') print(self.__dict__) with tf.name_scope('RNN'): # feed 变量 self.input_data = tf.placeholder( tf.float32, [None, self.struct[0] * self.width ]) # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder( tf.float32, [None, self.struct[-1]]) # N等于batch_size(训练)或_num_examples(测试) self.keep_prob = tf.placeholder(tf.float32) X = tf.split(self.input_data, self.width, axis=1) self.create_variable() self.pred = self.transform(X) self.build_train_step() #****************** 记录 ****************** for i in range(len(self.depth_parameter)): Summaries.scalars_histogram('_W' + str(i + 1), self.depth_parameter[i][0]) Summaries.scalars_histogram('_b' + str(i + 1), self.depth_parameter[i][1]) if i < len(self.depth_parameter) - 1: Summaries.scalars_histogram('_V' + str(i + 1), self.width_parameter[i]) tf.summary.scalar('loss', self.loss) tf.summary.scalar('accuracy', self.accuracy) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, 'RNN'))
def build_model(self): print(self.name + ':') print(self.__dict__) # feed 变量 self.input_data = tf.placeholder( tf.float32, [None, self.n_v]) # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder(tf.float32, [None, self.n_v]) # 权值 变量(初始化) """ tf.truncated_normal(shape=[self.n_v, self.n_h], stddev = np.sqrt(2 / (self.n_v + self.n_h))) tf.random_uniform(shape=[self.n_v, self.n_h], stddev = np.sqrt(6 / (self.n_v + self.n_h))) tf.glorot_uniform_initializer() """ self.W = tf.Variable(tf.truncated_normal( shape=[self.n_v, self.n_h], stddev=np.sqrt(2 / (self.n_v + self.n_h))), name='W') self.bh = tf.Variable(tf.constant(0.0, shape=[self.n_h]), name='bh') self.bv = tf.Variable(tf.constant(0.0, shape=[self.n_v]), name='bv') with tf.name_scope('CD-k'): # v0,h0 v0 = self.input_data # v0 h0, s_h0 = self.transform(v0) # h0 # vk,hk vk = self.reconstruction(s_h0) # v1 for k in range(self.cd_k - 1): _, s_hk = self.transform(vk) # trans(sample) vk = self.reconstruction(s_hk) # recon(compute) hk, _ = self.transform(vk) # hk self.pred = vk with tf.name_scope('Gradient_Descent'): # upd8 positive = tf.matmul(tf.expand_dims(v0, -1), tf.expand_dims(h0, 1)) negative = tf.matmul(tf.expand_dims(vk, -1), tf.expand_dims(hk, 1)) grad_W = tf.reduce_mean(tf.subtract(positive, negative), 0) grad_bh = tf.reduce_mean(tf.subtract(h0, hk), 0) grad_bv = tf.reduce_mean(tf.subtract(v0, vk), 0) self.w_upd8 = self.W.assign_add(grad_W * self.lr) self.bh_upd8 = self.bh.assign_add(grad_bh * self.lr) self.bv_upd8 = self.bv.assign_add(grad_bv * self.lr) # 构建训练步 self.train_batch = [self.w_upd8, self.bh_upd8, self.bv_upd8] self.build_train_step() #****************** Tensorboard ****************** Summaries.scalars_histogram('_W', self.W) Summaries.scalars_histogram('_bh', self.bh) Summaries.scalars_histogram('_bv', self.bv) tf.summary.scalar('loss', self.loss) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, self.name))
def build_model(self): print(self.name + ':') print(self.__dict__) # feed 变量 self.input_data = tf.placeholder( tf.float32, [None, self.n_x], name='X') # N等于batch_size(训练)或_num_examples(测试) self.recon_data = tf.placeholder(tf.float32, [None, self.n_x], name='recon_X') # 权值 变量(初始化) self.W = tf.Variable(tf.truncated_normal( shape=[self.n_x, self.n_h], stddev=np.sqrt(2 / (self.n_x + self.n_h))), name='W') self.bz = tf.Variable(tf.constant(0.0, shape=[self.n_x]), name='bz') self.bh = tf.Variable(tf.constant(0.0, shape=[self.n_h]), name='bh') self.var_list = [self.W, self.bh, self.bz] # 建模 x = self.input_data if self.ae_type == 'dae': # 去噪自编码器 [dae] x_ = self.add_noise(x) h = self.transform(x_) self.logits, self.pred = self.reconstruction(h) _loss = Loss(label=self.recon_data, logits=self.logits, out_func_name=self.de_func, loss_name=self.loss_func) loss_mat, _ = _loss.get_loss_mat() self.loss = self.denoising_loss(loss_mat) else: h = self.transform(x) # 自编码器 [ae] self.logits, self.pred = self.reconstruction(h) # 这部分损失共用 _loss = Loss(label=self.recon_data, logits=self.logits, out_func_name=self.de_func, loss_name=self.loss_func) self.loss = _loss.get_loss_func() #_,self.loss=_loss.get_loss_mat() if self.ae_type == 'sae': # 稀疏自编码器 [sae] self.loss = self.alpha * self.loss + self.beta * self.sparse_loss( h) # 构建训练步 self.build_train_step() #****************** Tensorboard ****************** if self.tbd: Summaries.scalars_histogram('_W', self.W) Summaries.scalars_histogram('_bz', self.bz) Summaries.scalars_histogram('_bh', self.bh) tf.summary.scalar('loss', self.loss) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, self.name))
def build_model(self): # feed 变量 self.input_data = tf.placeholder( tf.float32, [None, self.n_v]) # N等于batch_size(训练)或_num_examples(测试) # 权值 变量(初始化) self.W = tf.Variable(tf.truncated_normal(shape=[self.n_v, self.n_h], stddev=0.1), name='W') self.bh = tf.Variable(tf.constant(0.0, shape=[self.n_h]), name='bh') self.bv = tf.Variable(tf.constant(0.0, shape=[self.n_v]), name='bv') with tf.name_scope('CD-k'): # v0,h0 v0 = self.input_data # v0 h0, s_h0 = self.transform(v0) # h0 # vk,hk vk = self.reconstruction(s_h0) # v1 for k in range(self.cd_k - 1): _, s_hk = self.transform(vk) # trans(sample) vk = self.reconstruction(s_hk) # recon(compute) hk, _ = self.transform(vk) # hk with tf.name_scope('Gradient_Descent'): # upd8 positive = tf.matmul(tf.expand_dims(v0, -1), tf.expand_dims(h0, 1)) negative = tf.matmul(tf.expand_dims(vk, -1), tf.expand_dims(hk, 1)) self.w_upd8 = self.W.assign_add( tf.multiply( self.rbm_lr, tf.reduce_mean(tf.subtract(positive, negative), 0))) self.bh_upd8 = self.bh.assign_add( tf.multiply(self.rbm_lr, tf.reduce_mean(tf.subtract(h0, hk), 0))) self.bv_upd8 = self.bv.assign_add( tf.multiply(self.rbm_lr, tf.reduce_mean(tf.subtract(v0, vk), 0))) self.train_batch_cdk = [ self.w_upd8, self.bh_upd8, self.bv_upd8 ] # loss self.loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(v0, vk)))) #****************** Tensorboard ****************** Summaries.scalars_histogram('_W', self.W) Summaries.scalars_histogram('_bh', self.bh) Summaries.scalars_histogram('_bv', self.bv) tf.summary.scalar('loss', self.loss) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, tf.get_default_graph()._name_stack))
def build_model(self): print(self.name + ':') print(self.__dict__) # feed 变量 self.input_data = tf.placeholder( tf.float32, [None, self.n_x], name='X') # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder(tf.float32, [None, self.n_x], name='Y') self.A = tf.placeholder(tf.float32, [None, self.n_x], name='A') # 权值 变量(初始化) self.W = tf.Variable(tf.truncated_normal(shape=[self.n_x, self.n_y], stddev=0.1), name='W') self.bz = tf.Variable(tf.constant(0.1, shape=[self.n_x]), name='bz') self.by = tf.Variable(tf.constant(0.1, shape=[self.n_y]), name='by') self.p_mat = tf.Variable(tf.constant(self.p, shape=[1, self.n_y]), name='p_mat') self.var_list = [self.W, self.by, self.bz] # 建模 x = self.input_data y = self.transform(x) self.pred = self.reconstruction(y) if self.ae_type == 'dae': # 去噪自编码器 [dae] self.loss = self.get_denoising_loss(x, self.pred) else: _loss = Loss( label_data=self.input_data, # 自编码器 [ae] pred=self.pred, output_act_func=self.de_func) self.loss = _loss.get_loss_func(self.loss_func) if self.ae_type == 'sae': # 稀疏自编码器 [sae] self.loss = (1 - self.beta) * self.loss + self.beta * self.KL(y) # 构建训练步 self.build_train_step() #****************** Tensorboard ****************** Summaries.scalars_histogram('_W', self.W) Summaries.scalars_histogram('_bz', self.bz) Summaries.scalars_histogram('_by', self.by) tf.summary.scalar('loss', self.loss) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, self.name))
def build_model(self): # feed 变量 self.input_data = tf.placeholder( tf.float32, [None, self.n_x], name='X') # N等于batch_size(训练)或_num_examples(测试) self.A = tf.placeholder(tf.float32, [None, self.n_x], name='A') # 权值 变量(初始化) self.W = tf.Variable(tf.truncated_normal(shape=[self.n_x, self.n_y], stddev=0.1), name='W') self.bz = tf.Variable(tf.constant(0.1, shape=[self.n_x]), name='bz') self.by = tf.Variable(tf.constant(0.1, shape=[self.n_y]), name='by') self.p_mat = tf.Variable(tf.constant(self.p, shape=[1, self.n_y]), name='p_mat') self.var_list = [self.W, self.by, self.bz] # 建模 x = self.input_data y = self.transform(x) z = self.reconstruction(y) if self.ae_type == 'dae': # 去噪自编码器 [dae] self.loss = self.get_denoising_loss(x, z) else: _loss = Loss( label_data=self.input_data, # 自编码器 [ae] pred=z, output_act_func=self.de_func) self.loss = _loss.get_loss_func(self.loss_func) if self.ae_type == 'sae': # 稀疏自编码器 [sae] self.loss = (1 - self.beta) * self.loss + self.beta * self.KL(y) _optimization = Optimization(r=self.ae_lr, momentum=self.momentum, use_nesterov=True) self.train_batch_bp = _optimization.trainer(algorithm='sgd').minimize( self.loss, var_list=self.var_list) #****************** Tensorboard ****************** Summaries.scalars_histogram('_W', self.W) Summaries.scalars_histogram('_bz', self.bz) Summaries.scalars_histogram('_by', self.by) tf.summary.scalar('loss', self.loss) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, tf.get_default_graph()._name_stack))
def build_model(self): print("Start building model...") print('DBN:') print(self.__dict__) """ Pre-training """ if self.pre_train: # cd_k=0时,不进行预训练,相当于一个DNN # 构建dbm self.pt_model = DBM(units_type=self.units_type, dbm_struct=self.dbm_struct, rbm_epochs=self.rbm_epochs, batch_size=self.batch_size, cd_k=self.cd_k, rbm_lr=self.rbm_lr) """ Fine-tuning """ with tf.name_scope('DBN'): # feed 变量 self.input_data = tf.placeholder( tf.float32, [None, self.struct[0]]) # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder( tf.float32, [None, self.struct[-1]]) # N等于batch_size(训练)或_num_examples(测试) self.keep_prob = tf.placeholder(tf.float32) # 权值 变量(初始化) self.out_W = tf.Variable(tf.truncated_normal( shape=[self.struct[-2], self.struct[-1]], stddev=np.sqrt(2 / (self.struct[-2] + self.struct[-1]))), name='W_out') self.out_b = tf.Variable(tf.constant(0.0, shape=[self.struct[-1]]), name='b_out') # 构建dbn # 构建权值列表(dbn结构) self.parameter_list = list() if self.pre_train: for pt in self.pt_model.pt_list: self.parameter_list.append([pt.W, pt.bh]) else: for i in range(len(self.struct) - 2): W = tf.Variable(tf.truncated_normal( shape=[self.struct[i], self.struct[i + 1]], stddev=np.sqrt(2 / (self.struct[i] + self.struct[i + 1]))), name='W' + str(i + 1)) b = tf.Variable(tf.constant(0.0, shape=[self.struct[i + 1]]), name='b' + str(i + 1)) self.parameter_list.append([W, b]) self.parameter_list.append([self.out_W, self.out_b]) # 构建训练步 self.logist, self.pred = self.transform(self.input_data) self.build_train_step() #****************** 记录 ****************** if self.tbd: for i in range(len(self.parameter_list)): Summaries.scalars_histogram('_W' + str(i + 1), self.parameter_list[i][0]) Summaries.scalars_histogram('_b' + str(i + 1), self.parameter_list[i][1]) tf.summary.scalar('loss', self.loss) tf.summary.scalar('accuracy', self.accuracy) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, self.name))
def build_model(self): """ Pre-training """ # 构建dbm self.dbm = DBM(rbm_v_type=self.rbm_v_type, dbm_struct=self.dbm_struct, rbm_epochs=self.rbm_epochs, batch_size=self.batch_size, cd_k=self.cd_k, rbm_lr=self.rbm_lr) """ Fine-tuning """ with tf.name_scope('DBN'): # feed 变量 self.input_data = tf.placeholder( tf.float32, [None, self.dbn_struct[0] ]) # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder( tf.float32, [None, self.dbn_struct[-1] ]) # N等于batch_size(训练)或_num_examples(测试) # 权值 变量(初始化) self.out_W = tf.Variable(tf.truncated_normal( shape=[self.dbn_struct[-2], self.dbn_struct[-1]], stddev=0.1), name='W_out') self.out_b = tf.Variable(tf.constant(0.0, shape=[self.dbn_struct[-1]]), name='b_out') # 构建dbn # 构建权值列表(dbn结构) self.parameter_list = list() for rbm in self.dbm.rbm_list: self.parameter_list.append(rbm.W) self.parameter_list.append(rbm.bh) self.parameter_list.append(self.out_W) self.parameter_list.append(self.out_b) # 损失函数 self.pred = self.transform(self.input_data) _loss = Loss(label_data=self.label_data, pred=self.pred, output_act_func=self.output_act_func) self.loss = _loss.get_loss_func(self.loss_func) _optimization = Optimization(r=self.dbn_lr, momentum=self.momentum) self.train_batch_bp = _optimization.trainer( algorithm=self.bp_algorithm).minimize( self.loss, var_list=self.parameter_list) # 正确率 _ac = Accuracy(label_data=self.label_data, pred=self.pred) self.accuracy = _ac.accuracy() #****************** 记录 ****************** for i in range(len(self.parameter_list)): if i % 2 == 1: continue k = int(i / 2 + 1) W = self.parameter_list[i] b = self.parameter_list[i + 1] Summaries.scalars_histogram('_W' + str(k), W) Summaries.scalars_histogram('_b' + str(k), b) tf.summary.scalar('loss', self.loss) tf.summary.scalar('accuracy', self.accuracy) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, tf.get_default_graph()._name_stack))
def build_model(self): print("Start building model...") print('sup_sAE:') print(self.__dict__) """ Pre-training """ # 构建un_sae if self.pre_train: self.pt_model = unsupervised_sAE( loss_func=self. loss_func, # encoder:[sigmoid] || decoder:[sigmoid] with ‘cross_entropy’ | [relu] with ‘mse’ ae_type=self.ae_type, # ae | dae | sae act_type=self.act_type, noise_type=self. noise_type, # Gaussian noise (gs) | Masking noise (mn) beta=self.beta, # 惩罚因子权重(第二项损失的系数) p=self.p, # DAE:样本该维作为噪声的概率 / SAE稀疏性参数:期望的隐层平均活跃度(在训练批次上取平均) un_ae_struct=self.un_ae_struct, ae_epochs=self.ae_epochs, batch_size=self.batch_size, ae_lr=self.ae_lr) """ Fine-tuning """ with tf.name_scope('sup_sAE'): # feed 变量 self.input_data = tf.placeholder( tf.float32, [None, self.struct[0]]) # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder( tf.float32, [None, self.struct[-1]]) # N等于batch_size(训练)或_num_examples(测试) self.keep_prob = tf.placeholder(tf.float32) # 权值 变量(初始化) self.out_W = tf.Variable(tf.truncated_normal( shape=[self.struct[-2], self.struct[-1]], stddev=np.sqrt(2 / (self.struct[-2] + self.struct[-1]))), name='W_out') self.out_b = tf.Variable(tf.constant(0.0, shape=[self.struct[-1]]), name='b_out') # 构建sup_sae # 构建权值列表(sup_sae结构) self.parameter_list = list() if self.pre_train: for pt in self.pt_model.pt_list: self.parameter_list.append([pt.W, pt.bh]) else: for i in range(len(self.struct) - 2): W = tf.Variable(tf.truncated_normal( shape=[self.struct[i], self.struct[i + 1]], stddev=np.sqrt(2 / (self.struct[i] + self.struct[i + 1]))), name='W' + str(i + 1)) b = tf.Variable(tf.constant(0.0, shape=[self.struct[i + 1]]), name='b' + str(i + 1)) self.parameter_list.append([W, b]) self.parameter_list.append([self.out_W, self.out_b]) # 构建训练步 self.logist, self.pred = self.transform(self.input_data) self.build_train_step() #****************** 记录 ****************** if self.tbd: for i in range(len(self.parameter_list)): Summaries.scalars_histogram('_W' + str(i + 1), self.parameter_list[i][0]) Summaries.scalars_histogram('_b' + str(i + 1), self.parameter_list[i][1]) tf.summary.scalar('loss', self.loss) tf.summary.scalar('accuracy', self.accuracy) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, self.name))
def build_model(self): print("Start building model...") print('LSTM:') print(self.__dict__) with tf.name_scope('LSTM'): # feed 变量 self.input_data = tf.placeholder( tf.float32, [None, self.struct[0] * self.width ]) # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder( tf.float32, [None, self.struct[-1]]) # N等于batch_size(训练)或_num_examples(测试) self.keep_prob = tf.placeholder(tf.float32) X = tf.split(self.input_data, self.width, axis=1) self.create_variable() # 构建训练步 self.logist, self.pred = self.transform(X) self.build_train_step() #****************** 记录 ****************** if self.tbd: for i in range(len(self.struct) - 2): Summaries.scalars_histogram('_F' + str(i + 1), self.F_parameter[i][0]) Summaries.scalars_histogram('_I' + str(i + 1), self.I_parameter[i][0]) Summaries.scalars_histogram('_C' + str(i + 1), self.C_parameter[i][0]) Summaries.scalars_histogram('_O' + str(i + 1), self.O_parameter[i][0]) Summaries.scalars_histogram('_bf' + str(i + 1), self.F_parameter[i][1]) Summaries.scalars_histogram('_bi' + str(i + 1), self.I_parameter[i][1]) Summaries.scalars_histogram('_bc' + str(i + 1), self.C_parameter[i][1]) Summaries.scalars_histogram('_bo' + str(i + 1), self.O_parameter[i][1]) Summaries.scalars_histogram('_W', self.W) Summaries.scalars_histogram('_b', self.b) tf.summary.scalar('loss', self.loss) tf.summary.scalar('accuracy', self.accuracy) self.merge = tf.summary.merge( tf.get_collection(tf.GraphKeys.SUMMARIES, 'LSTM'))
def build_model(self): # feed self.input_data = tf.placeholder(tf.float32, [None, self.img_shape[0]*self.img_shape[1]]) # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder(tf.float32, [None, self.channels[-1]]) # N等于batch_size(训练)或_num_examples(测试) # reshape X X = tf.reshape(self.input_data, shape=[-1,self.img_shape[0] , self.img_shape[1], self.channels[0]]) # conv,max_pool self.W=list() self.b=list() c=0 p=0 for layer in self.layer_tp: if layer=='C': # W,b:conv name_W='W_conv'+str(c+1) name_b='b_conv'+str(c+1) W_shape=[self.fsize[c][0],self.fsize[c][1],self.channels[c],self.channels[c+1]] self.W.append(tf.Variable(tf.random_normal(shape=W_shape, stddev=0.1), name=name_W)) self.b.append(tf.Variable(tf.constant(0.1, shape=[self.channels[c+1]]),name=name_b)) # Tensorboard Summaries.scalars_histogram('_'+name_W,self.W[-1]) Summaries.scalars_histogram('_'+name_b,self.b[-1]) # 卷积操作 """transform < conv >""" X = self.conv2d(X, self.W[-1], self.b[-1]) c=c+1 else: k_shape=[1,self.ksize[p][0],self.ksize[p][1],1] # 池化操作 """transform < max_pool >""" X = self.max_pool(X, ksize=k_shape) p=p+1 # full_connect 全连接层 shape=X.get_shape().as_list() full_size=shape[1]* shape[2]*shape[3] X = tf.reshape(X, shape=[-1,full_size]) for i in range(c,len(self.channels)-1): if i==c: n1=full_size else: n1=self.channels[i] n2=self.channels[i+1] # W,b:full_connect name_W='W_full'+str(i+1) name_b='b_full'+str(i+1) self.W.append(tf.Variable(tf.random_normal(shape=[n1,n2], stddev=0.1), name=name_W)) self.b.append(tf.Variable(tf.constant(0.1, shape=[n2]),name=name_b)) # Tensorboard Summaries.scalars_histogram('_'+name_W,self.W[-1]) Summaries.scalars_histogram('_'+name_b,self.b[-1]) # 全连接 """transform < full_connect >""" z=tf.add(tf.matmul(X,self.W[-1]), self.b[-1]) if i==len(self.channels)-2: self.pred = act_func(self.output_act_func)(z) # Relu activation else: X = act_func(self.hidden_act_func)(z) # Relu activation if self.dropout<1: X = tf.nn.dropout(X, self.dropout) # Apply Dropout # loss,trainer self.parameter_list=list() for i in range(len(self.W)): self.parameter_list.append(self.W[i]) self.parameter_list.append(self.b[i]) _loss=Loss(label_data=self.label_data, pred=self.pred, output_act_func=self.output_act_func) self.loss=_loss.get_loss_func(self.loss_func) self.train_batch_bp=tf.train.AdamOptimizer(learning_rate=self.cnn_lr).minimize(self.loss, var_list=self.parameter_list) # accuracy _ac=Accuracy(label_data=self.label_data, pred=self.pred) self.accuracy=_ac.accuracy() # Tensorboard tf.summary.scalar('loss',self.loss) tf.summary.scalar('accuracy',self.accuracy) self.merge = tf.summary.merge(tf.get_collection(tf.GraphKeys.SUMMARIES,tf.get_default_graph()._name_stack))
def build_model(self): """ Pre-training """ # 构建un_sae self.un_sae = unsupervised_sAE( en_func=self.en_func, loss_func=self.loss_func, # encoder:[sigmoid] || decoder:[sigmoid] with ‘cross_entropy’ | [relu] with ‘mse’ ae_type=self.ae_type, # ae | dae | sae noise_type=self.noise_type, # Gaussian noise (gs) | Masking noise (mn) beta=self.beta, # 惩罚因子权重(第二项损失的系数) p=self.p, # DAE:样本该维作为噪声的概率 / SAE稀疏性参数:期望的隐层平均活跃度(在训练批次上取平均) un_ae_struct=self.un_ae_struct, ae_epochs=self.ae_epochs, batch_size=self.batch_size, ae_lr=self.ae_lr) """ Fine-tuning """ with tf.name_scope('sup_sAE'): # feed 变量 self.input_data = tf.placeholder(tf.float32, [None, self.sup_ae_struct[0]],name='X') # N等于batch_size(训练)或_num_examples(测试) self.label_data = tf.placeholder(tf.float32, [None, self.sup_ae_struct[-1]],name='Y') # N等于batch_size(训练)或_num_examples(测试) # 权值 变量(初始化) self.out_W = tf.Variable(tf.truncated_normal(shape=[self.sup_ae_struct[-2], self.sup_ae_struct[-1]], stddev=0.1), name='W-out') self.out_b = tf.Variable(tf.constant(0.1, shape=[self.sup_ae_struct[-1]]),name='b-out') # 构建sup_sae # 构建权值列表(sup_sae结构) self.parameter_list = list() for ae in self.un_sae.ae_list: self.parameter_list.append(ae.W) self.parameter_list.append(ae.by) self.parameter_list.append(self.out_W) self.parameter_list.append(self.out_b) # 损失函数 self.pred=self.transform(self.input_data) _loss=Loss(label_data=self.label_data, pred=self.pred, output_act_func=self.out_func) self.loss=_loss.get_loss_func(self.loss_func) _optimization=Optimization(r=self.ae_lr, momentum=0.5) self.train_batch_bp=_optimization.trainer(algorithm='sgd').minimize(self.loss, var_list=self.parameter_list) # 正确率 _ac=Accuracy(label_data=self.label_data, pred=self.pred) self.accuracy=_ac.accuracy() #****************** 记录 ****************** for i in range(len(self.parameter_list)): if i%2==1:continue k=int(i/2+1) W=self.parameter_list[i] b=self.parameter_list[i+1] Summaries.scalars_histogram('_W'+str(k),W) Summaries.scalars_histogram('_b'+str(k),b) tf.summary.scalar('loss',self.loss) tf.summary.scalar('accuracy',self.accuracy) self.merge = tf.summary.merge(tf.get_collection(tf.GraphKeys.SUMMARIES,tf.get_default_graph()._name_stack))