n = trainX.shape[0] for decay_count in decay: tf.reset_default_graph() with tf.Session() as sess: # Create the model x = tf.placeholder(tf.float32, [None, NUM_FEATURES]) y_ = tf.placeholder(tf.float32, [None, NUM_CLASSES]) # Build the graph for the deep net hidden_layer = layers.dense( x, 25, activation=tf.nn.relu, kernel_initializer=init.orthogonal(np.sqrt(2)), bias_initializer=init.zeros(), kernel_regularizer=l2_regularizer(decay_count)) output_layer = layers.dense( hidden_layer, 6, kernel_initializer=init.orthogonal(), bias_initializer=init.zeros(), kernel_regularizer=l2_regularizer(decay_count)) cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2( labels=y_, logits=output_layer) loss = tf.reduce_mean(cross_entropy) + get_regularization_loss() # Create the gradient descent optimizer with the given learning rate.
def build_net(self, scope): init = tf.random_uniform_initializer(0., 1.) if SHARED: #Shared layer with tf.variable_scope('shared'): for i, layer in enumerate(SHARED_LAYER): hidden = layers.dense( self.s, layer, tf.nn.tanh, kernel_initializer=init, kernel_regularizer=tf.contrib.layers.l2_regularizer( scale=L2_REG_LAMBDA), name='layer_%s' % (i + 1)) with tf.variable_scope('Actor'): a_output = layers.dense(hidden, self.n_actions, tf.nn.softmax, kernel_initializer=init, name='a_out') with tf.variable_scope('Critic'): c_output = layers.dense(hidden, 1, kernel_initializer=init, name='c_out') params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope + '/shared') return a_output, c_output, params else: # Separate Actor and Critic Network with tf.variable_scope('Actor'): #Create Actor Network for layer in ACTOR_LAYER: a_hidden = layers.dense( self.s, layer, tf.nn.relu6, kernel_initializer=init, name='a_hidden', kernel_regularizer=tf.contrib.layers.l2_regularizer( scale=L2_REG_LAMBDA)) a_output = layers.dense(a_hidden, self.n_actions, tf.nn.softmax, kernel_initializer=init, name='a_out') with tf.variable_scope('Critic'): #Create Critic Network for layer in CRITIC_LAYER: c_hidden = layers.dense( self.s, 128, tf.nn.tanh, kernel_initializer=init, name='c_hidden', kernel_regularizer=tf.contrib.layers.l2_regularizer( scale=L2_REG_LAMBDA)) c_output = layers.dense(c_hidden, 1, kernel_initializer=init, name='c_out') actor_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope + '/Actor') critic_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope + '/Critic') return a_output, c_output, actor_params, critic_params
def decoder(self, Z, verbose=True): """ Draws the decoder fo the network """ dec_net = [Z] prev_dec_shape = None num_div = len([ stride for lay_num, (filters, kernel_size, stride) in enumerate(self.decoder_dims) if stride == 2 ]) cur_shape = int(self.dims[1] / (2**(num_div - 1))) for lay_num, (filters, kernel_size, stride) in enumerate(self.decoder_dims): #print( [i for i in tf.trainable_variables() if 'generator/' in i.name]) if kernel_size > 0: # if this is a convolutional layer # this is the first layer and the first convolutional layer if (lay_num == 0) or (self.decoder_dims[lay_num - 1][1] == 0): dec_net.append( tf.reshape( layers.dense(dec_net[len(dec_net) - 1], cur_shape * cur_shape * filters, name='dec_' + str(lay_num), activation=self.default_activation), [self.batch_size, cur_shape, cur_shape, filters])) elif stride == 2: # if the spatial size of the previous layer is greater than the image size of the current layer # we need to resize the current network dims cur_shape *= 2 dec_net.append( tf.image.resize_nearest_neighbor( dec_net[len(dec_net) - 1], (cur_shape, cur_shape))) elif lay_num == len( self.decoder_dims) - 1: # if this is the last layer # append a normal layer dec_net.append( (layers.conv2d(dec_net[len(dec_net) - 1], filters=filters, kernel_size=kernel_size, strides=1, padding='same', name='dec_' + str(lay_num), activation=self.default_activation))) # If the next layer is not convolutional but this one is elif self.decoder_dims[lay_num + 1][1] == 0: # flatten this layers dec_net.append( tf.contrib.layers.flatten( layers.conv2d(dec_net[len(dec_net) - 1], filters=filters, kernel_size=kernel_size, strides=1, padding='same', name='dec_' + str(lay_num), activation=self.default_activation))) else: # append a normal layer dec_net.append( (layers.conv2d(dec_net[len(dec_net) - 1], filters=filters, kernel_size=kernel_size, strides=1, padding='same', name='dec_' + str(lay_num), activation=self.default_activation))) else: # if this is a dense layer # append the dense layer dec_net.append( layers.dense(dec_net[len(dec_net) - 1], units=filters, name='dec_' + str(lay_num), activation=self.default_activation)) # append the output layer if (self.dims[0] != shape(dec_net[-1])[1]) & (self.dims[1] != shape( dec_net[-1])[2]): print('warning: shape does not match image shape') dec_net.append( tf.image.resize_nearest_neighbor(dec_net[len(dec_net) - 1], (self.dims[0], self.dims[1]))) dec_net.append( layers.conv2d(dec_net[len(dec_net) - 1], self.dims[2], 1, strides=1, activation=tf.sigmoid, name='output_layer')) dec_net.append(tf.contrib.layers.flatten(dec_net[len(dec_net) - 1])) if verbose: print('Decoder shapes: ', [shape(i) for i in dec_net]) return dec_net
def build_encoder(self): """ 构建完整编码器 :return: """ with tf.variable_scope('encoder'): encoder_cell = self.build_encoder_cell(self.hidden_size, self.cell_type, self.layer_size) # self.encoder_embeddings可以理解为词向量词典,存储vocab_size个大小为embedding_size的词向量,随机初始化为正态分布的值; # tf.nn.embedding_lookup(params, ids)函数的用法主要是选取一个张量里面索引对应的元素;params可以是张量也可以是数组等,id就是对应的索引。 # [self.encoder_inputs,self.encoder_vocab_size]*[self.encoder_vocab_size, self.embedding_dim]=[self.encoder_inputs,self.embedding_dim] encoder_inputs_embedded = tf.nn.embedding_lookup( self.encoder_embeddings, self.encoder_inputs) encoder_inputs_embedded = layers.dense( encoder_inputs_embedded, # 全连接层 相当于添加一个层 self.hidden_size, use_bias=False, name='encoder_residual_projection') initial_state = encoder_cell.zero_state(self.batch_size, dtype=tf.float32) # 初始化值 if self.bidirection: # 双向RNN encoder_cell_bw = self.build_encoder_cell( self.hidden_size, self.cell_type, self.layer_size) ''' outputs为(output_fw, output_bw),是一个包含前向cell输出tensor和后向cell输出tensor组成的二元组。 output_states为(output_state_fw, output_state_bw),包含了前向和后向最后的隐藏状态的组成的二元组。 output_state_fw和output_state_bw的类型为LSTMStateTuple。 LSTMStateTuple由(c,h)组成,分别代表memory cell和hidden state。 ''' ((encoder_fw_outputs, encoder_bw_outputs), (encoder_fw_state, encoder_bw_state)) \ = tf.nn.bidirectional_dynamic_rnn( cell_bw=encoder_cell_bw, # 后向RNN cell_fw=encoder_cell, # 前向RNN inputs=encoder_inputs_embedded, # 输入 sequence_length=self.encoder_inputs_length, # 输入序列的实际长度 dtype=tf.float32, swap_memory=True) encoder_outputs = tf.concat( (encoder_bw_outputs, encoder_fw_outputs), 2) # 在第二个维度拼接 encoder_final_state = [] # output_state_fw和output_state_bw的类型为LSTMStateTuple。 # LSTMStateTuple由(c,h)组成,分别代表memory cell和hidden state。 for i in range(self.layer_size): c_fw, h_fw = encoder_fw_state[i] c_bw, h_bw = encoder_bw_state[i] c = tf.concat((c_fw, c_bw), axis=-1) # 在最高的维度进行拼接 h = tf.concat((h_fw, h_bw), axis=-1) encoder_final_state.append(LSTMStateTuple(c=c, h=h)) encoder_final_state = tuple(encoder_final_state) else: encoder_outputs, encoder_final_state = tf.nn.dynamic_rnn( cell=encoder_cell, inputs=encoder_inputs_embedded, sequence_length=self.encoder_inputs_length, dtype=tf.float32, initial_state=initial_state, swap_memory=True) return encoder_outputs, encoder_final_state
def build_encoder(self): """构建编码器""" # print("构建编码器") with tf.variable_scope('encoder'): # 构建 encoder_cell encoder_cell = self.build_encoder_cell() # 编码器的embedding运行在GPU/CPU上 with tf.device(_get_embed_device(self.input_vocab_size)): # 加载预训练好的embedding if self.pretrained_embedding: self.encoder_embeddings = tf.Variable(tf.constant( 0.0, shape=(self.input_vocab_size, self.embedding_size)), trainable=True, name='embeddings') self.encoder_embeddings_placeholder = tf.placeholder( tf.float32, (self.input_vocab_size, self.embedding_size)) self.encoder_embeddings_init = self.encoder_embeddings.assign( self.encoder_embeddings_placeholder) else: self.encoder_embeddings = tf.get_variable( name='embedding', shape=(self.input_vocab_size, self.embedding_size), initializer=self.initializer, dtype=tf.float32) # embedding之后的输入 shape = (batch_size, time_step, embedding_size) self.encoder_inputs_embedded = tf.nn.embedding_lookup( params=self.encoder_embeddings, ids=self.encoder_inputs) # 当时用残差网络时 if self.use_residual: self.encoder_inputs_embedded = layers.dense( self.encoder_inputs_embedded, self.hidden_units, use_bias=False, name='encoder_residual_projection') inputs = self.encoder_inputs_embedded if self.time_major: inputs = tf.transpose(inputs, (1, 0, 2)) if not self.bidirectional: # 单向 RNN (encoder_outputs, encoder_state) = tf.nn.dynamic_rnn( cell=encoder_cell, inputs=inputs, sequence_length=self.encoder_inputs_length, dtype=tf.float32, time_major=self.time_major, parallel_iterations=self.parallel_iterations, swap_memory=True) else: # 双向RNN # encoder_cell_fw = self.build_encoder_cell() encoder_cell_bw = self.build_encoder_cell() # backward cell ((encoder_fw_outputs, encoder_bw_outputs), (encoder_fw_state, encoder_bw_state)) = tf.nn.bidirectional_dynamic_rnn( cell_fw=encoder_cell, cell_bw=encoder_cell_bw, inputs=inputs, sequence_length=self.encoder_inputs_length, dtype=tf.float32, time_major=self.time_major, parallel_iterations=self.parallel_iterations, swap_memory=True) # 首先合并两个方向 RNN 的输出 encoder_outputs = tf.concat( (encoder_fw_outputs, encoder_bw_outputs), 2) encoder_state = [] for i in range(self.depth): encoder_state.append(encoder_fw_state[i]) encoder_state.append(encoder_bw_state[i]) encoder_state = tuple(encoder_state) return encoder_outputs, encoder_state
def fc(self, name, x, nb_nodes, *, norm_fn='none', norm_params=None, act_fn='none', winit_fn='xavier', binit_fn='zeros', has_bias=True, disp=True, collect_end_points=True): if callable(act_fn): act_fn_str = 'func' act_fn = act_fn else: act_fn_str = self.config.get(name + ' activation', act_fn) act_fn = get_activation(act_fn_str) if callable(norm_fn): norm_fn_str = 'func' norm_fn = norm_fn else: norm_fn_str = self.config.get(name + ' normalization', norm_fn) norm_fn = get_normalization(norm_fn_str) winit_fn_str = self.config.get(name + ' weightsinit', winit_fn) if 'special' in winit_fn_str: split = winit_fn_str.split() winit_name = split[0] if winit_name == 'glorot_uniform': input_nb_nodes = int(x.get_shape()[-1]) filters_stdev = np.sqrt(2.0 / (input_nb_nodes + nb_nodes)) winit_fn = self.uniform_initializer(filters_stdev) else: raise Exception('Error weights initializer function name : ' + winit_fn_str) else: winit_fn = get_weightsinit(winit_fn_str) binit_fn_str = self.config.get(name + ' biasesinit', binit_fn) binit_fn = get_weightsinit(binit_fn_str) if self.using_tcl_library: x = tcl.fully_connected(x, nb_nodes, activation_fn=act_fn, normalizer_fn=norm_fn, normalizer_params=norm_params, weights_initializer=winit_fn, scope=name) else: x = tl.dense(x, nb_nodes, use_bias=has_bias, kernel_initializer=winit_fn, bias_initializer=binit_fn, trainable=True, name=name) with tf.variable_scope(name): if norm_fn is not None: norm_params = norm_params or {} x = norm_fn(x, **norm_params) if act_fn is not None: x = act_fn(x) if disp: print('\t\tFC(' + str(name) + ') --> ', x.get_shape(), ' ', (act_fn_str, norm_fn_str, winit_fn_str)) if collect_end_points: self.end_points[name] = x return x
def __init__(self, state_size, action_size, hidden_layer_size, learning_rate=0.00025, model_name="model", global_step=None): self.state_size = state_size self.action_size = action_size self.hidden_layer_size = hidden_layer_size self.input = tf.placeholder(shape=[ None, self.state_size[0], self.state_size[1], self.state_size[2] ], dtype=tf.float32) self.input_normalize = (self.input - (255.0 / 2)) / (255.0 / 2) with tf.variable_scope(name_or_scope=model_name): self.conv1 = layer.conv2d(inputs=self.input_normalize, filters=32, activation=tf.nn.relu, kernel_size=[8, 8], strides=[4, 4], padding="SAME") self.conv2 = layer.conv2d(inputs=self.conv1, filters=64, activation=tf.nn.relu, kernel_size=[4, 4], strides=[2, 2], padding="SAME") self.conv3 = layer.conv2d(inputs=self.conv2, filters=64, activation=tf.nn.relu, kernel_size=[3, 3], strides=[1, 1], padding="SAME") self.flat = layer.flatten(self.conv3) self.L1 = layer.dense(self.flat, self.hidden_layer_size, activation=tf.nn.relu) self.A1 = layer.dense(self.L1, self.hidden_layer_size, activation=tf.nn.relu) self.Adventage = layer.dense(self.A1, self.action_size, activation=None) self.V1 = layer.dense(self.L1, self.hidden_layer_size, activation=tf.nn.relu) self.Value = layer.dense(self.V1, 1, activation=None) self.Q_Out = self.Value + tf.subtract( self.Adventage, tf.reduce_mean(self.Adventage, axis=1, keep_dims=True)) self.predict = tf.arg_max(self.Q_Out, 1) self.target_Q = tf.placeholder(shape=[None, self.action_size], dtype=tf.float32) self.loss = tf.losses.mean_squared_error(self.target_Q, self.Q_Out) self.UpdateModel = tf.train.AdamOptimizer( learning_rate=learning_rate).minimize(self.loss, global_step=global_step) self.trainable_var = tf.get_collection( tf.GraphKeys.TRAINABLE_VARIABLES, model_name)
import numpy as np import tensorflow as tf from tensorflow import layers from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("../MNIST_data/", one_hot=True) num_out = 10 x = tf.placeholder(tf.float32, [None, 784]) y = tf.placeholder(tf.int32) h1 = layers.dense(x, 200, activation=tf.nn.relu) h2 = layers.dense(x, 200, activation=tf.nn.relu) out = layers.dense(x, num_out, activation=None) loss = tf.reduce_mean( tf.losses.softmax_cross_entropy(onehot_labels=y, logits=out)) optim = tf.train.AdamOptimizer(learning_rate=1e-4) train_op = optim.minimize(loss) correct_prediction = tf.equal(tf.argmax(out, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) sess = tf.InteractiveSession() init = tf.global_variables_initializer() init.run() num_epoch = 20 batch_size = 100 num_iter_in_epoch = mnist.train.num_examples // batch_size
def discriminator(images, reuse=None, n_conv_layer=3): if channel_first_disc: channels_key = 'channels_first' channels_code = "NCHW" else: channels_key = 'channels_last' channels_code = "NHWC" n_conv_layer = int(np.ceil(np.log2(resolution_image / size_init))) n_filters = 1 print(' D: n-conv layer discriminator: ', n_conv_layer) print(' D: n-filters discriminator: ', n_filters) with tf.variable_scope( 'Discriminator', reuse=tf.AUTO_REUSE): # Needed for later, in order to # get variables of generator print(' D: input') print(images) if channel_first_disc: print('channel first disc ON') output = tf.reshape( images, [-1, channels, resolution_image, resolution_image]) else: output = tf.reshape( images, [-1, resolution_image, resolution_image, channels]) print(' D: channel reshape') print(output) # ----- LoopLayers, Conv, Leaky ----- # for i in range(n_conv_layer): print(' D: conv2d iter: ', i, ' - n_filters: ', n_filters) # GETTING THE WEIGHTS THAT WILL BE NORMALIZED. print(output) print("Kernel print:" + "(" + str(i) + ")") w = tf.get_variable("kernel" + str(i), shape=[ kernel_size[0], kernel_size[1], output.get_shape()[-1], n_filters * const_filt ]) print(w) print("constant print:" + "(" + str(i) + ")") b = tf.get_variable("constant" + str(i), [n_filters * const_filt], initializer=tf.constant_initializer(0.0)) print(b) if sngan: pesi = spectral_norm(w, i) else: pesi = w output = tf.nn.conv2d(input=output, filter=pesi, strides=[1, strides, strides, 1], padding='SAME', data_format=channels_code) + b print(output) output = tf.maximum(leakage * output, output) n_filters = int(n_filters * 2) output = tf.reshape( output, [-1, size_init * size_init * (int(n_filters / 2) * const_filt)]) print(' D: reshape linear layer') print(output) # ----- Layer4, Dense, Linear ----- # output = layers.dense(output, units=num_labels + 1) print(' D: dense layer output') print(output) scores_out = tf.identity(output[:, :1], name='scores_out') labels_out = tf.identity(output[:, 1:], name='labels_out') print(' D: scores output') print(scores_out) print(' D: labels output') print(labels_out) return scores_out, labels_out
def _build_graph(self): logging.debug("Building graph") # rnn_inputs: [batch_size, max_len, input_size] # rnn_inputs_len: [batch_size] # target_outputs: [batch_size, max_len, output_size] self.rnn_inputs, self.rnn_inputs_len, self.target_outputs = self.data_generator.inputs( self.para.mode, self.para.batch_size) #TODO recode for efficiency to allow parallelization for graph_layer_index in range(self.para.num_graph_layers): print(graph_layer_index) if graph_layer_index == 0: nodes_rnn_inputs = self.rnn_inputs rnn_length = self.para.max_len else: rnn_length = self.para.num_units nodes_rnn_outputs = [] for node_index in range(self.para.num_var): # graph mask : [num_var] graph_mask = self.graph_masks[node_index] # embedding size num_node_connections = np.sum(graph_mask) # node_rnn_inputs : [batch_size, max_len, num_node_connections] node_rnn_inputs = tf.reshape( tf.boolean_mask(nodes_rnn_inputs, graph_mask, axis=2), (-1, rnn_length, num_node_connections)) node_rnn_inputs_embed = tf.nn.relu( dense(node_rnn_inputs, self.para.num_units)) node_rnn_inputs_embed = tf.unstack(node_rnn_inputs_embed, axis=1) _, node_rnn_states = tf.nn.static_rnn( cell=self._build_rnn_cell(), inputs=node_rnn_inputs_embed, sequence_length=self.rnn_inputs_len, dtype=self.dtype, scope="graph" + str(graph_layer_index + 1) + "node" + str(node_index + 1) + "lstm") node_rnn_states = tf.concat( [ node_rnn_states[i][1] for i in range(self.para.num_layers) ], 1, ) # rnn_output = dense(node_rnn_states, 1) nodes_rnn_outputs.append(node_rnn_states) # if not last layer reload rnn outputs as input if graph_layer_index < self.para.num_graph_layers - 1: nodes_rnn_inputs = tf.stack(nodes_rnn_outputs, axis=2) else: self.final_rnn_states = tf.stack(nodes_rnn_outputs, axis=2) all_rnn_outputs = [] for node_index in range(self.para.num_var): # graph mask : [num_var] graph_mask = self.graph_masks[node_index] # embedding size num_node_connections = np.sum(graph_mask) node_rnn_outputs = tf.reshape( tf.boolean_mask(self.final_rnn_states, graph_mask, axis=2), (-1, rnn_length * num_node_connections)) node_output = dense(node_rnn_outputs, 1) all_rnn_outputs.append(node_output) self.all_rnn_outputs = tf.concat(all_rnn_outputs, axis=1) if self.para.highway > 0: reg_outputs = tf.transpose( self.rnn_inputs[:, -self.para.highway:, :], [0, 2, 1]) reg_outputs = dense(reg_outputs, 1) self.all_rnn_outputs += tf.squeeze(reg_outputs) if self.para.mode == "train" or self.para.mode == "validation": self.labels = self.target_outputs[:, self.para.max_len - 1, :] self.loss = self._compute_loss(outputs=self.all_rnn_outputs, labels=self.labels) elif self.para.mode == "test": self.labels = self.target_outputs[:, self.para.max_len - 1, :] if not self.para.mts: self.all_rnn_outputs = tf.sigmoid(self.all_rnn_outputs)
def f_net(inputs, num_outputs, is_training): inputs = inputs / 128 - 1.0 conv1 = layers.conv2d(inputs=inputs, filters=16, kernel_size=(8, 8), strides=1, kernel_regularizer=l2_regularizer(scale=1e-2), name='conv1') pool1 = layers.max_pooling2d(inputs=conv1, pool_size=3, strides=4, name='pool1') conv2 = layers.conv2d(inputs=pool1, filters=16, kernel_size=(5, 5), strides=1, kernel_regularizer=l2_regularizer(scale=1e-2), name='conv2') pool2 = layers.max_pooling2d(inputs=conv2, pool_size=3, strides=3, name='pool2') conv3 = layers.conv2d(inputs=pool2, filters=64, kernel_size=(3, 3), strides=1, kernel_regularizer=l2_regularizer(scale=1e-2), name='conv3') pool3 = layers.max_pooling2d( inputs=conv3, pool_size=3, strides=8, name='pool3', ) conv4 = layers.conv2d(inputs=pool3, filters=64, kernel_size=(3, 3), strides=1, kernel_regularizer=l2_regularizer(scale=1e-2), name='conv4') pool4 = layers.max_pooling2d(inputs=conv4, pool_size=3, strides=8, name='pool4') depth = pool4.get_shape()[1:].num_elements() inputs = tf.reshape(pool4, shape=[-1, depth]) hid1 = layers.dense(inputs=inputs, units=256, activation=tf.nn.relu, kernel_regularizer=l2_regularizer(scale=1e-2), name='hid1') hid2 = layers.dense(inputs=hid1, units=256, activation=tf.nn.relu, kernel_regularizer=l2_regularizer(scale=1e-2), name='hid2') q = layers.dense(inputs=hid2, units=num_outputs, activation=None, kernel_regularizer=l2_regularizer(scale=1e-2), name='q') q = tf.squeeze(q, name='out_sqz') return q
def build_encoder(self): """ 构建编码器 :return: """ with tf.variable_scope('encoder'): encoder_cell = self.build_encoder_cell() with tf.device(_get_embed_device(self.input_vocab_size)): if self.pretrained_embedding: self.encoder_embeddings = tf.Variable(tf.constant( 0.0, shape=(self.input_vocab_size, self.embedding_size)), trainable=True, name='embeddings') self.encoder_embeddings_placeholder = tf.placeholder( tf.float32, (self.input_vocab_size, self.embedding_size)) self.encoder_embeddings_init = self.encoder_embeddings.assign( self.encoder_embeddings_placeholder) else: self.encoder_embeddings = tf.get_variable( name='embeddings', shape=(self.input_vocab_size, self.embedding_size), initializer=self.initializer, dtype=tf.float32) self.encoder_inputs_embedded = tf.nn.embedding_lookup( params=self.encoder_embeddings, ids=self.encoder_inputs) if self.use_residual: self.encoder_inputs_embedded = layers.dense( self.encoder_inputs_embedded, self.hidden_size, use_bias=False, name='encoder_residual_projection') inputs = self.encoder_inputs_embedded if self.time_major: inputs = tf.transpose(inputs, (1, 0, 2)) if not self.bidirection: (encoder_outputs, encoder_state) = tf.nn.dynamic_rnn( cell=encoder_cell, inputs=inputs, sequence_length=self.encoder_inputs_length, dtype=tf.float32, time_major=self.time_major, parallel_iterations=self.parallel_iterations, swap_memory=True) else: # 多了合并操作 encoder_cell_bw = self.build_encoder_cell() ((encoder_fw_outputs, encoder_bw_outputs), (encoder_fw_state, encoder_bw_state)) = tf.nn.bidirectional_dynamic_rnn( cell_bw=encoder_cell_bw, cell_fw=encoder_cell, inputs=inputs, sequence_length=self.encoder_inputs_length, dtype=tf.float32, time_major=self.time_major, parallel_iterations=self.parallel_iterations, swap_memory=True) encoder_outputs = tf.concat( (encoder_bw_outputs, encoder_fw_outputs), 2) encoder_state = [] for i in range(self.depth): encoder_state.append(encoder_fw_state[i]) encoder_state.append(encoder_bw_state[i]) encoder_state = tuple(encoder_state) return encoder_outputs, encoder_state
def dense_layer(self, x, n_node, name): with tf.variable_scope(name, reuse=self.reuse): x = layer.dense(x, n_node, activation=None, name='_d') return x
def build_encoder(self): """ 构建编码器 :return:encoder_outputs, 最后一层rnn的输出 encoder_state,每一层的final state """ with tf.variable_scope('encoder'): encoder_cell = self.build_encoder_cell() # 编码器的embedding with tf.device(_get_embed_device(self.input_vocab_size)): # 加载训练好的embedding if self.pretrained_embedding: # 预训练模式 self.encoder_embeddings = tf.Variable(tf.constant( 0.0, shape=(self.input_vocab_size, self.embedding_size)), trainable=True, name='embeddings') self.encoder_embeddings_placeholder = tf.placeholder( tf.float32, (self.input_vocab_size, self.embedding_size)) self.encoder_embeddings_init = \ self.encoder_embeddings.assign( self.encoder_embeddings_placeholder) else: self.encoder_embeddings = tf.get_variable( name='embedding', shape=(self.input_vocab_size, self.embedding_size), initializer=self.initializer, dtype=tf.float32) # embedded之后的输入 shape = (batch_size, time_step, embedding_size) self.encoder_inputs_embedded = tf.nn.embedding_lookup( params=self.encoder_embeddings, ids=self.encoder_inputs) # 使用残差结构,先将输入的维度转换为隐藏层的维度 if self.use_residual: self.encoder_inputs_embedded = \ layers.dense(self.encoder_inputs_embedded, self.hidden_units, use_bias=False, name='encoder_residual_projection') inputs = self.encoder_inputs_embedded if self.time_major: inputs = tf.transpose(inputs, (1, 0, 2)) if not self.bidirectional: (encoder_outputs, encoder_state) = tf.nn.dynamic_rnn( cell=encoder_cell, inputs=inputs, sequence_length=self.encoder_inputs_length, dtype=tf.float32, time_major=self.time_major, parallel_iterations=self.parallel_iterations, swap_memory=True # 动态rnn,可以交换内存 ) else: encoder_cell_bw = self.build_encoder_cell() ((encoder_fw_outputs, encoder_bw_outputs), (encoder_fw_state, encoder_bw_state)) = tf.nn.bidirectional_dynamic_rnn( cell_fw=encoder_cell, cell_bw=encoder_cell_bw, inputs=inputs, sequence_length=self.encoder_inputs_length, dtype=tf.float32, time_major=self.time_major, parallel_iterations=self.parallel_iterations, swap_memory=True) encoder_outputs = tf.concat( (encoder_fw_outputs, encoder_bw_outputs), 2) encoder_state = [] for i in range(self.depth): c_fw, h_fw = encoder_fw_state[i] c_bw, h_bw = encoder_bw_state[i] c = tf.concat((c_fw, c_bw), axis=-1) h = tf.concat((h_fw, h_bw), axis=-1) encoder_state.append(LSTMStateTuple(c=c, h=h)) encoder_state = tuple(encoder_state) return encoder_outputs, encoder_state
def discriminator(images, reuse=None, n_conv_layer=3): """ :param images: images that are input of the discriminator :return: likeliness of the image """ if channel_first_disc == True: channels_key = 'channels_first' else: channels_key = 'channels_last' n_conv_layer = int(np.ceil(np.log2(resolution_image / size_init))) n_filters = 1 print(' D: n-conv layer discriminator: ', n_conv_layer) print(' D: n-filters discriminator: ', n_filters) with tf.variable_scope('Discriminator', reuse=tf.AUTO_REUSE): # Needed for later, in order to # get variables of generator print(' D: input') print(images) if channel_first_disc: print('channel first disc ON') output = tf.reshape(images, [-1, channels, resolution_image, resolution_image]) else: output = tf.reshape(images, [-1, resolution_image, resolution_image, channels]) print(' D: channel reshape') print(output) # ----- LoopLayers, Conv, Leaky ----- # karras_disc_in = [] for i in range(n_conv_layer): print(' D: conv2d iter: ', i, ' - n_filters: ', n_filters) output = layers.conv2d(output, filters=n_filters * const_filt, kernel_size=kernel_size, strides=strides, padding='same', data_format=channels_key) print(output) output = tf.maximum(leakage * output, output) n_filters = int(n_filters * 2) karras_disc_in.append(output) output = tf.reshape(output, [-1, size_init * size_init * (int(n_filters / 2) * const_filt)]) print(' D: reshape linear layer') print(output) # ----- Layer4, Dense, Linear ----- # output = layers.dense(output, units=num_labels + 1) print(' D: dense layer output') print(output) scores_out = tf.identity(output[:, :1], name='scores_out') labels_out = tf.identity(output[:, 1:], name='labels_out') print(' D: scores output') print(scores_out) print(' D: labels output') print(labels_out) return scores_out, labels_out
def generator(n_samples, noise_with_labels, reuse=None): """ :param n_samples: number of samples :param noise_with_labels: latent noise + labels :return: generated images """ with tf.variable_scope( 'Generator', reuse=tf.AUTO_REUSE): # Needed for later, in order to get # variables of discriminator # ----- Layer1, Dense, Batch, Leaky ----- # alpha = 0.01 output = layers.dense(inputs=noise_with_labels, units=4 * 4 * 4 * 64) output = layers.batch_normalization(output) output = tf.maximum(alpha * output, output) if channel_first: # size: 128 x 7 x 7 output = tf.reshape(output, (-1, 4 * 64, 4, 4)) bn_axis = 1 # [0, 2, 3] # first else: # size: 7 x 7 x 128 output = tf.reshape(output, (-1, 4, 4, 4 * 64)) bn_axis = -1 # [0, 1, 2] # last print('channel first: FALSE') print(output) # ----- Layer2, deConv, Batch, Leaky ----- # output = layers.conv2d_transpose(output, filters=4 * DIM, kernel_size=(5, 5), strides=2, padding='same') print(output) output = layers.batch_normalization(output, axis=bn_axis) output = tf.maximum(alpha * output, output) if channel_first: output = output[:, :, :7, :7] else: print('cutted:') print(output) output = output[:, :7, :7, :] # ----- Layer3, deConv, Batch, Leaky ----- # output = layers.conv2d_transpose(output, filters=2 * DIM, kernel_size=(5, 5), strides=2, padding='same') output = layers.batch_normalization(output, axis=bn_axis) output = tf.maximum(alpha * output, output) # ----- Layer4, deConv, Batch, Leaky ----- # output = layers.conv2d_transpose(output, filters=DIM, kernel_size=(5, 5), strides=2, padding='same') output = layers.batch_normalization(output, axis=bn_axis) output = tf.maximum(alpha * output, output) # ----- Layer5, deConv, Batch, Leaky ----- # output = layers.conv2d_transpose(output, filters=1, kernel_size=(5, 5), strides=1, padding='same') output = tf.nn.tanh(output) output = tf.reshape(output, [-1, OUTPUT_DIM]) print('Generator output size:') print(output) return output
def rnn(self): """CNN模型""" with tf.name_scope("rnn"): """构建编码器 """ # print("多层rnn模型") embedding_inputs = self.input_embedding() print("embedding_inputs", embedding_inputs.shape) rnn_cell = self.build_rnn_cell() # 目前這部的具体用意还不知为什么,有待考察 if self.config.use_residual: embedding_inputs = layers.dense(embedding_inputs, self.config.hidden_dim, activation=tf.nn.relu, use_bias=False, name='residual_projection') inputs = embedding_inputs if self.config.time_major: inputs = tf.transpose(inputs, (1, 0, 2)) if not self.config.bidirectional: # 单向 RNN (encoder_outputs, encoder_state) = tf.nn.dynamic_rnn( cell=rnn_cell, inputs=inputs, dtype=tf.float32, time_major=self.config.time_major) else: # 双向 RNN rnn_cell_bw = self.build_rnn_cell() ((encoder_fw_outputs, encoder_bw_outputs), (encoder_fw_state, encoder_bw_state)) = \ tf.nn.bidirectional_dynamic_rnn(cell_fw=rnn_cell, cell_bw=rnn_cell_bw, inputs=inputs, dtype=tf.float32, time_major=self.config.time_major ) # 首先合并两个方向 RNN 的输出 encoder_outputs = tf.concat( (encoder_fw_outputs, encoder_bw_outputs), 2) encoder_state = [] for i in range(self.config.num_layers): encoder_state.append(encoder_fw_state[i]) encoder_state.append(encoder_bw_state[i]) encoder_state = tuple(encoder_state) # _outputs, _ = self.build_rnn_model() print("_outputs", encoder_outputs.shape) last = encoder_outputs[:, -1, :] print("last", last.shape) with tf.name_scope("score"): cur_layer = last layer_dimension = [250, 100] # 神经网络层节点的个数 n_layers = len(layer_dimension) # 神经网络的层数 for i in range(0, n_layers): out_dimension = layer_dimension[i] fc = tf.layers.dense(inputs=cur_layer, \ units=out_dimension, \ activation=tf.nn.relu, \ activity_regularizer=tf.contrib.layers.l2_regularizer(0.001), \ kernel_regularizer=tf.contrib.layers.l2_regularizer(0.003), \ # name='fc1' ) cur_layer = tf.contrib.layers.dropout(fc, self.keep_prob) # 分类器,输出层 self.logits = tf.layers.dense(cur_layer, self.config.num_classes, name='fc2') self.y_pred = tf.argmax(tf.nn.softmax(self.logits), 1) # 预测类别 with tf.name_scope("loss"): # 使用优化方式,损失函数,交叉熵 cross_entropy = tf.nn.softmax_cross_entropy_with_logits( logits=self.logits, labels=self.input_y) self.loss = tf.reduce_mean(cross_entropy) with tf.name_scope("optimize"): # 优化器 self.optim = tf.train.AdamOptimizer( learning_rate=self.config.learning_rate).minimize(self.loss) with tf.name_scope("accuracy"): # 准确率 correct_pred = tf.equal(tf.argmax(self.input_y, 1), self.y_pred) self.acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
def f_net(inputs): inputs = inputs[0] inputs = inputs / 128 - 1.0 # (640, 640, 3*n) -> () conv1 = layers.conv2d(inputs=inputs, filters=16, kernel_size=(8, 8), strides=1, kernel_regularizer=l2_regularizer(scale=1e-2), activation=tf.nn.relu, name='conv1') print conv1.shape pool1 = layers.max_pooling2d(inputs=conv1, pool_size=3, strides=4, name='pool1') print pool1.shape conv2 = layers.conv2d(inputs=pool1, filters=16, kernel_size=(5, 5), strides=1, kernel_regularizer=l2_regularizer(scale=1e-2), activation=tf.nn.relu, name='conv2') print conv2.shape pool2 = layers.max_pooling2d(inputs=conv2, pool_size=3, strides=3, name='pool2') print pool2.shape conv3 = layers.conv2d(inputs=pool2, filters=64, kernel_size=(3, 3), strides=1, kernel_regularizer=l2_regularizer(scale=1e-2), activation=tf.nn.relu, name='conv3') print conv3.shape pool3 = layers.max_pooling2d( inputs=conv3, pool_size=3, strides=2, name='pool3', ) print pool3.shape depth = pool3.get_shape()[1:].num_elements() inputs = tf.reshape(pool3, shape=[-1, depth]) print inputs.shape hid1 = layers.dense(inputs=inputs, units=256, activation=tf.nn.relu, kernel_regularizer=l2_regularizer(scale=1e-2), name='hid1') print hid1.shape hid2 = layers.dense(inputs=hid1, units=256, activation=tf.nn.relu, kernel_regularizer=l2_regularizer(scale=1e-2), name='hid2_adv') print hid2.shape adv = layers.dense(inputs=hid2, units=NUM_PROXY_ACTION, activation=None, kernel_initializer=tf.random_uniform_initializer( -3e-3, 3e-3), kernel_regularizer=l2_regularizer(scale=1e-2), name='adv') print adv.shape hid2 = layers.dense(inputs=hid1, units=256, activation=tf.nn.relu, kernel_regularizer=l2_regularizer(scale=1e-2), name='hid2_v') print hid2.shape v = layers.dense(inputs=hid2, units=1, activation=None, kernel_initializer=tf.random_uniform_initializer( -3e-3, 3e-3), kernel_regularizer=l2_regularizer(scale=1e-2), name='v') print v.shape q = tf.add(adv, v, name='q') print q.shape return {"q": q}
def main(params): # load data, class data contains captions, images, image features (if avaliable) if params.gen_val_captions < 0: repartiton = False else: repartiton = True data = Data(params, True, params.image_net_weights_path, repartiton=repartiton, gen_val_cap=params.gen_val_captions) # load batch generator, repartiton to use more val set images in train gen_batch_size = params.batch_size if params.fine_tune: gen_batch_size = params.batch_size batch_gen = data.load_train_data_generator(gen_batch_size, params.fine_tune) # whether use presaved pretrained imagenet features (saved in pickle) # feature extractor after fine_tune will be saved in tf checkpoint # caption generation after fine_tune must be made with params.fine_tune=True pretrained = not params.fine_tune val_gen = data.get_valid_data(gen_batch_size, val_tr_unused=batch_gen.unused_cap_in, pretrained=pretrained) test_gen = data.get_test_data(gen_batch_size, pretrained=pretrained) # annotations vector of form <EOS>...<BOS><PAD>... ann_inputs_enc = tf.placeholder(tf.int32, [None, None]) ann_inputs_dec = tf.placeholder(tf.int32, [None, None]) ann_lengths = tf.placeholder(tf.int32, [None]) if params.fine_tune: # if fine_tune dont just load images_fv image_f_inputs = tf.placeholder(tf.float32, [None, 224, 224, 3]) else: # use prepared image features [batch_size, 4096] (fc2) image_f_inputs = tf.placeholder(tf.float32, [None, 4096]) if params.use_c_v or (params.prior == 'GMM' or params.prior == 'AG'): c_i = tf.placeholder(tf.float32, [None, 90]) else: c_i = ann_lengths # dummy tensor # because of past changes image_batch, cap_enc, cap_dec, cap_len, cl_vectors = image_f_inputs,\ ann_inputs_enc, ann_inputs_dec, ann_lengths, c_i # features, params.fine_tune stands for not using presaved imagenet weights # here, used this dummy placeholder during fine_tune, will remove it in # future releases, thats for saving image_net weights for futher usage image_f_inputs2 = tf.placeholder_with_default(tf.ones([1, 224, 224, 3]), shape=[None, 224, 224, 3], name='dummy_ps') if params.fine_tune: image_f_inputs2 = image_batch if params.mode == 'training' and params.fine_tune: cnn_dropout = params.cnn_dropout weights_regularizer = tf.contrib.layers.l2_regularizer( params.weight_decay) else: cnn_dropout = 1.0 weights_regularizer = None with tf.variable_scope("cnn", regularizer=weights_regularizer): image_embeddings = vgg16(image_f_inputs2, trainable_fe=params.fine_tune_fe, trainable_top=params.fine_tune_top, dropout_keep=cnn_dropout) if params.fine_tune: features = image_embeddings.fc2 else: features = image_batch # forward pass is expensive, so can use this method to reduce computation if params.num_captions > 1 and params.mode == 'training': # [b_s, 4096] features_tiled = tf.tile(tf.expand_dims(features, 1), [1, params.num_captions, 1]) features = tf.reshape(features_tiled, [ tf.shape(features)[0] * params.num_captions, params.cnn_feature_size ]) # [5 * b_s, 4096] # dictionary cap_dict = data.dictionary params.vocab_size = cap_dict.vocab_size # image features [b_size + f_size(4096)] -> [b_size + embed_size] images_fv = layers.dense(features, params.embed_size, name='imf_emb') # images_fv = tf.Print(images_fv, [tf.shape(features), features[0][0:10], # image_embeddings.imgs[0][:10], images_fv]) # encoder, input fv and ...<BOS>,get z if not params.no_encoder: encoder = Encoder(images_fv, cap_enc, cap_len, params) # decoder, input_fv, get x, x_logits (for generation) decoder = Decoder(images_fv, cap_dec, cap_len, params, cap_dict) if params.use_c_v or (params.prior == 'GMM' or params.prior == 'AG'): # cluster vectors from "Diverse and Accurate Image Description.." paper. # 80 is number of classes, for now hardcoded # for GMM-CVAE must be specified c_i_emb = layers.dense(cl_vectors, params.embed_size, name='cv_emb') # map cluster vectors into embedding space decoder.c_i = c_i_emb decoder.c_i_ph = cl_vectors if not params.no_encoder: encoder.c_i = c_i_emb encoder.c_i_ph = cl_vectors if not params.no_encoder: with tf.variable_scope("encoder"): qz, tm_list, tv_list = encoder.q_net() if params.prior == 'Normal': # kld between normal distributions KL(q, p), see Kingma et.al kld = -0.5 * tf.reduce_mean( tf.reduce_sum( 1 + tf.log(tf.square(qz.distribution.std) + 0.00001) - tf.square(qz.distribution.mean) - tf.square(qz.distribution.std), 1)) elif params.prior == 'GMM': # initialize sigma as constant, mu drawn randomly # TODO: finish GMM loss implementation c_means, c_sigma = init_clusters(params.num_clusters, params.latent_size) decoder.cap_clusters = c_means kld = -0.5 * tf.reduce_mean( tf.reduce_sum( 1 + tf.log(tf.square(qz.distribution.std) + 0.00001) - tf.square(qz.distribution.mean) - tf.square(qz.distribution.std), 1)) elif params.prior == 'AG': c_means, c_sigma = init_clusters(params.num_clusters, params.latent_size) decoder.cap_clusters = c_means kld_clusters = 0.5 + tf.log(qz.distribution.std+ 0.00001)\ - tf.log(c_sigma + 0.00001) - ( tf.square(qz.distribution.mean - tf.matmul( tf.squeeze(c_i), c_means)) + tf.square( qz.distribution.std))/(2*tf.square(c_sigma)+0.0000001) kld = -0.5 * tf.reduce_sum(kld_clusters, 1) with tf.variable_scope("decoder"): if params.no_encoder: dec_model, x_logits, shpe, _ = decoder.px_z_fi({}) else: dec_model, x_logits, shpe, _ = decoder.px_z_fi({'z': qz}) # calculate rec. loss, mask padded part labels_flat = tf.reshape(cap_enc, [-1]) ce_loss_padded = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=x_logits, labels=labels_flat) loss_mask = tf.sign(tf.to_float(labels_flat)) batch_loss = tf.div(tf.reduce_sum(tf.multiply(ce_loss_padded, loss_mask)), tf.reduce_sum(loss_mask), name="batch_loss") tf.losses.add_loss(batch_loss) rec_loss = tf.losses.get_total_loss() # kld weight annealing anneal = tf.placeholder_with_default(0, []) if params.fine_tune or params.restore: annealing = tf.constant(1.0) else: if params.ann_param > 1: annealing = (tf.tanh( (tf.to_float(anneal) - 1000 * params.ann_param) / 1000) + 1) / 2 else: annealing = tf.constant(1.0) # overall loss reconstruction loss - kl_regularization if not params.no_encoder: lower_bound = rec_loss + tf.multiply(tf.to_float(annealing), tf.to_float(kld)) / 10 else: lower_bound = rec_loss kld = tf.constant(0.0) # optimization, can print global norm for debugging optimize, global_step, global_norm = optimizers.non_cnn_optimizer( lower_bound, params) optimize_cnn = tf.constant(0.0) if params.fine_tune and params.mode == 'training': optimize_cnn, _ = optimizers.cnn_optimizer(lower_bound, params) # cnn parameters update # model restore vars_to_save = tf.trainable_variables() if not params.fine_tune_fe or not params.fine_tune_top: cnn_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, 'cnn') vars_to_save += cnn_vars saver = tf.train.Saver(vars_to_save, max_to_keep=params.max_checkpoints_to_keep) # m_builder = tf.saved_model.builder.SavedModelBuilder('./saved_model') config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run([ tf.global_variables_initializer(), tf.local_variables_initializer() ]) # train using batch generator, every iteration get # f(I), [batch_size, max_seq_len], seq_lengths if params.mode == "training": if params.logging: summary_writer = tf.summary.FileWriter(params.LOG_DIR, sess.graph) summary_writer.add_graph(sess.graph) if not params.restore: print("Loading imagenet weights for futher usage") image_embeddings.load_weights(params.image_net_weights_path, sess) if params.restore: print("Restoring from checkpoint") saver.restore( sess, "./checkpoints/{}.ckpt".format(params.checkpoint)) for e in range(params.num_epochs): gs = tf.train.global_step(sess, global_step) gs_epoch = 0 while True: def stop_condition(): num_examples = gs_epoch * params.batch_size if num_examples > params.num_ex_per_epoch: return True return False for f_images_batch,\ captions_batch, cl_batch, c_v in batch_gen.next_batch( use_obj_vectors=params.use_c_v, num_captions=params.num_captions): if params.num_captions > 1: captions_batch, cl_batch, c_v = preprocess_captions( captions_batch, cl_batch, c_v) feed = { image_f_inputs: f_images_batch, ann_inputs_enc: captions_batch[1], ann_inputs_dec: captions_batch[0], ann_lengths: cl_batch, anneal: gs } if params.use_c_v or (params.prior == 'GMM' or params.prior == 'AG'): feed.update({c_i: c_v[:, 1:]}) gs = tf.train.global_step(sess, global_step) feed.update({anneal: gs}) # if gs_epoch == 0: # print(sess.run(debug_print, feed)) kl, rl, lb, _, _, ann = sess.run([ kld, rec_loss, lower_bound, optimize, optimize_cnn, annealing ], feed) gs_epoch += 1 if gs % 500 == 0: print("Epoch: {} Iteration: {} VLB: {} " "Rec Loss: {}".format( e, gs, np.mean(lb), rl)) if not params.no_encoder: print("Annealing coefficient:" "{} KLD: {}".format(ann, np.mean(kl))) if stop_condition(): break if stop_condition(): break print("Epoch: {} Iteration: {} VLB: {} Rec Loss: {}".format( e, gs, np.mean(lb), rl, )) def validate(): val_rec = [] for f_images_batch, captions_batch, cl_batch, c_v in val_gen.next_batch( use_obj_vectors=params.use_c_v, num_captions=params.num_captions): gs = tf.train.global_step(sess, global_step) if params.num_captions > 1: captions_batch, cl_batch, c_v = preprocess_captions( captions_batch, cl_batch, c_v) feed = { image_f_inputs: f_images_batch, ann_inputs_enc: captions_batch[1], ann_inputs_dec: captions_batch[0], ann_lengths: cl_batch, anneal: gs } if params.use_c_v or (params.prior == 'GMM' or params.prior == 'AG'): feed.update({c_i: c_v[:, 1:]}) rl = sess.run([rec_loss], feed_dict=feed) val_rec.append(rl) print("Validation reconstruction loss: {}".format( np.mean(val_rec))) print("-----------------------------------------------") validate() # save model if not os.path.exists("./checkpoints"): os.makedirs("./checkpoints") save_path = saver.save( sess, "./checkpoints/{}.ckpt".format(params.checkpoint)) print("Model saved in file: %s" % save_path) # builder.add_meta_graph_and_variables(sess, ["main_model"]) if params.use_hdf5 and params.fine_tune: batch_gen.h5f.close() # run inference if params.mode == "inference": inference.inference(params, decoder, val_gen, test_gen, image_f_inputs, saver, sess)
def __init__(self, name): with tf.variable_scope(name): self.GAMMA = 0.99 self.nActions = 3 self.state_shape = [80, 80, 1] self.state = tf.placeholder(tf.float32, [None, *self.state_shape], name='state') net = layers.conv2d(self.state, 32, 8, 4, activation=tf.nn.relu) net = layers.conv2d(net, 64, 4, 2, activation=tf.nn.relu) net = layers.conv2d(net, 64, 3, 1, activation=tf.nn.relu) net = tf.contrib.layers.flatten(net) net = layers.dense(net, 512, activation=tf.nn.relu) #Use Shared network to define actor logtis actor_logits = layers.dense(net, self.nActions, activation=None, name='actor_logits') self.action_prob = tf.nn.softmax(actor_logits, name='action_prob') #Use Shared network to define critic value self.critic_value = tf.squeeze( layers.dense(net, 1, activation=None, name='critic_value')) self.actions = tf.placeholder(tf.int32, [None], name='actions') self.advantage = tf.placeholder(tf.float32, [None], name='advantage') self.Return = tf.placeholder(tf.float32, [None], name='Return') action_onehot = tf.one_hot(self.actions, self.nActions, name="action_onehot") single_action_prob = tf.reduce_mean(self.action_prob * action_onehot, axis=1) #Create actor Loss entropy = tf.reduce_sum(-self.action_prob * tf.log(self.action_prob + 1e-7), axis=1) log_action_prob = tf.log(single_action_prob + 1e-7) actor_loss = -tf.reduce_mean(log_action_prob * self.advantage + entropy * 0.005) #Create Critic loss critic_loss = tf.losses.mean_squared_error( labels=self.Return, predictions=self.critic_value) #define Total Loss self.total_loss = actor_loss + critic_loss * 0.5 self.optimizer = tf.train.RMSPropOptimizer(learning_rate=1e-3, decay=0.99) var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=name) self.gradients = self.optimizer.compute_gradients( self.total_loss, var_list) self.gradients_placeholders = [] for grad, var in self.gradients: self.gradients_placeholders.append( (tf.placeholder(var.dtype, shape=var.get_shape()), var)) self.apply_gradients = self.optimizer.apply_gradients( self.gradients_placeholders) self.graph_var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=name)
def get_q_values_op(self, state, scope, reuse=False): """ Returns Q values for all actions Args: state: (tf tensor) shape = (batch_size, img height, img width, nchannels) scope: (string) scope name, that specifies if target network or not reuse: (bool) reuse of variables in the scope Returns: out: (tf tensor) of shape = (batch_size, num_actions) """ # this information might be useful num_actions = self.env.action_space.n ############################################################## """ TODO: implement the computation of Q values like in the paper https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf https://www.cs.toronto.edu/~vmnih/docs/dqn.pdf you may find the section "model architecture" of the appendix of the nature paper particulary useful. store your result in out of shape = (batch_size, num_actions) HINT: - You may find the following functions useful: - tf.layers.conv2d - tf.layers.flatten - tf.layers.dense - Make sure to also specify the scope and reuse """ ############################################################## ################ YOUR CODE HERE - 10-15 lines ################ with tf.variable_scope(scope, reuse=reuse) as _: conv1 = layers.conv2d(state, filters=32, kernel_size=8, strides=4, activation=tf.nn.relu) conv2 = layers.conv2d(conv1, filters=64, kernel_size=4, strides=2, activation=tf.nn.relu) conv3 = layers.conv2d(conv2, filters=64, kernel_size=3, strides=1, activation=tf.nn.relu) flatten = layers.flatten(conv3) fc1 = layers.dense(flatten, units=512, activation=tf.nn.relu) out = layers.dense(fc1, units=num_actions) pass ############################################################## ######################## END YOUR CODE ####################### return out
def generator(n_samples, noise_with_labels, reuse=None): """ :param n_samples: number of samples :param noise_with_labels: latent noise + labels :return: generated images """ lod_in = tf.cast( tf.get_variable('lod', initializer=np.float32(0.0), trainable=False), 'float32') # (if image size is a power of 2 --> you can: n_filter = image_res/n_filter) # get number of layers and filters n_conv_layer = int(np.ceil(np.log2(resolution_image / size_init))) n_filters = int(2**(n_conv_layer - 1)) print(' G: n-conv layer generator: ', n_conv_layer) print(' G: n-filters generator: ', n_filters) with tf.variable_scope( 'Generator', reuse=tf.AUTO_REUSE): # Needed for later, in order to # get variables of discriminator # ----- Layer1, Dense, Batch, Leaky ----- # print(' G: units dense generator: ', channels * (size_init * size_init) * (n_filters * const_filt)) output = layers.dense(inputs=noise_with_labels, units=channels * (size_init * size_init) * (n_filters * const_filt)) output = layers.batch_normalization(output) output = tf.maximum(leakage * output, output) print(' G: dense layer') print(output) if channel_first_gen: # size: 128 x 7 x 7 output = tf.reshape( output, (-1, n_filters * const_filt * channels, size_init, size_init)) bn_axis = 1 # [0, 2, 3] # first else: # size: 7 x 7 x 128 output = tf.reshape( output, (-1, size_init, size_init, n_filters * const_filt * channels)) bn_axis = -1 # [0, 1, 2] # last print(' G: channel reshape:') print(output) # ----- LoopLayers, deConv, Batch, Leaky ----- # img_out_list = [] for i in range(n_conv_layer): if resolution_image == 28 and size_init * (1 + i) == 8: if channel_first_gen: output = output[:, :, :7, :7] else: output = output[:, :7, :7, :] print(' G: cut mnist, iteration: ', i) print(output) print(' G: conv2d_transpose iter', i, ' - tot filters: ', n_filters * const_filt * channels, ' - n_filters: ', n_filters) output = layers.conv2d_transpose(output, filters=n_filters * const_filt * channels, kernel_size=kernel_size, strides=strides, padding='same') print(output) output = layers.batch_normalization(output, axis=bn_axis) output = tf.maximum(leakage * output, output) # relu n_filters = int(n_filters / 2) # ----------------------------------------------------------------------------- # KARRAS img_new = toRGB(output) if i == 0: img_out = img_new else: img_out = upscale2d(img_out) with tf.variable_scope('Layer%d' % i): img_out = lerp_clip(img_new, img_out, lod_in - i) img_out_list.append(img_out) return img_out, img_out_list
# experiment with small datasets trainX = trainX[:1000] trainY = trainY[:1000] for neuron_count in neurons: tf.reset_default_graph() with tf.Session() as sess: # Create the model lr = tf.placeholder(tf.float32, []) x = tf.placeholder(tf.float32, [None, NUM_FEATURES]) y_ = tf.placeholder(tf.float32, [None, 1]) # Build the graph for the deep net hidden_layer = layers.dense(x, neuron_count, activation=tf.nn.relu, kernel_initializer=init.orthogonal(np.sqrt(2)), bias_initializer=init.zeros(), kernel_regularizer=l2_regularizer(1e-3)) output_layer = layers.dense(hidden_layer, 1, kernel_initializer=init.orthogonal(), bias_initializer=init.zeros(), kernel_regularizer=l2_regularizer(1e-3)) loss = tf.reduce_mean(tf.square(y_ - output_layer) ) + get_regularization_loss() # Create the gradient descent optimizer with the given learning rate. optimizer = tf.train.GradientDescentOptimizer(lr) train_op = optimizer.minimize(loss) error = tf.reduce_mean(tf.square(y_ - output_layer)) fold_errors = [] for o in range(NUM_FOLDS): sess.run(tf.global_variables_initializer())
def discriminator(images, lod_in=1, reuse=None): """ :param images: images that are input of the discriminator :return: likeliness of the image """ lod_in = tf.cast( tf.get_variable('lod', initializer=np.float32(0.0), trainable=False), 'float32') if channel_first_disc == True: channels_key = 'channels_first' else: channels_key = 'channels_last' n_conv_layer = int(np.ceil(np.log2(resolution_image / size_init))) n_filters = 1 print(' D: n-conv layer discriminator: ', n_conv_layer) print(' D: n-filters discriminator: ', n_filters) with tf.variable_scope( 'Discriminator', reuse=tf.AUTO_REUSE): # Needed for later, in order to # get variables of generator print(' D: input') print(images) if channel_first_disc: print('channel first disc ON') output = tf.reshape( images, [-1, channels, resolution_image, resolution_image]) else: output = tf.reshape( images, [-1, resolution_image, resolution_image, channels]) print(' D: channel reshape') print(output) # ----- LoopLayers, Conv, Leaky ----- # img = output output = fromRGB(output, n_filters) for i in range(n_conv_layer - 1, -1, -1): print(' D: conv2d iter: ', i, ' - n_filters: ', n_filters) output = layers.conv2d(output, filters=n_filters * const_filt, kernel_size=kernel_size, strides=strides, padding='same', data_format=channels_key) print(output) output = tf.maximum(leakage * output, output) n_filters = int(n_filters * 2) x = output img = downscale2d(img) y = fromRGB(img, i - 1) with tf.variable_scope('Layer%d' % i): x = lerp_clip(x, y, lod_in - i) output = x # ---------------------------------------------------------- output = tf.reshape( output, [-1, size_init * size_init * (int(n_filters / 2) * const_filt)]) print(' D: reshape linear layer') print(output) # ----- Layer4, Dense, Linear ----- # output = layers.dense(output, units=num_labels + 1) print(' D: dense layer output') print(output) scores_out = tf.identity(output[:, :1], name='scores_out') labels_out = tf.identity(output[:, 1:], name='labels_out') print(' D: scores output') print(scores_out) print(' D: labels output') print(labels_out) return scores_out, labels_out
def ICM(self): # Input s_current = tf.placeholder(tf.float32, shape = [None, self.img_size, self.img_size, self.Num_stacking * self.Num_colorChannel]) s_current = (s_current - (255.0/2)) / (255.0/2) s_next = tf.placeholder(tf.float32, shape = [None, self.img_size, self.img_size, self.Num_stacking * self.Num_colorChannel]) s_next = (s_next - (255.0/2)) / (255.0/2) with tf.variable_scope('curiosity'): # Convolution variables w_conv1 = self.conv_weight_variable('_w_conv1', [3,3,self.Num_stacking * self.Num_colorChannel,32]) b_conv1 = self.bias_variable('_b_conv1',[32]) w_conv2 = self.conv_weight_variable('_w_conv2', [3,3,32,32]) b_conv2 = self.bias_variable('_b_conv2',[32]) w_conv3 = self.conv_weight_variable('_w_conv3', [3,3,32,32]) b_conv3 = self.bias_variable('_b_conv3',[32]) w_conv4 = self.conv_weight_variable('_w_conv4', [3,3,32,32]) b_conv4 = self.bias_variable('_b_conv4',[32]) # Feature Vector s_conv1 = tf.nn.elu(self.conv2d(s_current, w_conv1, 2) + b_conv1) s_conv2 = tf.nn.elu(self.conv2d(s_conv1, w_conv2, 2) + b_conv2) s_conv3 = tf.nn.elu(self.conv2d(s_conv2, w_conv3, 2) + b_conv3) s_conv4 = tf.nn.elu(self.conv2d(s_conv3, w_conv4, 2) + b_conv4) s_conv4_flat_dim = s_conv4.shape[1]*s_conv4.shape[2]*s_conv4.shape[3] s_conv4_flat = tf.reshape(s_conv4, [tf.shape(s_conv4)[0], s_conv4_flat_dim]) s_next_conv1 = tf.nn.elu(self.conv2d(s_next, w_conv1, 2) + b_conv1) s_next_conv2 = tf.nn.elu(self.conv2d(s_next_conv1, w_conv2, 2) + b_conv2) s_next_conv3 = tf.nn.elu(self.conv2d(s_next_conv2, w_conv3, 2) + b_conv3) s_next_conv4 = tf.nn.elu(self.conv2d(s_next_conv3, w_conv4, 2) + b_conv4) s_next_conv4_flat_dim = s_next_conv4.shape[1]*s_next_conv4.shape[2]*s_next_conv4.shape[3] s_next_conv4_flat = tf.reshape(s_next_conv4, [tf.shape(s_next_conv4)[0], s_next_conv4_flat_dim]) # Forward Model a_t = tf.placeholder(tf.float32, shape = [None, self.Num_action]) input_forward = tf.concat([s_conv4_flat, a_t], 1) forward_fc1 = layer.dense(input_forward, 256, activation=tf.nn.relu) forward_fc1 = tf.concat([forward_fc1, a_t], 1) forward_fc2 = layer.dense(forward_fc1, s_next_conv4_flat.shape[1], activation=None) r_i = (self.eta * 0.5) * tf.reduce_sum(tf.square(tf.subtract(forward_fc2, s_next_conv4_flat)), axis = 1) Lf = tf.losses.mean_squared_error(forward_fc2, s_next_conv4_flat) # Inverse Model input_inverse = tf.concat([s_conv4_flat, s_next_conv4_flat], 1) inverse_fc1 = layer.dense(input_inverse, 256, activation=tf.nn.relu) inverse_fc2 = layer.dense(inverse_fc1, self.Num_action, activation=tf.nn.softmax) Li = tf.losses.softmax_cross_entropy(a_t, inverse_fc2) return s_current, s_next, a_t, r_i, Lf, Li
def make_network(self, input_opr, name, train=True, reuse=False, batch_size=0): w_reg = tf.contrib.layers.l2_regularizer(self.L2_REG) with tf.variable_scope(name, reuse=reuse): if self.greyscale: input_opr = tf.image.rgb_to_grayscale(input_opr) conv1 = tf.layers.conv2d(inputs=input_opr, filters=8, kernel_size=4, strides=2, activation=tf.nn.relu6, trainable=train) conv2 = tf.layers.conv2d(inputs=conv1, filters=16, kernel_size=3, strides=2, activation=tf.nn.relu6, trainable=train) conv3 = tf.layers.conv2d(inputs=conv2, filters=32, kernel_size=3, strides=2, activation=tf.nn.relu6, trainable=train) conv4 = tf.layers.conv2d(inputs=conv3, filters=64, kernel_size=3, strides=2, activation=tf.nn.relu6, trainable=train) conv5 = tf.layers.conv2d(inputs=conv4, filters=128, kernel_size=3, strides=1, activation=tf.nn.relu6, trainable=train) conv6 = tf.layers.conv2d(inputs=conv5, filters=256, kernel_size=3, strides=1, activation=tf.nn.relu6, trainable=train) flatten = layers.flatten(inputs=conv6) fc1 = layers.dense(flatten, units=self.num_unit, activation=tf.nn.relu6, trainable=train, kernel_regularizer=w_reg) fc2 = layers.dense(fc1, units=self.num_unit, activation=tf.nn.relu6, trainable=train, kernel_regularizer=w_reg) value_out = self.value_output_layer(fc1, train, w_reg) if self.is_continuous: policy_out = self.continuous_policy_output_layer( fc2, train, w_reg) else: policy_out = self.discrete_policy_output_layer( fc2, train, w_reg) params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=name) return value_out, policy_out, params, None, None
def encoder(self, X, verbose=True): """ Draws the encoder of the network """ enc_net = [ tf.reshape( X, [self.batch_size, self.dims[0], self.dims[1], self.dims[2]]) ] for lay_num, (filters, kernel_size, stride) in enumerate(self.encoder_dims): if kernel_size > 0: # if this is a convolutional layer if lay_num == len( self.encoder_dims) - 1: # if this is the last layer enc_net.append( tf.contrib.layers.flatten( layers.conv2d(enc_net[len(enc_net) - 1], filters=filters, kernel_size=kernel_size, strides=stride, padding='same', name='enc_' + str(lay_num), activation=self.default_activation))) else: if self.encoder_dims[lay_num + 1][1] == 0: # flatten this layer enc_net.append( tf.contrib.layers.flatten( layers.conv2d( enc_net[len(enc_net) - 1], filters=filters, kernel_size=kernel_size, strides=stride, padding='same', name='enc_' + str(lay_num), activation=self.default_activation))) else: enc_net.append( layers.conv2d(enc_net[len(enc_net) - 1], filters=filters, kernel_size=kernel_size, strides=stride, padding='same', name='enc_' + str(lay_num), activation=self.default_activation)) else: enc_net.append( layers.dense(enc_net[len(enc_net) - 1], units=filters, name='enc_' + str(lay_num), activation=self.default_activation)) enc_shapes = [shape(i) for i in enc_net] # append latent layer if self.latent_loss == 'VAE': z_log_sigma_sq = layers.dense(enc_net[len(enc_net) - 1], units=self.hidden_size, activation=None, name='z_log_sigma_squared') else: z_log_sigma_sq = 0 enc_net.append( layers.dense(enc_net[len(enc_net) - 1], units=self.hidden_size, activation=None, name='latent_layer')) # 32, 2 if verbose: print('Encoder shapes: ', enc_shapes) return enc_net, enc_shapes, z_log_sigma_sq
def generator(n_samples, noise_with_labels, reuse=None): """ :param n_samples: number of samples :param noise_with_labels: latent noise + labels :return: generated images """ # (if image size is a power of 2 --> you can: n_filter = image_res/n_filter) # get number of layers and filters n_conv_layer = int(np.ceil(np.log2(resolution_image / size_init))) n_filters = int(2 ** (n_conv_layer - 1)) print(' G: n-conv layer generator: ', n_conv_layer) print(' G: n-filters generator: ', n_filters) with tf.variable_scope('Generator', reuse=tf.AUTO_REUSE): # Needed for later, in order to # get variables of discriminator # ----- Layer1, Dense, Batch, Leaky ----- # print(' G: units dense generator: ', channels * (size_init * size_init) * (n_filters * const_filt)) output = layers.dense(inputs=noise_with_labels, units=channels * (size_init * size_init) * (n_filters * const_filt)) output = layers.batch_normalization(output) output = tf.maximum(leakage * output, output) print(' G: dense layer') print(output) if channel_first_gen: # size: 128 x 7 x 7 output = tf.reshape(output, (-1, n_filters * const_filt * channels, size_init, size_init)) bn_axis = 1 # [0, 2, 3] # first else: # size: 7 x 7 x 128 output = tf.reshape(output, (-1, size_init, size_init, n_filters * const_filt * channels)) bn_axis = -1 # [0, 1, 2] # last print(' G: channel reshape:') print(output) # ----- LoopLayers, deConv, Batch, Leaky ----- # karras_gen_out = [] for i in range(n_conv_layer): if resolution_image == 28 and size_init * (1 + i) == 8: if channel_first_gen: output = output[:, :, :7, :7] else: output = output[:, :7, :7, :] print(' G: cut mnist, iteration: ', i) print(output) print(' G: conv2d_transpose iter', i, ' - tot filters: ', n_filters * const_filt * channels, ' - n_filters: ', n_filters) output = layers.conv2d_transpose(output, filters=n_filters * const_filt * channels, kernel_size=kernel_size, strides=strides, padding='same') print(output) output = layers.batch_normalization(output, axis=bn_axis) output = tf.maximum(leakage * output, output) # relu n_filters = int(n_filters / 2) karras_gen_out.append(output) # ----- LastLayer, deConv, Batch, Leaky ----- # print(' G: last conv2d_transpose layer - n filters layer: ', channels) output = layers.conv2d_transpose(output, filters=1 * channels, kernel_size=kernel_size, strides=1, padding='same') print(output) output = tf.nn.tanh(output) output = tf.reshape(output, [-1, OUTPUT_DIM]) print(' G: output reshape') print(output) return output
def __init__(self, state_size, action_size, hidden_layer_size, learning_rate, name): if isinstance(state_size, list) and len(state_size) == 3: self.mode = 'visual' elif isinstance(state_size, int): self.mode = 'vector' self.state_size = state_size self.action_size = action_size self.hidden_layer_size = hidden_layer_size self.name = name with tf.variable_scope(name): if self.mode == 'visual': self.observation = tf.placeholder( tf.float32, shape=[None, *self.state_size], name="critic_observation") self.O1 = layer.flatten( layer.conv2d(self.observation, 64, 3, activation=tf.nn.leaky_relu)) self.action = tf.placeholder(tf.float32, shape=[None, self.action_size], name="critic_action") self.A1 = layer.dense(self.action, self.hidden_layer_size // 2, activation=tf.nn.leaky_relu) self.L1 = tf.concat([self.O1, self.A1], 1) self.L1 = layer.dense(self.L1, self.hidden_layer_size, activation=tf.nn.leaky_relu) self.L2 = layer.dense(self.L1, self.hidden_layer_size, activation=tf.nn.leaky_relu) self.L3 = layer.dense(self.L2, self.hidden_layer_size, activation=tf.nn.leaky_relu) self.value = layer.dense(self.L3, 1, activation=None) elif self.mode == 'vector': self.observation = tf.placeholder( tf.float32, shape=[None, self.state_size], name="critic_observation") self.O1 = layer.dense(self.observation, self.hidden_layer_size // 2, activation=tf.nn.leaky_relu) self.action = tf.placeholder(tf.float32, shape=[None, self.action_size], name="critic_action") self.A1 = layer.dense(self.action, self.hidden_layer_size // 2, activation=tf.nn.leaky_relu) self.L1 = tf.concat([self.O1, self.A1], 1) self.L1 = layer.dense(self.L1, self.hidden_layer_size, activation=tf.nn.leaky_relu) self.L2 = layer.dense(self.L1, self.hidden_layer_size, activation=tf.nn.leaky_relu) self.L3 = layer.dense(self.L2, self.hidden_layer_size, activation=tf.nn.leaky_relu) self.value = layer.dense(self.L3, 1, activation=None) self.true_value = tf.placeholder(tf.float32, name="true_value") self.loss = tf.losses.huber_loss(self.true_value, self.value) self.Adam = tf.train.AdamOptimizer(learning_rate) self.Update = self.Adam.minimize(self.loss) self.trainable_var = tf.get_collection( tf.GraphKeys.TRAINABLE_VARIABLES, self.name)
def f_net(inputs): """ action_num is set 5. :param inputs: :return: """ inputs = inputs[0] inputs = inputs / 128 - 1.0 action_num = 3 l2 = 1e-4 # (350, 350, 3*n) -> () conv1 = layers.conv2d(inputs=inputs, filters=16, kernel_size=(8, 8), strides=1, kernel_regularizer=l2_regularizer(scale=l2), activation=tf.nn.relu, name='conv1') print conv1.shape pool1 = layers.max_pooling2d(inputs=conv1, pool_size=3, strides=4, name='pool1') print pool1.shape conv2 = layers.conv2d(inputs=pool1, filters=16, kernel_size=(5, 5), strides=1, kernel_regularizer=l2_regularizer(scale=l2), activation=tf.nn.relu, name='conv2') print conv2.shape pool2 = layers.max_pooling2d(inputs=conv2, pool_size=3, strides=3, name='pool2') print pool2.shape conv3 = layers.conv2d(inputs=pool2, filters=64, kernel_size=(3, 3), strides=1, kernel_regularizer=l2_regularizer(scale=l2), activation=tf.nn.relu, name='conv3') print conv3.shape pool3 = layers.max_pooling2d( inputs=conv3, pool_size=3, strides=2, name='pool3', ) print pool3.shape depth = pool3.get_shape()[1:].num_elements() inputs = tf.reshape(pool3, shape=[-1, depth]) inputs = tf.stop_gradient(inputs) print inputs.shape hid1 = layers.dense(inputs=inputs, units=256, activation=tf.nn.relu, kernel_regularizer=l2_regularizer(scale=l2), name='hid1') print hid1.shape hid2 = layers.dense(inputs=hid1, units=256, activation=tf.nn.relu, kernel_regularizer=l2_regularizer(scale=l2), name='hid2') print hid2.shape pi = layers.dense(inputs=hid2, units=action_num, activation=tf.nn.softmax, kernel_regularizer=l2_regularizer(scale=l2), name='pi') pi = tf.stop_gradient(pi) q = layers.dense(inputs=hid2, units=action_num, kernel_regularizer=l2_regularizer(scale=l2), name='q') return {"pi": pi, "q": q}