<jupyter_start><jupyter_text>Basic python<jupyter_code>import numpy as np
import torch as tc
import matplotlib.pyplot as plt
%matplotlib inline
arr = [1, 2, 3, 4, 5]
nparr = np.array(arr)
nparr1 = np.array([1.,2.,3.,4.,5])
nparr == nparr1
np.shape(nparr)
X_train = tc.FloatTensor([[1,2,3]])
y_train = tc.FloatTensor([[2, 4, 6]])


plt.scatter(X_train, y_train)
X = np.linspace(0, 10, 11)
y = np.linspace(0, 10, 11)
plt.plot(X,y)

W = tc.zeros(1, requires_grad = True)
b = tc.zeros(1, requires_grad=True)
n_data = len(X_train)
num_epochs = 5000
learning_rate = 0.01
import torch.nn as nn
import torch.nn.functional as F
from torch import optim
print(cost)
print(W)
print(b)
num_epochs = 1000
W = tc.zeros(1, requires_grad = True)
b = tc.zeros(1, requires_grad=True)
for epoch in range(num_epochs):
    hypothesis = X_train * W + b
    cost = tc.mean((hypothesis-y_train)**2)
    optimizer = optim.SGD([W, b], lr = 0.01)
    optimizer.zero_grad()#grad초기화
    cost.backward() #cost의 기울기 계산
    optimizer.step()    # W와 b업데이트

    if epoch%100 == 0:
        print("Epoch{:4d} ,W:{:.3f},  b:{:.3f} , Cost:{:.6f}".format(epoch,W.item(), b.item(), cost.item()))<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup