python类unpack()的实例源码

rot_mnist12K_model.py 文件源码 项目:TI-pooling 作者: dlaptev 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def define_model(x,
                 keep_prob,
                 number_of_classes,
                 number_of_filters,
                 number_of_fc_features):
  splitted = tf.unpack(x, axis=4)
  branches = []
  with tf.variable_scope('branches') as scope:  
    for index, tensor_slice in enumerate(splitted):
      branches.append(single_branch(splitted[index],
                      number_of_filters,
                      number_of_fc_features))
      if (index == 0):
        scope.reuse_variables()
    concatenated = tf.pack(branches, axis=2)
    ti_pooled = tf.reduce_max(concatenated, reduction_indices=[2])
    drop = tf.nn.dropout(ti_pooled, keep_prob)
  with tf.variable_scope('fc2'):
    logits = fc(drop,
                [number_of_fc_features, number_of_classes],
                [number_of_classes])
  return logits
tflearn_seq2seq.py 文件源码 项目:tflearn_seq2seq 作者: ichuang 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def sequence_loss(self, y_pred, y_true):
        '''
        Loss function for the seq2seq RNN.  Reshape predicted and true (label) tensors, generate dummy weights,
        then use seq2seq.sequence_loss to actually compute the loss function.
        '''
        if self.verbose > 2: print ("my_sequence_loss y_pred=%s, y_true=%s" % (y_pred, y_true))
        logits = tf.unpack(y_pred, axis=1)      # list of [-1, num_decoder_synbols] elements
        targets = tf.unpack(y_true, axis=1)     # y_true has shape [-1, self.out_seq_len]; unpack to list of self.out_seq_len [-1] elements
        if self.verbose > 2:
            print ("my_sequence_loss logits=%s" % (logits,))
            print ("my_sequence_loss targets=%s" % (targets,))
        weights = [tf.ones_like(yp, dtype=tf.float32) for yp in targets]
        if self.verbose > 4: print ("my_sequence_loss weights=%s" % (weights,))
        sl = seq2seq.sequence_loss(logits, targets, weights)
        if self.verbose > 2: print ("my_sequence_loss return = %s" % sl)
        return sl
dm_utils.py 文件源码 项目:deep-makeover 作者: david-gpu 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def distort_image(image):
    """Perform random distortions to the given 4D image and return result"""

    # Switch to 3D as that's what these operations require
    slices = tf.unpack(image)
    output = []

    # Perform pixel-wise distortions
    for image in slices:
        image  = tf.image.random_flip_left_right(image)
        image  = tf.image.random_saturation(image, .2, 2.)
        image += tf.truncated_normal(image.get_shape(), stddev=.05)
        image  = tf.image.random_contrast(image, .85, 1.15)
        image  = tf.image.random_brightness(image, .3)

        output.append(image)

    # Go back to 4D
    image   = tf.pack(output)

    return image
helpers.py 文件源码 项目:2048-RL-DRQN 作者: Mostafa-Samir 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def Unroll(axis, num=None):
    """
    defines an _OperationalLayer that unpacks a tensor along a given axis

    Parameters:
    ----------
    axis: int
    num: int
        the numeber if tensors to unpack form the gievn tensor
    Returns: _OperationalLayer
    """

    def unroll_op(obj, X):
        return tf.unpack(X, obj.params[0], 1)

    return _OperationalLayer(unroll_op, [num, axis])
helpers.py 文件源码 项目:2048-RL-DRQN 作者: Mostafa-Samir 项目源码 文件源码 阅读 52 收藏 0 点赞 0 评论 0
def Unroll(axis, num=None):
    """
    defines an _OperationalLayer that unpacks a tensor along a given axis

    Parameters:
    ----------
    axis: int
    num: int
        the numeber if tensors to unpack form the gievn tensor
    Returns: _OperationalLayer
    """

    def unroll_op(obj, X):
        return tf.unpack(X, obj.params[0], 1)

    return _OperationalLayer(unroll_op, [num, axis])
