我是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.
答案 0 :(得分:0)
您不应通过混合使用功能性API和顺序性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)
请注意,我们尚未设置卷积权重,因此它们会自动初始化为随机值。