Initializes weights of LSTM layer.
    Weights and biases are initialized with uniform(-init_weight, init_weight)
    distribution.

    :param lstm: instance of torch.nn.LSTM
    :param init_weight: range for the uniform initializer
174230
def init_lstm_(lstm, init_weight=0.1):
    
    # Initialize hidden-hidden weights
    init.uniform_(lstm.weight_hh_l0.data, -init_weight, init_weight)
    # Initialize input-hidden weights:
    init.uniform_(lstm.weight_ih_l0.data, -init_weight, init_weight)

    # Initialize bias. PyTorch LSTM has two biases, one for input-hidden GEMM
    # and the other for hidden-hidden GEMM. Here input-hidden bias is
    # initialized with uniform distribution and hidden-hidden bias is
    # initialized with zeros.
    init.uniform_(lstm.bias_ih_l0.data, -init_weight, init_weight)
    init.zeros_(lstm.bias_hh_l0.data)

    if lstm.bidirectional:
        init.uniform_(lstm.weight_hh_l0_reverse.data, -init_weight, init_weight)
        init.uniform_(lstm.weight_ih_l0_reverse.data, -init_weight, init_weight)

        init.uniform_(lstm.bias_ih_l0_reverse.data, -init_weight, init_weight)
        init.zeros_(lstm.bias_hh_l0_reverse.data)
156209
def _init_model(self):
        
        model = Sequential()
        model.add(ConvLSTM2D(filters=self.filters, kernel_size=self.kernel_size, activation=self.activation,
                             input_shape=[self.time_delay] + list(self.image_size) + [self.channels],
                             data_format='channels_last', return_sequences=True))
        model.add(BatchNormalization())
        model.add(ConvLSTM2D(filters=self.filters, kernel_size=self.kernel_size, activation=self.activation,
                             input_shape=(self.time_delay, self.channels) + self.image_size,
                             data_format='channels_last', return_sequences=True))
        model.add(BatchNormalization())
        model.add(ConvLSTM2D(filters=self.filters, kernel_size=self.kernel_size, activation=self.activation))
        model.add(BatchNormalization())
        model.add(Conv2D(filters=1, kernel_size=self.kernel_size, activation="sigmoid", data_format="channels_last"))
        model.add(Flatten())
        model.add(Dense(units=len(self.emotion_map.keys()), activation="sigmoid"))
        if self.verbose:
            model.summary()
        self.model = model
288545
@Override
    public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException {
        this.weights = new HashMap<>();
        if (weights.containsKey(ALPHA))
            this.weights.put(PReLUParamInitializer.WEIGHT_KEY, weights.get(ALPHA));
        else
            throw new InvalidKerasConfigurationException("Parameter " + ALPHA + " does not exist in weights");
        if (weights.size() > 1) {
            Set<String> paramNames = weights.keySet();
            paramNames.remove(ALPHA);
            String unknownParamNames = paramNames.toString();
            log.warn("Attemping to set weights for unknown parameters: "
                    + unknownParamNames.substring(1, unknownParamNames.length() - 1));
        }
    }
50754
def initialize(self, useRandomEncoder):
    
    self.setRandomSeed(self.seed)

    self.dim = numpy.shape(self.spatialConfig)[-1]

    self.spatialMap = dict( zip( map(tuple, list(self.spatialConfig)),
                              self.sensoryInputElements))

    self.lengthMotorInput1D = (2*self.maxDisplacement + 1) * \
                                                    self.numActiveBitsMotorInput

    uniqueSensoryElements = list(set(self.sensoryInputElementsPool))

    if useRandomEncoder:
      self.sensoryEncoder = SDRCategoryEncoder(n=1024,
                                w=self.numActiveBitsSensoryInput,
                                categoryList=uniqueSensoryElements,
                                forced=True)
      self.lengthSensoryInput = self.sensoryEncoder.getWidth()

    else:
      self.lengthSensoryInput = (len(self.sensoryInputElementsPool)+1) * \
                                          self.numActiveBitsSensoryInput

      self.sensoryEncoder = CategoryEncoder(w=self.numActiveBitsSensoryInput,
                            categoryList=uniqueSensoryElements, forced=True)

    motorEncoder1D =  ScalarEncoder(n=self.lengthMotorInput1D,
                                    w=self.numActiveBitsMotorInput,
                                    minval=-self.maxDisplacement,
                                    maxval=self.maxDisplacement,
                                    clipInput=True,
                                    forced=True)

    self.motorEncoder = VectorEncoder(length=self.dim, encoder=motorEncoder1D)
