Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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:
### Here is how to setup and run a trivial TensorFlow model ###
# Load TensorFlow (I couldn't get this to work without setting sys.argv... )
import sys
sys.argv = ['']
import tensorflow as tf
# Define a "hello world" TensorFlow model adding two numbers
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)
sess = tf.Session()
result =
result = python.get("result.tolist()")
# Tada! :)
## [1] 42
Copy link

billyvreeland commented Feb 3, 2016

This is nice example. I have been trying to do something similar but am running into a "cannot import name pywrap_tensorflow" error when trying to import tensorflow. I know this is typically a circular reference error, but I haven't been able to figure out what is causing it. I was just curious if you ran into it when getting things set up.


Copy link

dennisthemenace2 commented Apr 27, 2016

i get a problem when i have huger data sets. it breaks execution
Fehler in python.exec("\n for i in range(10):\n, feed_dict={x: trainX, y_: trainY})\n ") :

Copy link

prakashjayy commented Aug 11, 2016

No module named tensorflow.

I get this error when I run tensorflow from R. However from the python console it is running fine. What can be the reason?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment