keras编写代码,dense层维度多出一维,求大佬解疑答惑

keras编写代码,dense层维度多出一维,求大佬解疑答惑
轴承故障的一维数据诊断和分类
我原先用TensorFlow编写的空洞门卷积代码,然后接了两个双向LSTM层和两个dense层,是可以运行程序的,为了出图比较方便,用Keras重新编写了我的代码。其中空洞门卷积是我自己编写的代码,TensorFlow版本如下:

    def conv1d_block(self, x, filters, kernel_size, dr, pd='SAME',name="name"):
        """
        gated dilation conv1d layer
        """
        glu = tf.sigmoid(tf.layers.conv1d(x, filters, kernel_size, dilation_rate=dr, padding=pd))
        print('glu=',glu)
        conv = tf.tanh(tf.layers.conv1d(x, filters, kernel_size, dilation_rate=dr, padding=pd))
        print('conv=',conv)
        conv3 = tf.layers.conv1d(x, filters, kernel_size, dilation_rate=dr, padding=pd)
        cog = tf.multiply(conv, glu)
        cog1 = tf.multiply(1-glu, conv3)
        outputs = tf.add(cog, cog1)
        print('outputs=',outputs)
        return outputs

然后用Keras编写的代码如下:

def conv1d_block(input,filters, kernerl_size, strides, conv_padding,dr,name ):
    """
    gated dilation conv1d layer
    """
    glu =Conv1D(filters=filters, kernel_size=kernerl_size, strides=strides,
           padding=conv_padding,dilation_rate=dr )(input)
    glu=k.sigmoid(glu)
    print('glu=',glu)
    conv = Conv1D(filters=filters, kernel_size=kernerl_size, strides=strides,
           padding=conv_padding,dilation_rate=dr )(input)
    conv=k.tanh(conv)
    print('conv=',conv)
    conv3 = Conv1D(filters=filters, kernel_size=kernerl_size, strides=strides,
           padding=conv_padding,dilation_rate=dr )(input)
    weight_1 = Lambda(lambda x: x * 0.8)
    weight_2 = Lambda(lambda x: x * 0.2)
    weight_gru1 = weight_1(conv)
    weight_gru2 = weight_2(glu)
    weight_gru3 = weight_1(1-glu)
    weight_gru4 = weight_2(conv3)
    cog = Multiply()([weight_gru1, weight_gru2])
    cog1 = Multiply()([weight_gru3, weight_gru4])
    outputs = Add()([cog, cog1])
    print('outputs=',outputs)
    return outputs

print的时候Keras的输出是:

'''inputs_01 (?, 1200, 1)
glu= Tensor("Sigmoid:0", shape=(?, 1200, 16), dtype=float32)
conv= Tensor("Tanh:0", shape=(?, 1200, 16), dtype=float32)
outputs= Tensor("add_1/add:0", shape=(?, 1200, 16), dtype=float32)
glu= Tensor("Sigmoid_1:0", shape=(?, 1200, 16), dtype=float32)
conv= Tensor("Tanh_1:0", shape=(?, 1200, 16), dtype=float32)
outputs= Tensor("add_2/add:0", shape=(?, 1200, 16), dtype=float32)
glu= Tensor("Sigmoid_2:0", shape=(?, 1200, 16), dtype=float32)
conv= Tensor("Tanh_2:0", shape=(?, 1200, 16), dtype=float32)
outputs= Tensor("add_3/add:0", shape=(?, 1200, 16), dtype=float32)
glu= Tensor("Sigmoid_3:0", shape=(?, 1200, 16), dtype=float32)
conv= Tensor("Tanh_3:0", shape=(?, 1200, 16), dtype=float32)
outputs= Tensor("add_4/add:0", shape=(?, 1200, 16), dtype=float32)
con4 Tensor("add_4/add:0", shape=(?, 1200, 16), dtype=float32)
lstm Tensor("lstmt1/transpose_1:0", shape=(?, ?, 100), dtype=float32)
lstm2 Tensor("lstmt2/transpose_1:0", shape=(?, ?, 100), dtype=float32)
megre============================================================ (?, ?, 200)
merge1========================= Tensor("lambda_9/ExpandDims:0", shape=(?, ?, 200, 1), dtype=float32)
dense1 Tensor("Dense1/Relu:0", shape=(?, 1200, 200, 200), dtype=float32)

TensorFlow的输出为:

input_shape= (1200,)
glu= Tensor("Sigmoid:0", shape=(?, 1, 16), dtype=float32)
conv= Tensor("Tanh:0", shape=(?, 1, 16), dtype=float32)
outputs= Tensor("Add:0", shape=(?, 1, 16), dtype=float32)
cov1 (?, 1, 16)
glu= Tensor("Sigmoid_1:0", shape=(?, 1, 16), dtype=float32)
conv= Tensor("Tanh_1:0", shape=(?, 1, 16), dtype=float32)
outputs= Tensor("Add_1:0", shape=(?, 1, 16), dtype=float32)
cov2 (?, 1, 16)
glu= Tensor("Sigmoid_2:0", shape=(?, 1, 16), dtype=float32)
conv= Tensor("Tanh_2:0", shape=(?, 1, 16), dtype=float32)
outputs= Tensor("Add_2:0", shape=(?, 1, 16), dtype=float32)
cov3 (?, 1, 16)
glu= Tensor("Sigmoid_3:0", shape=(?, 1, 16), dtype=float32)
conv= Tensor("Tanh_3:0", shape=(?, 1, 16), dtype=float32)
layer_output (?, 200)
(?, 10)
self.layer_final_output Tensor("dense2/BiasAdd:0", shape=(?, 10), dtype=float32)

TensorFlow的dense层输出就只有一个维度,但是Keras变成了两维,多出的一维是input的长度,而且进行Keras尽力model的时候运行时,出现了如下错误:

Traceback (most recent call last):
  File "D:/1111/NEW Train/Gru-train/Keras-GRU.py", line 151, in <module>
    model = Model(inputs=inputs_01, outputs=Dense2)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 231, in _init_graph_network
    self.inputs, self.outputs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 1366, in _map_graph_network
    tensor_index=tensor_index)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 1353, in build_map
    node_index, tensor_index)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 1353, in build_map
    node_index, tensor_index)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 1353, in build_map
    node_index, tensor_index)
  [Previous line repeated 4 more times]
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 1325, in build_map
    node = layer._inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

这个问题困扰了好多天,一直没有解决,不知道是我编写的这个代码块有问题还是中间哪出错了(Keras后面是普通的两个LSTM和两个dense层),还请大佬们答疑解惑,谢谢

你可能感兴趣的:(轴承分析,python)