python类cast()的实例源码

CNN_train.py 文件源码 项目:Humour-Detection 作者: srishti-1795 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def shared_dataset(data_xy, borrow=True):
        """ Function that loads the dataset into shared variables

        The reason we store our dataset in shared variables is to allow
        Theano to copy it into the GPU memory (when code is run on GPU).
        Since copying data into the GPU is slow, copying a minibatch everytime
        is needed (the default behaviour if the data is not in a shared
        variable) would lead to a large decrease in performance.
        """
        data_x, data_y = data_xy
        shared_x = theano.shared(np.asarray(data_x,
                                               dtype=theano.config.floatX),
                                 borrow=borrow)
        shared_y = theano.shared(np.asarray(data_y,
                                               dtype=theano.config.floatX),
                                 borrow=borrow)
        return shared_x, T.cast(shared_y, 'int32')
exp_rnn.py 文件源码 项目:world_merlin 作者: pbaljeka 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def parameter_prediction(self, test_set_x):  #, batch_size
        """ This function is to predict the output of NN

        :param test_set_x: input features for a testing sentence
        :type test_set_x: python array variable
        :returns: predicted features

        """


        n_test_set_x = test_set_x.shape[0]

        test_out = theano.function([], self.final_layer.output,
              givens={self.x: test_set_x[0:n_test_set_x], self.is_train: np.cast['int32'](0)}, on_unused_input='ignore')

        predict_parameter = test_out()

        return predict_parameter
exp_rnn.py 文件源码 项目:world_merlin 作者: pbaljeka 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def parameter_prediction_S2S(self, test_set_x, test_set_d):  
        """ This function is to predict the output of NN

        :param test_set_x: input features for a testing sentence
        :param test_set_d: phone durations for a testing sentence
        :type test_set_x: python array variable
        :type test_set_d: python array variable
        :returns: predicted features

        """

        n_test_set_x = test_set_x.shape[0]

        test_out = theano.function([], self.final_layer.output,
                givens={self.x: test_set_x[0:n_test_set_x], self.d: test_set_d[0:n_test_set_x], self.is_train: np.cast['int32'](0)}, on_unused_input='ignore')

        predict_parameter = test_out()

        return predict_parameter
deep_rnn.py 文件源码 项目:world_merlin 作者: pbaljeka 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def parameter_prediction(self, test_set_x):  #, batch_size
        """ This function is to predict the output of NN

        :param test_set_x: input features for a testing sentence
        :type test_set_x: python array variable
        :returns: predicted features

        """


        n_test_set_x = test_set_x.shape[0]

        test_out = theano.function([], self.final_layer.output,
              givens={self.x: test_set_x[0:n_test_set_x], self.is_train: np.cast['int32'](0)}, on_unused_input='ignore')

        predict_parameter = test_out()

        return predict_parameter
layers.py 文件源码 项目:DL-Benchmarks 作者: DL-Benchmarks 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def __init__(self, input, prob_drop=0.5):

        self.prob_drop = prob_drop
        self.prob_keep = 1.0 - prob_drop
        self.flag_on = theano.shared(np.cast[theano.config.floatX](1.0))
        self.flag_off = 1.0 - self.flag_on  # 1 during test

        seed_this = DropoutLayer.seed_common.randint(0, 2**31-1)
        mask_rng = theano.tensor.shared_randomstreams.RandomStreams(seed_this)
        self.mask = mask_rng.binomial(n=1, p=self.prob_keep, size=input.shape)

        self.output = \
            self.flag_on * T.cast(self.mask, theano.config.floatX) * input + \
            self.flag_off * self.prob_keep * input

        DropoutLayer.layers.append(self)

        print 'dropout layer with P_drop: ' + str(self.prob_drop)
exp_rnn.py 文件源码 项目:mimicry.ai 作者: fizerkhan 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def parameter_prediction(self, test_set_x):  #, batch_size
        """ This function is to predict the output of NN

        :param test_set_x: input features for a testing sentence
        :type test_set_x: python array variable
        :returns: predicted features

        """


        n_test_set_x = test_set_x.shape[0]

        test_out = theano.function([], self.final_layer.output,
              givens={self.x: test_set_x[0:n_test_set_x], self.is_train: np.cast['int32'](0)}, on_unused_input='ignore')

        predict_parameter = test_out()

        return predict_parameter