50007
def initialize_means_weights(data, clusters, init_means=None, init_weights=None, initialization='tsvd', max_assign_weight=0.75):
    
    genes, cells = data.shape
    if init_means is None:
        if init_weights is not None:
            if len(init_weights.shape)==1:
                means = initialize_means(data, init_weights, clusters)
            else:
                means = initialize_means(data, init_weights.argmax(0),
                        clusters, max_assign_weight=max_assign_weight)
        elif initialization=='cluster':
            assignments, means = poisson_cluster(data, clusters)
            if init_weights is None:
                init_weights = initialize_from_assignments(assignments, clusters,
                        max_assign_weight=max_assign_weight)
        elif initialization=='kmpp':
            means, assignments = kmeans_pp(data, clusters)
        elif initialization=='km':
            km = KMeans(clusters)
            assignments = km.fit_predict(log1p(cell_normalize(data)).T)
            init_weights = initialize_from_assignments(assignments, clusters,
                    max_assign_weight)
            means = initialize_means(data, assignments, clusters)
        elif initialization=='tsvd':
            n_components = min(50, genes-1)
            #tsvd = TruncatedSVD(min(50, genes-1))
            km = KMeans(clusters)
            # remove dependence on sklearn tsvd b/c it has a bug that
            # prevents it from working properly on long inputs 
            # if num elements > 2**31
            #data_reduced = tsvd.fit_transform(log1p(cell_normalize(data)).T)
            U, Sigma, VT = randomized_svd(log1p(cell_normalize(data)).T,
                    n_components)
            data_reduced = U*Sigma
            assignments = km.fit_predict(data_reduced)
            init_weights = initialize_from_assignments(assignments, clusters,
                    max_assign_weight)
            means = initialize_means(data, assignments, clusters)
        elif initialization == 'random' or initialization == 'rand':
            # choose k random cells and set means to those
            selected_cells = np.random.choice(range(cells), size=clusters,
                    replace=False)
            means = data[:, selected_cells]
            if sparse.issparse(means):
                means = means.toarray()
    else:
        means = init_means.copy()
    means = means.astype(float)
    if init_weights is None:
        if init_means is not None:
            if initialization == 'cluster':
                assignments, means = poisson_cluster(data, clusters,
                        init=init_means, max_iters=1)
                w_init = initialize_from_assignments(assignments, clusters,
                        max_assign_weight)
            elif initialization == 'km':
                km = KMeans(clusters, init=log1p(init_means.T), max_iter=1)
                assignments = km.fit_predict(log1p(cell_normalize(data)).T)
                w_init = initialize_from_assignments(assignments, clusters,
                        max_assign_weight)
            else:
                w_init = np.random.random((clusters, cells))
                w_init = w_init/w_init.sum(0)
        else:
            w_init = np.random.random((clusters, cells))
            w_init = w_init/w_init.sum(0)
    else:
        if len(init_weights.shape)==1:
            init_weights = initialize_from_assignments(init_weights, clusters,
                    max_assign_weight)
        w_init = init_weights.copy()
    return means, w_init
193298
def setInitialCenters(self, centers, weights):
        
        self._model = StreamingKMeansModel(centers, weights)
        return self
