0

I'm going through tensorflow example of water droplets on water, code:

#Import libraries for simulation
import tensorflow as tf
import numpy as np

#Imports for visualization
import PIL.Image
from io import BytesIO
from IPython.display import clear_output, Image, display

#A function for displaying the state of the pond's surface as an image.
def DisplayArray(a, fmt='jpeg', rng=[0,1]):
  """Display an array as a picture."""
  a = (a - rng[0])/float(rng[1] - rng[0])*255
  a = np.uint8(np.clip(a, 0, 255))
  f = BytesIO()
  PIL.Image.fromarray(a).save(f, fmt)
  clear_output(wait = True)
  display(Image(data=f.getvalue()))

sess = tf.InteractiveSession()

def make_kernel(a):
  """Transform a 2D array into a convolution kernel"""
  a = np.asarray(a)
  a = a.reshape(list(a.shape) + [1,1])
  return tf.constant(a, dtype=1)

def simple_conv(x, k):
  """A simplified 2D convolution operation"""
  x = tf.expand_dims(tf.expand_dims(x, 0), -1)
  y = tf.nn.depthwise_conv2d(x, k, [1, 1, 1, 1], padding='SAME')
  return y[0, :, :, 0]

def laplace(x):
  """Compute the 2D laplacian of an array"""
  laplace_k = make_kernel([[0.5, 1.0, 0.5],
                           [1.0, -6., 1.0],
                           [0.5, 1.0, 0.5]])
  return simple_conv(x, laplace_k)

N = 500

# Initial Conditions -- some rain drops hit a pond

# Set everything to zero
u_init = np.zeros([N, N], dtype=np.float32)
ut_init = np.zeros([N, N], dtype=np.float32)

# Some rain drops hit a pond at random points
for n in range(40):
  a,b = np.random.randint(0, N, 2)
  u_init[a,b] = np.random.uniform()

DisplayArray(u_init, rng=[-0.1, 0.1])

# Parameters:
# eps -- time resolution
# damping -- wave damping
eps = tf.placeholder(tf.float32, shape=())
damping = tf.placeholder(tf.float32, shape=())

# Create variables for simulation state
U  = tf.Variable(u_init)
Ut = tf.Variable(ut_init)

# Discretized PDE update rules
U_ = U + eps * Ut
Ut_ = Ut + eps * (laplace(U) - damping * Ut)

# Operation to update the state
step = tf.group(
  U.assign(U_),
  Ut.assign(Ut_))

# Initialize state to initial conditions
tf.global_variables_initializer().run()

# Run 1000 steps of PDE
for i in range(1000):
  # Step simulation
  step.run({eps: 0.03, damping: 0.04})
  DisplayArray(U.eval(), rng=[-0.1, 0.1])

Then from Ipython I import partial_d but it doesn't generate the animation.

enter image description here

Anyone who's ever used tensorflow know how to fix this? Google mentions Ipython Notebook, couldn't find/set that up but I do have jupyter and latest Ipython installed.

Sam B.
  • 2,703
  • 8
  • 40
  • 78

2 Answers2

1

Have you used jupyter before? I think you need to start your notebook server and run the code from within there. Try running jupyter notebook and then importing your code into the notebook. Alternatively you could just copy and paste your code into a code cell and skip importing.

I'm unfamiliar with the example you are referring to but I don't think it's a TF problem. See how you do with running it through jupyter (the new name for iPython to clear up any confusion).

JCooke
  • 950
  • 1
  • 5
  • 17
  • I'm familiar with python, just installed jupyter and I did ran the code but couldn't figure out how to run a script from there. Here's two snapshots of this https://drive.google.com/file/d/0B0nxIjitvEABMkQzZDktcUNyV3c/view?usp=sharing https://drive.google.com/file/d/0B0nxIjitvEABUmppc3JyREg1ZDg/view?usp=sharing – Sam B. Mar 27 '17 at 09:48
  • Click new, new notebook. Ensure that it is correctly connected to a python kernel (it should be) and then + a new code cell, copy your code in and run. Can I **strongly** suggest you take a few minutes to look into jupyter notebooks. They are super helpful when it comes to writing python, especially with things such as Tensorflow. I use it all the time for my TF development. Don't open the .py file though as you can't run that in jupyter. You could import using the command you mentioned previously but I'd just put it in a cell in a fresh notebook. – JCooke Mar 27 '17 at 14:53
  • now that I figured out how to use it yeah just copying it into the cell then running it works, how do you import code? – Sam B. Mar 27 '17 at 18:45
  • I believe there is an import command but i've never used it. I usually start from scratch but you could just copy your code into cells if you don't have much. I would look into how you can use jupyter to optimise your workflow and split your code up between cells. It really helps me with Tensorflow! For more info on importing code with magic commands see [this](http://stackoverflow.com/questions/21034373/how-to-load-edit-run-save-text-files-py-into-an-ipython-notebook-cell) For further jupyter questions feel free to create a new question, link it here and I can help as this q has been answered – JCooke Mar 28 '17 at 08:50
0

This got to me to speed on how to use jupyter and tensorflow to generate the animation of ripples.

Sam B.
  • 2,703
  • 8
  • 40
  • 78