beamsearch.py 文件源码 项目:TextGAN 作者: ankitkv 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def _tile_along_beam(cls, beam_size, state):
        if nest.is_sequence(state):
            return nest_map(
                lambda val: cls._tile_along_beam(beam_size, val),
                state
            )

        if not isinstance(state, tf.Tensor):
            raise ValueError("State should be a sequence or tensor")

        tensor = state

        tensor_shape = tensor.get_shape().with_rank_at_least(1)

        try:
            new_first_dim = tensor_shape[0] * beam_size
        except:
            new_first_dim = None

        dynamic_tensor_shape = tf.unpack(tf.shape(tensor))
        res = tf.expand_dims(tensor, 1)
        res = tf.tile(res, [1, beam_size] + [1] * (tensor_shape.ndims-1))
        res = tf.reshape(res, [-1] + list(dynamic_tensor_shape[1:]))
        res.set_shape([new_first_dim] + list(tensor_shape[1:]))
        return res
dcn.py 文件源码 项目:dcn.tf 作者: beopst 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def extract_patches(inputs, size, offsets):

    batch_size = inputs.get_shape()[0]

    padded = tf.pad(inputs, [[0,0],[2,2],[2,2],[0,0]])
    unpacked = tf.unpack(tf.squeeze(padded))

    extra_margins = tf.constant([1,1,2,2])

    sliced_list = []
    for i in xrange(batch_size.value):

        margins = tf.random_shuffle(extra_margins)
        margins = margins[:2]
        start_pts = tf.sub(offsets[i,:],margins)
        sliced = tf.slice(unpacked[i],start_pts,size)
        sliced_list.append(sliced)

    patches = tf.pack(sliced_list)
    patches = tf.expand_dims(patches,3)

    return patches
char2vec.py 文件源码 项目:twitter_langid 作者: ajaech 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def reverse_seq(input_seq, lengths):
  """Reverse a list of Tensors up to specified lengths.
  Args:
    input_seq: Sequence of seq_len tensors of dimension (batch_size, depth)
    lengths:   A tensor of dimension batch_size, containing lengths for each
               sequence in the batch. If "None" is specified, simply reverses
               the list.
  Returns:
    time-reversed sequence
  """
  for input_ in input_seq:
    input_.set_shape(input_.get_shape().with_rank(2))

  # Join into (time, batch_size, depth)
  s_joined = tf.pack(input_seq)

  # Reverse along dimension 0
  s_reversed = tf.reverse_sequence(s_joined, lengths, 0, 1)
  # Split again into list
  result = tf.unpack(s_reversed)
  return result
tf_seq_lstm.py 文件源码 项目:RecursiveNN 作者: sapruash 项目源码 文件源码 阅读 43 收藏 0 点赞 0 评论 0
def compute_states(self,emb):

        def unpack_sequence(tensor):
            return tf.unpack(tf.transpose(tensor, perm=[1, 0, 2]))


        with tf.variable_scope("Composition",initializer=
                               tf.contrib.layers.xavier_initializer(),regularizer=
                               tf.contrib.layers.l2_regularizer(self.reg)):
            cell = rnn_cell.LSTMCell(self.hidden_dim)
            #tf.cond(tf.less(self.dropout
            #if tf.less(self.dropout, tf.constant(1.0)):
            cell = rnn_cell.DropoutWrapper(cell,
                                           output_keep_prob=self.dropout,input_keep_prob=self.dropout)
            #output, state = rnn.dynamic_rnn(cell,emb,sequence_length=self.lngths,dtype=tf.float32)
            outputs,_=rnn.rnn(cell,unpack_sequence(emb),sequence_length=self.lngths,dtype=tf.float32)
            #output = pack_sequence(outputs)

        sum_out=tf.reduce_sum(tf.pack(outputs),[0])
        sent_rep = tf.div(sum_out,tf.expand_dims(tf.to_float(self.lngths),1))
        final_state=sent_rep
        return final_state
build_resnet_sdc.py 文件源码 项目:tensorflow-litterbox 作者: rwightman 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _build_global_context(
        net,
        is_training=False,
        bayesian=False,
        dropout_keep_prob=0.8):

    with tf.variable_scope('GlobalContext'):
        # Reduce feature dimension before LSTM to reduce param count
        net = slim.conv2d(net, 1024, 1, padding='VALID', scope='conv_reduce_1x1')

        #net = slim.dropout(net, dropout_keep_prob, is_training=bayesian or is_training, scope='Dropout')

        rows = tf.unpack(net, axis=1)
        net = tf.pack(
            [lstm.bidir_lstm(r, 512, scope='row%d' % i) for i, r in enumerate(rows)],
            axis=1)
        print('Horizontal LSTM', net.get_shape())

        cols = tf.unpack(net, axis=2)
        net = tf.pack(
            [lstm.bidir_lstm(r, 512, scope='col%d' % i) for i, r in enumerate(cols)],
            axis=2)
        print('Vertical LSTM', net.get_shape())

    return net
