在Tensorflow中将常数矩阵引入乘法时遇到困难

在Tensorflow中将常数矩阵引入乘法时遇到困难,tensorflow,neural-network,Tensorflow,Neural Network,我正在尝试实现一个未完全连接的层。我有一个矩阵,它在变量连通性_matrix中指定了我想要的连通性,这是一个由1和0组成的numpy数组 我目前尝试实现该层的方法是将权重成对乘以该连通矩阵F: 在tensorflow中,这是正确的方法吗?这是我到目前为止所拥有的 import numpy as np import tensorflow as tf import tflearn num_input = 10 num_layer1 = 313 num_output = 700 # For ex

我正在尝试实现一个完全连接的层。我有一个矩阵,它在变量
连通性_matrix
中指定了我想要的连通性,这是一个由1和0组成的numpy数组

我目前尝试实现该层的方法是将权重成对乘以该连通矩阵
F

在tensorflow中,这是正确的方法吗?这是我到目前为止所拥有的

import numpy as np
import tensorflow as tf
import tflearn

num_input = 10
num_layer1 = 313
num_output = 700

# For example:
connectivity_matrix = np.array(np.random.choice([0, 1], size=(num_layer1, num_output)), dtype='float32')

input = tflearn.input_data(shape=[None, num_input])

# Here is where I specify the connectivity in tensorflow
connectivity = tf.constant(connectivity_matrix, shape=[num_layer1, num_output])

# One basic, fully connected layer
layer1 = tflearn.fully_connected(input, num_layer1, activation='relu')

# Here is where I want to have a non-fully connected layer
W = tf.Variable(tf.random_uniform([num_layer1, num_output]))
b = tf.Variable(tf.zeros([num_output]))
# so take a fully connected W, and do a pairwise multiplication with my tf_connectivity matrix
W_filtered = tf.mul(connectivity, W)
output = tf.matmul(layer1, W_filtered) + b

在每次迭代中屏蔽掉不需要的连接应该是可行的,但我不确定收敛特性是什么样的。足够小的学习率可以吗

另一种方法是惩罚成本函数中不需要的权重。您可以使用掩码矩阵,在不需要的连接处使用1,在需要的连接处使用0(或具有更平滑的过渡)。这将乘以权重,平方/缩放并添加到成本函数中。这应该更加顺利地衔接


附言:如果你在这方面取得了进展,听到你的评论会很高兴,因为我也在处理这个问题

就我的目的而言,我上面使用的方法效果很好。然而,我没有取得任何进一步的进展