python类RandomStreams()的实例源码

sa.py 文件源码 项目:structured-output-ae 作者: sbelharbi 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __init__(self,
                 input,
                 nvis,
                 nhid,
                 rnd=None,
                 theano_rng=None,
                 bhid=None,
                 cost_type=CostType.CrossEntropy,
                 bvis=None):
        super(SparseAutoencoder, self).__init__(
            input=input,
            nvis=nvis,
            nhid=nhid,
            rnd=rnd,
            bhid=bhid,
            cost_type=cost_type,
            bvis=bvis)
        if not theano_rng:
            theano_rng = RandomStreams(rnd.randint(2 ** 30))
        self.theano_rng = theano_rng
mask_generator.py 文件源码 项目:Neural-Photo-Editor 作者: ajbrock 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def reset(self):
        # Set Original ordering
        self.ordering.set_value(np.arange(self._input_size, dtype=theano.config.floatX))

        # Reset RandomStreams
        self._rng.seed(self._random_seed)

        # Initial layer connectivity
        self.layers_connectivity[0].set_value((self.ordering + 1).eval())
        for i in range(1, len(self.layers_connectivity)-1):
            self.layers_connectivity[i].set_value(np.zeros((self._hidden_sizes[i-1]), dtype=theano.config.floatX))
        self.layers_connectivity[-1].set_value(self.ordering.get_value())

        # Reset MRG_RandomStreams (GPU)
        self._mrng.rstate = self._initial_mrng_rstate
        for state, value in zip(self._mrng.state_updates, self._initial_mrng_state_updates):
            state[0].set_value(value)

        self.sample_connectivity()
MIDS.py 文件源码 项目:MIDS 作者: freegraphics 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def main(load_id):
        consts = Consts()
        consts.load_from_ids = load_id
        rng = numpy.random.RandomState()
        theano_rng = RandomStreams(rng.randint(2 ** 30))
        user_lines = UserLines(rng = rng,theano_rng = theano_rng,consts = consts)
        rating_info = numpy.zeros(1,dtype=theano.config.floatX)
        wday = 4 # friday
        rating_info[0] = get_aranged(value = wday, min_value = 0, max_value = 6)
        #user_id = user_lines.rng.randint(low=0,high=user_lines.matrix_ids.users_count)
        #user_ids = user_lines.__find_nearest(user_id,5)
        user_indices = [user_lines.rng.randint(low=0,high=len(user_lines.users_cvs)-1) for it in numpy.arange(5)]
        user_ids = [user_lines.users_cvs.at[indice,"id"] for indice in user_indices]
        #user_lines.build_line_for_rand_user(rating_info = rating_info, user_ids = user_ids, consts = consts)
        user_lines.build_rate_for_rand_user(rating_info = rating_info, user_ids = user_ids, consts = consts)
        sys.stdout.write("all done\n")
        return
model.py 文件源码 项目:eqnet 作者: mast-group 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def __init__(self, memory_size: int, num_node_types: int, max_num_children: int, hyperparameters: dict,
                 rng: RandomStreams, name: str = "single_layer_combination"):
        self.__memory_size = memory_size
        self.__rng = rng
        self.__name = name
        self.__hyperparameters = hyperparameters

        w = np.random.randn(num_node_types, memory_size, max_num_children * memory_size) * \
            10 ** self.__hyperparameters["log_init_scale_embedding"]
        self.__w = theano.shared(w.astype(theano.config.floatX), name=name + ":w")

        bias = np.random.randn(num_node_types, memory_size) * 10 ** self.__hyperparameters["log_init_scale_embedding"]
        self.__bias = theano.shared(bias.astype(theano.config.floatX), name=name + ":b")

        self.__w_with_dropout = \
            dropout(self.__hyperparameters['dropout_rate'], self.__rng, self.__w, True)
