def embedding_lookup(input_ids, vocab_size, embedding_size, initializer, scope, reuse=None, dtype=tf.float32): with tf.variable_scope(scope, reuse=reuse): lookup_table = tf.get_variables('lookup_table', [vocab_size, embedding_size], initializer=initializer, dtype=dtype) return tf.nn.embedding_lookup(lookup_table, input_ids)
def fs_net(self): #add embedding embeddings = tf.get_variables('weight_mat', dtype=tf.float32, shape=(self.vocab_size, self.embedding_dim)) x_embedding = tf.nn.embedding_lookup(embeddings, self.X) _, encoder_fw_states, encoder_bw_states = self.bi_gru( self.X, "stack_encode_bi_gru", self.encoder_n_neurons) encoder_feats = tf.concat( [encoder_fw_states[-1], encoder_bw_states[-1]], axis=-1) #[batch_size,2*self.encoder_n_neurons] encoder_expand_feats = tf.expand_dims(encoder_feats, axis=1) decoder_input = tf.tile( encoder_expand_feats, [1, self.n_steps, 1 ]) #[batch_size,self.n_steps,2*self.encoder_n_neurons] decoder_output, decoder_fw_states, decoder_bw_states = self.bi_gru( decoder_input, "stack_decode_bi_gru", self.decoder_n_neurons) decoder_feats = tf.concat( [decoder_fw_states[-1], decoder_bw_states[-1]], axis=-1) #[batch_size,2*self.decoder_n_neurons] element_wise_product = encoder_feats * decoder_feats element_wise_absolute = tf.abs(encoder_feats - decoder_feats) cls_feats = tf.concat([ encoder_feats, decoder_feats, element_wise_product, element_wise_absolute ], axis=-1) cls_dense_1 = tf.layers.dense( inputs=cls_feats, units=self.n_neurons, activation=tf.nn.selu, kernel_regularizer=tf.contrib.layers.l2_regularizer(0.003)) cls_dense_2 = tf.layers.dense( inputs=cls_dense_1, units=self.n_outputs, activation=tf.nn.selu, kernel_regularizer=tf.contrib.layers.l2_regularizer(0.003), name="softmax") return cls_dense_2, decoder_output
def conv2d(input_, output_dim, k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02, name='conv2d'): with tf.variable_scope(name): w = tf.get_variables( 'w', [k_h, k_w, input_.get_shape()[-1], output_dim], initializer=tf.truncated_normal_initializer(stddev=stddev)) conv = tf.nn.conv2d(input_, w, strides=[1, d_h, d_w, 1], padding='SAME') biases = tf.get_variable('biases', [output_dim], initializer=tf.constant_initializer(0.0)) conv = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape()) return conv
def inference(input_tensor, train, regularizer): with tf.variable_scope('layer1-conv1'): conv1_weights = tf.get_variable( "weight", [CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_DEEP], initializer=tf.truncated_normal_initializer(stddev=0.1) ) conv1_biases=tf.get_variable( "biases", [CONV1_DEEP], initializer=tf.constant_initializer(0.0) ) conv1 = tf.nn.conv2d( input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME' ) relu1 = tf.nn.relu(tf.nn.bias_add(conv1_biases))#bias_add手动传播 with tf.variable_scope('layer-pool1'): pool1 = tf.nn.max_pool( relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME' ) with tf.variable_scope('layer-conv2'): conv2_weights = tf.get_variables( 'weight', )
def inference(images, batch_size, n_classes): ''' build model args: images: image batch, 4D tensor, tf.float32, [batch_size, width, height, channels] return: output tensor with the computed logits, float, [batch_size, n_classes] ''' with tf.variable_scope('conv1') as scope: weights = tf.get_variable('weights', shape = [3,3,3,16], dtype = tf.float32, initializer = tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) biases = tf.get_variables('biases', shape = [16], dtype = tf.float32, initializer = tf.constant_initializer(0.1)) conv = tf.nn.conv2d(images, weights, strides=[1,1,1,1], padding='SAME') pre_activation = tf.nn.bias_add(conv, biases) conv1 = tf.nn.relu(pre_activation, name = scope.name) with tf.variable_scope('pooling1_lrn') as scope: pool1 = tf.nn.max_pool(conv1, ksize = [1,3,3,1], strides = [1,2,2,1], padding = 'SAME', name = 'pooling1') norm1 = tf.nn.lrn(pool1, depth_radius=4, bias = 1.0, alpha=0.001/9.0, beta=0.75,name='norm1') with tf.variable_scope('conv2') as scope: weights = tf.get_variable('weights', shape=[3,3,16,16], dtype=tf.float32, initializer = tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32)) biases = tf.get_variable('biases', shape=[16], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) conv = tf.nn.conv2d(norm1, weights, strides=[1,1,1,1], padding = 'SAME') pre_activation = tf.nn.bias_add(conv, biases) conv2 = tf.nn.relu(pre_activation, name='conv2') with tf.variable_scope('pooling2_lrn') as scope: norm2 = tf.nn.lrn(conv2, depth_radius=4, bias=1.0, alpha = 0.001/9.0, beta=0.75,name='norm2') pool2 = tf.nn.max_pool(norm2,ksize=[1,3,3,1],strides=[1,1,1,1], padding='SAME',name='pooling2') with tf.variable_scope('local3') as scope: reshape = tf.reshape(pool2,shape=[batch_size, -1]) dim = reshape.get_shape()[1].value weights = tf.get_variable('weights', shape=[dim,128], dtype=tf.float32, initializer=tf.truncated_normal_initalizer(stddev=0.005, dtype=tf.float32)) biases = tf.get_variable('biases', shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) local3 = tf.nn.relu(tf.matmul(reshape, weights)+biases, name=scope.name) with tf.variable_scope('local4') as scope: weights = tf.get_variable('weights', shape=[128.128], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32)) biases = tf.get_variable('biases', shape=[128], dtype = tf.float32, initializer = tf.constant_initializer(0.1)) local4 = tf.nn.relu(tf.matmul(local3,weights)+biases, name='local4') with tf.variable_scope('softmax_layer') as scope: weights = tf.get_variable('softmax_linear', shape=[128,n_classes], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.lofat32)) biases = tf.get_variable('biases', shape=[n_classes], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) softmax_liner=tf.add(tf.matmul(local4, weights),biases,name='softmax_linear') return softmax_layer
saver = tf.train.Saver() with tf.Session() as sess: sess.run(init) save_path = saver.save(sess, './save.ckpt') saver = tf.train.Saver() save_path = saver.save(sess, './save.ckpt') saver.restore(sess, '/save.ckpt') with tf.variable_scope('a)variable_scope') as scope: initializer = tf.constant_initializer(value=3) var3 = tf.get_variable(name='var3', shape=[1], dtype=tf.float32, initializer=initializer) scope.reuse_variables() var3_reuse = tf.get_variables(name='var3')
def build_policy_network_op(self, scope="policy_network"): """ Build the policy network, construct the tensorflow operation to sample actions from the policy network outputs, and compute the log probabilities of the actions taken (for computing the loss later). These operations are stored in self.sampled_action and self.logprob. Must handle both settings of self.discrete. Args: scope: the scope of the neural network TODO: Discrete case: action_logits: the logits for each action HINT: use build_mlp, check self.config for layer_size and n_layers self.sampled_action: sample from these logits HINT: use tf.multinomial + tf.squeeze self.logprob: compute the log probabilities of the taken actions HINT: 1. tf.nn.sparse_softmax_cross_entropy_with_logits computes the *negative* log probabilities of labels, given logits. 2. taken actions are different than sampled actions! Continuous case: To build a policy in a continuous action space domain, we will have the model output the means of each action dimension, and then sample from a multivariate normal distribution with these means and trainable standard deviation. That is, the action a_t ~ N( mu(o_t), sigma) where mu(o_t) is the network that outputs the means for each action dimension, and sigma is a trainable variable for the standard deviations. N here is a multivariate gaussian distribution with the given parameters. action_means: the predicted means for each action dimension. HINT: use build_mlp, check self.config for layer_size and n_layers log_std: a trainable variable for the log standard deviations. HINT: think about why we use log std as the trainable variable instead of std HINT: use tf.get_variables self.sampled_actions: sample from the gaussian distribution as described above HINT: use tf.random_normal HINT: use re-parametrization to obtain N(mu, sigma) from N(0, 1) self.lobprob: the log probabilities of the taken actions HINT: use tf.contrib.distributions.MultivariateNormalDiag """ ####################################################### ######### YOUR CODE HERE - 5-10 lines. ############ #mlp_input,output_size,scope,n_layers,size,output_activation = None # #print("layers") #print(self.config.n_layers) #print(self.config.layer_size) #print(self.observation_placeholder) #print(self.discrete) if self.discrete: action_logits = build_mlp(self.observation_placeholder, self.action_dim, scope, self.config.n_layers, self.config.layer_size, output_activation=self.config.activation) #print("action") #print(action_logits) #print(tf.multinomial(logits=action_logits, num_samples=1)) #self.sampled_action = tf.squeeze(tf.multinomial(logits=action_logits, num_samples=1)) self.sampled_action = tf.multinomial(action_logits, 1) self.sampled_action = tf.reshape(self.sampled_action, [ -1, ]) self.logprob = -tf.nn.sparse_softmax_cross_entropy_with_logits( labels=self.action_placeholder, logits=action_logits) else: action_means = build_mlp(self.observation_placeholder, self.action_dim, scope, self.config.n_layers, self.config.layer_size) log_std = tf.get_variables("log_std", [1, self.action_dim], trainable=True) self.sampled_action = tf.random_normal(self.action_dim, action_means, log_std) self.logprob = tf.contrib.distributions.MultivariateNormalDiag( self.action_placeholder, log_std)