exp_rnn.py 文件源码 项目:mimicry.ai 作者: fizerkhan 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def parameter_prediction_S2S(self, test_set_x, test_set_d):  
        """ This function is to predict the output of NN

        :param test_set_x: input features for a testing sentence
        :param test_set_d: phone durations for a testing sentence
        :type test_set_x: python array variable
        :type test_set_d: python array variable
        :returns: predicted features

        """

        n_test_set_x = test_set_x.shape[0]

        test_out = theano.function([], self.final_layer.output,
                givens={self.x: test_set_x[0:n_test_set_x], self.d: test_set_d[0:n_test_set_x], self.is_train: np.cast['int32'](0)}, on_unused_input='ignore')

        predict_parameter = test_out()

        return predict_parameter
deep_rnn.py 文件源码 项目:mimicry.ai 作者: fizerkhan 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def parameter_prediction(self, test_set_x):  #, batch_size
        """ This function is to predict the output of NN

        :param test_set_x: input features for a testing sentence
        :type test_set_x: python array variable
        :returns: predicted features

        """


        n_test_set_x = test_set_x.shape[0]

        test_out = theano.function([], self.final_layer.output,
              givens={self.x: test_set_x[0:n_test_set_x], self.is_train: np.cast['int32'](0)}, on_unused_input='ignore')

        predict_parameter = test_out()

        return predict_parameter
metrics.py 文件源码 项目:odin 作者: imito 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
def categorical_accuracy(y_pred, y_true, top_k=1, reduction=tf.reduce_mean,
                         name="CategoricalAccuracy"):
  """ Non-differentiable """
  with tf.variable_scope(name):
    if y_true.get_shape().ndims == y_pred.get_shape().ndims:
      y_true = tf.argmax(y_true, axis=-1)
    elif y_true.get_shape().ndims != y_pred.get_shape().ndims - 1:
      raise TypeError('rank mismatch between y_true and y_pred')
    if top_k == 1:
      # standard categorical accuracy
      top = tf.argmax(y_pred, axis=-1)
      y_true = tf.cast(y_true, top.dtype.base_dtype)
      match_values = tf.equal(top, y_true)
    else:
      match_values = tf.nn.in_top_k(y_pred, tf.cast(y_true, 'int32'),
                                    k=top_k)
    match_values = tf.cast(match_values, dtype='float32')
    return reduction(match_values)
metrics.py 文件源码 项目:odin 作者: imito 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def to_llr(x, name="LogLikelihoodRatio"):
  ''' Convert a matrix of probabilities into log-likelihood ratio
  :math:`LLR = log(\\frac{prob(data|target)}{prob(data|non-target)})`
  '''
  if not is_tensor(x):
    x /= np.sum(x, axis=-1, keepdims=True)
    x = np.clip(x, 10e-8, 1. - 10e-8)
    return np.log(x / (np.cast(1., x.dtype) - x))
  else:
    with tf.variable_scope(name):
      x /= tf.reduce_sum(x, axis=-1, keepdims=True)
      x = tf.clip_by_value(x, 10e-8, 1. - 10e-8)
      return tf.log(x / (tf.cast(1., x.dtype.base_dtype) - x))


# ===========================================================================
# Speech task metrics
# ===========================================================================
rand.py 文件源码 项目:odin 作者: imito 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def glorot_uniform(shape, gain=1.0, c01b=False):
  orig_shape = shape
  if c01b:
    if len(shape) != 4:
      raise RuntimeError(
          "If c01b is True, only shapes of length 4 are accepted")
    n1, n2 = shape[0], shape[3]
    receptive_field_size = shape[1] * shape[2]
  else:
    if len(shape) < 2:
      shape = (1,) + tuple(shape)
    n1, n2 = shape[:2]
    receptive_field_size = np.prod(shape[2:])

  std = gain * np.sqrt(2.0 / ((n1 + n2) * receptive_field_size))
  a = 0.0 - np.sqrt(3) * std
  b = 0.0 + np.sqrt(3) * std
  return np.cast[floatX](
      get_rng().uniform(low=a, high=b, size=orig_shape))