layers.py 文件源码 项目:eqnet 作者: mast-group 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def __init__(self, embeddings, memory_size: int, embeddings_size: int, hyperparameters: dict, rng: RandomStreams,
                 name="SequenceGRU", use_centroid=False):
        """
        :param embeddings: the embedding matrix
        """
        self.__name = name
        self.__embeddings = embeddings
        self.__memory_size = memory_size
        self.__embeddings_size = embeddings_size
        self.__hyperparameters = hyperparameters
        self.__rng = rng

        if use_centroid:
            self.__gru = GruCentroidsCell(memory_size, embeddings_size, hyperparameters['num_centroids'],
                                          hyperparameters['centroid_use_rate'], self.__rng, self.__name + ":GRUCell",
                                          hyperparameters['log_init_noise'])
        else:
            self.__gru = GruCell(memory_size, embeddings_size, self.__name + ":GRUCell",
                                 hyperparameters['log_init_noise'])

        self.__params = {self.__name + ":" + n: v for n, v in self.__gru.get_params().items()}
layers.py 文件源码 项目:eqnet 作者: mast-group 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def __init__(self, embeddings, memory_size: int, embeddings_size: int, hyperparameters: dict, rng: RandomStreams,
                 name="SequenceAveragingGRU", use_centroid=False):
        """
        :param embeddings: the embedding matrix
        """
        self.__name = name
        self.__embeddings = embeddings
        self.__memory_size = memory_size
        self.__embeddings_size = embeddings_size
        self.__hyperparameters = hyperparameters
        self.__rng = rng

        if use_centroid:
            self.__gru = GruCentroidsCell(memory_size, embeddings_size, hyperparameters['num_centroids'],
                                          hyperparameters['centroid_use_rate'], self.__rng, self.__name + ":GRUCell",
                                          hyperparameters['log_init_noise'])
        else:
            self.__gru = GruCell(memory_size, embeddings_size, self.__name + ":GRUCell",
                                 hyperparameters['log_init_noise'])

        self.__params = {self.__name + ":" + n: v for n, v in self.__gru.get_params().items()}
rnnlm.py 文件源码 项目:lmkit 作者: jiangnanhugo 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __init__(self, n_input, n_hidden, n_output, cell='gru', optimizer='sgd', p=0.5,bptt=-1):
        self.x = T.imatrix('batched_sequence_x')  # n_batch, maxlen 
        self.x_mask = T.fmatrix('x_mask')
        self.y = T.imatrix('batched_sequence_y') 
        self.y_mask = T.fmatrix('y_mask')
        self.n_input = n_input 
        self.n_hidden = n_hidden 
        self.n_output = n_output 

        init_Embd = np.asarray(np.random.uniform(low=-np.sqrt(1. / n_output), high=np.sqrt(1. / n_output), size=(n_output, n_input)), dtype=theano.config.floatX) 

        self.E = theano.shared(value=init_Embd, name='word_embedding',borrow=True) 
        self.cell = cell 
        self.optimizer = optimizer 
        self.p = p
        self.bptt=bptt


        self.is_train = T.iscalar('is_train')

        self.rng = RandomStreams(1234)
        self.build()
rnnlm.py 文件源码 项目:lmkit 作者: jiangnanhugo 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def __init__(self,n_input,n_hidden,n_output,cell='gru',optimizer='sgd',p=0):
        self.x=T.imatrix('batched_sequence_x')  # n_batch, maxlen
        self.x_mask=T.matrix('x_mask')
        self.y=T.imatrix('batched_sequence_y')
        self.y_mask=T.matrix('y_mask')

        self.n_input=n_input
        self.n_hidden=n_hidden
        self.n_output=n_output
        init_Embd=np.asarray(np.random.uniform(low=-np.sqrt(1./n_output),
                                               high=np.sqrt(1./n_output),
                                               size=(n_output,n_input)),
                           dtype=theano.config.floatX)
        self.E=theano.shared(value=init_Embd,name='word_embedding')

        self.cell=cell
        self.optimizer=optimizer
        self.p=p
        self.is_train=T.iscalar('is_train')
        self.n_batch=T.iscalar('n_batch')

        self.epsilon=1.0e-15
        self.rng=RandomStreams(1234)
        self.build()
