Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyndna/070bea8b11b297e8bf37 to your computer and use it in GitHub Desktop.
Save dyndna/070bea8b11b297e8bf37 to your computer and use it in GitHub Desktop.
An example of building a TensorFlow model from R using rPython
### An example of building a TensorFlow model from R using rPython ###
# For this script you need to
# 1. Have python 2.7 installed.
# 2. Install the rPython package in R.
# 3. Install Google's TensorFlow library as per these instructions:
# http://www.tensorflow.org/get_started/os_setup.md#binary_installation
### Here is how to setup and run a trivial TensorFlow model ###
# Load TensorFlow (I couldn't get this to work without setting sys.argv... )
library(rPython)
python.exec("
import sys
sys.argv = ['']
import tensorflow as tf
")
# Define a "hello world" TensorFlow model adding two numbers
python.exec("
a = tf.constant(10)
b = tf.constant(32)
sum = a + b
")
#Instantiate a TensorFlow session, and get the result into R.
# (we need the .tolist() to convert from the result into something
# that can be serialized by JSON and imported into R)
python.exec("
sess = tf.Session()
result = sess.run(sum)
")
result = python.get("result.tolist()")
# Tada! :)
print(result)
## [1] 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment