我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用torch.uniform()。
def reset(self, stdv=None): if stdv is not None: stdv = stdv * math.sqrt(3) self.weight.uniform_(-stdv, stdv) self.bias.uniform_(-stdv, stdv) else: ninp = torch.Tensor(self.nOutputPlane).zero_() for i in range(self.connTable.size(0)): idx = int(self.connTable[i][1]) ninp[idx] += 1 for k in range(self.connTable.size(0)): idx = int(self.connTable[k][1]) stdv = 1. / math.sqrt(self.kW*self.kH*ninp[idx]) self.weight[k].uniform_(-stdv, stdv) for k in range(self.bias.size(0)): stdv = 1. / math.sqrt(self.kW*self.kH*ninp[k]) # TODO: torch.uniform self.bias[k] = random.uniform(-stdv, stdv)
def reset(self, stdv=None): if stdv is not None: stdv = stdv * math.sqrt(3) self.weight.uniform_(-stdv, stdv) self.bias.uniform_(-stdv, stdv) else: ninp = torch.Tensor(self.nOutputPlane).zero_() for i in range(self.connTable.size(0)): idx = int(self.connTable[i,1]) ninp[idx] += 1 for k in range(self.connTable.size(0)): idx = int(self.connTable[k,1]) stdv = 1. / math.sqrt(self.kW*self.kH*ninp[idx]) self.weight.select(0, k).uniform_(-stdv, stdv) for k in range(self.bias.size(0)): stdv = 1. / math.sqrt(self.kW * self.kH * ninp[k]) # TODO: torch.uniform self.bias[k] = random.uniform(-stdv, stdv)
def reset(self, stdv=None): if stdv is not None: stdv = stdv * math.sqrt(3) self.weight.uniform_(-stdv, stdv) self.bias.uniform_(-stdv, stdv) else: ninp = torch.Tensor(self.nOutputPlane).zero_() for i in range(self.connTable.size(0)): idx = int(self.connTable[i][1]) ninp[idx] += 1 for k in range(self.connTable.size(0)): idx = int(self.connTable[k][1]) stdv = 1. / math.sqrt(self.kW * self.kH * ninp[idx]) self.weight[k].uniform_(-stdv, stdv) for k in range(self.bias.size(0)): stdv = 1. / math.sqrt(self.kW * self.kH * ninp[k]) # TODO: torch.uniform self.bias[k] = random.uniform(-stdv, stdv)
def reset(self, stdv=None): if stdv is not None: stdv = stdv * math.sqrt(3) self.weight.uniform_(-stdv, stdv) self.bias.uniform_(-stdv, stdv) else: ninp = torch.Tensor(self.nOutputPlane).zero_() for i in range(self.connTable.size(0)): idx = int(self.connTable[i, 1]) ninp[idx] += 1 for k in range(self.connTable.size(0)): idx = int(self.connTable[k, 1]) stdv = 1. / math.sqrt(self.kW * self.kH * ninp[idx]) self.weight.select(0, k).uniform_(-stdv, stdv) for k in range(self.bias.size(0)): stdv = 1. / math.sqrt(self.kW * self.kH * ninp[k]) # TODO: torch.uniform self.bias[k] = random.uniform(-stdv, stdv)
def test_from_file(self): size = 10000 with tempfile.NamedTemporaryFile() as f: s1 = torch.FloatStorage.from_file(f.name, True, size) t1 = torch.FloatTensor(s1).copy_(torch.randn(size)) # check mapping s2 = torch.FloatStorage.from_file(f.name, True, size) t2 = torch.FloatTensor(s2) self.assertEqual(t1, t2, 0) # check changes to t1 from t2 rnum = random.uniform(-1, 1) t1.fill_(rnum) self.assertEqual(t1, t2, 0) # check changes to t2 from t1 rnum = random.uniform(-1, 1) t2.fill_(rnum) self.assertEqual(t1, t2, 0)
def test_clamp(self): m1 = torch.rand(100).mul(5).add(-2.5) # uniform in [-2.5, 2.5] # just in case we're extremely lucky. min_val = -1 max_val = 1 m1[1] = min_val m1[2] = max_val res1 = m1.clone() res1.clamp_(min_val, max_val) res2 = m1.clone() for i in iter_indices(res2): res2[i] = max(min_val, min(max_val, res2[i])) self.assertEqual(res1, res2)
def test_conv2(self): x = torch.rand(math.floor(torch.uniform(50, 100)), math.floor(torch.uniform(50, 100))) k = torch.rand(math.floor(torch.uniform(10, 20)), math.floor(torch.uniform(10, 20))) imvc = torch.conv2(x, k) imvc2 = torch.conv2(x, k, 'V') imfc = torch.conv2(x, k, 'F') ki = k.clone() ks = k.storage() kis = ki.storage() for i in range(ks.size()-1, 0, -1): kis[ks.size()-i+1] = ks[i] #for i=ks.size(), 1, -1 do kis[ks.size()-i+1]=ks[i] end imvx = torch.xcorr2(x, ki) imvx2 = torch.xcorr2(x, ki, 'V') imfx = torch.xcorr2(x, ki, 'F') self.assertEqual(imvc, imvc2, 0, 'torch.conv2') self.assertEqual(imvc, imvx, 0, 'torch.conv2') self.assertEqual(imvc, imvx2, 0, 'torch.conv2') self.assertEqual(imfc, imfx, 0, 'torch.conv2') self.assertLessEqual(math.abs(x.dot(x) - torch.xcorr2(x, x)[0][0]), 1e-10, 'torch.conv2') xx = torch.Tensor(2, x.size(1), x.size(2)) xx[1].copy_(x) xx[2].copy_(x) kk = torch.Tensor(2, k.size(1), k.size(2)) kk[1].copy_(k) kk[2].copy_(k) immvc = torch.conv2(xx, kk) immvc2 = torch.conv2(xx, kk, 'V') immfc = torch.conv2(xx, kk, 'F') self.assertEqual(immvc[0], immvc[1], 0, 'torch.conv2') self.assertEqual(immvc[0], imvc, 0, 'torch.conv2') self.assertEqual(immvc2[0], imvc2, 0, 'torch.conv2') self.assertEqual(immfc[0], immfc[1], 0, 'torch.conv2') self.assertEqual(immfc[0], imfc, 0, 'torch.conv2')
def test_clamp(self): m1 = torch.rand(100).mul(5).add(-2.5) # uniform in [-2.5, 2.5] # just in case we're extremely lucky. min_val = -1 max_val = 1 m1[1] = min_val m1[2] = max_val res1 = m1.clone() res1.clamp_(min_val, max_val) res2 = m1.clone() for i in iter_indices(res2): res2[i] = max(min_val, min(max_val, res2[i])) self.assertEqual(res1, res2) res1 = torch.clamp(m1, min=min_val) res2 = m1.clone() for i in iter_indices(res2): res2[i] = max(min_val, res2[i]) self.assertEqual(res1, res2) res1 = torch.clamp(m1, max=max_val) res2 = m1.clone() for i in iter_indices(res2): res2[i] = min(max_val, res2[i]) self.assertEqual(res1, res2)
def test_conv2(self): x = torch.rand(math.floor(torch.uniform(50, 100)), math.floor(torch.uniform(50, 100))) k = torch.rand(math.floor(torch.uniform(10, 20)), math.floor(torch.uniform(10, 20))) imvc = torch.conv2(x, k) imvc2 = torch.conv2(x, k, 'V') imfc = torch.conv2(x, k, 'F') ki = k.clone() ks = k.storage() kis = ki.storage() for i in range(ks.size() - 1, 0, -1): kis[ks.size() - i + 1] = ks[i] # for i=ks.size(), 1, -1 do kis[ks.size()-i+1]=ks[i] end imvx = torch.xcorr2(x, ki) imvx2 = torch.xcorr2(x, ki, 'V') imfx = torch.xcorr2(x, ki, 'F') self.assertEqual(imvc, imvc2, 0, 'torch.conv2') self.assertEqual(imvc, imvx, 0, 'torch.conv2') self.assertEqual(imvc, imvx2, 0, 'torch.conv2') self.assertEqual(imfc, imfx, 0, 'torch.conv2') self.assertLessEqual(math.abs(x.dot(x) - torch.xcorr2(x, x)[0][0]), 1e-10, 'torch.conv2') xx = torch.Tensor(2, x.size(1), x.size(2)) xx[1].copy_(x) xx[2].copy_(x) kk = torch.Tensor(2, k.size(1), k.size(2)) kk[1].copy_(k) kk[2].copy_(k) immvc = torch.conv2(xx, kk) immvc2 = torch.conv2(xx, kk, 'V') immfc = torch.conv2(xx, kk, 'F') self.assertEqual(immvc[0], immvc[1], 0, 'torch.conv2') self.assertEqual(immvc[0], imvc, 0, 'torch.conv2') self.assertEqual(immvc2[0], imvc2, 0, 'torch.conv2') self.assertEqual(immfc[0], immfc[1], 0, 'torch.conv2') self.assertEqual(immfc[0], imfc, 0, 'torch.conv2')
def test_conv3(self): x = torch.rand(math.floor(torch.uniform(20, 40)), math.floor(torch.uniform(20, 40)), math.floor(torch.uniform(20, 40))) k = torch.rand(math.floor(torch.uniform(5, 10)), math.floor(torch.uniform(5, 10)), math.floor(torch.uniform(5, 10))) imvc = torch.conv3(x, k) imvc2 = torch.conv3(x, k, 'V') imfc = torch.conv3(x, k, 'F') ki = k.clone(); ks = k.storage() kis = ki.storage() for i in range(ks.size()-1, 0, -1): kis[ks.size()-i+1] = ks[i] imvx = torch.xcorr3(x, ki) imvx2 = torch.xcorr3(x, ki, 'V') imfx = torch.xcorr3(x, ki, 'F') self.assertEqual(imvc, imvc2, 0, 'torch.conv3') self.assertEqual(imvc, imvx, 0, 'torch.conv3') self.assertEqual(imvc, imvx2, 0, 'torch.conv3') self.assertEqual(imfc, imfx, 0, 'torch.conv3') self.assertLessEqual(math.abs(x.dot(x) - torch.xcorr3(x, x)[0][0][0]), 4e-10, 'torch.conv3') xx = torch.Tensor(2, x.size(1), x.size(2), x.size(3)) xx[1].copy_(x) xx[2].copy_(x) kk = torch.Tensor(2, k.size(1), k.size(2), k.size(3)) kk[1].copy_(k) kk[2].copy_(k) immvc = torch.conv3(xx, kk) immvc2 = torch.conv3(xx, kk, 'V') immfc = torch.conv3(xx, kk, 'F') self.assertEqual(immvc[0], immvc[1], 0, 'torch.conv3') self.assertEqual(immvc[0], imvc, 0, 'torch.conv3') self.assertEqual(immvc2[0], imvc2, 0, 'torch.conv3') self.assertEqual(immfc[0], immfc[1], 0, 'torch.conv3') self.assertEqual(immfc[0], imfc, 0, 'torch.conv3')
def test_conv3(self): x = torch.rand(math.floor(torch.uniform(20, 40)), math.floor(torch.uniform(20, 40)), math.floor(torch.uniform(20, 40))) k = torch.rand(math.floor(torch.uniform(5, 10)), math.floor(torch.uniform(5, 10)), math.floor(torch.uniform(5, 10))) imvc = torch.conv3(x, k) imvc2 = torch.conv3(x, k, 'V') imfc = torch.conv3(x, k, 'F') ki = k.clone() ks = k.storage() kis = ki.storage() for i in range(ks.size() - 1, 0, -1): kis[ks.size() - i + 1] = ks[i] imvx = torch.xcorr3(x, ki) imvx2 = torch.xcorr3(x, ki, 'V') imfx = torch.xcorr3(x, ki, 'F') self.assertEqual(imvc, imvc2, 0, 'torch.conv3') self.assertEqual(imvc, imvx, 0, 'torch.conv3') self.assertEqual(imvc, imvx2, 0, 'torch.conv3') self.assertEqual(imfc, imfx, 0, 'torch.conv3') self.assertLessEqual(math.abs(x.dot(x) - torch.xcorr3(x, x)[0][0][0]), 4e-10, 'torch.conv3') xx = torch.Tensor(2, x.size(1), x.size(2), x.size(3)) xx[1].copy_(x) xx[2].copy_(x) kk = torch.Tensor(2, k.size(1), k.size(2), k.size(3)) kk[1].copy_(k) kk[2].copy_(k) immvc = torch.conv3(xx, kk) immvc2 = torch.conv3(xx, kk, 'V') immfc = torch.conv3(xx, kk, 'F') self.assertEqual(immvc[0], immvc[1], 0, 'torch.conv3') self.assertEqual(immvc[0], imvc, 0, 'torch.conv3') self.assertEqual(immvc2[0], imvc2, 0, 'torch.conv3') self.assertEqual(immfc[0], immfc[1], 0, 'torch.conv3') self.assertEqual(immfc[0], imfc, 0, 'torch.conv3')