Pul*_*arg -1 python machine-learning python-3.x unsupervised-learning deep-learning
class RBM():
def __init__(self, nv, nh):
self.W = torch.randn(nh, nv)
self.a = torch.randn(1, nh)
self.b = torch.randn(1, nv)
def sample_h(self, x):
wx = torch.mm(x, self.W.t())
activation = wx + self.a.expand_as(wx)
p_h_given_v = torch.sigmoid(activation)
return p_h_given_v, torch.bernoulli(p_h_given_v)
def sample_v(self, y):
wy = torch.mm(y, self.W)
activation = wy + self.b.expand_as(wy)
p_v_given_h = torch.sigmoid(activation)
return p_v_given_h, torch.bernoulli(p_v_given_h)
def train(self, v0, vk, ph0, phk):
self.W += torch.mm(v0.t(), ph0) - torch.mm(vk.t(), phk)
self.b += torch.sum((v0 - vk), 0)
self.a += torch.sum((ph0 - phk), 0)
Run Code Online (Sandbox Code Playgroud)
错误:
火车上(自己,v0,vk,ph0,phk)
19 return p_v_given_h, torch.bernoulli(p_v_given_h)
20 def train(self, v0, vk, ph0, phk):
---> 21 self.W += torch.mm(v0.t(), ph0) - torch.mm(vk.t(), phk)
22 self.b += torch.sum((v0 - vk), 0)
23 self.a += torch.sum((ph0 - phk), 0)
Run Code Online (Sandbox Code Playgroud)
RuntimeError:张量(1682)的扩展大小必须与非单维度1上的现有大小(100)相匹配
小智 5
print(rbm.W.size()) 将向您显示torch.Size([100,1682])
print((torch.mm(v0.t(), ph0)-torch.mm(vk.t(), phk)).size()) 将向您显示torch.Size([1682,100])
所以看起来应该是这样的:
self.W += (torch.mm(v0.t(), ph0) - torch.mm(vk.t(), phk)).t()
Run Code Online (Sandbox Code Playgroud)
代替 self.W += torch.mm(v0.t(), ph0) - torch.mm(vk.t(), phk)
| 归档时间: |
|
| 查看次数: |
764 次 |
| 最近记录: |