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
在tensorflow/tflearn中使用自定义层定义连接_Tensorflow_Tflearn - Fatal编程技术网

在tensorflow/tflearn中使用自定义层定义连接

在tensorflow/tflearn中使用自定义层定义连接,tensorflow,tflearn,Tensorflow,Tflearn,我希望使用矩阵指定激活节点之间的连接,而不是完全连接的层。例如: 我有一个连接到10节点层的20节点层。使用典型的全连接层,我的W矩阵为20 x 10,其中b向量大小为10 我的激活看起来像是relu(Wx+b) 如果我有一个由1和0组成的矩阵,其大小与W相同,我们称之为F,我可以在W和F之间进行成对乘法,以移除第一层(20个节点)和第二层(10个节点)之间的连接 这是我目前的代码: F.shape # (20, 10) import tflearn import tensorflow as t

我希望使用矩阵指定激活节点之间的连接,而不是完全连接的层。例如:

我有一个连接到10节点层的20节点层。使用典型的全连接层,我的
W
矩阵为20 x 10,其中
b
向量大小为10

我的激活看起来像是
relu(Wx+b)

如果我有一个由1和0组成的矩阵,其大小与
W
相同,我们称之为
F
,我可以在
W
F
之间进行成对乘法,以移除第一层(20个节点)和第二层(10个节点)之间的连接

这是我目前的代码:

F.shape
# (20, 10)
import tflearn
import tensorflow as tf

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

first = tflearn.fully_connected(input, 20, activation='relu')
# Here is where I want to use a custom function, that uses my F matrix
# I dont want the second layer to be fully connected to the first, 
# I want only connections that are ones (and not zeros) in F

# Currently:
second = tflearn.fully_connected(first, 10, activation='relu')
# What I want:
second = tflearn.custom_layer(first, my_fun)
我的乐趣给了我:
relu((FW)X+b)
FW
是成对乘法


如何创建此函数?我似乎找不到tflearn如何实现的示例,但我也知道tflearn也允许基本tensorflow函数

严格使用tflearn很难做到这一点,但如果您愿意包含基本tensorflow操作,那么它很简单:

F.shape
# (20, 10)
import tflearn
import tensorflow as tf

input = tflearn.input_data(shape=[None, num_input])
tf_F = tf.constant(F, shape=[20, 10])

first = tflearn.fully_connected(input, 20, activation='relu')
# Here is where I want to use a custom function, that uses my F matrix
# I want only connections that are ones (and not zeros) in F

# Old:
# second = tflearn.fully_connected(first, 10, activation='relu')
# Solution:
W = tf.Variable(tf.random_uniform([20, 10]), name='Weights')
b = tf.Variable(tf.zeros([10]), name='biases')
W_filtered = tf.mul(tf_F, W)
second = tf.matmul( W_filtered, first) + b