我怀疑这一点已经被问到了,尽管在我的搜索中,其他许多问题都有一些特殊的独特问题,这些问题似乎不适用于我的情况(或者解决方案在我的头上)。
我在tensorflow中有一个标准的前馈神经网络,它使用rank2输入张量[None, n_features]大小,[n_features, n_neurons]的权值,从而形成一个隐藏的tf.matmul(inputs, weight) = [None, 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)我的最终目标是拥有Hidden1 = [None, n_type, n_neurons]。
然而,我没有生成期望的张量形状,而是得到了形状[n_type, n_type, n_neurons]的张量。我不是线性代数的专家,我尝试过几个维数顺序的组合,但没有成功。可以用rank3张量与tf.matmul相乘吗?我应该在这里做个整容手术还是换位手术?
发布于 2018-05-23 02:54:10
根据OP的评论编辑
您可以将输入的特征向量压平以形成[-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张量。
为了构建块对角矩阵,我使用了另一个答案(来自这里)。
这看起来就像
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答案
根据tf.matmul (参考文献)的文档,您所乘的张量需要具有相同的级别。
当秩为>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]https://stackoverflow.com/questions/50478928
复制相似问题