import torch
import torch.optim as optim
import tensorflow as tf
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
텐서플로
x_data = [11,12,13,14,15,16,17]
x_data_2 = [0, 0, 0, 0, 1, 1, 1]
y_data = [35, 44, 56, 72, 192, 200, 208]
w = tf.Variable(tf.random.normal([1]))
w_2 = tf.Variable(tf.random.normal([1]))
b = tf.Variable(tf.random.normal([1]))
learning_rate = 0.0005
for i in range(50001):
with tf.GradientTape() as tape:
hypothesis = w * x_data + w_2 * x_data_2 + b
cost = tf.reduce_mean(tf.square(hypothesis - y_data))
w_grad, w_2_grad, b_grad = tape.gradient(cost, [w, w_2, b])
w.assign_sub(learning_rate * w_grad)
w_2.assign_sub(learning_rate * w_2_grad)
b.assign_sub(learning_rate * b_grad)
if i % 500 == 0:
print(f'Step : {i}, Cost : {int(cost)}, Weight : {w.numpy()[0], w_2.numpy()[0]}, Bias : {b.numpy()[0]}')
temp_x = 18
temp_x_2 = 1
print()
print(f"온도가 {temp_x}도이며 프로모션 진행여부 {temp_x_2} 일 때, 에어컨 판매 예상 갯수 :", int(w * temp_x + w_2 * temp_x_2 + b))
파이토치
x_data = torch.FloatTensor([[11], [12], [13], [14], [15], [16], [17]])
x_data_2 = torch.FloatTensor([[0], [0], [0], [0], [1], [1], [1]])
y_data = torch.FloatTensor([[35], [44], [56], [72], [192], [200], [208]])
w = torch.zeros(1, requires_grad=True)
w_2 = torch.zeros(1, requires_grad=True)
b = torch.zeros(1, requires_grad=True)
learning_rate = 0.0005
optimizer = optim.SGD([w,w_2,b], learning_rate)
for i in range(50001) :
hypothesis = w * x_data + w_2 * x_data_2 + b
cost = torch.mean((hypothesis - y_data)**2)
optimizer.zero_grad()
cost.backward()
optimizer.step()
if i % 500 == 0 :
print(f'Step : {i}, Cost : {cost.item()}, Weight : {w.item()}, {w_2.item()}, Bias : {b.item()}')
temp_x = 18
temp_x_2 = 1
print()
print(f"온도가 {temp_x}도이며 프로모션 진행여부 {temp_x_2} 일 때, 에어컨 판매 예상 갯수 :", int(w * temp_x + w_2 * temp_x_2+ b))
파이토치(with nn_module)
class MultivariateLinearRegressionModel(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(2, 1)
def forward(self, x):
return self.linear(x)
x_data_1 = torch.FloatTensor([[11], [12], [13], [14], [15], [16], [17]])
x_data_2 = torch.FloatTensor([[0], [0], [0], [0], [1], [1], [1]])
x_data = torch.cat([x_data_1, x_data_2], dim = 1)
# print(x_data)
y_data = torch.FloatTensor([[35], [44], [56], [72], [192], [200], [208]])
learning_rate = 0.0005
model = MultivariateLinearRegressionModel()
optimizer = optim.SGD(model.parameters(), learning_rate)
for i in range(50001) :
prediction = model(x_data)
cost = F.mse_loss(prediction, y_data)
optimizer.zero_grad()
cost.backward()
optimizer.step()
if i % 500 == 0 :
print(f'Step : {i}, Cost : {cost.item()}')
temp_x_1 = torch.FloatTensor([[18]])
temp_x_2 = torch.FloatTensor([[1]])
temp_x = torch.cat([temp_x_1, temp_x_2], dim = 1)
print()
print(f'에어컨 판매 예상 갯수 : {int(model(temp_x).item())}')
반응형
'AI > PyTorch' 카테고리의 다른 글
PyTorch-실습 : 파이토치 소프트맥스 회귀 구현(vs TF) (0) | 2020.09.16 |
---|---|
PyTorch-실습 : 파이토치 로지스틱 회귀 구현(vs TF) (0) | 2020.09.16 |
PyTorch-실습 : 파이토치 선형 회귀 구현(vs TF) (0) | 2020.09.16 |
PyTorch-실습 : Tensor 조작하기(2) (0) | 2020.09.16 |
PyTorch-실습 : Tensor 조작하기(1) (0) | 2020.09.16 |