欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Pytorch中使用樣本權(quán)重(sample_weight)的正確方法

 更新時(shí)間:2019年08月17日 11:22:22   作者:dlchang_chang  
今天小編就為大家分享一篇在Pytorch中使用樣本權(quán)重(sample_weight)的正確方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

step:

1.將標(biāo)簽轉(zhuǎn)換為one-hot形式。

2.將每一個(gè)one-hot標(biāo)簽中的1改為預(yù)設(shè)樣本權(quán)重的值

即可在Pytorch中使用樣本權(quán)重。

eg:

對(duì)于單個(gè)樣本:loss = - Q * log(P),如下:

P = [0.1,0.2,0.4,0.3]
Q = [0,0,1,0]
loss = -Q * np.log(P)

增加樣本權(quán)重則為loss = - Q * log(P) *sample_weight

P = [0.1,0.2,0.4,0.3]
Q = [0,0,sample_weight,0]
loss_samle_weight = -Q * np.log(P)

在pytorch中示例程序

train_data = np.load(open('train_data.npy','rb'))
train_labels = []
for i in range(8):
  train_labels += [i] *100
train_labels = np.array(train_labels)
train_labels = to_categorical(train_labels).astype("float32")
sample_1 = [random.random() for i in range(len(train_data))]
for i in range(len(train_data)):
  floor = i / 100
  train_labels[i][floor] = sample_1[i]
train_data = torch.from_numpy(train_data) 
train_labels = torch.from_numpy(train_labels) 
dataset = dataf.TensorDataset(train_data,train_labels) 
trainloader = dataf.DataLoader(dataset, batch_size=batch_size, shuffle=True)

對(duì)應(yīng)one-target的多分類交叉熵?fù)p失函數(shù)如下:

def my_loss(outputs, targets):
  
  output2 = outputs - torch.max(outputs, 1, True)[0]
 
 
  P = torch.exp(output2) / torch.sum(torch.exp(output2), 1,True) + 1e-10
 
 
  loss = -torch.mean(targets * torch.log(P))
 
 
  return loss

以上這篇在Pytorch中使用樣本權(quán)重(sample_weight)的正確方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論