dynamic_m2_model.py 文件源码 项目:diversity_based_attention 作者: PrekshaNema25 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def loss_op(self, outputs, labels, weights, len_vocab):

        """ Calculate the loss from the predicted outputs and the labels

            Args:
                outputs : A list of tensors of size [batch_size * num_symbols]
                labels : A list of tensors of size [sequence_length * batch_size]

            Returns:
                loss: loss of type float
        """

        _labels = tf.unpack(labels)
        all_ones       = [tf.ones(shape=tf.shape(_labels[0])) for _ in range(len(_labels))]
        weights = tf.to_float(weights)
        _weights = tf.unpack(weights)
        #print(_weights[0].get_shape())
        loss_per_batch = sequence_loss(outputs, _labels, _weights)

        self.calculated_loss =  loss_per_batch
        return loss_per_batch
inference_model.py 文件源码 项目:diversity_based_attention 作者: PrekshaNema25 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def loss_op(self, outputs, labels, weights):

        """ Calculate the loss from the predicted outputs and the labels

            Args:
                outputs : A list of tensors of size [batch_size * num_symbols]
                labels : A list of tensors of size [sequence_length * batch_size]

            Returns:
                loss: loss of type float
        """

        _labels = tf.unpack(labels)
        all_ones       = [tf.ones(shape=tf.shape(_labels[0])) for _ in range(len(_labels))]
        weights = tf.to_float(weights)
        _weights = tf.unpack(weights)
        #print(_weights[0].get_shape())
        loss_per_batch = sequence_loss(outputs, _labels, _weights)

        self.calculated_loss =  loss_per_batch
        return loss_per_batch
dynamic_simple_soft_distraction_model.py 文件源码 项目:diversity_based_attention 作者: PrekshaNema25 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def loss_op(self, outputs, labels, weights, len_vocab):

        """ Calculate the loss from the predicted outputs and the labels

            Args:
                outputs : A list of tensors of size [batch_size * num_symbols]
                labels : A list of tensors of size [sequence_length * batch_size]

            Returns:
                loss: loss of type float
        """

        _labels = tf.unpack(labels)
        all_ones       = [tf.ones(shape=tf.shape(_labels[0])) for _ in range(len(_labels))]
        weights = tf.to_float(weights)
        _weights = tf.unpack(weights)
        #print(_weights[0].get_shape())
        loss_per_batch = sequence_loss(outputs, _labels, _weights)

        self.calculated_loss =  loss_per_batch
        return loss_per_batch
dynamic_only_m2_model.py 文件源码 项目:diversity_based_attention 作者: PrekshaNema25 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def loss_op(self, outputs, labels, weights, len_vocab):

        """ Calculate the loss from the predicted outputs and the labels

            Args:
                outputs : A list of tensors of size [batch_size * num_symbols]
                labels : A list of tensors of size [sequence_length * batch_size]

            Returns:
                loss: loss of type float
        """

        _labels = tf.unpack(labels)
        all_ones       = [tf.ones(shape=tf.shape(_labels[0])) for _ in range(len(_labels))]
        weights = tf.to_float(weights)
        _weights = tf.unpack(weights)
        #print(_weights[0].get_shape())
        loss_per_batch = sequence_loss(outputs, _labels, _weights)

        self.calculated_loss =  loss_per_batch
        return loss_per_batch
dynamic_simple_hard_distraction_model.py 文件源码 项目:diversity_based_attention 作者: PrekshaNema25 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def loss_op(self, outputs, labels, weights, len_vocab):

        """ Calculate the loss from the predicted outputs and the labels

            Args:
                outputs : A list of tensors of size [batch_size * num_symbols]
                labels : A list of tensors of size [sequence_length * batch_size]

            Returns:
                loss: loss of type float
        """

        _labels = tf.unpack(labels)
        all_ones       = [tf.ones(shape=tf.shape(_labels[0])) for _ in range(len(_labels))]
        weights = tf.to_float(weights)
        _weights = tf.unpack(weights)
        #print(_weights[0].get_shape())
        loss_per_batch = sequence_loss(outputs, _labels, _weights)

        self.calculated_loss =  loss_per_batch
        return loss_per_batch