945542
def reset_parameters(self, init_weight):
        
        stdv = 1. / math.sqrt(self.num_units)
        self.linear_att.data.uniform_(-init_weight, init_weight)

        if self.normalize:
            self.normalize_scalar.data.fill_(stdv)
            self.normalize_bias.data.zero_()
747770
def initialize(self):
    
    if self._pooler is None:
      params = {
        "inputWidth": self.inputWidth,
        "lateralInputWidths": [self.cellCount] * self.numOtherCorticalColumns,
        "cellCount": self.cellCount,
        "sdrSize": self.sdrSize,
        "onlineLearning": self.onlineLearning,
        "maxSdrSize": self.maxSdrSize,
        "minSdrSize": self.minSdrSize,
        "synPermProximalInc": self.synPermProximalInc,
        "synPermProximalDec": self.synPermProximalDec,
        "initialProximalPermanence": self.initialProximalPermanence,
        "minThresholdProximal": self.minThresholdProximal,
        "sampleSizeProximal": self.sampleSizeProximal,
        "connectedPermanenceProximal": self.connectedPermanenceProximal,
        "predictedInhibitionThreshold": self.predictedInhibitionThreshold,
        "synPermDistalInc": self.synPermDistalInc,
        "synPermDistalDec": self.synPermDistalDec,
        "initialDistalPermanence": self.initialDistalPermanence,
        "activationThresholdDistal": self.activationThresholdDistal,
        "sampleSizeDistal": self.sampleSizeDistal,
        "connectedPermanenceDistal": self.connectedPermanenceDistal,
        "inertiaFactor": self.inertiaFactor,
        "seed": self.seed,
      }
      self._pooler = ColumnPooler(**params)
286996
@Override
    public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException {
        this.weights = new HashMap<>();
        if (weights.containsKey(conf.getKERAS_PARAM_NAME_W()))
            this.weights.put(DefaultParamInitializer.WEIGHT_KEY, weights.get(conf.getKERAS_PARAM_NAME_W()));
        else
            throw new InvalidKerasConfigurationException(
                    "Parameter " + conf.getKERAS_PARAM_NAME_W() + " does not exist in weights");
        if (hasBias) {
            if (weights.containsKey(conf.getKERAS_PARAM_NAME_B()))
                this.weights.put(DefaultParamInitializer.BIAS_KEY, weights.get(conf.getKERAS_PARAM_NAME_B()));
            else
                throw new InvalidKerasConfigurationException(
                        "Parameter " + conf.getKERAS_PARAM_NAME_B() + " does not exist in weights");
        }
        removeDefaultWeights(weights, conf);
    }
273422
def init_params(self, initializer=mx.init.Uniform(0.01), **kwargs):
        
        self._module.init_params(initializer=initializer, **kwargs)
1123446
def _init_norm(self, weights):
    """"""
    with tf.variable_scope("init_norm"):
      flat = tf.reshape(weights, [-1, self.layer_depth])
      return tf.reshape(tf.norm(flat, axis=0), (self.layer_depth,))