rnnlm.py 文件源码 项目:lmkit 作者: jiangnanhugo 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def __init__(self, n_input, n_hidden, n_output, cell='gru', optimizer='sgd', p=0.5,bptt=-1):
        self.x = T.imatrix('batched_sequence_x')  # n_batch, maxlen 
        self.x_mask = T.fmatrix('x_mask')
        self.y = T.imatrix('batched_sequence_y') 
        self.y_mask = T.fmatrix('y_mask')
        self.n_input = n_input 
        self.n_hidden = n_hidden 
        self.n_output = n_output 

        init_Embd = np.asarray(np.random.uniform(low=-np.sqrt(1. / n_output), high=np.sqrt(1. / n_output), size=(n_output, n_input)), dtype=theano.config.floatX) 

        self.E = theano.shared(value=init_Embd, name='word_embedding',borrow=True) 
        self.cell = cell 
        self.optimizer = optimizer 
        self.p = p
        self.bptt=bptt


        self.is_train = T.iscalar('is_train')

        self.rng = RandomStreams(1234)
        self.build()
FullyTrainedVaeLstmAdamTrainer.py 文件源码 项目:deep-motion-analysis 作者: Brimborough 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def __init__(self, rng, batchsize, epochs=100, alpha=0.001, beta1=0.9, beta2=0.999, eps=1e-08, gamma=0.1, cost='mse'):
        self.alpha = alpha
        self.beta1 = beta1
        self.beta2 = beta2
        self.eps = eps
        self.gamma = gamma
        self.rng = rng
        self.theano_rng = RandomStreams(rng.randint(2 ** 30))
        self.epochs = epochs
        self.batchsize = batchsize
        if   cost == 'mse':
            self.cost = lambda network, x, y: T.mean((network(x) - y)**2)
        elif cost == 'cross_entropy':
            self.cost = lambda network, x, y: T.nnet.binary_crossentropy(network(x), y).mean()
        else:
            self.cost = cost
AdamTrainer.py 文件源码 项目:deep-motion-analysis 作者: Brimborough 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def __init__(self, rng, batchsize, epochs=100, alpha=0.001, beta1=0.9, beta2=0.999, eps=1e-08, gamma=0.1, cost='mse'):
        self.alpha = alpha
        self.beta1 = beta1
        self.beta2 = beta2
        self.eps = eps
        self.gamma = gamma
        self.rng = rng
        self.theano_rng = RandomStreams(rng.randint(2 ** 30))
        self.epochs = epochs
        self.batchsize = batchsize
        if   cost == 'mse':
            self.cost = lambda network, x, y: T.mean((network(x) - y)**2)
        elif cost == 'cross_entropy':
            self.cost = lambda network, x, y: T.nnet.binary_crossentropy(network(x), y).mean()
        else:
            self.cost = cost
Conv2DLayer.py 文件源码 项目:deep-motion-analysis 作者: Brimborough 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __init__(self, rng, filter_shape, input_shape, scale=1.0):

        self.filter_shape = filter_shape
        self.input_shape = input_shape
        self.output_shape = (input_shape[0], filter_shape[0], input_shape[2], input_shape[3])
        self.input_units = np.prod(self.input_shape)
        self.output_units = np.prod(self.output_shape)

        self.theano_rng = RandomStreams(rng.randint(2 ** 30))

        fan_in = np.prod(filter_shape[1:])
        fan_out = filter_shape[0] * np.prod(filter_shape[2:])

        W_bound = scale * np.sqrt(6. / (fan_in + fan_out))
        W = np.asarray(
                rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
                dtype=theano.config.floatX)

        b = np.zeros((filter_shape[0],), dtype=theano.config.floatX)

        self.W = theano.shared(value=W, borrow=True)
        self.b = theano.shared(value=b, borrow=True)
        self.params = [self.W, self.b]
