network_sparse.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:pruning_with_tensorflow 作者: ex4sperans 项目源码 文件源码
def _build_network(self,
                       inputs: tf.Tensor,
                       sparse_layers: list,
                       activation_fn: callable) -> tf.Tensor:

        with tf.variable_scope('network'):

            net = inputs

            self.weight_tensors = []

            bias_initializer = tf.constant_initializer(0.1)

            for i, layer in enumerate(sparse_layers):

                with tf.variable_scope('layer_{layer}'.format(layer=i+1)):

                    # create variables based on sparse values                    
                    with tf.variable_scope('sparse'):

                        indicies = tf.get_variable(name='indicies',
                                                   initializer=layer.indices,
                                                   dtype=tf.int16)

                        values = tf.get_variable(name='values',
                                                 initializer=layer.values,
                                                 dtype=tf.float32)

                        dense_shape = tf.get_variable(name='dense_shape',
                                                      initializer=layer.dense_shape,
                                                      dtype=tf.int64)

                    # create a weight tensor based on the created variables
                    weights = tf.sparse_to_dense(tf.cast(indicies, tf.int64),
                                                 dense_shape,
                                                 values)

                    self.weight_tensors.append(weights)

                    name = 'bias'
                    bias = tf.get_variable(name=name,
                                           initializer=layer.bias)

                    net = tf.matmul(net, weights) + bias

                    if i < len(sparse_layers) - 1:
                        net = activation_fn(net)

            return net
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号