dynamic_m1_model.py 文件源码 项目:diversity_based_attention 作者: PrekshaNema25 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def loss_op(self, outputs, labels, weights, len_vocab):

        """ Calculate the loss from the predicted outputs and the labels

            Args:
                outputs : A list of tensors of size [batch_size * num_symbols]
                labels : A list of tensors of size [sequence_length * batch_size]

            Returns:
                loss: loss of type float
        """

        _labels = tf.unpack(labels)
        all_ones       = [tf.ones(shape=tf.shape(_labels[0])) for _ in range(len(_labels))]
        weights = tf.to_float(weights)
        _weights = tf.unpack(weights)
        #print(_weights[0].get_shape())
        loss_per_batch = sequence_loss(outputs, _labels, _weights)

        self.calculated_loss =  loss_per_batch
        return loss_per_batch
Multi-LSTM-bigram-based.py 文件源码 项目:TensorFlowHub 作者: MJFND 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def lstm_cell1(i, o, state):
    """Create a LSTM cell. See e.g.: http://arxiv.org/pdf/1402.1128v1.pdf
    Note that in this formulation, we omit the various connections between the
    previous state and the gates."""    
    m_input2 = tf.pack([i for _ in range(m_rows)])
    m_saved_output2 = tf.pack([o for _ in range(m_rows)])

   # m_input2 = tf.nn.dropout(m_input2, keep_prob)
    m_all = tf.batch_matmul(m_input2, m_input_w2) + tf.batch_matmul(m_saved_output2, m_middle_w2) + m_biases
    m_all = tf.unpack(m_all)

    input_gate = tf.sigmoid(m_all[m_input_index])
    forget_gate = tf.sigmoid(m_all[m_forget_index])
    update = m_all[m_update_index]
    state = forget_gate * state + input_gate * tf.tanh(update)
    output_gate = tf.sigmoid(m_all[m_output_index])

    return output_gate * tf.tanh(state), state

  # Input data.
video_data_utils.py 文件源码 项目:ActionVLAD 作者: rohitgirdhar 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def decoderFn(num_samples=1, modality='rgb'):
  class decoder_func(slim.data_decoder.DataDecoder):
    @staticmethod
    def list_items():
      return ['image', 'label']

    @staticmethod
    def decode(data, items):
      with tf.name_scope('decode_video'):
        if modality == 'rgb':
          data.set_shape((num_samples,))
        elif modality.startswith('flow'):
          optical_flow_frames = int(modality[4:])
          data.set_shape((num_samples, 2 * optical_flow_frames))
        elif modality.startswith('rgb+flow'):
          optical_flow_frames = int(modality[-2:])
          data.set_shape((num_samples, 1 + 2 * optical_flow_frames))
        else:
          logging.error('Unknown modality %s\n' % modality)
        image_buffer = [_decode_from_string(el, modality) for
                        el in tf.unpack(data)]
        # image_buffer = tf.pack(image_buffer)
        return image_buffer
  return decoder_func
model_seg+pos.py 文件源码 项目:tensorflow-CWS-LSTM 作者: elvinpoon 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def unpack_sequence(tensor):
    """Split the single tensor of a sequence into a list of frames."""
    return tf.unpack(tf.transpose(tensor, perm=[1, 0, 2]))
core.py 文件源码 项目:gmn 作者: sbos 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def flatten(output):
        sh = tf.unpack(tf.shape(output))
        batch, output_shape = sh[0], sh[1:]
        flat_shape = 1
        for d in output_shape:
            flat_shape *= d

        return tf.reshape(output, tf.pack([batch, flat_shape]))