LSTM.py 文件源码 项目:deep-motion-analysis 作者: Brimborough 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __init__(self, options, shape, rng, drop=0, zone_hidden=0, zone_cell=0, prefix="lstm",
                 bn=False, clip_gradients=False, mask=None):

        self.nsteps = shape
        self.mask = mask if mask is None else '' #TODO: Make mask
        self.prefix = prefix
        #TODO: Replace options and update the step function
        self.options = options
        self.clip_gradients = clip_gradients
        self.params = init_params(param_init_lstm(options=options, params=[], prefix=prefix))
        #TODO: Sort shapes, can have input,hidden for W, U = hidden,hidden, b = hidden
        # Saves upon changing code lots below.
        self.bninput = BatchNormLayer(None, shape) if bn else lambda x: x
        self.bnhidden = BatchNormLayer(None, shape) if bn else lambda x: x
        self.bncell = BatchNormLayer(None, shape) if bn else lambda x: x
        # Add BN params to layer (for SGD)
        if bn:
            self.params += self.bnhidden.params + self.bninput.params + self.bncell.params
        self.dropout = drop
        self.zoneout = {'h': zone_hidden, 'c': zone_cell}
        self.theano_rng = RandomStreams(rng.randint(2 ** 30))
VaeLstmAdamTrainer.py 文件源码 项目:deep-motion-analysis 作者: Brimborough 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def __init__(self, rng, batchsize, epochs=100, alpha=0.001, beta1=0.9, beta2=0.999, eps=1e-08, gamma=0.1, cost='mse'):
        self.alpha = alpha
        self.beta1 = beta1
        self.beta2 = beta2
        self.eps = eps
        self.gamma = gamma
        self.rng = rng
        self.theano_rng = RandomStreams(rng.randint(2 ** 30))
        self.epochs = epochs
        self.batchsize = batchsize
        if   cost == 'mse':
            self.cost = lambda network, x, y: T.mean((network(x) - y)**2)
        elif cost == 'cross_entropy':
            self.cost = lambda network, x, y: T.nnet.binary_crossentropy(network(x), y).mean()
        else:
            self.cost = cost
main.py 文件源码 项目:doublecnn 作者: Shuangfei 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def __init__(self, incoming, num_filters, filter_size, stride=(1,1),
                 pad=0, untie_biases=False, kernel_size=3, kernel_pool_size=1,
                 W=lasagne.init.GlorotUniform(), b=lasagne.init.Constant(0.),
                 nonlinearity=lasagne.nonlinearities.rectify, flip_filters=True,
                 convolution=theano.tensor.nnet.conv2d, **kwargs):
        super(DoubleConvLayer, self).__init__(incoming, num_filters, filter_size,
                                              stride, 0, untie_biases, W, b,
                                              nonlinearity, flip_filters, n=2,
                                              **kwargs)
        self.convolution = convolution
        self.kernel_size = kernel_size
        self.pool_size = kernel_pool_size
        self.filter_offset = self.filter_size[0] - self.kernel_size + 1

        self.n_times = self.filter_offset ** 2
        self.rng = RandomStreams(123)
rnnlm.py 文件源码 项目:adaptiveSM 作者: OlivierShi 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __init__(self, n_input, n_hidden, n_batch, n_output, optimizer=sgd, p=0.5, use_adaptive_softmax=True):
        self.x = T.imatrix('batched_sequence_x')  # n_batch, maxlen
        self.x_mask = T.matrix('x_mask')
        self.y = T.imatrix('batched_sequence_y')
        self.y_mask = T.matrix('y_mask')

        self.n_input = n_input
        self.n_hidden = n_hidden
        self.n_output = n_output
        init_Embd = np.asarray(np.random.uniform(low=-np.sqrt(1. / n_output),
                                                 high=np.sqrt(1. / n_output),
                                                 size=(n_output, n_input)),
                               dtype=theano.config.floatX)
        self.E = theano.shared(value=init_Embd, name='word_embedding',borrow=True)

        self.optimizer = optimizer
        self.p = p
        self.is_train = T.iscalar('is_train')
        self.n_batch = n_batch
        self.epsilon = 1.0e-15
        self.rng = RandomStreams(1234)
        self.use_adaptive_softmax = use_adaptive_softmax
        self.build()