86064
@Override
    public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException {
        this.weights = new HashMap<>();


        INDArray W;
        if (weights.containsKey(conf.getKERAS_PARAM_NAME_W()))
            W = weights.get(conf.getKERAS_PARAM_NAME_W());
        else
            throw new InvalidKerasConfigurationException(
                    "Keras SimpleRNN layer does not contain parameter " + conf.getKERAS_PARAM_NAME_W());
        this.weights.put(SimpleRnnParamInitializer.WEIGHT_KEY, W);


        INDArray RW;
        if (weights.containsKey(conf.getKERAS_PARAM_NAME_RW()))
            RW = weights.get(conf.getKERAS_PARAM_NAME_RW());
        else
            throw new InvalidKerasConfigurationException(
                    "Keras SimpleRNN layer does not contain parameter " + conf.getKERAS_PARAM_NAME_RW());
        this.weights.put(SimpleRnnParamInitializer.RECURRENT_WEIGHT_KEY, RW);


        INDArray b;
        if (weights.containsKey(conf.getKERAS_PARAM_NAME_B()))
            b = weights.get(conf.getKERAS_PARAM_NAME_B());
        else
            throw new InvalidKerasConfigurationException(
                    "Keras SimpleRNN layer does not contain parameter " + conf.getKERAS_PARAM_NAME_B());
        this.weights.put(SimpleRnnParamInitializer.BIAS_KEY, b);


        if (weights.size() > NUM_TRAINABLE_PARAMS) {
            Set<String> paramNames = weights.keySet();
            paramNames.remove(conf.getKERAS_PARAM_NAME_B());
            paramNames.remove(conf.getKERAS_PARAM_NAME_W());
            paramNames.remove(conf.getKERAS_PARAM_NAME_RW());
            String unknownParamNames = paramNames.toString();
            log.warn("Attemping to set weights for unknown parameters: "
                    + unknownParamNames.substring(1, unknownParamNames.length() - 1));
        }
    }
241070
def _init_network(self):
        """"""
        self.network = Network(self.problem.layers)
        self.weights = Matrices(self.network.shapes)
        if self.load:
            loaded = np.load(self.load)
            assert loaded.shape == self.weights.shape, (
                'weights to load must match problem definition')
            self.weights.flat = loaded
        else:
            self.weights.flat = np.random.normal(
                self.problem.weight_mean, self.problem.weight_scale,
                len(self.weights.flat))
1236149
def load_tf_weights_in_gpt2(model, gpt2_checkpoint_path):
    
    try:
        import re
        import numpy as np
        import tensorflow as tf
    except ImportError:
        print("Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see "
            "https://www.tensorflow.org/install/ for installation instructions.")
        raise
    tf_path = os.path.abspath(gpt2_checkpoint_path)
    print("Converting TensorFlow checkpoint from {}".format(tf_path))
    # Load weights from TF model
    init_vars = tf.train.list_variables(tf_path)
    names = []
    arrays = []
    for name, shape in init_vars:
        print("Loading TF weight {} with shape {}".format(name, shape))
        array = tf.train.load_variable(tf_path, name)
        names.append(name)
        arrays.append(array.squeeze())

    for name, array in zip(names, arrays):
        name = name[6:]  # skip "model/"
        name = name.split('/')
        pointer = model
        for m_name in name:
            if re.fullmatch(r'[A-Za-z]+\d+', m_name):
                l = re.split(r'(\d+)', m_name)
            else:
                l = [m_name]
            if l[0] == 'w' or l[0] == 'g':
                pointer = getattr(pointer, 'weight')
            elif l[0] == 'b':
                pointer = getattr(pointer, 'bias')
            elif l[0] == 'wpe' or l[0] == 'wte':
                pointer = getattr(pointer, l[0])
                pointer = getattr(pointer, 'weight')
            else:
                pointer = getattr(pointer, l[0])
            if len(l) >= 2:
                num = int(l[1])
                pointer = pointer[num]
        try:
            assert pointer.shape == array.shape
        except AssertionError as e:
            e.args += (pointer.shape, array.shape)
            raise
        print("Initialize PyTorch weight {}".format(name))
        pointer.data = torch.from_numpy(array)
    return model
