Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
content badge Iframe embedding
swh:1:cnt:24c0b73f489e9b96ec2cee562ba80462e27da210

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
"""
Class of ExpertWeights
"""

import numpy as np
import tensorflow as tf

class ExpertWeights(object):
    def __init__(self, rng, shape , name):
        """rng"""
        self.initialRNG   = rng
        
        """shape"""
        self.weight_shape =  shape                    #4/8 * out * in
        self.bias_shape   =  (shape[0],shape[1],1)    #4/8 * out * 1
        
        """alpha and beta"""
        self.alpha        =  tf.Variable(self.initial_alpha(), name=name+'alpha') 
        self.beta         =  tf.Variable(self.initial_beta(),  name=name+'beta') 
    
        
    """initialize parameters for experts i.e. alpha and beta"""
    def initial_alpha_np(self):
        shape = self.weight_shape
        rng   = self.initialRNG
        alpha_bound = np.sqrt(6. / np.prod(shape[-2:]))
        alpha = np.asarray(
            rng.uniform(low=-alpha_bound, high=alpha_bound, size=shape),
            dtype=np.float32)
        return alpha
    
    def initial_alpha(self):
        alpha = self.initial_alpha_np()
        return tf.convert_to_tensor(alpha, dtype = tf.float32)
    
    def initial_beta(self):
        return tf.zeros(self.bias_shape, tf.float32)
    
    def get_NNweight(self, controlweights, batch_size):  
        a = tf.expand_dims(self.alpha, 1)                           #4*out*in   -> 4*1*out*in
        a = tf.tile(a, [1,batch_size,1,1])                          #4*1*out*in -> 4*?*out*in
        w = tf.expand_dims(tf.expand_dims(controlweights, -1), -1)  #4*?        -> 4*?*1*1   
        r = w * a                                                   #4*?*1*1 m 4*?*out*in
        return tf.reduce_sum(r , axis = 0)                          #?*out*in
        
        
    def get_NNbias(self, controlweights, batch_size):
        b = tf.expand_dims(self.beta, 1)                            #4*out*1   -> 4*1*out*1
        b = tf.tile(b, [1,batch_size,1,1])                          #4*1*out*1 -> 4*?*out*1
        w = tf.expand_dims(tf.expand_dims(controlweights, -1), -1)  #4*?        -> 4*?*1*1  
        r = w * b                                                   #4*?*1*1 m 4*?*out*1
        return tf.reduce_sum(r , axis = 0)                          #?*out*1



def save_EP(alpha, beta, filename, num_experts):
    for i in range(len(alpha)):
        for j in range(num_experts):
            a = alpha[i][j]
            b = beta[i][j]
            a.tofile(filename+'/cp%0i_a%0i.bin' % (i,j))
            b.tofile(filename+'/cp%0i_b%0i.bin' % (i,j))



    
    
    
    
"""
def regularization_penalty(alpha, gamma):
    number_alpha = len(alpha)
    penalty = 0
    for i in range(number_alpha):
        penalty += tf.reduce_mean(tf.abs(alpha[i]))
    return gamma * penalty / number_alpha
"""

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API