【问题标题】:Fail to feed forward 1D signal to 1D convolution in Keras无法将 1D 信号前馈到 Keras 中的 1D 卷积
【发布时间】:2018-11-08 02:43:42
【问题描述】:

我是 Keras 的新手。给定输入数组,我想执行一维卷积。我该怎么做?我已经写了代码,但它无法运行

from keras.models import Sequential
from keras.layers import Conv1D
import numpy as np
import tensorflow as tf
from keras.models import Model

input = np.array(tf.constant([1,2,3,4,5,6,7,8]))   
model = Sequential()
model.add(Conv1D(1,3,strides=1,padding='same', name='conv'))

layer_model = Model(inputs=input,outputs=model.get_layer('conv').output)
conv_output = layer_model.predict(f)    
print (conv_output)

这是错误

    AttributeError                            Traceback (most recent call last)
<ipython-input-13-31db6e7dcb90> in <module>()
     10 
     11 layer_model = Model(inputs=input,
---> 12                                 outputs=model.get_layer('conv').output)
     13 conv_output = layer_model.predict(f)
     14 print (conv_output)

/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in output(self)
    808         if not self._inbound_nodes:
    809             raise AttributeError('Layer ' + self.name +
--> 810                                  ' has no inbound nodes.')
    811         if len(self._inbound_nodes) > 1:
    812             raise AttributeError('Layer ' + self.name +

AttributeError: Layer conv has no inbound nodes.

【问题讨论】:

  • 你不能跑是什么意思?你得到什么错误信息?请添加所有这些细节。此外,您似乎正在混合使用功能和顺序 API,这是行不通的。
  • 嗨。我已经更新了代码和错误。你能帮我看看吗?

标签: keras


【解决方案1】:

您不应该通过混合功能和顺序 API 来复杂化自己,更简单的方法是:

from keras.models import Sequential
from keras.layers import Conv1D
import numpy as np

input = np.array([1,2,3,4,5,6,7,8])
input = input.reshape(1, 8, 1) # (samples, width, channels)   

model = Sequential()
model.add(Conv1D(1,3,padding='same', input_shape=(8, 1)))

conv_output = model.predict(input)    
print(conv_output)

请注意,我们没有设置卷积权重,因此它们会自动初始化为随机值。

【讨论】:

  • 谢谢。我知道了。你能告诉我如何打印我可以手动验证的 conv 的重量吗?
猜你喜欢
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 2019-02-21
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
相关资源
最近更新 更多