controller.py 文件源码 项目:Buffe 作者: bentzinir 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def _connect(self, game_params, solver_params):

        self.dt = game_params['dt']
        self.w = game_params['width']
        self.inv_m = np.float32(1./game_params['m'])
        self.v_max = game_params['v_max']
        self.c_0_w_accel = solver_params['controler_0']['w_accel']
        self.c_0_w_progress = solver_params['controler_0']['w_progress']
        self.c_1_w_progress = solver_params['controler_1']['w_progress']
        self.c_1_w_mines = solver_params['controler_1']['w_mines']
        self.c_1_w_step_size = solver_params['controler_1']['w_step_size']
        self.d_mines = game_params['d_mines']
        self.n_mines = game_params['n_mines']
        self.v_max = game_params['v_max']
        self.switch_interval = solver_params['switch_interval']
        self.srng = RandomStreams()
utils.py 文件源码 项目:GRAN 作者: jiwoongim 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def get_corrupted_input(rng, input, corruption_level, ntype='zeromask'):
    ''' depending on requirement, returns input corrupted by zeromask/gaussian/salt&pepper'''
    MRG = RNG_MRG.MRG_RandomStreams(rng.randint(2 ** 30))
    #theano_rng = RandomStreams()
    if corruption_level == 0.0:
        return input

    if ntype=='zeromask':
        return MRG.binomial(size=input.shape, n=1, p=1-corruption_level,dtype=theano.config.floatX) * input
    elif ntype=='gaussian':
        return input + MRG.normal(size = input.shape, avg = 0.0,
                std = corruption_level, dtype = theano.config.floatX)
    elif ntype=='salt_pepper':

        # salt and pepper noise
        print 'DAE uses salt and pepper noise'
        a = MRG.binomial(size=input.shape, n=1,\
                p=1-corruption_level,dtype=theano.config.floatX)
        b = MRG.binomial(size=input.shape, n=1,\
                p=corruption_level,dtype=theano.config.floatX)

        c = T.eq(a,0) * b
        return input * a + c
da.py 文件源码 项目:structured-output-ae 作者: sbelharbi 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def __init__(self,
                 input,
                 nvis,
                 nhid,
                 rnd=None,
                 theano_rng=None,
                 bhid=None,
                 cost_type=CostType.MeanSquared,
                 momentum=1,
                 L1_reg=-1,
                 L2_reg=-1,
                 sparse_initialize=False,
                 nonlinearity=NonLinearity.TANH,
                 bvis=None,
                 tied_weights=True,
                 reverse=False,
                 corruption_level=0.):
        super(DenoisingAutoencoder, self).__init__(
            input=input,
            nvis=nvis,
            nhid=nhid,
            rnd=rnd,
            bhid=bhid,
            cost_type=cost_type,
            momentum=momentum,
            L1_reg=L1_reg,
            L2_reg=L2_reg,
            sparse_initialize=sparse_initialize,
            nonlinearity=nonlinearity,
            bvis=bvis,
            tied_weights=tied_weights,
            reverse=reverse)
        self.corruption_level = corruption_level

        if not theano_rng:
            theano_rng = RandomStreams(rnd.randint(2 ** 30))
        self.theano_rng = theano_rng

    # Overrite this function:
