首页 文章

如何选择1-D信号(accelrometer信号)的张量形状?由于张量形状,我一直得到VlaueError

提问于
浏览
0

我正在尝试为CNN建模1-D信号,但我无法理解等级错误 .

我的程序是这样的:

#Weights
def init_weights(shape):
    init_random_dist = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(init_random_dist)


#Bias
def init_bias(shape):
    init_bias = tf.constant(0.1,shape=shape)
    return tf.Variable(init_bias)

def conv1d(x,W):
    #x is input accelration data and W is corresponding weight
    x = tf.cast(x, tf.float32)
    tf.nn.conv1d(x,W,stride=1,padding='VALID')

def convolution_layer(input_x,shape):
    w = init_weights(shape)
    b = init_bias([shape[3]])
    return tf.nn.relu(conv1d(input_x,w)+b)
  • 现在占位符

x = tf.placeholder(tf.float32,shape=[1,1,200,1])

y_true = tf.placeholder(tf.float32,shape=[None,6])

使用 con_layer_1 = convolution_layer(x,shape=[1,20,1,32]) 创建第一层时,我得到排名 ValueError ,我无法脱欧 . 错误陈述是:

ValueError: Shape must be rank 4 but is rank 5 for 'conv1d_20/Conv2D' (op: 'Conv2D') with input shapes: [1,1,1,200,1], [1,1,20,1,32].

1 回答

  • 1

    nn.conv1d 的输入和权重形状不正确 . nn.conv1d 的输入形状应为: [ batch_size, input_length, input_channels] ,权重矩阵的大小应为 [filter_size, inputs_channels, output_channels] . 所以你需要将代码更改为:

    def convolution_layer(input_x,shape):
       w = init_weights(shape)
       b = init_bias([shape[2]])
       return tf.nn.relu(conv1d(input_x,w)+b)
    
    x = tf.placeholder(tf.float32,shape=[1,200,1])
    
    y_true = tf.placeholder(tf.float32,shape=[None,6])
    
    con_layer_1 = convolution_layer(x,shape=[20,1,32])
    

    注意:你应该尝试使用 tf.layers API来处理权重分配和所有 .

相关问题