python - 在训练期间固定神经网络中的一部分权重

标签 python tensorflow theano pycaffe pytorch

我正在考虑创建一个定制的神经网络。基本结构和往常一样,但我想截断层与层之间的连接。例如,如果我构建一个具有两个隐藏层的网络,我想删除一些权重并保留其他权重,如下所示:

enter image description here

这不是传统的dropout(以避免过度拟合),因为应该指定并固定剩余的权重(连接)。

python有什么方法可以做到吗? Tensorflow、pytorch、theano 或任何其他模块?

最佳答案

是的,您可以在 tensorflow 中执行此操作。

你的 tensorflow 代码中会有这样的层:

m = tf.Variable( [width,height] , dtype=tf.float32  ))
b = tf.Variable( [height] , dtype=tf.float32  ))
h = tf.sigmoid( tf.matmul( x,m ) + b )

你想要的是一些新的矩阵,我们称它为 k 表示 kill。它会杀死特定的神经连接。神经连接在 m 中定义。这将是您的新配置

k = tf.Constant( kill_matrix , dtype=tf.float32 )
m = tf.Variable( [width,height] , dtype=tf.float32  )
b = tf.Variable( [height] , dtype=tf.float32  )
h = tf.sigmoid( tf.matmul( x, tf.multiply(m,k) ) + b )

您的 kill_matrix 是一个由 1 和 0 组成的矩阵。为您想要保留的每个神经连接插入一个 1,为您想要杀死的每个神经连接插入一个 0。

关于python - 在训练期间固定神经网络中的一部分权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43851657/

相关文章:

python - Cython:对于 i 来自 1 <= i < N

python - Keras 后端(tensorflow)与 Keras

python - Theano 中的属性错误

python - 在 virtualenv 中使用 CUDA 为 Theano 服务

python - 如何在每个 pytest-xdist 进程中运行设置

python 应用程序到 exe 无法在 WinSRV2003 上运行

tensorflow - 将tensorflow-serving-api 1.15版本安装到Centos 8

python - 'tensorflow' 没有属性 'to_int32'

python - 如何将 numpy.ndarray 对象转换为 theano.tensor?

python - Django - 将模型字段复制到另一个字段