ca.py 文件源码 项目:structured-output-ae 作者: sbelharbi 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def __init__(self,
                 input,
                 nvis,
                 nhid,
                 rnd=None,
                 theano_rng=None,
                 bhid=None,
                 sigma=0.06,
                 nonlinearity=NonLinearity.SIGMOID,
                 cost_type=CostType.MeanSquared,
                 bvis=None):

        self.sigma = sigma
        super(ContractiveAutoencoder, self).__init(
            input=input,
            nvis=nvis,
            nhid=nhid,
            rnd=rnd,
            bhid=bhid,
            cost_type=cost_type,
            nonlinearity=nonlinearity,
            sparse_initialize=True,
            bvis=bvis)
        # Create a Theano random generator that gives symbolic random values
        if not theano_rng:
            theano_rng = RandomStreams(rnd.randint(2**30))
        self.theano_rng = theano_rng
mask_generator.py 文件源码 项目:Neural-Photo-Editor 作者: ajbrock 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __init__(self, input_size, hidden_sizes, l, random_seed=1234):
        self._random_seed = random_seed
        self._mrng = MRG_RandomStreams(seed=random_seed)
        self._rng = RandomStreams(seed=random_seed)

        self._hidden_sizes = hidden_sizes
        self._input_size = input_size
        self._l = l

        self.ordering = theano.shared(value=np.arange(input_size, dtype=theano.config.floatX), name='ordering', borrow=False)

        # Initial layer connectivity
        self.layers_connectivity = [theano.shared(value=(self.ordering + 1).eval(), name='layer_connectivity_input', borrow=False)]
        for i in range(len(self._hidden_sizes)):
            self.layers_connectivity += [theano.shared(value=np.zeros((self._hidden_sizes[i]), dtype=theano.config.floatX), name='layer_connectivity_hidden{0}'.format(i), borrow=False)]
        self.layers_connectivity += [self.ordering]

        ## Theano functions
        new_ordering = self._rng.shuffle_row_elements(self.ordering)
        self.shuffle_ordering = theano.function(name='shuffle_ordering',
                                                inputs=[],
                                                updates=[(self.ordering, new_ordering), (self.layers_connectivity[0], new_ordering + 1)])

        self.layers_connectivity_updates = []
        for i in range(len(self._hidden_sizes)):
            self.layers_connectivity_updates += [self._get_hidden_layer_connectivity(i)]
        # self.layers_connectivity_updates = [self._get_hidden_layer_connectivity(i) for i in range(len(self._hidden_sizes))]  # WTF THIS DO NOT WORK
        self.sample_connectivity = theano.function(name='sample_connectivity',
                                                   inputs=[],
                                                   updates=[(self.layers_connectivity[i+1], self.layers_connectivity_updates[i]) for i in range(len(self._hidden_sizes))])

        # Save random initial state
        self._initial_mrng_rstate = copy.deepcopy(self._mrng.rstate)
        self._initial_mrng_state_updates = [state_update[0].get_value() for state_update in self._mrng.state_updates]

        # Ensuring valid initial connectivity
        self.sample_connectivity()
utils.py 文件源码 项目:nn-patterns 作者: pikinder 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def get_conv_xy(layer, deterministic=True):
    w_np = layer.W.get_value()
    input_layer = layer.input_layer
    if layer.pad == 'same':
        input_layer = L.PadLayer(layer.input_layer,
                                 width=np.array(w_np.shape[2:])/2,
                                 batch_ndim=2)
    input_shape = L.get_output_shape(input_layer)
    max_x = input_shape[2] - w_np.shape[2]
    max_y = input_shape[3] - w_np.shape[3]
    srng = RandomStreams()
    patch_x = srng.random_integers(low=0, high=max_x)
    patch_y = srng.random_integers(low=0, high=max_y)

    #print("input_shape shape: ", input_shape)
    #print("pad: \"%s\""% (layer.pad,))
    #print(" stride: " ,layer.stride)
    #print("max_x %d max_y %d"%(max_x,max_y))

    x = L.get_output(input_layer, deterministic=deterministic)
    x = x[:, :,
          patch_x:patch_x + w_np.shape[2], patch_y:patch_y + w_np.shape[3]]
    x = T.flatten(x, 2)  # N,D

    w = layer.W
    if layer.flip_filters:
        w = w[:, :, ::-1, ::-1]
    w = T.flatten(w, outdim=2).T  # D,O
    y = T.dot(x, w) # N,O
    if layer.b is not None:
        y += T.shape_padaxis(layer.b, axis=0)
    return x, y