rand.py 文件源码 项目:odin 作者: imito 项目源码 文件源码 阅读 49 收藏 0 点赞 0 评论 0
def he_normal(shape, gain=1.0, c01b=False):
  if gain == 'relu':
    gain = np.sqrt(2)

  if c01b:
    if len(shape) != 4:
      raise RuntimeError(
          "If c01b is True, only shapes of length 4 are accepted")
    fan_in = np.prod(shape[:3])
  else:
    if len(shape) <= 2:
      fan_in = shape[0]
    elif len(shape) > 2:
      fan_in = np.prod(shape[1:])

  std = gain * np.sqrt(1.0 / fan_in)
  return np.cast[floatX](
      get_rng().normal(0.0, std, size=shape))
rand.py 文件源码 项目:odin 作者: imito 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def orthogonal(shape, gain=1.0):
  if gain == 'relu':
    gain = np.sqrt(2)

  if len(shape) < 2:
    raise RuntimeError("Only shapes of length 2 or more are supported, but "
                       "given shape:%s" % str(shape))

  flat_shape = (shape[0], np.prod(shape[1:]))
  a = get_rng().normal(0.0, 1.0, flat_shape)
  u, _, v = np.linalg.svd(a, full_matrices=False)
  # pick the one with the correct shape
  q = u if u.shape == flat_shape else v
  q = q.reshape(shape)
  return np.cast[floatX](gain * q)


# ===========================================================================
# Fast initialization
# ===========================================================================
nn.py 文件源码 项目:DeepMonster 作者: olimastro 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def adam_updates(params, cost, lr=0.001, mom1=0.9, mom2=0.999):
    updates = []
    grads = T.grad(cost, params)
    t = th.shared(np.cast[th.config.floatX](1.))
    for p, g in zip(params, grads):
        v = th.shared(np.cast[th.config.floatX](p.get_value() * 0.))
        mg = th.shared(np.cast[th.config.floatX](p.get_value() * 0.))
        v_t = mom1*v + (1. - mom1)*g
        mg_t = mom2*mg + (1. - mom2)*T.square(g)
        v_hat = v_t / (1. - mom1 ** t)
        mg_hat = mg_t / (1. - mom2 ** t)
        g_t = v_hat / T.sqrt(mg_hat + 1e-8)
        p_t = p - lr * g_t
        updates.append((v, v_t))
        updates.append((mg, mg_t))
        updates.append((p, p_t))
    updates.append((t, t+1))
    return updates
nn.py 文件源码 项目:DeepMonster 作者: olimastro 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def get_output_for(self, input, deterministic=False, set_bn_updates=True, **kwargs):
        if deterministic:
            norm_features = (input-self.avg_batch_mean.dimshuffle(*self.dimshuffle_args)) / T.sqrt(1e-6 + self.avg_batch_var).dimshuffle(*self.dimshuffle_args)
        else:
            batch_mean = T.mean(input,axis=self.axes_to_sum).flatten()
            centered_input = input-batch_mean.dimshuffle(*self.dimshuffle_args)
            batch_var = T.mean(T.square(centered_input),axis=self.axes_to_sum).flatten()
            batch_stdv = T.sqrt(1e-6 + batch_var)
            norm_features = centered_input / batch_stdv.dimshuffle(*self.dimshuffle_args)

            # BN updates
            if set_bn_updates:
                new_m = 0.9*self.avg_batch_mean + 0.1*batch_mean
                new_v = 0.9*self.avg_batch_var + T.cast((0.1*input.shape[0])/(input.shape[0]-1),th.config.floatX)*batch_var
                self.bn_updates = [(self.avg_batch_mean, new_m), (self.avg_batch_var, new_v)]

        if hasattr(self, 'g'):
            activation = norm_features*self.g.dimshuffle(*self.dimshuffle_args)
        else:
            activation = norm_features
        if hasattr(self, 'b'):
            activation += self.b.dimshuffle(*self.dimshuffle_args)

        return self.nonlinearity(activation)
