<jupyter_start><jupyter_text>3.1 线性回归<jupyter_code>import torch
from time import time

print(torch.__version__)
a = torch.ones(1000)
b = torch.ones(1000)<jupyter_output><empty_output><jupyter_text>将这两个向量按元素逐一做标量加法:<jupyter_code>start = time()
c = torch.zeros(1000)
for i in range(1000):
    c[i] = a[i] + b[i]
print(time() - start)<jupyter_output>0.008157014846801758<jupyter_text>将这两个向量直接做矢量加法:<jupyter_code>start = time()
d = a + b
print(time() - start)<jupyter_output>0.0001227855682373047<jupyter_text>**结果很明显，后者比前者更省时。因此，我们应该尽可能采用矢量计算，以提升计算效率。** 广播机制例子🌰：<jupyter_code>a = torch.ones(3)
b = 10
print(a + b)<jupyter_output>tensor([11., 11., 11.])
{'fraction_non_alphanumeric': 0.1617873651771957, 'fraction_numerical': 0.1016949152542373, 'mean_word_length': 8.285714285714286, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '84499', 'license': 'Apache-2.0', 'n_tokens_mistral': 391, 'n_tokens_neox': 359, 'n_words': 38, 'path': 'code/chapter03_DL-basics/3.1_linear-regression.ipynb', 'repo_name': 'StanleySongPro/Dive-into-DL-PyTorch', 'size': '649'}