methods.py 文件源码 项目:lightML 作者: jfzhang95 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def dropout(X, dropout_prob=0.0):
    retain_prob = 1 - dropout_prob
    srng = RandomStreams(seed=1234)
    X *= srng.binomial(X.shape, p=retain_prob, dtype=theano.config.floatX)
    X /= retain_prob
    return X

# def dropout(x, dropout_prob):
#     if dropout_prob < 0. or dropout_prob > 1.:
#         raise Exception('Dropout level must be in interval [0, 1]')
#     retain_prob = 1. - dropout_prob
#     sample=np.random.binomial(n=1, p=retain_prob, size=x.shape)
#     x *= sample
#     x /= retain_prob
#     return x
tools.py 文件源码 项目:cortex 作者: rdevon 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def get_srng():
    '''Shared Randomstream.

    '''
    srng = SRandomStreams(random.randint(0, 1000000))
    return srng
layers.py 文件源码 项目:e2e-ie-release 作者: rasmusbergpalm 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def __init__(self, incoming, input_size, output_size, W=init.Normal(), dropout=0., **kwargs):
        super(DropoutEmbeddingLayer, self).__init__(incoming, **kwargs)

        self.input_size = input_size
        self.output_size = output_size
        self.dropout = dropout
        self._srng = RandomStreams(get_rng().randint(1, 2147462579))

        self.W = self.add_param(W, (input_size, output_size), name="W")
models.py 文件源码 项目:SERT 作者: cvangysel 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def _negative_sampling(self, num_negative_samples, target_indices):
        assert num_negative_samples > 0

        logging.debug('Stochastically sampling %d negative instances '
                      'out of %d classes (%.2f%%).',
                      num_negative_samples, self.num_entities,
                      100.0 *
                      float(num_negative_samples) / self.num_entities)

        from theano.tensor.shared_randomstreams import RandomStreams

        srng = RandomStreams(
            seed=np.random.randint(low=0, high=(1 << 30)))

        rng_sample_size = (self.batch_size, num_negative_samples,)

        logging.debug(
            'Using %s for random sample generation of %s tensors.',
            RandomStreams, rng_sample_size)

        logging.debug('For every batch %d random integers are sampled.',
                      np.prod(rng_sample_size))

        random_negative_indices = srng.choice(
            rng_sample_size,
            a=self.num_entities,
            p=self.clazz_distribution)

        if self.__DEBUG__:
            random_negative_indices = theano.printing.Print(
                'random_negative_indices')(random_negative_indices)

        return random_negative_indices
layers.py 文件源码 项目:merlin 作者: CSTR-Edinburgh 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __init__(self, rng, x, n_in, n_out, W = None, b = None, activation = T.tanh, p=0.0, training=0):
        n_in = int(n_in)  # ensure sizes have integer type
        n_out = int(n_out)# ensure sizes have integer type

        self.x = x

        if p > 0.0:
            if training==1:
                srng = RandomStreams(seed=123456)
                self.x = T.switch(srng.binomial(size=x.shape, p=p), x, 0)
            else:
                self.x =  (1-p) * x


        # initialize with 0 the weights W as a matrix of shape (n_in, n_out)
        if W is None:
            W_value = numpy.asarray(rng.normal(0.0, 1.0/numpy.sqrt(n_in),
                      size=(n_in, n_out)), dtype=theano.config.floatX)
            W = theano.shared(value=W_value,
                              name='W', borrow=True)
        if b is None:
            b = theano.shared(value=numpy.zeros((n_out,),
                              dtype=theano.config.floatX),
                              name='b', borrow=True)

        self.W = W
        self.b = b

        self.delta_W = theano.shared(value = numpy.zeros((n_in,n_out),
                                     dtype=theano.config.floatX), name='delta_W')

        self.delta_b = theano.shared(value = numpy.zeros_like(self.b.get_value(borrow=True),
                                     dtype=theano.config.floatX), name='delta_b')

        self.output = T.dot(self.x, self.W) + self.b
        self.output = activation(self.output)

        self.params = [self.W, self.b]
        self.delta_params = [self.delta_W, self.delta_b]
