Взвешенная потеря образца в Керасе Tensorflow - PullRequest
1 голос
/ 02 апреля 2020

Я хотел бы построить убыток, который придает индивидуальный вес каждому образцу и работает не только во время обучения. Он также должен работать для проверки и набора тестов.

Что я пробовал до сих пор:

    def MMSE2(targets, preds,sample_weight): 
        #some calculations...       
        return loss


    input_dim = Input(shape = (dim, ),name='rating_in')
    weights_tensor = Input(shape=(dim,),name='weights')

    encoder,decoder = AddLayers(neurons,setup['AFunction'],
                                 setup['BatchNorm'],setup['Dropout'],setup['Layers'],dim,setup['Noise'])

    encoded = encoder(input_dim)    
    decoded = decoder(encoded)

    autoencoder = Model([input_dim,weights_tensor], decoded)
    autoencoder.add_loss(MMSE2(input_dim,decoded,weights_tensor))
    autoencoder.compile(optimizer='adam')


    history = autoencoder.fit(x=[helper.trainx,helper.trainy,helper.trainm],
                              validation_data= [helper.valx,helper.valy,helper.valm],
                              epochs = setup['Epochs'], batch_size = setup['BatchSize'])

Работает без данных проверки.

Ошибка с данными проверки:


  File "<ipython-input-11-fe466c688bcd>", line 3, in <module>
    epochs = setup['Epochs'], batch_size = setup['BatchSize'])

  File "C:\Users\Admin\Anaconda3\envs\tf21\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 819, in fit
    use_multiprocessing=use_multiprocessing)

  File "C:\Users\Admin\Anaconda3\envs\tf21\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 235, in fit
    use_multiprocessing=use_multiprocessing)

  File "C:\Users\Admin\Anaconda3\envs\tf21\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 614, in _process_training_inputs
    distribution_strategy=distribution_strategy)

  File "C:\Users\Admin\Anaconda3\envs\tf21\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 646, in _process_inputs
    x, y, sample_weight=sample_weights)

  File "C:\Users\Admin\Anaconda3\envs\tf21\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2383, in _standardize_user_data
    batch_size=batch_size)

  File "C:\Users\Admin\Anaconda3\envs\tf21\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2410, in _standardize_tensors
    exception_prefix='input')

  File "C:\Users\Admin\Anaconda3\envs\tf21\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py", line 539, in standardize_input_data
    str(data)[:200] + '...')

ValueError: Error when checking model input: 
the list of Numpy arrays that you are passing to your model is not the size the model expected. 
Expected to see 2 array(s), for inputs ['rating_in', 'weights'] 
but instead got the following list of 1 arrays: [array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 5....

1 Ответ

0 голосов
/ 05 апреля 2020

Я перешел от метода .fit () к низкоуровневому API, где вы записываете обучение в циклах. Пример

Затем измените функцию потерь.

def MMSE(targets,mask,preds):  
    num_rating = math.reduce_sum(mask) #count ratings
    loss = targets-preds
    loss = mask * loss
    loss = math.square(loss)
    loss = math.reduce_sum(loss)
    loss = loss / num_rating

    #in a single line
    #loss = math.reduce_sum(math.square(mask*(preds - targets))) / num_rating 

    return loss
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...