English 中文(简体)
两用数据中的火rch培训
原标题:PyTorch training from binary data

鉴于职能范围为[0,xmax]的减少,如果是参数,我想用Py Torch书写一个方案,从一些双面价值的培训数据中学习l的价值。 我拥有的培训数据是(x_i,g(x_i)), i=1,2,...,n,在 f(x)>时,g(x)=1;t 和其他ise 页: 1 这里的t是固定门槛值。 我界定了这一类别。

class Function(torch.nn.Module):
    def __init__(self, l_init): # l is the parameter, its initial value is l_init
        self.l = torch.nn.Parameter(torch.tensor(l_init, dtype=float))
    def forward(self, x):
        return f(x)

我还有一个变量train_data,其中train_data[0]为x_i s(一个浮动阵列)和train_data***为g(x_i) s(a 0/1阵列)。 即时发送<条码>功能模块:

f = Function()

将损失功能界定为

loss_func = lambda x, y: (x - y) ** 2

对于每一项培训,我对<代码>pred = Function(train_data[0])进行了计算,我不得不在不失去梯度功能的情况下计算损失。 但是,如果我写了字的话,我会写一下。

loss = loss_func((pred > t).float(), train_data[0])

梯度功能将丧失。 我应该怎样利用这一双轨培训数据来学习升值?

问题回答

https://github.com/ZouJiu1/CNN_numpy/blob/master/net/loss.py#L36-L66

(pred > t) can not be derivated or loss the gradient information. (pred > t) should be used when you predict input. it should not be used when you train.

您可以通过使用BCEloss和BEWith式行车来做到这一点。 无损损失

你的屋顶界定了这种损失功能,因此,培训——数据应当是一种热形式。

BCEloss

https://pytorch.org/docs/stable/generated/torch.nn.BCELoss.html?highlight=bce#torch.nn.BCELoss

loss_func = torch.nn.BCEloss()
train_datas = torch.zeros(2)
train_datas[train_data[1]] = 1
softmax = torch.nn.Softmax(pred, dim = -1)
pred = softmax(pred)
loss = loss_func(pred, train_datas)

当你预测投入时,使用

softmax = torch.nn.Softmax(pred, dim = -1)
pred = softmax(pred)
for i in range(len(pred)):
    if pred[i] > 0.6:
        ...do something...
        break

BCEWithLogitsLoss

https://pytorch.org/docs/stable/生成/torch.nn.BCEWithlogitsLoss.html?highlight=bce#torch.nn.

loss_func = torch.nn.BCEWithLogitsLoss()
train_datas = torch.zeros(2)
train_datas[train_data[1]] = 1
loss = loss_func(pred, train_datas)

当你预测投入时,使用

softmax = torch.nn.Softmax(pred, dim = -1)
pred = softmax(pred)
for i in range(len(pred)):
    if pred[i] > 0.6:
        ...do something...
        break

besides those, you can use crossentropyloss in multi classes classify too. https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html?highlight=cross#torch.nn.CrossEntropyLoss





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签