我在NN的一侧有原始1d数据,在另一侧有1d标签。输入数据通过具有可训练参数和特定激活功能的图层进行转换。我想使用我不想训练的外部算法添加最终转换(仅训练前面几层中的参数)。 我该怎么办?
更新: 在使用子过程实现外部算法之前,我尝试使用简单的测试来仅输出一个常数(1.0)。这是代码:
inputs=keras.Input(shape=(174,1))
labels=padded_label
x = keras.layers.Conv1D(filters=4,kernel_size=8,strides=1,input_shape=(174,1),padding='same')(inputs)
x = keras.layers.Dense(1, activation='sigmoid')(x)
def grande(x):
return np.ones((x.shape))
predictions=keras.layers.Lambda(grande,trainable=False)(x)
model = keras.Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
其中,标签是形状为(174,1)的矢量,其值为0或1。 这不起作用并引发错误:
TypeError Traceback (most recent call last)
<ipython-input-35-b23e29737fa2> in <module>()
8 #return sp.check_output('echo 1.0',shell=True)
9 return np.ones((x.shape))
---> 10 predictions=keras.layers.Lambda(grande,trainable=False)(x)
11 model = keras.Model(inputs=inputs, outputs=predictions)
12 model.compile(optimizer='rmsprop',
/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/engine /base_layer.py in __call__(self, inputs, *args, **kwargs)
701
702 if not in_deferred_mode:
--> 703 outputs = self.call(inputs, *args, **kwargs)
704 if outputs is None:
705 raise ValueError('A layer\'s `call` method should return a Tensor '
/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/layers/core.py in call(self, inputs, mask)
707 if generic_utils.has_arg(self.function, 'mask'):
708 arguments['mask'] = mask
--> 709 return self.function(inputs, **arguments)
710
711 def compute_mask(self, inputs, mask=None):
<ipython-input-35-b23e29737fa2> in grande(x)
7 print(x)
8 #return sp.check_output('echo 1.0',shell=True)
----> 9 return np.ones((x.shape))
10 predictions=keras.layers.Lambda(grande,trainable=False)(x)
11 model = keras.Model(inputs=inputs, outputs=predictions)
/usr/local/lib/python3.5/dist-packages/numpy/core/numeric.py in ones(shape, dtype, order)
201
202 """
--> 203 a = empty(shape, dtype, order)
204 multiarray.copyto(a, 1, casting='unsafe')
205 return a
TypeError: __index__ returned non-int (type NoneType)
如何为输出赋予合适的形状?