def __init__(self, num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
vocab_size, embed_size,d_model,d_k,d_v,h,num_layer,is_training,
initializer=tf.random_normal_initializer(stddev=0.1),clip_gradients=5.0,l2_lambda=0.0001,use_residual_conn=False):
"""init all hyperparameter here"""
super(Transformer, self).__init__(d_model, d_k, d_v, sequence_length, h, batch_size, num_layer=num_layer) #init some fields by using parent class.
self.num_classes = num_classes
self.sequence_length = sequence_length
self.vocab_size = vocab_size
self.embed_size = d_model
self.learning_rate = tf.Variable(learning_rate, trainable=False, name="learning_rate")
self.learning_rate_decay_half_op = tf.assign(self.learning_rate, self.learning_rate * 0.5)
self.initializer = initializer
self.clip_gradients=clip_gradients
self.l2_lambda=l2_lambda
self.is_training=is_training #self.is_training=tf.placeholder(tf.bool,name="is_training") #tf.bool #is_training
self.input_x = tf.placeholder(tf.int32, [self.batch_size, self.sequence_length], name="input_x") #x batch_size
self.input_y_label = tf.placeholder(tf.int32, [self.batch_size], name="input_y_label")
self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
self.global_step = tf.Variable(0, trainable=False, name="Global_Step")
self.epoch_step = tf.Variable(0, trainable=False, name="Epoch_Step")
self.epoch_increment = tf.assign(self.epoch_step, tf.add(self.epoch_step, tf.constant(1)))
self.decay_steps, self.decay_rate = decay_steps, decay_rate
self.use_residual_conn=use_residual_conn
self.instantiate_weights()
self.logits = self.inference() #logits shape:[batch_size,self.num_classes]
self.predictions = tf.argmax(self.logits, axis=1, name="predictions")
correct_prediction = tf.equal(tf.cast(self.predictions, tf.int32),self.input_y_label)
self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name="Accuracy") # shape=()
if self.is_training is False:# if it is not training, then no need to calculate loss and back-propagation.
return
self.loss_val = self.loss()
self.train_op = self.train()
a2_transformer_classification.py 文件源码
python
阅读 26
收藏 0
点赞 0
评论 0
评论列表
文章目录