我正在尝试在Tensorflow 2.0上构建CNN架构,该架构需要多个时间信号文件,进行一些特征提取,添加所有特征并输出一个值(估计的剩余使用寿命)。
用于分类头的输入的形状的一个示例是(14976、128、5、5),借助自定义添加功能,它立即变为(1、128、5、5),并将具有该形状(1,1)的最后。在这种情况下,标签将为3610(与批次的大小直接相关)。
我可以使用RUL = np.full(preproc_imgs.shape[0], RUL)
使它“工作”,但是我感觉它没有输出应有的内容...
我的问题是:我可以通过任何方式将单个值作为模型训练的标签吗?
这是我的模型(分类头):
class MyAddLayer(keras.layers.Layer):
def call(self, inputs):
'''
elems = np.array([1, 2, 3, 4, 5, 6])
sum = scan(lambda a, x: a + x, elems)
# sum == [1, 3, 6, 10, 15, 21]
'''
print('inputs[-1]', inputs[-1])
sum = tf.scan(lambda a, x: a + x, inputs)
print('sum', sum[-1])
return tf.expand_dims(sum[-1], 0)
class EstimatedRUL(tf.keras.Model):
def __init__(self, name='EstimatedRUL'):
super(EstimatedRUL, self).__init__(name=name)
self.add = MyAddLayer()
self.conv_5 = keras.layers.Conv2D(5, (3, 3), padding='same', kernel_initializer='glorot_normal',
bias_initializer='glorot_normal')
####### Estimated RUL #######
self.flat = keras.layers.Flatten()
self.dense = keras.layers.Dense(128, kernel_initializer='glorot_normal', bias_initializer='glorot_normal')
self.leaky = keras.layers.LeakyReLU()
self.rul = keras.layers.Dense(1, activation='sigmoid', kernel_initializer='glorot_normal',
bias_initializer='glorot_normal')
####### Estimated RUL #######
def call(self, inputs):
print('shape of the input: ', inputs.shape)
a = self.add(inputs)
print('shape of added input: ', a.shape)
c5 = self.conv_5(a)
flat = self.flat(c5)
dense = self.dense(flat)
leaky = self.leaky(dense)
rul = self.rul(leaky)
print('about to return the rul with shape: ', rul.shape)
return rul
这是身体:
inputs = keras.Input(shape=(160, 160, 3))
base_model = keras.applications.mobilenet.MobileNet(input_shape=(160, 160, 3), include_top=False,
weights='imagenet', classes=2)(inputs)
base_model.trainable = False
构建模型后
model_1 = keras.Model(inputs=inputs, outputs=base_model, name='first')
model_1.compile(optimizer=adam, loss=los, metrics=['mean_absolute_percentage_error', RMSE])
print(model_1.summary())
model_2 = EstimatedRUL.EstimatedRUL()
model_2.compile(optimizer=adam, loss=los, metrics=['mean_absolute_percentage_error', RMSE])
使用这些参数
los = 'mean_absolute_error'
lr = 0.0001
####### Custom metrics #######
def RMSE(y_true, y_pred):
return tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(y_true, y_pred))))
####### Custom optimizer #######
adam = keras.optimizers.Adam(learning_rate=lr)
通过preproc_imgs = model_1.predict(train_images)
获得中间结果并将其输入到model_2中以获得估计的剩余使用寿命
(在这种情况下)完整的错误消息是:ValueError: Please provide as model targets either a single array or a list of arrays. You passed y=3610