1.torch.randn()
#表示生成10行1列的矩阵
>>> print(torch.randn(10))
tensor([ 0.2404, 0.1553, 0.9415, -1.4893, -0.5702, 0.1510, -1.9383, -1.3285,
1.9475, 0.5191])
#表示生成3行7列的矩阵
>>> print(torch.randn(3,7))
tensor([[-0.0585, 0.7210, 0.0596, -0.4930, 0.1570, 1.0687, -1.1394],
[ 3.0782, -0.2311, 0.1482, -0.3761, 0.7074, -1.2496, -0.4011],
[ 0.8792, -0.2779, -0.0510, 0.0839, 0.0405, -0.3757, 0.5353]])
#表示生成3行4列的矩阵,每个矩阵由5行7列的矩阵组成
>>> print(torch.randn(3,4,5,7))
tensor([[[[-1.5769, 0.2419, -0.5868, -0.1063, 1.3775, 0.2525, 0.9875],
[-0.6063, -0.1322, -0.7904, 0.1302, -0.1658, -0.3401, 0.2370],
[-0.9917, 1.0001, 0.6126, -0.1851, 1.0017, 0.8889, -0.0244],
[-0.4924, 0.5012, -0.8785, 0.0968, 1.4721, 0.7539, -0.4921],
[-0.1021, 0.3572, -1.4037, -0.6478, 0.3062, 0.6476, -0.2969]],
[[ 1.1890, -0.3115, 1.1767, 0.2928, 0.0390, -0.2468, 2.1554],
[-1.6433, -0.6190, -1.2238, 0.8265, 0.6606, -0.6318, -1.1565],
[ 0.4185, -0.6345, -0.4282, -2.7952, 0.6514, -0.4509, -0.9177],
[ 0.4955, 0.1723, 0.3209, -1.0996, 0.4690, -0.1139, -0.0224],
[-0.9791, 0.8259, 1.2577, 2.2184, 0.9699, 0.8780, 0.3876]],
[[-0.6944, -1.1339, -0.0226, -0.2396, -0.7640, 0.3367, 1.3382],
[-0.1035, -0.6598, -1.1466, -2.1089, 1.6265, 0.9400, 1.7371],
[-2.1663, 0.4829, 0.9560, -0.0897, -0.8028, 0.5874, 0.7805],
[-0.4544, 0.3491, -0.8602, -2.1368, 0.7891, -0.0866, -1.0528],
[-0.3847, -0.7381, -0.0796, 0.9381, 0.4102, -0.6421, 0.6569]],
[[ 1.7909, 1.8070, 1.9781, 0.1744, 0.4654, 0.1699, 1.7617],
[-1.3079, -0.2636, 1.4659, 1.4563, 1.5756, -0.2794, 0.2988],
[-1.3613, 0.1639, -1.4990, 0.9130, -0.9895, 0.8622, 1.5201],
[-0.7516, -1.1907, 0.5809, -1.2971, -0.0990, -1.5611, -1.3456],
[ 0.7150, -1.7344, 0.8067, 0.6613, 0.1570, 0.8911, -0.8433]]],
[[[ 1.0246, 0.1455, 0.0153, -0.3716, 0.0327, 0.2722, -1.0645],
[-1.1506, -0.4070, 1.7139, -0.9553, 0.4746, -0.6803, -0.5326],
[-1.0869, 0.2114, 0.5734, 1.4219, -0.8341, 0.4975, 0.0552],
[-0.3174, 2.5000, -0.0537, -0.8024, -0.7332, -0.4389, -0.0832],
[ 1.0840, 0.4139, 1.9582, -1.1746, -0.4922, -1.6308, 0.4970]],
[[ 0.5620, 0.9670, -1.5087, -0.5367, 1.1533, 0.5771, 0.7971],
[ 1.5197, 0.9345, 1.9864, 1.7906, 1.5021, -0.0080, -0.5722],
[-0.4029, -1.1087, -0.7542, -0.5647, 0.3224, -1.3724, -1.7829],
[ 1.5960, 0.5988, -0.1203, 0.4854, 0.0474, -1.0549, -0.4340],
[-1.5121, -0.2610, -1.2912, -0.3814, -0.1792, 1.5958, -0.7794]],
[[-0.9935, 0.0514, -0.5887, 0.4746, -1.2608, 1.2408, -1.4667],
[ 0.2221, -1.2183, 1.4918, -0.0342, 0.9353, -0.6949, -1.4736],
[-0.9205, 1.4535, -0.6945, 0.4575, 1.1579, -0.2706, -0.3936],
[ 0.6763, -0.0519, -0.8416, -0.2351, 0.0309, -1.9047, -1.3954],
[-1.3733, -0.4315, 0.2281, 1.0353, -0.4927, 1.5695, -1.8960]],
[[ 2.9079, 0.7202, 0.0867, -1.1760, 0.1149, -0.4039, 2.2260],
[ 0.6643, 0.0450, 1.0521, 1.1080, 0.1285, 0.2523, -0.1265],
[-0.3117, -0.7383, -0.2497, 0.5455, -1.1887, 0.6445, 2.5453],
[ 1.5697, 0.2740, -0.5679, -1.3099, 0.2619, 0.3816, -0.1587],
[ 0.2626, -0.2919, 1.8030, -0.7255, -0.6814, -0.3998, 0.0349]]],
[[[-0.0180, 1.1783, 0.0545, -0.5904, 2.1198, 0.1767, -0.4513],
[ 0.3815, 0.0154, 0.7989, 1.1422, 0.8220, 0.6564, 1.8591],
[ 1.5829, 0.3615, 1.4223, 0.7114, -0.5724, -0.7661, -0.4396],
[-0.4273, -0.7763, 1.1050, 0.0252, -0.8989, 0.3818, 0.8155],
[ 0.3036, -0.4567, 1.4826, -2.1209, -0.1647, 1.5700, 0.2828]],
[[ 1.9126, -0.9452, 0.5136, 1.1199, 0.2789, -1.6411, 0.4620],
[-0.0253, -1.1983, 0.2417, -0.6314, 0.2280, -0.6911, 0.9335],
[-0.7067, -0.7373, -0.5243, -1.5660, -1.6268, 0.4014, -0.8195],
[-1.0538, -1.7657, -0.4699, -0.4545, -0.6200, 0.2584, -1.4731],
[ 0.6076, -0.7046, -0.3294, 0.3833, -0.3924, 0.1269, 0.7618]],
[[-0.0238, -2.3625, 1.1116, -0.4800, 2.0954, -0.7863, 0.0848],
[-1.0785, 1.6630, -0.5565, 0.4222, -0.3833, 1.1850, -0.5977],
[-0.6167, 1.0896, -0.5004, 0.4051, -0.7550, -1.5005, 1.0142],
[-0.7001, 0.7948, -0.0656, -0.0716, -0.4193, 0.4981, 0.9962],
[-0.1734, -1.2223, 1.0788, 1.2734, 0.1327, 1.1607, -0.6811]],
[[-2.5511, -0.0265, -0.4980, 1.1688, -0.6372, -0.3048, -0.0291],
[-0.4156, -0.6856, 0.1805, 2.6037, -0.1376, 0.4971, 0.1500],
[ 0.6073, 0.3128, 0.6700, 0.9330, 0.0262, -0.0907, 1.0923],
[ 1.0287, -0.3822, 0.0512, 1.4120, 0.2241, -0.7554, 1.4128],
[-0.6244, 0.1827, 0.3909, 1.5041, 0.0608, -1.0038, -0.5171]]]])
2.torch.view()
#一开始看了半天都看不懂,可能是没实践,一实践就懂了
#torch.view()就相当于重整型,下面就是原来是(2,6)的矩阵弄成(3,4)的矩阵
a=torch.randn(2,6)
b=a.view(3,4)
#也可以写成
b=a.view(3,-1)