tensorflowvisu.py 文件源码 项目:tensorflow-mnist-tutorial 作者: jaskru 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def tf_format_mnist_images(X, Y, Y_, n=100, lines=10):
    correct_prediction = tf.equal(tf.argmax(Y,1), tf.argmax(Y_,1))
    correctly_recognised_indices = tf.squeeze(tf.where(correct_prediction), [1])  # indices of correctly recognised images
    incorrectly_recognised_indices = tf.squeeze(tf.where(tf.logical_not(correct_prediction)), [1]) # indices of incorrectly recognised images
    everything_incorrect_first = tf.concat(0, [incorrectly_recognised_indices, correctly_recognised_indices]) # images reordered with indeces of unrecognised images first
    everything_incorrect_first = tf.slice(everything_incorrect_first, [0], [n]) # compute first 100 only - no space to display more anyway
    # compute n=100 digits to display only
    Xs = tf.gather(X, everything_incorrect_first)
    Ys = tf.gather(Y, everything_incorrect_first)
    Ys_ = tf.gather(Y_, everything_incorrect_first)
    correct_prediction_s = tf.gather(correct_prediction, everything_incorrect_first)

    digits_left = tf.image.grayscale_to_rgb(tensorflowvisu_digits.digits_left())
    correct_tags = tf.gather(digits_left, tf.argmax(Ys_, 1)) # correct digits to be printed on the images
    digits_right = tf.image.grayscale_to_rgb(tensorflowvisu_digits.digits_right())
    computed_tags = tf.gather(digits_right, tf.argmax(Ys, 1)) # computed digits to be printed on the images
    #superimposed_digits = correct_tags+computed_tags
    superimposed_digits = tf.select(correct_prediction_s, tf.zeros_like(correct_tags),correct_tags+computed_tags) # only pring the correct and computed digits on unrecognised images
    correct_bkg   = tf.reshape(tf.tile([1.3,1.3,1.3], [28*28]), [1, 28,28,3]) # white background
    incorrect_bkg = tf.reshape(tf.tile([1.3,1.0,1.0], [28*28]), [1, 28,28,3]) # red background
    recognised_bkg = tf.gather(tf.concat(0, [incorrect_bkg, correct_bkg]), tf.cast(correct_prediction_s, tf.int32)) # pick either the red or the white background depending on recognised status

    I = tf.image.grayscale_to_rgb(Xs)
    I = ((1-(I+superimposed_digits))*recognised_bkg)/1.3 # stencil extra data on top of images and reorder them unrecognised first
    I = tf.image.convert_image_dtype(I, tf.uint8, saturate=True)
    Islices = [] # 100 images => 10x10 image block
    for imslice in range(lines):
        Islices.append(tf.concat(1, tf.unpack(tf.slice(I, [imslice*n//lines,0,0,0], [n//lines,28,28,3]))))
    I = tf.concat(0, Islices)
    return I

# n = HISTOGRAM_BUCKETS (global)
# Buckets the data into n buckets so that there are an equal number of data points in
# each bucket. Returns n+1 bucket boundaries. Spreads the reaminder data.size % n more
# or less evenly among the central buckets.
# data: 1-D ndarray containing float data, MUST BE SORTED in ascending order
#    n: integer, the number of desired output buckets
# return value: ndarray, 1-D vector of size n+1 containing the bucket boundaries
#               the first value is the min of the data, the last value is the max
model.py 文件源码 项目:tf-char-cnn-lstm 作者: hejunqing 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def loss_graph(logits, batch_size, num_unroll_steps):
    with tf.variable_scope('Loss'):
        targets = tf.placeholder(tf.int64, [batch_size, num_unroll_steps], name='targets')
        # target_list = [tf.squeeze(x, [1]) for x in tf.split(1, num_unroll_steps, targets)]
        target_list=tf.unpack(targets, axis=1)#hjq

        loss= tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits, target_list), name='loss')
        # reduce_mean to reduce_sum,hjq

    return adict(
        targets=targets,
        loss=loss
    )
target_lstm.py 文件源码 项目:CodeGAN 作者: keon 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def create_output_unit(self, params):
        self.Wo = tf.Variable(self.params[13])
        self.bo = tf.Variable(self.params[14])
        params.extend([self.Wo, self.bo])

        def unit(hidden_memory_tuple):
            hidden_state, c_prev = tf.unpack(hidden_memory_tuple)
            # hidden_state : batch x hidden_dim
            logits = tf.matmul(hidden_state, self.Wo) + self.bo
            # output = tf.nn.softmax(logits)
            return logits

        return unit
rollout.py 文件源码 项目:CodeGAN 作者: keon 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def create_output_unit(self):
        self.Wo = tf.identity(self.lstm.Wo)
        self.bo = tf.identity(self.lstm.bo)

        def unit(hidden_memory_tuple):
            hidden_state, c_prev = tf.unpack(hidden_memory_tuple)
            # hidden_state : batch x hidden_dim
            logits = tf.matmul(hidden_state, self.Wo) + self.bo
            # output = tf.nn.softmax(logits)
            return logits

        return unit
model.py 文件源码 项目:CodeGAN 作者: keon 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def create_output_unit(self, params):
        self.Wo = tf.Variable(self.init_matrix([self.hidden_dim, self.num_emb]))
        self.bo = tf.Variable(self.init_matrix([self.num_emb]))
        params.extend([self.Wo, self.bo])

        def unit(hidden_memory_tuple):
            hidden_state, c_prev = tf.unpack(hidden_memory_tuple)
            # hidden_state : batch x hidden_dim
            logits = tf.matmul(hidden_state, self.Wo) + self.bo
            # output = tf.nn.softmax(logits)
            return logits

        return unit
11-lstm-tensorflow-char-pat.py 文件源码 项目:albemarle 作者: SeanTater 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def RNN(inputs, lens, name, reuse):
    print ("Building network " + name)
    # Define weights
    inputs = tf.gather(one_hots, inputs)
    weights = tf.Variable(tf.random_normal([__n_hidden, n_output]), name=name+"_weights")
    biases = tf.Variable(tf.random_normal([n_output]), name=name+"_biases")

    # Define a lstm cell with tensorflow

    outputs, states = rnn.dynamic_rnn(
        __cell_kind(__n_hidden),
        inputs,
        sequence_length=lens,
        dtype=tf.float32,
        scope=name,
        time_major=False)

    # Prepare data shape to match `rnn` function requirements
    # Current data input shape: (__batch_size, __n_steps, n_input)
    # Required shape: '__n_steps' tensors list of shape (__batch_size, n_input)

    '''outputs, states = rnn.rnn(
        __cell_kind(__n_hidden),
        tf.unpack(tf.transpose(inputs, [1, 0, 2])),
        sequence_length=lens,
        dtype=tf.float32,
        scope=name)
    outputs = tf.transpose(tf.pack(outputs), [1, 0, 2])'''
    print ("Done building network " + name)

    # Asserts are actually documentation: they can't be out of date
    assert outputs.get_shape() == (__batch_size, __n_steps, __n_hidden)
    # Linear activation, using rnn output for each char
    # Reshaping here for a `batch` matrix multiply
    # It's faster than `batch_matmul` probably because it can guarantee a
    # static shape
    outputs = tf.reshape(outputs, [__batch_size * __n_steps, __n_hidden])
    finals = tf.matmul(outputs, weights)
    return tf.reshape(finals, [__batch_size, __n_steps, n_output]) + biases

# tf Graph input
deepModel.py 文件源码 项目:DeepPicker-python 作者: nejyeah 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def __preprocess_particle(self, batch_data):
        # scale the image to the model input size
        #batch_data = tf.image.resize_images(batch_data, self.num_col, self.num_row)
        # get the scale tensor shape
        batch_data_shape = batch_data.get_shape().as_list()
        # uppack the tensor into sub-tensor
        batch_data_list = tf.unpack(batch_data)
        for i in xrange(batch_data_shape[0]):
            # Pass image tensor object to a PIL image
            image = Image.fromarray(batch_data_list[i].eval())
            # Use PIL or other library of the sort to rotate
            random_degree = random.randint(0, 359)
            rotated = Image.Image.rotate(image, random_degree)
            # Convert rotated image back to tensor
            rotated_tensor = tf.convert_to_tensor(np.array(rotated))
            #slice_image = tf.slice(batch_data, [i, 0, 0, 0], [1, -1, -1, -1])
            #slice_image_reshape = tf.reshape(slice_image, [batch_data_shape[1], batch_data_shape[2], batch_data_shape[3]])
            #distorted_image = tf.image.random_flip_up_down(batch_data_list[i], seed = 1234)
            #distorted_image = tf.image.random_flip_left_right(distorted_image, seed = 1234)
            #distorted_image = tf.image.random_brightness(distorted_image, max_delta=63)
            #distorted_image = tf.image.random_contrast(distorted_image, lower=0.2, upper=1.8)
            # Subtract off the mean and divide by the variance of the pixels.
            distorted_image = tf.image.per_image_whitening(rotated_tensor)
            batch_data_list[i] = distorted_image
        # pack the list of tensor into one tensor
        batch_data = tf.pack(batch_data_list)
        return batch_data
model.py 文件源码 项目:language-model 作者: beamandrew 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def __init__(self,params):
        config = tf.ConfigProto(allow_soft_placement=True)
        self.sess = tf.Session(config = config)
        K.set_session(self.sess)
        # Pull out all of the parameters
        self.batch_size = params['batch_size']
        self.seq_len = params['seq_len']
        self.vocab_size = params['vocab_size']
        self.embed_size = params['embed_size']
        self.hidden_dim = params['hidden_dim']
        self.num_layers = params['num_layers']
        with tf.device('/gpu:0'):
            # Set up the input placeholder
            self.input_seq = tf.placeholder(tf.float32, shape=[None, self.seq_len])
            # Build the RNN
            self.rnn = Embedding(self.vocab_size + 1, self.embed_size, input_length=self.seq_len)(self.input_seq)
        with tf.device('/gpu:1'):
            for l in range(self.num_layers):
                self.rnn = LSTM(output_dim=self.hidden_dim, return_sequences=True, name='rnn_1')(self.rnn)
            rnn_output = tf.unpack(self.rnn, axis=1)
            self.w_proj = tf.Variable(tf.zeros([self.vocab_size, self.hidden_dim]))
            self.b_proj = tf.Variable(tf.zeros([self.vocab_size]))
            self.output_seq = tf.placeholder(tf.int64, shape=([None, self.seq_len]))
            losses = []
            outputs = []
            for t in range(self.seq_len):
                rnn_t = rnn_output[t]
                y_t = tf.reshape(self.output_seq[:, t],[-1,1])
                step_loss = tf.nn.sampled_softmax_loss(weights=self.w_proj, biases=self.b_proj, inputs=rnn_t,
                                                       labels=y_t, num_sampled=512, num_classes=self.vocab_size)
                losses.append(step_loss)
                outputs.append(tf.matmul(rnn_t, tf.transpose(self.w_proj)) + self.b_proj)
            self.step_losses = losses
            self.output = outputs
            self.loss = tf.reduce_mean(self.step_losses)
            self.softmax = tf.nn.softmax(self.output)
decoder.py 文件源码 项目:tfkaldi 作者: vrenkens 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __init__(self, classifier, input_dim, max_length):
        '''
        NnetDecoder constructor, creates the decoding graph

        Args:
            classifier: the classifier that will be used for decoding
            input_dim: the input dimension to the nnnetgraph
        '''

        self.graph = tf.Graph()
        self.max_length = max_length

        with self.graph.as_default():

            #create the inputs placeholder
            self.inputs = tf.placeholder(
                tf.float32, shape=[max_length, input_dim], name='inputs')

            #create the sequence length placeholder
            self.seq_length = tf.placeholder(
                tf.int32, shape=[1], name='seq_length')

            split_inputs = tf.unpack(tf.expand_dims(self.inputs, 1))

            #create the decoding graph
            logits, _, self.saver, _ = classifier(split_inputs, self.seq_length,
                                               is_training=False, reuse=False,
                                               scope='Classifier')

            #convert logits to non sequence for the softmax computation
            logits = seq_convertors.seq2nonseq(logits, self.seq_length)

            #compute the outputs
            self.outputs = tf.nn.softmax(logits)

        #specify that the graph can no longer be modified after this point
        self.graph.finalize()
seq_convertors.py 文件源码 项目:tfkaldi 作者: vrenkens 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def seq2nonseq(tensorlist, seq_length, name=None):
    '''
    Convert sequential data to non sequential data

    Args:
        tensorlist: the sequential data, wich is a list containing an N x F
            tensor for each time step where N is the batch size and F is the
            input dimension
        seq_length: a vector containing the sequence lengths
        name: [optional] the name of the operation

    Returns:
        non sequential data, which is a TxF tensor where T is the sum of all
        sequence lengths
    '''

    with tf.name_scope(name or 'seq2nonseq'):
        #convert the list for each time step to a list for each sequence
        sequences = tf.unpack(tf.pack(tensorlist), axis=1)

        #remove the padding from sequences
        sequences = [tf.gather(sequences[s], tf.range(seq_length[s]))
                     for s in range(len(sequences))]

        #concatenate the sequences
        tensor = tf.concat(0, sequences)

    return tensor


问题


面经


文章

微信
公众号

扫码关注公众号