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
Python 如何确定秩为3的输入张量的权重维数?_Python_Tensorflow_Neural Network - Fatal编程技术网

Python 如何确定秩为3的输入张量的权重维数?

Python 如何确定秩为3的输入张量的权重维数?,python,tensorflow,neural-network,Python,Tensorflow,Neural Network,我正试图为三通道输入(三轴加速度计数据)的活动分类设计一个自动编码器 输入张量的形状为[None,200,3]([Batch size,window size,number of Channel]),在第一层中,我想简单地将输入层的维数减少到[None,150,3]。以下是创建占位符和第一层的代码: import tensorflow as tf def denseLayer(inputVal,weight,bias): return tf.nn.relu((tf.matmul(inp

我正试图为三通道输入(三轴加速度计数据)的活动分类设计一个自动编码器

输入张量的形状为
[None,200,3]
([Batch size,window size,number of Channel]),在第一层中,我想简单地将输入层的维数减少到
[None,150,3]
。以下是创建占位符和第一层的代码:

import tensorflow as tf

def denseLayer(inputVal,weight,bias):
    return tf.nn.relu((tf.matmul(inputVal,weight)+bias))


x = tf.placeholder(dtype=tf.float32,shape=[None,200,3]) #Input tensor
wIn = tf.get_variable(name='wIn',initializer=tf.truncated_normal(stddev=0.1,dtype=tf.float32,shape=[200,150]))

bIn = tf.get_variable(name='bIn',initializer=tf.constant(value = 0.1,shape=[150,3],dtype=tf.float32))


firstLayer = denseLayer(x,weight=wIn,bias=bIn)
当然,此代码将导致错误(由于
x
wIn
之间的排名不同),我无法确定
wIn
变量的形状,以获得所需的
第一层
形状,即
[None,150,3]

以下是最终网络的外观(简化版,层数较少):

我想这正是你想要的:

import tensorflow as tf

def denseLayer(inputVal, weight, bias):
    # Each input "channel" uses the corresponding set of weights
    value = tf.einsum('nic,ijc->njc', inputVal, weight) + bias
    return tf.nn.relu(value)
#Input tensor
x = tf.placeholder(dtype=tf.float32, shape=[None, 200, 3])
# Weights and biases have three "channels" each
wIn = tf.get_variable(name='wIn',
                      shape=[200, 150, 3],
                      initializer=tf.truncated_normal_initializer(stddev=0.1))
bIn = tf.get_variable(name='bIn',
                      shape=[150, 3],
                      initializer=tf.constant_initializer(value=0.1))
firstLayer = denseLayer(x, weight=wIn, bias=bIn)
print(firstLayer)
# Tensor("Relu:0", shape=(?, 150, 3), dtype=float32)

这里的
wIn
可以看作是应用于每个输入通道的三组
[200150]
参数。我认为这是在这种情况下实现这一点的最简单方法。

您尝试的方法有点不寻常,因为通常使用“平坦”特征向量(例如,在您的情况下,600元素的输入向量,而不是200个3元素向量,450元素的输出向量,而不是150个3元素向量)。因此,对于一个给定的示例,您有一个输入
[200,3]
,并且想要一个输出
[150,3]
,但是网络的连接是什么?例如,每个输出向量的“x”坐标应该连接到输入向量的所有“x”、“y”和“z”坐标,还是只连接到输入向量的“x”坐标?@jdehesa我添加了一个图表来消除混淆(“x”坐标应该只连接到“x”坐标,以便在输出层成功地重新创建输入)我明白了,我很欣赏这个图表。还有一件事,对于每一层,您是否希望对三个坐标使用相同的权重集(就像使用一个层三次)?或者你想要有“x”权重、“y”权重和“z”权重(比如有三个“平行”层)?@jdehesa我相信对每个轴使用不同的权重将提高性能,而不是限制权重参数在每个轴上具有相同的值。但这是否意味着我必须为每个轴创建3个权重变量?