本文整理汇总了Python中torch.autograd.Variable.mm方法的典型用法代码示例。如果您正苦于以下问题:Python Variable.mm方法的具体用法?Python Variable.mm怎么用?Python Variable.mm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.autograd.Variable
的用法示例。
在下文中一共展示了Variable.mm方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import mm [as 别名]
def main():
dtype = torch.FloatTensor
N, d_in, H, d_out = 64, 1000, 100, 10 # d_in表示输入维度,d_out输出维度,H是隐藏层维度数
x = Variable(torch.randn(N, d_in).type(dtype), requires_grad=False)
y = Variable(torch.randn(N, d_out).type(dtype), requires_grad=False)
w1 = Variable(torch.randn(d_in, H).type(dtype), requires_grad=True)
w2 = Variable(torch.randn(H, d_out).type(dtype), requires_grad=True)
learning_rate = 1e-6
for t in range(500):
relu = MyRelu()
y_pred = relu(x.mm(w1)).mm(w2)
loss = (y_pred - y).pow(2).sum()
loss.backward()
w1.data -= learning_rate * w1.grad.data
w2.data -= learning_rate * w2.grad.data
w1.grad.data.zero_()
w2.grad.data.zero_()
print(loss.data[0])
示例2: test_var_gradient_keeps_id_during_send_
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import mm [as 别名]
def test_var_gradient_keeps_id_during_send_(self):
# PyTorch has a tendency to delete var.grad python objects
# and re-initialize them (resulting in new/random ids)
# we have fixed this bug and recorded how it was fixed
# as well as the creation of this unit test in the following
# video (1:50:00 - 2:00:00) ish
# https://www.twitch.tv/videos/275838386
# this is our hook
hook = TorchHook(verbose=False)
local = hook.local_worker
local.verbose = False
remote = VirtualWorker(id=1, hook=hook, verbose=False)
local.add_worker(remote)
data = Var(torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]))
target = Var(torch.FloatTensor([[0], [0], [1], [1]]))
model = Var(torch.zeros(2, 1), requires_grad=True)
# generates grad objects on model
pred = data.mm(model)
loss = ((pred - target)**2).sum()
loss.backward()
# the grad's true id
original_data_id = model.data.id + 0
original_grad_id = model.grad.data.id + 0
model.send_(remote)
assert model.data.id == original_data_id
assert model.grad.data.id == original_grad_id
示例3: test_send_var_with_gradient
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import mm [as 别名]
def test_send_var_with_gradient(self):
# previously, there was a bug involving sending variables with graidents
# to remote tensors. This bug was documented in Issue 1350
# https://github.com/OpenMined/PySyft/issues/1350
# this is our hook
hook = TorchHook(verbose=False)
local = hook.local_worker
local.verbose = False
remote = VirtualWorker(id=1, hook=hook, verbose=False)
local.add_worker(remote)
data = Var(torch.FloatTensor([[0, 0], [0, 1], [1, 0], [1, 1]]))
target = Var(torch.FloatTensor([[0], [0], [1], [1]]))
model = Var(torch.zeros(2, 1), requires_grad=True)
# generates grad objects on model
pred = data.mm(model)
loss = ((pred - target)**2).sum()
loss.backward()
# ensure that model and all (grand)children are owned by the local worker
assert model.owners[0].id == local.id
assert model.data.owners[0].id == local.id
# if you get a failure here saying that model.grad.owners does not exist
# check in hooks.py - _hook_new_grad(). self.grad_backup has probably either
# been deleted or is being run at the wrong time (see comments there)
assert model.grad.owners[0].id == local.id
assert model.grad.data.owners[0].id == local.id
# ensure that objects are not yet pointers (haven't sent it yet)
assert not model.is_pointer
assert not model.data.is_pointer
assert not model.grad.is_pointer
assert not model.grad.data.is_pointer
model.send_(remote)
# ensures that object ids do not change during the sending process
assert model.owners[0].id == remote.id
assert model.data.owners[0].id == remote.id
assert model.grad.owners[0].id == remote.id
assert model.grad.data.owners[0].id == remote.id
# ensures that all local objects are now pointers
assert model.is_pointer
assert model.data.is_pointer
assert model.grad.is_pointer
assert model.grad.data.is_pointer
# makes sure that tensors actually get sent to remote worker
assert model.id in remote._objects
assert model.data.id in remote._objects
assert model.grad.id in remote._objects
assert model.grad.data.id in remote._objects
示例4: _predict
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import mm [as 别名]
def _predict(self, x, get_raw_results=False, **kwargs):
if not isinstance(x, Variable):
x = Variable(torch.from_numpy(np.asarray(x).astype(np.float32)))
rs = x.mm(self._w)
rs = rs.add_(self._b.expand_as(rs)).squeeze(1)
if get_raw_results:
return rs
return torch.sign(rs)
示例5: Variable
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import mm [as 别名]
plt.title("Loss for Category {}".format(cat_id_to_name.get(C[category])), fontsize = 20)
plt.legend(fontsize = 20)
plt.ylabel("Loss", fontsize = 20)
plt.xlabel("Epoch", fontsize = 20)
# In[79]:
## Test Set
X = Variable(torch.from_numpy(feat_test).type(dtype),
requires_grad=False)
Y_pred = X.mm(w1)
Y_original = Variable(torch.from_numpy(y_test).type(dtype),
requires_grad=False)
# In[80]:
(y_pro_test >= 0.5 ).sum()
# In[81]:
y_pro_test = 1/(1+torch.exp(-Y_pred))
开发者ID:EmilyYanW,项目名称:Machine_Learning,代码行数:32,代码来源:8.Computer_Vision_Image_Detection_and_Retrieval.py
示例6: backward
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import mm [as 别名]
def backward(ctx, grad_output):
input, = ctx.saved_tensors
grad_input = grad_output.clone()
grad_input[input < 0] = 0
return grad_input
dtype = torch.FloatTensor
N, D_in, H, D_out = 64, 1000, 100, 10
x = Variable(torch.randn(N, D_in).type(dtype), requires_grad=False)
y = Variable(torch.randn(N, D_out).type(dtype), requires_grad=False)
w1 = Variable(torch.randn(D_in, H).type(dtype), requires_grad=True)
w2 = Variable(torch.randn(H, D_out).type(dtype), requires_grad=True)
learning_rate = 1e-6
for t in range(1000):
relu = MyReLu.apply
y_pred = relu(x.mm(w1)).mm(w2)
loss = (y_pred - y).pow(2).sum()
print (t, loss.data[0])
loss.backward()
w1.data -= learning_rate * w1.grad.data
w2.data -= learning_rate * w2.grad.data
w1.grad.data.zero_()
w2.grad.data.zero_()
示例7: Variable
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import mm [as 别名]
x = Variable(torch.randn(N, D_in).type(dtype), requires_grad=False)
y = Variable(torch.randn(N, D_out).type(dtype), requires_grad=False)
# Create random Tensors for weights, and wrap them in Variables.
# Setting requires_grad=True indicates that we want to compute gradients with
# respect to these Variables during the backward pass.
w1 = Variable(torch.randn(D_in, H).type(dtype), requires_grad=True)
w2 = Variable(torch.randn(H, D_out).type(dtype), requires_grad=True)
learning_rate = 1e-6
for t in range(500):
# Forward pass: compute predicted y using operations on Variables; these
# are exactly the same operations we used to compute the forward pass using
# Tensors, but we do not need to keep references to intermediate values since
# we are not implementing the backward pass by hand.
y_pred = x.mm(w1).clamp(min=0).mm(w2)
# Compute and print loss using operations on Variables.
# Now loss is a Variable of shape (1,) and loss.data is a Tensor of shape
# (1,); loss.data[0] is a scalar value holding the loss.
loss = (y_pred - y).pow(2).sum()
print(t, loss.data[0])
# Manually zero the gradients before running the backward pass
w1.grad.data.zero_()
w2.grad.data.zero_()
# Use autograd to compute the backward pass. This call will compute the
# gradient of loss with respect to all Variables with requires_grad=True.
# After this call w1.grad and w2.grad will be Variables holding the gradient
# of the loss with respect to w1 and w2 respectively.