271724
@Override
    public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException {
        this.weights = new HashMap<>();

        INDArray dW;
        if (weights.containsKey(conf.getLAYER_PARAM_NAME_DEPTH_WISE_KERNEL())) {
            dW = weights.get(conf.getLAYER_PARAM_NAME_DEPTH_WISE_KERNEL());
            dW = dW.permute(3, 2, 0, 1);
        } else
            throw new InvalidKerasConfigurationException(
                    "Keras SeparableConvolution2D layer does not contain parameter "
                            + conf.getLAYER_PARAM_NAME_DEPTH_WISE_KERNEL());

        this.weights.put(SeparableConvolutionParamInitializer.DEPTH_WISE_WEIGHT_KEY, dW);

        INDArray pW;
        if (weights.containsKey(conf.getLAYER_PARAM_NAME_POINT_WISE_KERNEL())) {
            pW = weights.get(conf.getLAYER_PARAM_NAME_POINT_WISE_KERNEL());
            pW = pW.permute(3, 2, 0, 1);
        }
        else
            throw new InvalidKerasConfigurationException(
                    "Keras SeparableConvolution2D layer does not contain parameter "
                            + conf.getLAYER_PARAM_NAME_POINT_WISE_KERNEL());

        this.weights.put(SeparableConvolutionParamInitializer.POINT_WISE_WEIGHT_KEY, pW);

        if (hasBias) {
            INDArray bias;
            if (kerasMajorVersion == 2 && weights.containsKey("bias"))
                bias = weights.get("bias");
            else if (kerasMajorVersion == 1 && weights.containsKey("b"))
                bias = weights.get("b");
            else
                throw new InvalidKerasConfigurationException(
                        "Keras SeparableConvolution2D layer does not contain bias parameter");
            this.weights.put(SeparableConvolutionParamInitializer.BIAS_KEY, bias);

        }

    }
1130075
public void initialize(RuleRegressionNode oldLearningNode) {
		if(oldLearningNode.perceptron!=null)
		{
			this.perceptron=new Perceptron(oldLearningNode.perceptron);
			this.perceptron.resetError();
			this.perceptron.setLearningRatio(oldLearningNode.learningRatio);
		}

		if(oldLearningNode.targetMean!=null)
		{
			this.targetMean= new TargetMean(oldLearningNode.targetMean);
			this.targetMean.resetError();
		}
		//reset statistics
		this.nodeStatistics.setValue(0, 0);
		this.nodeStatistics.setValue(1, 0);
		this.nodeStatistics.setValue(2, 0);
	}
737520
def initialize(self):
    

    if self._tm is None:
      params = {
        "columnCount": self.columnCount,
        "basalInputSize": self.basalInputWidth,
        "apicalInputSize": self.apicalInputWidth,
        "cellsPerColumn": self.cellsPerColumn,
        "activationThreshold": self.activationThreshold,
        "initialPermanence": self.initialPermanence,
        "connectedPermanence": self.connectedPermanence,
        "minThreshold": self.minThreshold,
        "sampleSize": self.sampleSize,
        "permanenceIncrement": self.permanenceIncrement,
        "permanenceDecrement": self.permanenceDecrement,
        "basalPredictedSegmentDecrement": self.basalPredictedSegmentDecrement,
        "apicalPredictedSegmentDecrement": self.apicalPredictedSegmentDecrement,
        "maxSynapsesPerSegment": self.maxSynapsesPerSegment,
        "seed": self.seed,
      }

      if self.implementation == "ApicalTiebreakCPP":
        params["learnOnOneCell"] = self.learnOnOneCell
        params["maxSegmentsPerCell"] = self.maxSegmentsPerCell

        import htmresearch_core.experimental
        cls = htmresearch_core.experimental.ApicalTiebreakPairMemory

      elif self.implementation == "ApicalTiebreak":
        params["reducedBasalThreshold"] = self.reducedBasalThreshold

        import htmresearch.algorithms.apical_tiebreak_temporal_memory
        cls = htmresearch.algorithms.apical_tiebreak_temporal_memory.ApicalTiebreakPairMemory

      elif self.implementation == "ApicalDependent":
        params["reducedBasalThreshold"] = self.reducedBasalThreshold

        import htmresearch.algorithms.apical_dependent_temporal_memory
        cls = htmresearch.algorithms.apical_dependent_temporal_memory.TripleMemory

      else:
        raise ValueError("Unrecognized implementation %s" % self.implementation)

      self._tm = cls(**params)