首页 文章

在TensorFlow中使用线性代数帮助新手(等级3张量)

提问于
浏览
2

我怀疑这已经被问到了,虽然在我的搜索中,许多其他问题都有特定的独特问题,这些问题似乎不适用于我的情况(或者解决方案可能超出我的想法) .

我在tensorflow中有一个标准的前馈神经网络,其行为正确,大小为[None,n_features]的rank2输入张量,[n_features,n_neurons]的权重导致tf.matmul的隐藏层(输入,权重)= [无,n_neurons] .

但是,我想在输入和输出中将维度扩展一维 . 例如,我想拥有

inputs = tf.placeholder("float", shape=[None, n_type, n_features])
weight= tf.Variable(FNN_weight_initializer([n_type, n_features, n_neurons]))
Hidden1 = tf.matmul(inputs, weight)

and my end goal here is to have Hidden1 = [None, n_type, n_neurons].

然而,不是生成所需的张量形状,而是获得形状张量[n_type,n_type,n_neurons] . 我不是线性代数专家,我尝试了一些维度顺序的组合但没有成功 . 是否有可能将rank3张量乘以tf.matmul?我应该在这里的某个地方进行重塑或转置操作吗?

1 回答

  • 2

    EDIT according to OP's comment

    您可以展平输入要素向量以形成 [-1, n_type * n_features] ,应用精心选择的矩阵乘法并将输出从 [-1, n_type * n_neurons] 重新整形为 [-1, n_type, n_neurons]

    操作张量将是块对角 [n_type * n_features, n_type * n_neurons] ,每个块是 weights 中的 n_type 张量之一 .

    为了构建块对角矩阵,我使用了另一个答案(来自here

    这看起来像

    inputs = tf.placeholder("float", shape=[None, n_type, n_features])
    inputs = tf.reshape(inputs, shape=[-1, n_type * n_features])
    
    weights = tf.Variable(FNN_weight_initializer([n_type, n_features, n_neurons]))
    
    split_weights = tf.split(weights, num_or_size_splits=n_type, axis=1)
    # each element of split_weights is a tensor of shape : [1, n_features, n_neurons] -> need to squeeze
    split_weights = tf.map_fn(lambda elt : tf.squeeze(elt, axis=0), split_weights)
    
    block_matrix = block_diagonal(split_weights) # from the abovementioned reference
    
    Hidden1 = tf.matmul(inputs, block_matrix)
    # shape : [None, n_type * n_neurons]
    
    Hidden1 = tf.reshape(Hidden1, [-1, n_type, n_neurons])
    # shape : [None, n_type, n_neurons]
    

    Orignal answer

    根据 tf.matmulreference)的文档,您乘以的张量需要具有相同的等级 .

    当等级为 >2 时,只有最后两个维度需要与矩阵乘法兼容,第一个其他维度需要完全匹配 .

    因此,对于“是否有可能将rank3张量与tf.matmul相乘?”,答案是“是的,这是可能的,但从概念上讲,它仍然是2级乘法” .

    因此,有必要进行一些重塑:

    inputs = tf.placeholder("float", shape=[None, n_type, n_features])
    
    inputs = tf.reshape(inputs, shape=[-1, n_type, 1, n_features])
    
    weights = tf.Variable(FNN_weight_initializer([n_type, n_features, n_neurons]))
    
    weights = tf.expand_dims(weights, 0)
    # shape : [1, n_type, n_features, n_neurons]
    
    weights = tf.tile(weights, [tf.shape(inputs)[0], 1, 1, 1])
    # shape : [None, n_type, n_features, n_neurons]
    
    Hidden1 = tf.matmul(inputs, weights)
    # shape : [None, n_type, 1, n_neurons]
    
    Hidden1 = tf.reshape(Hidden1, [-1, n_type, n_neurons])
    # shape : [None, n_type, n_neurons]
    

相关问题