【问题标题】:Having trouble introducing a constant matrix to multiplication in Tensorflow无法在 Tensorflow 中引入常数矩阵进行乘法运算
【发布时间】:2017-09-07 03:44:52
【问题描述】:

我正在尝试实现一个完全连接的层。我有一个矩阵,它在变量connectivity_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

【问题讨论】:

    标签: tensorflow neural-network


    【解决方案1】:

    在每次迭代中屏蔽不需要的连接应该可以工作,但我不确定收敛属性是什么样的。足够小的学习率可能没问题?

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

    P.S.:如果你在这方面取得了进展,很高兴听到你的 cmets,因为我也在解决这个问题。

    【讨论】:

    • 我上面使用的方法很适合我的目的。但是我还没有取得任何进一步的进展
    猜你喜欢
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多