【问题标题】:Weighted Sample Loss in Keras TensorflowKeras Tensorflow 中的加权样本损失
【发布时间】:2020-04-02 06:07:00
【问题描述】:

我想建立一个损失,为每个样本赋予单独的权重,并且不仅在训练期间有效。它也应该适用于验证和测试集。

到目前为止我已经尝试过:

    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....

【问题讨论】:

    标签: python tensorflow keras loss


    【解决方案1】:

    我已从 .fit() 方法切换到低级 API 方法,您可以在其中循环记录训练。 Example

    然后改变损失函数。

    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
    

    【讨论】:

      猜你喜欢
      • 2020-07-28
      • 1970-01-01
      • 2020-04-18
      • 2019-01-20
      • 2021-09-06
      • 2020-12-21
      • 2021-12-15
      • 1970-01-01
      • 2016-12-18
      相关资源
      最近更新 更多