Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有一种方法可以在训练中消除弱体重?例如,如果权重的绝对值低于.05,只需将该权重设置为0即可_Python_Tensorflow_Keras_Keras Layer - Fatal编程技术网

Python 有没有一种方法可以在训练中消除弱体重?例如,如果权重的绝对值低于.05,只需将该权重设置为0即可

Python 有没有一种方法可以在训练中消除弱体重?例如,如果权重的绝对值低于.05,只需将该权重设置为0即可,python,tensorflow,keras,keras-layer,Python,Tensorflow,Keras,Keras Layer,我遇到了麻烦,似乎你不能直接编辑一个张量,或者简单地将它转换成numpy,然后在训练期间以这种形式编辑它。在某种程度上,我所寻找的是与和中存在的clip函数相反的函数。我不想确保所有值都在最小值和最大值之间,而是想将最小值和最大值之间的所有值切换为0。最小值和最大值很可能是相同的值,因此它会将绝对值小于某个输入值的任何权重归零 class DeleteWeakConnectionsDenseLayer(keras.layers.Layer): def __init__(self, units,

我遇到了麻烦,似乎你不能直接编辑一个张量,或者简单地将它转换成numpy,然后在训练期间以这种形式编辑它。在某种程度上,我所寻找的是与和中存在的clip函数相反的函数。我不想确保所有值都在最小值和最大值之间,而是想将最小值和最大值之间的所有值切换为0。最小值和最大值很可能是相同的值,因此它会将绝对值小于某个输入值的任何权重归零

class DeleteWeakConnectionsDenseLayer(keras.layers.Layer):
def __init__(self, units, weak_threshold, **kwargs):
    super(DeleteWeakConnectionsDenseLayer, self).__init__(**kwargs)
    self.units = units
    self.weak_threshold = weak_threshold

def build(self, input_shape):

    self.w = self.add_weight(
        shape=(input_shape[-1], self.units),
        initializer="random_normal",
        trainable=True,
    )
    self.b = self.add_weight(
        shape=(self.units,), initializer="random_normal", trainable=True
    )

def call(self, inputs, training=False):
    if training:
        new_weights = #Code Here such that weights whose absolute value is below self.weakthreshold are reassigned to 0
        self.w.assign(new_weights)  # Assign preserves tf.Variable
    else:
        pass #could think about multiplying all weights by a constant here
    return tf.nn.relu(tf.matmul(inputs, self.w) + self.b)
请尝试以下代码:

  def call(self, inputs, training=False):
      if training:
          mask = tf.abs(self.w) > self.weak_threshold
          new_weights = self.w * tf.cast(mask, tf.float32)
          self.w.assign(new_weights)  # Assign preserves tf.Variable
      else:
          pass #could think about multiplying all weights by a constant here
      return tf.nn.relu(tf.matmul(inputs, self.w) + self.b)
请尝试以下代码:

  def call(self, inputs, training=False):
      if training:
          mask = tf.abs(self.w) > self.weak_threshold
          new_weights = self.w * tf.cast(mask, tf.float32)
          self.w.assign(new_weights)  # Assign preserves tf.Variable
      else:
          pass #could think about multiplying all weights by a constant here
      return tf.nn.relu(tf.matmul(inputs, self.w) + self.b)

这不适用于负数吗?请查看编辑后的答案这不适用于负数吗?请查看编辑后的答案