Tensorflow/Tensorflow for R

Tensorflow for R 설치해보기

딥스탯 2017. 6. 5. 18:45

Tensorflow for R 설치

install.packages("tensorflow") require(tensorflow) install_tensorflow()

를 하면 설치가 잘 되다가 에러가 뜬다.

     Error: Required package curl not found. Please run: install.packages('curl')


그래서 curl을 설치해주고 다시 install_tensorflow()를 했다.

install.packages("curl") install_tensorflow()

생각보다 금방 설치한다.


실행 예제 1 (Hello, TensorFlow!)

sess = tf$Session()
hello <- tf$constant('Hello, TensorFlow!')
sess$run(hello)

(출처 : https://tensorflow.rstudio.com/)


    b'Hello, TensorFlow!' 



실행 예제 2

# Create 100 phony x, y data points, y = x * 0.1 + 0.3
x_data <- runif(100, min=0, max=1)
y_data <- x_data * 0.1 + 0.3

# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0))
b <- tf$Variable(tf$zeros(shape(1L)))
y <- W * x_data + b

# Minimize the mean squared errors.
loss <- tf$reduce_mean((y - y_data) ^ 2)
optimizer <- tf$train$GradientDescentOptimizer(0.5)
train <- optimizer$minimize(loss)

# Launch the graph and initialize the variables.
sess = tf$Session()
sess$run(tf$global_variables_initializer())

# Fit the line (Learns best fit is W: 0.1, b: 0.3)
for (step in 1:201) {
  sess$run(train)
  if (step %% 20 == 0)
    cat(step, "-", sess$run(W), sess$run(b), "\n")
}

(출처 : https://tensorflow.rstudio.com/)


    20 - 0.04115903 0.3310893 

    40 - 0.08205463 0.3094816 

    60 - 0.09452701 0.3028917 

    80 - 0.09833086 0.3008819 

    100 - 0.09949093 0.300269 

    120 - 0.09984475 0.300082 

    140 - 0.09995265 0.300025 

    160 - 0.09998556 0.3000076 

    180 - 0.0999956 0.3000023 

    200 - 0.09999865 0.3000007 


잘 돌아간다.

예전에 설치 할 때는 python을 설치하고 거기에 tensorflow를 완벽하게 설치하고 PATH도 지정해주고 등등 엄청 힘들었던거 같은데

설치과정이 엄청 단순해져서 좋다.


GPU버전 설치하기

require(tensorflow) install_tensorflow(gpu = TRUE)

를 하면 된다고 하는데 GPU가 CUDA가 지원되지 않으므로 설치하는데 실패했다.

(추측) 설명을 보니 CUDA 설치 후 CUDNN 설치하고 위의 코드를 돌려야하는 듯 하다.


설치 참고 사이트


https://github.com/rstudio/tensorflow

https://tensorflow.rstudio.com/