如何在keras中为单个模型使用两个损失函数?

时间:2018-01-12 20:44:20

标签: machine-learning neural-network keras

我想将一个keras模型用于两个不同的目的。一个使用CTC损失函数,另一个使用分类Crossentropy损失函数。 这是我在keras中的模型:

def train(run_name, start_epoch, stop_epoch, img_w,type_t):

  input_data = Input(name='the_input', shape=input_shape, dtype='float32')
  inner = Conv2D(conv_filters, kernel_size, padding='same',
                 activation=act, kernel_initializer='he_normal',
                 name='conv1')(input_data)
  inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max1')(inner)
  inner = Conv2D(conv_filters, kernel_size, padding='same',
                 activation=act, kernel_initializer='he_normal',
                 name='conv2')(inner)
  inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max2')(inner)
  inner_act = Flatten()(inner_act)

  inner_2 = Dense(1,
                    name='dense3')(inner_act)
  inner_out = Activation('sigmoid')(inner_2)
  conv_to_rnn_dims = (img_w // (pool_size ** 2), (img_h // (pool_size ** 2)) * conv_filters)
  inner = Reshape(target_shape=conv_to_rnn_dims, name='reshape')(inner)


  inner = Dense(time_dense_size, activation=act, name='dense1')(inner)

  gru_1 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru1')(inner)
  gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru1_b')(inner)
  gru1_merged = add([gru_1, gru_1b])
  gru_2 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru2')(gru1_merged)
  gru_2b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru2_b')(gru1_merged)

  inner = Dense(FindOutPutShape(), kernel_initializer='he_normal',
                name='dense2')(concatenate([gru_2, gru_2b]))
  y_pred = Activation('softmax', name='softmax')(inner)
  Model(inputs=input_data, outputs=y_pred)

  labels = Input(name='the_labels', shape=[img_gen.absolute_max_string_len], dtype='float32')
  input_length = Input(name='input_length', shape=[1], dtype='int64')
  label_length = Input(name='label_length', shape=[1], dtype='int64')


  loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length])

  # clipnorm seems to speeds up convergence
  sgd = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)

  model_1 = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)
  model_2 = Model(inputs=input_data, outputs=inner_out)

我想进行一场minmax游戏,在开始的时候,我要训练model_1,而不是我要训练同一型号的model_1,但是很少的层会被切掉,这就是model_2。权重是否相同。如果没有,那么可以执行此任务吗?

0 个答案:

没有答案