MIDS.py 文件源码 项目:MIDS 作者: freegraphics 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def train_rates():
    consts = Consts()
    rng = numpy.random.RandomState()
    theano_rng = RandomStreams(rng.randint(2 ** 30))
    rs = RecommenderSystem(rng= rng,theano_rng = theano_rng,consts=consts)
    validate_loss_min = 0
    validate_loss = 0
    for i in numpy.arange(100000):
        lt = time.time()
        for j in numpy.arange(consts.ids_move_count):
            loss_rates = rs.train_rates(learning_rate = consts.result_learning_rate)
            t1 = time.time()
            if t1>lt+1:
                sys.stdout.write("\t\t\t\t\t\t\t\t\t\r")
                sys.stdout.write("[%d] loss = %f , val = %f valmin = %f\r" % (i,loss_rates,validate_loss,validate_loss_min))
                lt = lt+1
        trace_rates(i + (consts.load_from_ids*consts.save_cycles),loss_rates,validate_loss_min,validate_loss,consts.trace_rates_file_name)
        if i % consts.save_cycles == 0:
            rs.save_rates((i/consts.save_cycles) + consts.load_from_ids,consts)
        if i % consts.validate_cycles == 0:
            validate_loss = rs.validate_rates(consts=consts)
            if validate_loss_min==0 or validate_loss<validate_loss_min:
                validate_loss_min = validate_loss
                rs.save_rates(0,consts)
        consts.update_index(i + (consts.load_from_ids*consts.save_cycles))

    return
randomdropouttest.py 文件源码 项目:neural_network 作者: mnpappo 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def randdrop(x, level, noise_shape=None, seed=None):
    '''Sets entries in `x` to zero at random,
    while scaling the entire tensor.
    # Arguments
        x: tensor
        level: fraction of the entries in the tensor
            that will be set to 0.
        noise_shape: shape for randomly generated keep/drop flags,
            must be broadcastable to the shape of `x`
        seed: random seed to ensure determinism.
    '''
    # if level < 0. or level >= 1:
    #     raise Exception('Dropout level must be in interval [0, 1[.')
    if seed is None:
        seed = np.random.randint(1337)

    rng = RandomStreams(seed=seed)
    retain_prob = 1 - level

    if noise_shape is None:
        random_tensor = rng.binomial(x.shape, p=retain_prob, dtype=x.dtype)
    else:
        random_tensor = rng.binomial(noise_shape, p=retain_prob, dtype=x.dtype)
        random_tensor = T.patternbroadcast(random_tensor, [dim == 1 for dim in noise_shape])

    x *= random_tensor
    x /= retain_prob
    return x
sparse_lstm.py 文件源码 项目:sequence-based-recommendations 作者: rdevooght 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def __init__(self, incoming, num_units, num_outputs=0.01, **kwargs):
        super(BlackoutLayer, self).__init__(incoming, num_units, **kwargs)
        self._srng = RandomStreams(get_rng().randint(1, 2147462579))
        if num_outputs < 1:
            num_outputs = num_outputs * num_units
        self.num_outputs = int(num_outputs)


问题


面经


文章

微信
公众号

扫码关注公众号