T6SB_conv_net_sentence.py 文件源码 项目:SE16-Task6-Stance-Detection 作者: nestle1993 项目源码 文件源码 阅读 48 收藏 0 点赞 0 评论 0
def shared_dataset(data_xy, borrow=True):
        """ Function that loads the dataset into shared variables

        The reason we store our dataset in shared variables is to allow
        Theano to copy it into the GPU memory (when code is run on GPU).
        Since copying data into the GPU is slow, copying a minibatch everytime
        is needed (the default behaviour if the data is not in a shared
        variable) would lead to a large decrease in performance.
        """
        data_x, data_y = data_xy
        shared_x = theano.shared(np.asarray(data_x,
                                               dtype=theano.config.floatX),
                                 borrow=borrow)
        shared_y = theano.shared(np.asarray(data_y,
                                               dtype=theano.config.floatX),
                                 borrow=borrow)
        return shared_x, T.cast(shared_y, 'int32')
T6SA_conv_net_sentence.py 文件源码 项目:SE16-Task6-Stance-Detection 作者: nestle1993 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def shared_dataset(data_xy, borrow=True):
        """ Function that loads the dataset into shared variables

        The reason we store our dataset in shared variables is to allow
        Theano to copy it into the GPU memory (when code is run on GPU).
        Since copying data into the GPU is slow, copying a minibatch everytime
        is needed (the default behaviour if the data is not in a shared
        variable) would lead to a large decrease in performance.
        """
        data_x, data_y = data_xy
        shared_x = theano.shared(np.asarray(data_x,
                                               dtype=theano.config.floatX),
                                 borrow=borrow)
        shared_y = theano.shared(np.asarray(data_y,
                                               dtype=theano.config.floatX),
                                 borrow=borrow)
        return shared_x, T.cast(shared_y, 'int32')
learning_rule.py 文件源码 项目:Attentive_reader 作者: caglar 项目源码 文件源码 阅读 44 收藏 0 点赞 0 评论 0
def __init__(self,
                 init_momentum,
                 averaging_coeff=0.95,
                 stabilizer=1e-2,
                 use_first_order=False,
                 bound_inc=False,
                 momentum_clipping=None):
        init_momentum = float(init_momentum)
        assert init_momentum >= 0.
        assert init_momentum <= 1.
        averaging_coeff = float(averaging_coeff)
        assert averaging_coeff >= 0.
        assert averaging_coeff <= 1.
        stabilizer = float(stabilizer)
        assert stabilizer >= 0.

        self.__dict__.update(locals())
        del self.self
        self.momentum = sharedX(self.init_momentum)

        self.momentum_clipping = momentum_clipping
        if momentum_clipping is not None:
            self.momentum_clipping = np.cast[config.floatX](momentum_clipping)
learning_rule.py 文件源码 项目:Attentive_reader 作者: caglar 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self,
                 init_momentum=0.9,
                 averaging_coeff=0.99,
                 stabilizer=1e-4,
                 update_param_norm_ratio=0.003,
                 gradient_clipping=None):
        init_momentum = float(init_momentum)
        assert init_momentum >= 0.
        assert init_momentum <= 1.
        averaging_coeff = float(averaging_coeff)
        assert averaging_coeff >= 0.
        assert averaging_coeff <= 1.
        stabilizer = float(stabilizer)
        assert stabilizer >= 0.

        self.__dict__.update(locals())
        del self.self
        self.momentum = sharedX(self.init_momentum)
        self.update_param_norm_ratio = update_param_norm_ratio

        self.gradient_clipping = gradient_clipping
        if gradient_clipping is not None:
            self.gradient_clipping = np.cast[config.floatX](gradient_clipping)
utils.py 文件源码 项目:Attentive_reader 作者: caglar 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def as_floatX(variable):
    """
       This code is taken from pylearn2:
       Casts a given variable into dtype config.floatX
       numpy ndarrays will remain numpy ndarrays
       python floats will become 0-D ndarrays
       all other types will be treated as theano tensors
    """

    if isinstance(variable, float):
        return numpy.cast[theano.config.floatX](variable)

    if isinstance(variable, numpy.ndarray):
        return numpy.cast[theano.config.floatX](variable)

    return theano.tensor.cast(variable, theano.config.floatX)


问题


面经


文章

微信
公众号

扫码关注公众号