我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.split()。
def conv(input, kernel, biases, k_h, k_w, c_o, s_h, s_w, padding="VALID", group=1): '''From https://github.com/ethereon/caffe-tensorflow ''' c_i = input.get_shape()[-1] assert c_i%group==0 assert c_o%group==0 convolve = lambda i, k: tf.nn.conv2d(i, k, [1, s_h, s_w, 1], padding=padding) if group==1: conv = convolve(input, kernel) else: input_groups = tf.split(3, group, input) kernel_groups = tf.split(3, group, kernel) output_groups = [convolve(i, k) for i,k in zip(input_groups, kernel_groups)] conv = tf.concat(3, output_groups) return tf.reshape(tf.nn.bias_add(conv, biases), [-1]+conv.get_shape().as_list()[1:])
def __call__(self, left_state, right_state, extra_input=None): with tf.variable_scope('TreeLSTM'): c1, h1 = left_state c2, h2 = right_state if extra_input is not None: input_concat = tf.concat((extra_input, h1, h2), axis=1) else: input_concat = tf.concat((h1, h2), axis=1) concat = tf.layers.dense(input_concat, 5 * self._num_cells) i, f1, f2, o, g = tf.split(concat, 5, axis=1) i = tf.sigmoid(i) f1 = tf.sigmoid(f1) f2 = tf.sigmoid(f2) o = tf.sigmoid(o) g = tf.tanh(g) cnew = f1 * c1 + f2 * c2 + i * g hnew = o * cnew newstate = LSTMStateTuple(c=cnew, h=hnew) return hnew, newstate
def conv(input, kernel, biases, k_h, k_w, c_o, s_h, s_w, padding="VALID", group=1): '''From https://github.com/ethereon/caffe-tensorflow ''' c_i = input.get_shape()[-1] assert c_i % group == 0 assert c_o % group == 0 convolve = lambda i, k: tf.nn.conv2d(i, k, [1, s_h, s_w, 1], padding=padding) if group == 1: conv = convolve(input, kernel) else: input_groups = tf.split(3, group, input) kernel_groups = tf.split(3, group, kernel) output_groups = [convolve(i, k) for i, k in zip(input_groups, kernel_groups)] conv = tf.concat(3, output_groups) return tf.reshape(tf.nn.bias_add(conv, biases), [-1] + conv.get_shape().as_list()[1:])
def __call__(self, inputs, state, scope=None): """Long short-term memory cell (LSTM).""" with tf.variable_scope(scope or type(self).__name__): c, h = state # Parameters of gates are concatenated into one multiply for efficiency. concat = rnn_ops.linear([inputs, h], 4 * self._num_units, True) # i = input_gate, j = new_input, f = forget_gate, o = output_gate i, j, f, o = tf.split(value=concat, num_or_size_splits=4, axis=1) if self._layer_norm: i = rnn_ops.layer_norm(i, name="i") j = rnn_ops.layer_norm(j, name="j") f = rnn_ops.layer_norm(f, name="f") o = rnn_ops.layer_norm(o, name="o") new_c = (c * tf.sigmoid(f + self._forget_bias) + tf.sigmoid(i) * self._activation(j)) new_h = self._activation(new_c) * tf.sigmoid(o) new_state = tf.contrib.rnn.LSTMStateTuple(new_c, new_h) return new_h, new_state
def baseline_forward(self, X, size, n_class): shape = X.get_shape() _X = tf.transpose(X, [1, 0, 2]) # batch_size x sentence_length x word_length -> batch_size x sentence_length x word_length _X = tf.reshape(_X, [-1, int(shape[2])]) # (batch_size x sentence_length) x word_length seq = tf.split(0, int(shape[1]), _X) # sentence_length x (batch_size x word_length) with tf.name_scope("LSTM"): lstm_cell = rnn_cell.BasicLSTMCell(size, forget_bias=1.0) outputs, states = rnn.rnn(lstm_cell, seq, dtype=tf.float32) with tf.name_scope("LSTM-Classifier"): W = tf.Variable(tf.random_normal([size, n_class]), name="W") b = tf.Variable(tf.random_normal([n_class]), name="b") output = tf.matmul(outputs[-1], W) + b return output
def _create(self): # Concat bridge inputs on the depth dimensions bridge_input = nest.map_structure( lambda x: tf.reshape(x, [self.batch_size, _total_tensor_depth(x)]), self._bridge_input) bridge_input_flat = nest.flatten([bridge_input]) bridge_input_concat = tf.concat(bridge_input_flat, 1) state_size_splits = nest.flatten(self.decoder_state_size) total_decoder_state_size = sum(state_size_splits) # Pass bridge inputs through a fully connected layer layer initial_state_flat = tf.contrib.layers.fully_connected( inputs=bridge_input_concat, num_outputs=total_decoder_state_size, activation_fn=self._activation_fn) # Shape back into required state size initial_state = tf.split(initial_state_flat, state_size_splits, axis=1) return nest.pack_sequence_as(self.decoder_state_size, initial_state)
def loss(c_fuse, s_fuse, labels): """Add L2Loss to all the trainable variables. Add summary for "Loss" and "Loss/avg". Args: c_fuse: Contours output map from inference(). s_fuse: Segments output map from inference(). labels: Labels from distorted_inputs or inputs(). Returns: Loss tensor of type float. """ # Calculate the average cross entropy loss across the batch. # Split the labels tensor into contours and segments image tensors # Each has shape [FLAGS.batch_size, 696, 520, 1] contours_labels, segments_labels = tf.split(labels, 2, 3) _add_cross_entropy(contours_labels, c_fuse, 'c') _add_cross_entropy(segments_labels, s_fuse, 's') return tf.add_n(tf.get_collection('losses'), name='total_loss')
def get_show_preds(c_fuse, s_fuse): """Compute and view logits. Args: c_fuse: Contours fuse layer. s_fuse: Segments fuse layer. Returns: c_logits: Softmax applied to contours fuse layer. s_logits: Softmax applied to segments fuse layer. """ # Index 1 of fuse layers correspond to foreground, so discard index 0. _, c_logits = tf.split(tf.cast(tf.nn.softmax(c_fuse), tf.float32), 2, 3) _, s_logits = tf.split(tf.cast(tf.nn.softmax(s_fuse), tf.float32), 2, 3) tf.summary.image('c_logits', c_logits) tf.summary.image('s_logits', s_logits) return c_logits, s_logits
def unpack_grad_tuple(gv, gpt): """Unpack a previously packed collection of gradient tensors. Args: gv: A (grad, var) pair to be unpacked. gpt: A GradPackTuple describing the packing operation that produced gv. Returns: A list of (grad, var) pairs corresponding to the values that were originally packed into gv, maybe following subsequent operations like reduction. """ elt_widths = [x.num_elements() for x in gpt.shapes] with tf.device(gv[0][0].device): with tf.name_scope('unpack'): splits = tf.split(gv[0], elt_widths) unpacked_gv = [] for idx, s in enumerate(splits): unpacked_gv.append((tf.reshape(s, gpt.shapes[idx]), gpt.vars[idx])) return unpacked_gv
def get_label_queue(self,batch_size): tf_labels = tf.convert_to_tensor(self.attr.values, dtype=tf.uint8)#0,1 with tf.name_scope('label_queue'): uint_label=tf.train.slice_input_producer([tf_labels])[0] label=tf.to_float(uint_label) #All labels, not just those in causal_model dict_data={sl:tl for sl,tl in zip(self.label_names,tf.split(label,len(self.label_names)))} num_preprocess_threads = max(self.num_worker-3,1) data_batch = tf.train.shuffle_batch( dict_data, batch_size=batch_size, num_threads=num_preprocess_threads, capacity=self.min_queue_examples + 3 * batch_size, min_after_dequeue=self.min_queue_examples, ) return data_batch
def distribute_input_data(data_loader,num_gpu): ''' data_loader is a dictionary of tensors that are fed into our model This function takes that dictionary of n*batch_size dimension tensors and breaks it up into n dictionaries with the same key of tensors with dimension batch_size. One is given to each gpu ''' if num_gpu==0: return {'/cpu:0':data_loader} gpus=get_available_gpus() if num_gpu > len(gpus): raise ValueError('number of gpus specified={}, more than gpus available={}'.format(num_gpu,len(gpus))) gpus=gpus[:num_gpu] data_by_gpu={g:{} for g in gpus} for key,value in data_loader.items(): spl_vals=tf.split(value,num_gpu) for gpu,val in zip(gpus,spl_vals): data_by_gpu[gpu][key]=val return data_by_gpu
def rgb_to_bgr(self, inputs): if True: if True: VGG_MEAN = [103.939, 116.779, 123.68] try: red, green, blue = tf.split(inputs, 3, 3) except: red, green, blue = tf.split(3,3,inputs) #assert red.get_shape().as_list()[1:] == [224, 224, 1] #assert green.get_shape().as_list()[1:] == [224, 224, 1] #assert blue.get_shape().as_list()[1:] == [224, 224, 1] try: bgr = tf.concat([ blue - VGG_MEAN[0], green - VGG_MEAN[1], red - VGG_MEAN[2]], axis=3) except: bgr = tf.concat(3,[ blue - VGG_MEAN[0], green - VGG_MEAN[1], red - VGG_MEAN[2]]) return bgr
def rnn_story(self): """ run rnn for story to get last hidden state input is: story: [batch_size,story_length,embed_size] :return: last hidden state. [batch_size,embed_size] """ # 1.split input to get lists. input_split=tf.split(self.story_embedding,self.story_length,axis=1) #a list.length is:story_length.each element is:[batch_size,1,embed_size] input_list=[tf.squeeze(x,axis=1) for x in input_split] #a list.length is:story_length.each element is:[batch_size,embed_size] # 2.init keys(w_all) and values(h_all) of memory h_all=tf.get_variable("hidden_states",shape=[self.block_size,self.dimension],initializer=self.initializer)# [block_size,hidden_size] w_all=tf.get_variable("keys", shape=[self.block_size,self.dimension],initializer=self.initializer)# [block_size,hidden_size] # 3.expand keys and values to prepare operation of rnn w_all_expand=tf.tile(tf.expand_dims(w_all,axis=0),[self.batch_size,1,1]) #[batch_size,block_size,hidden_size] h_all_expand=tf.tile(tf.expand_dims(h_all,axis=0),[self.batch_size,1,1]) #[batch_size,block_size,hidden_size] # 4. run rnn using input with cell. for i,input in enumerate(input_list): h_all_expand=self.cell(input,h_all_expand,w_all_expand,i) #w_all:[batch_size,block_size,hidden_size]; h_all:[batch_size,block_size,hidden_size] return h_all_expand #[batch_size,block_size,hidden_size]
def gru_forward_word_level(self, embedded_words): """ :param embedded_words:[batch_size*num_sentences,sentence_length,embed_size] :return:forward hidden state: a list.length is sentence_length, each element is [batch_size*num_sentences,hidden_size] """ # split embedded_words embedded_words_splitted = tf.split(embedded_words, self.sequence_length, axis=1) # it is a list,length is sentence_length, each element is [batch_size*num_sentences,1,embed_size] embedded_words_squeeze = [tf.squeeze(x, axis=1) for x in embedded_words_splitted] # it is a list,length is sentence_length, each element is [batch_size*num_sentences,embed_size] # demension_1=embedded_words_squeeze[0].get_shape().dims[0] h_t = tf.ones((self.batch_size * self.num_sentences, self.hidden_size)) #TODO self.hidden_size h_t =int(tf.get_shape(embedded_words_squeeze[0])[0]) # tf.ones([self.batch_size*self.num_sentences, self.hidden_size]) # [batch_size*num_sentences,embed_size] h_t_forward_list = [] for time_step, Xt in enumerate(embedded_words_squeeze): # Xt: [batch_size*num_sentences,embed_size] h_t = self.gru_single_step_word_level(Xt,h_t) # [batch_size*num_sentences,embed_size]<------Xt:[batch_size*num_sentences,embed_size];h_t:[batch_size*num_sentences,embed_size] h_t_forward_list.append(h_t) return h_t_forward_list # a list,length is sentence_length, each element is [batch_size*num_sentences,hidden_size] # backward gru for first level: word level
def gru_backward_word_level(self, embedded_words): """ :param embedded_words:[batch_size*num_sentences,sentence_length,embed_size] :return: backward hidden state:a list.length is sentence_length, each element is [batch_size*num_sentences,hidden_size] """ # split embedded_words embedded_words_splitted = tf.split(embedded_words, self.sequence_length, axis=1) # it is a list,length is sentence_length, each element is [batch_size*num_sentences,1,embed_size] embedded_words_squeeze = [tf.squeeze(x, axis=1) for x in embedded_words_splitted] # it is a list,length is sentence_length, each element is [batch_size*num_sentences,embed_size] embedded_words_squeeze.reverse() # it is a list,length is sentence_length, each element is [batch_size*num_sentences,embed_size] # demension_1=int(tf.get_shape(embedded_words_squeeze[0])[0]) #h_t = tf.ones([self.batch_size*self.num_sentences, self.hidden_size]) h_t = tf.ones((self.batch_size * self.num_sentences, self.hidden_size)) h_t_backward_list = [] for time_step, Xt in enumerate(embedded_words_squeeze): h_t = self.gru_single_step_word_level(Xt, h_t) h_t_backward_list.append(h_t) h_t_backward_list.reverse() #ADD 2017.06.14 return h_t_backward_list # forward gru for second level: sentence level
def gru_forward_sentence_level(self, sentence_representation): """ :param sentence_representation: [batch_size,num_sentences,hidden_size*2] :return:forward hidden state: a list,length is num_sentences, each element is [batch_size,hidden_size] """ # split embedded_words sentence_representation_splitted = tf.split(sentence_representation, self.num_sentences, axis=1) # it is a list.length is num_sentences,each element is [batch_size,1,hidden_size*2] sentence_representation_squeeze = [tf.squeeze(x, axis=1) for x in sentence_representation_splitted] # it is a list.length is num_sentences,each element is [batch_size, hidden_size*2] # demension_1 = int(tf.get_shape(sentence_representation_squeeze[0])[0]) #scalar: batch_size h_t = tf.ones((self.batch_size, self.hidden_size * 2)) # TODO h_t_forward_list = [] for time_step, Xt in enumerate(sentence_representation_squeeze): # Xt:[batch_size, hidden_size*2] h_t = self.gru_single_step_sentence_level(Xt, h_t) # h_t:[batch_size,hidden_size]<---------Xt:[batch_size, hidden_size*2]; h_t:[batch_size, hidden_size*2] h_t_forward_list.append(h_t) return h_t_forward_list # a list,length is num_sentences, each element is [batch_size,hidden_size] # backward gru for second level: sentence level
def gru_backward_sentence_level(self, sentence_representation): """ :param sentence_representation: [batch_size,num_sentences,hidden_size*2] :return:forward hidden state: a list,length is num_sentences, each element is [batch_size,hidden_size] """ # split embedded_words sentence_representation_splitted = tf.split(sentence_representation, self.num_sentences, axis=1) # it is a list.length is num_sentences,each element is [batch_size,1,hidden_size*2] sentence_representation_squeeze = [tf.squeeze(x, axis=1) for x in sentence_representation_splitted] # it is a list.length is num_sentences,each element is [batch_size, hidden_size*2] sentence_representation_squeeze.reverse() # demension_1 = int(tf.get_shape(sentence_representation_squeeze[0])[0]) # scalar: batch_size h_t = tf.ones((self.batch_size, self.hidden_size * 2)) h_t_forward_list = [] for time_step, Xt in enumerate(sentence_representation_squeeze): # Xt:[batch_size, hidden_size*2] h_t = self.gru_single_step_sentence_level(Xt,h_t) # h_t:[batch_size,hidden_size]<---------Xt:[batch_size, hidden_size*2]; h_t:[batch_size, hidden_size*2] h_t_forward_list.append(h_t) h_t_forward_list.reverse() #ADD 2017.06.14 return h_t_forward_list # a list,length is num_sentences, each element is [batch_size,hidden_size]
def split_input(inputs, num_gpus=1): if not isinstance(num_gpus, list): n_gpus = num_gpus else: n_gpus = len(num_gpus) if n_gpus == 1: return [inputs] temp_args = {v: tf.split(inputs[v], axis=0, num_or_size_splits=num_gpus) for v in inputs} list_of_args = [{now_arg: temp_args[now_arg][ind] for now_arg in temp_args} for ind in xrange(n_gpus)] return list_of_args
def conll2modeldata(data): """ Converts the document into a dictionary, with the required format for the model. Args: data: dict with conll string Returns: dict like: { "clusters": [[[1024,1024],[1024,1025]],[[876,876], [767,765], [541,544]]], "doc_key": "nw", "sentences": [["This", "is", "the", "first", "sentence", "."], ["This", "is", "the", "second", "."]], "speakers": [["spk1", "spk1", "spk1", "spk1", "spk1", "spk1"], ["spk2", "spk2", "spk2", "spk2", "spk2"]] } """ conll_str = data['conll_str'] document_state = DocumentState() line_list = conll_str.split('\n') for line in line_list: document = handle_line(line, document_state) if document is not None: model_file = document return model_file
def conv(self, input, kernel, biases, k_h, k_w, c_o, s_h, s_w, padding="VALID", group=1): '''From https://github.com/ethereon/caffe-tensorflow ''' c_i = input.get_shape()[-1] assert c_i%group==0 assert c_o%group==0 convolve = lambda i, k: tf.nn.conv2d(i, k, [1, s_h, s_w, 1], padding=padding) if group==1: conv = convolve(input, kernel) else: #input_groups = tf.split(3, group, input) #kernel_groups = tf.split(3, group, kernel) input_groups = tf.split(input, group, 3) kernel_groups = tf.split(kernel, group, 3) output_groups = [convolve(i, k) for i,k in zip(input_groups, kernel_groups)] #conv = tf.concat(3, output_groups) conv = tf.concat(output_groups, 3) return tf.reshape(tf.nn.bias_add(conv, biases), [-1]+conv.get_shape().as_list()[1:])
def __call__(self, inputs, state, scope=None): num_proj = self._num_units if self._num_proj is None else self._num_proj c_prev = tf.slice(state, [0, 0], [-1, self._num_units]) m_prev = tf.slice(state, [0, self._num_units], [-1, num_proj]) input_size = inputs.get_shape().with_rank(2)[1] if input_size.value is None: raise ValueError("Could not infer input size from inputs.get_shape()[-1]") with tf.variable_scope(type(self).__name__, initializer=self._initializer): # "LSTMCell" # i = input_gate, j = new_input, f = forget_gate, o = output_gate cell_inputs = tf.concat(1, [inputs, m_prev]) lstm_matrix = tf.nn.bias_add(tf.matmul(cell_inputs, self._concat_w), self._b) i, j, f, o = tf.split(1, 4, lstm_matrix) c = tf.sigmoid(f + 1.0) * c_prev + tf.sigmoid(i) * tf.tanh(j) m = tf.sigmoid(o) * tf.tanh(c) if self._num_proj is not None: m = tf.matmul(m, self._concat_w_proj) new_state = tf.concat(1, [c, m]) return m, new_state
def read_labeled_image_list(data_dir, data_list): """Reads txt file containing paths to images and ground truth masks. Args: data_dir: path to the directory with images and masks. data_list: path to the file with lines of the form '/path/to/image /path/to/mask'. Returns: Two lists with all file names for images and masks, respectively. """ f = open(data_list, 'r') images = [] masks = [] shape = [] for line in f: image, mask = line.strip("\n").split(' ') images.append(data_dir + image) shape.append(ndimage.imread(data_dir + image).shape[:2]) masks.append(data_dir + mask) return images, masks, shape
def get_cell(self, prev_keyboard, prev_state_enco): """ a RNN decoder See parent class for arguments details """ axis = 1 # The first dimension is the batch, we split the keys assert prev_keyboard.get_shape()[axis].value == music.NB_NOTES inputs = tf.split(axis, music.NB_NOTES, prev_keyboard) outputs, final_state = tf.nn.seq2seq.rnn_decoder( decoder_inputs=inputs, initial_state=prev_state_enco, cell=self.rnn_cell # TODO: Which loop function (should use prediction) ? : Should take the previous generated input/ground truth (as the global model loop_fct). Need to add a new bool placeholder ) # Is it better to do the projection before or after the packing ? next_keys = [] for output in outputs: next_keys.append(self.project_key(output)) next_keyboard = tf.concat(axis, next_keys) return next_keyboard, final_state
def get_cell(self, prev_keyboard, prev_state): """ a RNN encoder See parent class for arguments details """ prev_state_enco, prev_state_deco = prev_state axis = 1 # The first dimension is the batch, we split the keys assert prev_keyboard.get_shape()[axis].value == music.NB_NOTES inputs = tf.split(axis, music.NB_NOTES, prev_keyboard) _, final_state = tf.nn.rnn( self.rnn_cell, inputs, initial_state=prev_state_deco ) return final_state
def load_embeddings(filename): """Loads embedings, returns weight matrix and dict from words to indices.""" weight_vectors = [] word_idx = {} with codecs.open(filename, encoding='utf-8') as f: for line in f: word, vec = line.split(u' ', 1) word_idx[word] = len(weight_vectors) weight_vectors.append(np.array(vec.split(), dtype=np.float32)) # Annoying implementation detail; '(' and ')' are replaced by '-LRB-' and # '-RRB-' respectively in the parse-trees. word_idx[u'-LRB-'] = word_idx.pop(u'(') word_idx[u'-RRB-'] = word_idx.pop(u')') # Random embedding vector for unknown words. weight_vectors.append(np.random.uniform( -0.05, 0.05, weight_vectors[0].shape).astype(np.float32)) return np.stack(weight_vectors), word_idx
def __call__(self, inputs, state, scope=None): with tf.variable_scope(scope or type(self).__name__): lhs, rhs = state c0, h0 = lhs c1, h1 = rhs concat = tf.contrib.layers.linear( tf.concat([inputs, h0, h1], 1), 5 * self._num_units) # i = input_gate, j = new_input, f = forget_gate, o = output_gate i, j, f0, f1, o = tf.split(value=concat, num_or_size_splits=5, axis=1) j = self._activation(j) if not isinstance(self._keep_prob, float) or self._keep_prob < 1: j = tf.nn.dropout(j, self._keep_prob, seed=self._seed) new_c = (c0 * tf.sigmoid(f0 + self._forget_bias) + c1 * tf.sigmoid(f1 + self._forget_bias) + tf.sigmoid(i) * j) new_h = self._activation(new_c) * tf.sigmoid(o) new_state = tf.contrib.rnn.LSTMStateTuple(new_c, new_h) return new_h, new_state
def test(self, sess, token_ids): # We decode one sentence at a time. token_ids = data_utils.padding(token_ids) target_ids = data_utils.padding([data_utils.GO_ID]) y_ids = data_utils.padding([data_utils.EOS_ID]) encoder_inputs, decoder_inputs, _, _ = data_utils.nextRandomBatch([(token_ids, target_ids, y_ids)], batch_size=1) prediction = sess.run(self.prediction, feed_dict={ self.encoder_inputs: encoder_inputs, self.decoder_inputs: decoder_inputs }) pred_max = tf.arg_max(prediction, 1) # prediction = tf.split(0, self.num_steps, prediction) # # This is a greedy decoder - outputs are just argmaxes of output_logits. # outputs = [int(np.argmax(predict)) for predict in prediction] # # If there is an EOS symbol in outputs, cut them at that point. # if data_utils.EOS_ID in outputs: # outputs = outputs[:outputs.index(data_utils.EOS_ID)] return pred_max.eval()
def bilinear_answer_layer(size, encoded_question, question_length, encoded_support, support_length, support2question, answer2support, is_eval, beam_size=1, max_span_size=10000): """Answer layer for multiple paragraph QA.""" # computing single time attention over question size = encoded_support.get_shape()[-1].value question_state = compute_question_state(encoded_question, question_length) # compute logits hidden = tf.gather(tf.layers.dense(question_state, 2 * size, name="hidden"), support2question) hidden_start, hidden_end = tf.split(hidden, 2, 1) support_mask = misc.mask_for_lengths(support_length) start_scores = tf.einsum('ik,ijk->ij', hidden_start, encoded_support) start_scores = start_scores + support_mask end_scores = tf.einsum('ik,ijk->ij', hidden_end, encoded_support) end_scores = end_scores + support_mask return compute_spans(start_scores, end_scores, answer2support, is_eval, support2question, beam_size, max_span_size)
def __call__(self, inputs, initial_state=None, dtype=tf.float32, sequence_length=None, scope=None): num_gates = 3 if self._with_residual else 2 transformed = tf.layers.dense(inputs, num_gates * self._num_units, bias_initializer=tf.constant_initializer(self._constant_bias)) gates = tf.split(transformed, num_gates, axis=2) forget_gate = tf.sigmoid(gates[1]) transformed_inputs = (1.0 - forget_gate) * gates[0] if self._with_residual: residual_gate = tf.sigmoid(gates[2]) inputs *= (1.0 - residual_gate) new_inputs = tf.concat([inputs, transformed_inputs, forget_gate, residual_gate], axis=2) else: new_inputs = tf.concat([transformed_inputs, forget_gate], axis=2) return self._rnn(new_inputs, initial_state, dtype, sequence_length, scope)
def gated_resnet(x, a=None, h=None, nonlinearity=concat_elu, conv=conv2d, init=False, counters={}, ema=None, dropout_p=0., **kwargs): xs = int_shape(x) num_filters = xs[-1] c1 = conv(nonlinearity(x), num_filters) if a is not None: # add short-cut connection if auxiliary input 'a' is given c1 += nin(nonlinearity(a), num_filters) c1 = nonlinearity(c1) if dropout_p > 0: c1 = tf.nn.dropout(c1, keep_prob=1. - dropout_p) c2 = conv(c1, num_filters * 2, init_scale=0.1) # add projection of h vector if included: conditional generation if h is not None: with tf.variable_scope(get_name('conditional_weights', counters)): hw = get_var_maybe_avg('hw', ema, shape=[int_shape(h)[-1], 2 * num_filters], dtype=tf.float32, initializer=tf.random_normal_initializer(0, 0.05), trainable=True) if init: hw = hw.initialized_value() c2 += tf.reshape(tf.matmul(h, hw), [xs[0], 1, 1, 2 * num_filters]) # Is this 3,2 or 2,3 ? a, b = tf.split(c2, 2, 3) c3 = a * tf.nn.sigmoid(b) return x + c3
def pre(self, inputs, scope=None): """Preprocess inputs to be used by the cell. Assumes [N, J, *] [x, u]""" is_train = self._is_train keep_prob = self._keep_prob gate_size = self._gate_size with tf.variable_scope(scope or "pre"): x, u, _, _ = tf.split(2, 4, tf.slice(inputs, [0, 0, gate_size], [-1, -1, -1])) # [N, J, d] a_raw = linear([x * u], gate_size, True, scope='a_raw', var_on_cpu=self._var_on_cpu, wd=self._wd, initializer=self._initializer) a = tf.sigmoid(a_raw - self._forget_bias, name='a') if keep_prob < 1.0: x = tf.cond(is_train, lambda: tf.nn.dropout(x, keep_prob), lambda: x) u = tf.cond(is_train, lambda: tf.nn.dropout(u, keep_prob), lambda: u) v_t = tf.nn.tanh(linear([x, u], self._num_units, True, var_on_cpu=self._var_on_cpu, wd=self._wd, scope='v_raw'), name='v') new_inputs = tf.concat(2, [a, x, u, v_t]) # [N, J, 3*d + 1] return new_inputs
def __call__(self, inputs, state, scope=None): gate_size = self._gate_size with tf.variable_scope(scope or type(self).__name__): # "RSMCell" with tf.name_scope("Split"): # Reset gate and update gate. a = tf.slice(inputs, [0, 0], [-1, gate_size]) x, u, v_t = tf.split(1, 3, tf.slice(inputs, [0, gate_size], [-1, -1])) o = tf.slice(state, [0, 0], [-1, 1]) h, v = tf.split(1, 2, tf.slice(state, [0, gate_size], [-1, -1])) with tf.variable_scope("Main"): r_raw = linear([x * u], 1, True, scope='r_raw', var_on_cpu=self._var_on_cpu, initializer=self._initializer) r = tf.sigmoid(r_raw, name='a') new_o = a * r + (1 - a) * o new_v = a * v_t + (1 - a) * v g = r * v_t new_h = a * g + (1 - a) * h with tf.name_scope("Concat"): new_state = tf.concat(1, [new_o, new_h, new_v]) outputs = tf.concat(1, [a, r, x, new_h, new_v, g]) return outputs, new_state
def batch_set_value(tuples): '''Sets the values of many tensor variables at once. # Arguments tuples: a list of tuples `(tensor, value)`. `value` should be a Numpy array. ''' if tuples: assign_ops = [] feed_dict = {} for x, value in tuples: value = np.asarray(value) tf_dtype = _convert_string_dtype(x.dtype.name.split('_')[0]) if hasattr(x, '_assign_placeholder'): assign_placeholder = x._assign_placeholder assign_op = x._assign_op else: assign_placeholder = tf.placeholder(tf_dtype, shape=value.shape) assign_op = x.assign(assign_placeholder) x._assign_placeholder = assign_placeholder x._assign_op = assign_op assign_ops.append(assign_op) feed_dict[assign_placeholder] = value get_session().run(assign_ops, feed_dict=feed_dict)
def step(self, hprev, x): if self.layer_normalization: ln = apply_ln(self) x_ru = ln(tf.matmul(x, self.W_x_ru), "x_ru") h_ru = ln(tf.matmul(hprev, self.W_h_ru), "h_ru") x_r, x_u = tf.split(split_dim=1, num_split=2, value=x_ru) h_r, h_u = tf.split(split_dim=1, num_split=2, value=h_ru) x_c = ln(tf.matmul(x, self.W_xc), "x_c") h_c = ln(tf.matmul(hprev, self.W_hc), "h_c") r = self.gate_nonlinearity(x_r + h_r) u = self.gate_nonlinearity(x_u + h_u) c = self.nonlinearity(x_c + r * h_c) h = (1 - u) * hprev + u * c return h else: xb_ruc = tf.matmul(x, self.W_x_ruc) + tf.reshape(self.b_ruc, (1, -1)) h_ruc = tf.matmul(hprev, self.W_h_ruc) xb_r, xb_u, xb_c = tf.split(split_dim=1, num_split=3, value=xb_ruc) h_r, h_u, h_c = tf.split(split_dim=1, num_split=3, value=h_ruc) r = self.gate_nonlinearity(xb_r + h_r) u = self.gate_nonlinearity(xb_u + h_u) c = self.nonlinearity(xb_c + r * h_c) h = (1 - u) * hprev + u * c return h
def create_trunk(self, images): red, green, blue = tf.split(images*255, 3, axis=3) images = tf.concat([blue, green, red], 3) - MEAN_COLOR with slim.arg_scope(resnet_v1.resnet_arg_scope(is_training=self.training, weight_decay=self.weight_decay, batch_norm_decay=args.bn_decay)): blocks = [ resnet_utils.Block( 'block1', bottleneck, [(256, 64, 1)] * 3), resnet_utils.Block( 'block2', bottleneck, [(512, 128, 2)] + [(512, 128, 1)] * 3), resnet_utils.Block( 'block3', bottleneck, [(1024, 256, 2)] + [(1024, 256, 1)] * self.num_block3), resnet_utils.Block( 'block4', bottleneck, [(2048, 512, 2)] + [(2048, 512, 1)] * 2) ] net, endpoints = resnet_v1.resnet_v1(images, blocks, global_pool=False, reuse=self.reuse, scope=self.scope) self.outputs = endpoints self.add_extra_layers(net)
def lstm(xs, ms, s, scope, nh, init_scale=1.0): nbatch, nin = [v.value for v in xs[0].get_shape()] nsteps = len(xs) with tf.variable_scope(scope): wx = tf.get_variable("wx", [nin, nh*4], initializer=ortho_init(init_scale)) wh = tf.get_variable("wh", [nh, nh*4], initializer=ortho_init(init_scale)) b = tf.get_variable("b", [nh*4], initializer=tf.constant_initializer(0.0)) c, h = tf.split(axis=1, num_or_size_splits=2, value=s) for idx, (x, m) in enumerate(zip(xs, ms)): c = c*(1-m) h = h*(1-m) z = tf.matmul(x, wx) + tf.matmul(h, wh) + b i, f, o, u = tf.split(axis=1, num_or_size_splits=4, value=z) i = tf.nn.sigmoid(i) f = tf.nn.sigmoid(f) o = tf.nn.sigmoid(o) u = tf.tanh(u) c = f*c + i*u h = o*tf.tanh(c) xs[idx] = h s = tf.concat(axis=1, values=[c, h]) return xs, s
def _flat_reconstruction_loss(self, flat_x_target, flat_rnn_output): split_x_target = tf.split(flat_x_target, self._output_depths, axis=-1) split_rnn_output = tf.split( flat_rnn_output, self._output_depths, axis=-1) losses = [] truths = [] predictions = [] metric_map = {} for i in range(len(self._output_depths)): l, m, t, p = ( super(MultiOutCategoricalLstmDecoder, self)._flat_reconstruction_loss( split_x_target[i], split_rnn_output[i])) losses.append(l) truths.append(t) predictions.append(p) for k, v in m.items(): metric_map['%s_%d' % (k, i)] = v return (tf.reduce_sum(losses, axis=0), metric_map, tf.stack(truths), tf.stack(predictions))
def decode(roi, deltas): with tf.name_scope('BoundingBoxTransform/decode'): (roi_width, roi_height, roi_urx, roi_ury) = get_width_upright(roi) dx, dy, dw, dh = tf.split(deltas, 4, axis=1) pred_ur_x = dx * roi_width + roi_urx pred_ur_y = dy * roi_height + roi_ury pred_w = tf.exp(dw) * roi_width pred_h = tf.exp(dh) * roi_height bbox_x1 = pred_ur_x - 0.5 * pred_w bbox_y1 = pred_ur_y - 0.5 * pred_h # This -1. extra is different from reference implementation. bbox_x2 = pred_ur_x + 0.5 * pred_w - 1. bbox_y2 = pred_ur_y + 0.5 * pred_h - 1. bboxes = tf.concat( [bbox_x1, bbox_y1, bbox_x2, bbox_y2], axis=1) return bboxes
def _decoder(self, z): """Define p(x|z) network""" if z is None: mean = None stddev = None logits = None class_predictions = None input_sample = self.epsilon else: z = tf.reshape(z, [-1, self.flags['hidden_size'] * 2]) mean, stddev = tf.split(1, 2, z) # Compute latent variables (z) by calculating mean, stddev stddev = tf.sqrt(tf.exp(stddev)) mlp = Layers(mean) mlp.fc(self.flags['num_classes']) class_predictions = mlp.get_output() logits = tf.nn.softmax(class_predictions) input_sample = mean + self.epsilon * stddev decoder = Layers(tf.expand_dims(tf.expand_dims(input_sample, 1), 1)) decoder.deconv2d(3, 128, padding='VALID') decoder.deconv2d(3, 64, padding='VALID', stride=2) decoder.deconv2d(3, 64, stride=2) decoder.deconv2d(5, 32, stride=2) decoder.deconv2d(7, 1, activation_fn=tf.nn.tanh, s_value=None) return decoder.get_output(), mean, stddev, class_predictions, logits
def _decoder(self, z): """ Define p(x|z) network""" if z is None: mean = None stddev = None input_sample = self.epsilon else: z = tf.reshape(z, [-1, self.flags['hidden_size'] * 2]) print(z.get_shape()) mean, stddev = tf.split(1, 2, z) stddev = tf.sqrt(tf.exp(stddev)) input_sample = mean + self.epsilon * stddev decoder = Layers(tf.expand_dims(tf.expand_dims(input_sample, 1), 1)) decoder.deconv2d(3, 128, padding='VALID') decoder.deconv2d(3, 128, padding='VALID', stride=2) decoder.deconv2d(3, 64, stride=2) decoder.deconv2d(3, 64, stride=2) decoder.deconv2d(5, 1, activation_fn=tf.nn.tanh, s_value=None) return decoder.get_output(), mean, stddev
def __call__(self, inputs, state, scope=None): """Long short-term memory cell (LSTM).""" with tf.variable_scope(scope or type(self).__name__): c, h = state # change bias argument to False since LN will add bias via shift concat = tf.nn.rnn_cell._linear( [inputs, h], 4 * self._num_units, False) i, j, f, o = tf.split(1, 4, concat) # add layer normalization to each gate i = ln(i, scope='i/') j = ln(j, scope='j/') f = ln(f, scope='f/') o = ln(o, scope='o/') new_c = (c * tf.nn.sigmoid(f + self._forget_bias) + tf.nn.sigmoid(i) * self._activation(j)) # add layer_normalization in calculation of new hidden state new_h = self._activation( ln(new_c, scope='new_h/')) * tf.nn.sigmoid(o) new_state = tf.nn.rnn_cell.LSTMStateTuple(new_c, new_h) return new_h, new_state
def read_labeled_image_list(data_dir, data_list): """Reads txt file containing paths to images and ground truth masks. Args: data_dir: path to the directory with images and masks. data_list: path to the file with lines of the form '/path/to/image /path/to/mask'. Returns: Two lists with all file names for images and masks, respectively. """ f = open(data_list, 'r') images = [] masks = [] for line in f: image, mask = line.strip("\n").split(' ') images.append(data_dir + image) masks.append(data_dir + mask) return images, masks
def encoder(self, x): with tf.variable_scope('encoder'): net = resnet_utils.conv2d_same(x, 64, 7, stride=2, scope='conv1') net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [0, 0]]) x = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID', scope='pool1') x_features_all, _ = resnet_v1.resnet_v1(x, self._blocks_encoder, global_pool=False, include_root_block=False, scope=self._resnet_scope) x_features_all = tf.reduce_mean(x_features_all, axis=[1, 2]) x_features_labeled, x_features_unlabeled = tf.split(x_features_all, 2) x_features_tiled = tf.tile(x_features_unlabeled, [self._num_classes, 1]) # (100, 256) --> (2100, 256) x_features = tf.concat([x_features_labeled, x_features_tiled], 0) # (2100, 256) --> (2200, 256) return x_features