[TIL]RMSE(Root Mean Squared Error)

Why?

  • List Comprehension이 익숙치 않아 연습차원에 기존 Evaluation Metric을 줄여보려는 시도로 작성
  • 작성하는 김에 numpy, sklearn, torch 모두 작성.
  • $RMSE = \sqrt {\sum\limits_{i=1}^{n} (\hat{y_{i}} - y_{i})^2 \over n}$

Code

import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error
import torch

label = np.random.random((1, 5)).reshape(5,-1)
pred = np.random.random((1, 5)).reshape(5,-1)
# label = np.array([1,2,3,4,5])
# pred = np.array([0,2,5,7,8])

def rmse(label, pred):
    return   (sum([pow(diff,2) for diff in  [y_hat-y  for y_hat, y in zip(pred, label)]]) / len(label)) ** (1/2)

def torch_rmse(label,pred):
  return (torch.Tensor(pred)- torch.Tensor(label)).pow(2).mean().sqrt()

np.round(rmse(label,pred),2) == np.round(mean_squared_error(label,pred,squared=False),2) == np.round(torch_rmse(label,pred),2)

Tip

  • 아래와 같은  Error  발생시 Numpy Array 변환 Function 을 torch.from_numpy() → torch.Tensor로 변경한다.
  • torch.from_numpy()torch.Tensor()는 return type은 Tensor로 동일하나, from_numpy는 기존 input의 data type을 그대로 가지고 오고, Tensor는 FloatTensor를 기본으로 가지고 오면서 그 차이에서 발생하는 에러임 (참고)
RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead.

Read more

[책]Reshuffle: Who wins when AI restacks the knowledge economy

[책]Reshuffle: Who wins when AI restacks the knowledge economy

원래는 Amazon에 가서 Personal Knowledge Managment에 관한 책을 사려고 했다. Sketch Your Mind라는 책이었는데, 그 때 이 책 “Reshuffle”을 발견하였다. AI가 어떻게 Knowledge Economy를 흔들 것가? 라는 부제를 훑어보면서 저자가 쓴 다른 책을 보게 되었는데 거기에 내가 좋아했던 책을쓴 저자라는 것을 알게 되었다. 그래서 크게 고민하지 않고 구매를 하고

By Bongho, Lee
[책]올라운드투자, 누군가의 투자일기

[책]올라운드투자, 누군가의 투자일기

“올라운드 투자”라는 제목을 보았을 때는, “올라운드 플레이어”가 생각이 났다. “올라운드”라는 표현을 오랜만에 들어본 까닭이었다. 그럼에도 불구하고 이 책을 고른 것은 저자가 그간 보여준 컨텐츠에 대한 신뢰가 있던 까닭이었다. 컨텐츠를 다양하게 보는 편이지만 깊이가 아주 있지는 않았다. 여기서 깊이라 함은 기존 전문적인 정량적 분석의 내용의 수준을 말하는 것이다.

By Bongho, Lee
내가 놓치고 있던 미래, 먼저 온 미래를 읽고

내가 놓치고 있던 미래, 먼저 온 미래를 읽고

장강명 작가의 책은, 유학시절 읽고 처음이었다. 유학시절 "한국이 싫어서"라는 책은 동기부여가 상당히 되는 책이었다. 한국을 떠나 새로운 정채성을 학생으로서 Build up 해나가고 있던 상황에서 이 책은 제목부터 꽤 솔깃하였다. 물론 결말이 기억날 정도로 인상깊은 책은 아니었지만 말이다. 그렇게 시간이 흘러 장강명 작가의 책은 더 이상 읽지 않던

By Bongho, Lee