TensorFlow scatter_nd function is not working with placeholder and complex input - python-2.7

I am using tf.scatter_nd to update complex values at some index.
It seems the real and imaginary parts are somehow getting added together by this function. My question is how to make it work with placeholders. Here is the minimal working example where the variables b and e should have same values.
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
update=np.asarray([1.+2j])
idx=tf.constant( [[0]])
shp=tf.constant([1])
# works with constants
a=tf.constant(update)
b=tf.scatter_nd(idx,a,shp)
with tf.Session() as sess:
print sess.run(b) # correct output: 1.+2j
#Does not work with placeholders
d=tf.placeholder(tf.complex128)
e=tf.scatter_nd(idx,d,shp)
with tf.Session() as sess:
print sess.run(e,feed_dict={d:update}) # WRONG output: 3.+0j
I am using Anaconda python 2.7 + TensorFlow 1.7 GPU version installed using the conda command.
Edit:
The issue occurs when running the code on GPU. CPU version works correctly.
Here is the updated code to reproduce the issue in TensorFlow-GPU 1.8 installed using Anaconda Python 2.7.
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
update=np.asarray([1.+2j])
idx=tf.constant( [[0]])
shp=tf.constant([1])
a=tf.placeholder(tf.complex128)
with tf.device("/cpu:0"):
b=tf.scatter_nd(idx,a,shp)
with tf.device("/gpu:0"):
c=tf.scatter_nd(idx,a,shp)
with tf.Session() as sess:
print 'Correct output on CPU', sess.run(b,feed_dict={a:update})
print 'Wrong output on GPU',sess.run(c,feed_dict={a:update})
I saw this thread and this thread but could not find how to resolve it. Is there any alternative to tf.scatter_nd that will run on GPU?

Related

Tensorflow- bidirectional_dynamic_rnn: Attempt to reuse RNNCell

The following code (taken from - https://github.com/dennybritz/tf-rnn/blob/master/bidirectional_rnn.ipynb)
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
# Create input data
X = np.random.randn(2, 10, 8)
# The second example is of length 6
X[1,6:] = 0
X_lengths = [10, 6]
cell = tf.contrib.rnn.LSTMCell(num_units=64, state_is_tuple=True)
outputs, states = tf.nn.bidirectional_dynamic_rnn(
cell_fw=cell,
cell_bw=cell,
dtype=tf.float64,
sequence_length=X_lengths,
inputs=X)
output_fw, output_bw = outputs
states_fw, states_bw = states
is giving the following error for
tensorflow - 1.1 for both 2.7 and 3.5
ValueError: Attempt to reuse RNNCell <tensorflow.contrib.rnn.python.ops.core_rnn_cell_impl.LSTMCell object at 0x10ce0c2b0>
with a different variable scope than its first use. First use of cell was with scope
'bidirectional_rnn/fw/lstm_cell', this attempt is with scope 'bidirectional_rnn/bw/lstm_cell'.
Please create a new instance of the cell if you would like it to use a different set of weights.
If before you were using: MultiRNNCell([LSTMCell(...)] * num_layers), change to:
MultiRNNCell([LSTMCell(...) for _ in range(num_layers)]). If before you were using the same cell
instance as both the forward and reverse cell of a bidirectional RNN, simply create two instances
(one for forward, one for reverse). In May 2017, we will start transitioning this cell's behavior to use
existing stored weights, if any, when it is called with scope=None (which can lead to silent model degradation,
so this error will remain until then.)
But it is working in
tensorflow - 1.0.1 for python 3.5 (did not test on python - 2.7)
I tried with multiple code examples I found online but
tf.nn.bidirectional_dynamic_rnn
is giving the same error with tensorflow - 1.1
Is there a bug in tensorflow 1.1 or am i just missing something?
Sorry you ran into this. I can confirm that the error appears in 1.1 (docker run -it gcr.io/tensorflow/tensorflow:1.1.0 python) but not in 1.2 RC0 (docker run -it gcr.io/tensorflow/tensorflow:1.2.0-rc0 python).
So it looks like either 1.2-rc0 or 1.0.1 are your options for the moment.

Error using matplotlib after updating iPython [duplicate]

This question already has answers here:
IPython Notebook locale error [duplicate]
(4 answers)
Closed 6 years ago.
After updating IPython I constantly have problems with matplotlib. At the beginning of my notebook I have
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import scipy
from qutip import *
import time
Which generate a screen full of issues but the final part is
/Users/murray/anaconda/lib/python2.7/locale.pyc in _parse_localename(localename)
473 elif code == 'C':
474 return None, None
--> 475 raise ValueError, 'unknown locale: %s' % localename
476
477 def _build_localename(localetuple):
ValueError: unknown locale: UTF-8
There were other issues before this which I managed to fix. Similar things have been reported here but no solution which works for me. One solution I found online suggested running
export LANG="it_IT.UTF-8"
in the terminal window (plus about 8 other similar commands). This worked but everytime I restart the notebook I have to reenter all of this. As you might guess I am not an expert - I would assume there is a more permanent fix for this problem
As a work around, you can put export LANG="it_IT.UTF-8" and the "8 other similar commands" into your .profile (assuming your are on Mac OS X).
At the end of this file /Users/murray/.profile write:
# Fix for matplotlib imports in IPython
export LANG="it_IT.UTF-8"
# your other 8 lines here without the # in front
You need to start a new terminal window. In there, start a new IPython session.

Python multiprocessing stops

I am a novice user of a cluster running in RedHat Enterprise Linux. I run python script (version 2.6.5) by using bsub command. Somehow this python program just stops during the multiprocessing. The program goes like:
from multiprocessing import Pool
import multiprocessing
def pop_genomics(chrom):
os.system('run analysis on DNA')
os.system('run analysis on DNA')
os.system('run analysis on DNA')
os.system('run analysis on DNA')
print 'Finished!'
return 'Done'
pool = multiprocessing.Pool(multiprocessing.cpu_count())
finalfiledirs=pool.map(pop_genomics, chroms)
pool.close()
pool.join()
I get 'Finished!' message from all workers, but this program does not proceed beyond that 'finalfiledirs=pool.map(pop_genomics, chroms)' line. Can you suggest why this is happening?
You should be getting an error on that line because on the
pool.map(pop_genomics,chroms)
you never pass any parameters to pop_genomics, so you need to add some so it would be:
pool.map(pop_genomics(parameters),chroms)

Loading an image using Pyglet

I am playing around with pyglet 1.2alpha-1 and Python 3.3. I have the following (extremely simple) application and cannot figure out what my issue is:
import pyglet
window = pyglet.window.Window()
#image = pyglet.resource.image('img1.jpg')
image = pyglet.image.load('img1.jpg')
label = pyglet.text.Label('Hello, World!!',
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2,
anchor_x='center', anchor_y='center')
#window.event
def on_draw():
window.clear()
label.draw()
# image.blit(0,0)
pyglet.app.run()
With the above code, my text label will appear as long as image.blit(0, 0) is commented out. However, if I try to display the image, the program crashes with the following error:
File "C:\Python33\lib\site-packages\pyglet\gl\lib.py", line 105, in errcheck
raise GLException(msg)
pyglet.gl.lib.GLException: b'invalid value'
I also get the above error if I try to use pyglet.resource.image instead of pyglet.image.load (the image and py file are in the same directory).
Any one know how I can fix this issue?
I am using Python 3.3, pyglet 1.2alpha-1, and Windows 8.
The code -including the image.blit- runs fine for me. I'm using python 2.7.3, pyglet 1.1.4
There's nothing wrong with the code. You might consider trying other python and pyglet versions for the time being (until pyglet has a new stable release)
This isn't a "fix", but might at least determine if it's fixable or not (mine was not). (From the Pyglet mailing group.)
You can verify whether the system does not even support Textures greater than 1024, by running this code (Python 3+):
from ctypes import c_long
from pyglet.gl import glGetIntegerv, GL_MAX_TEXTURE_SIZE
i = c_long()
glGetIntegerv(GL_MAX_TEXTURE_SIZE, i)
print (i) # output: c_long(1024) (or higher)
That is the maximum texture size your system supports. If it's 1024, then any larger pictures will raise an Exception. (And the only fix is, get a better system).

IPython Notebook Sympy Math Rendering

I have just started with using IPython Notebook and have been fascinated by its power. I have been using a few examples available on the net to get started with. I was following this tutorial: http://nbviewer.ipython.org/url/finiterank.com/cuadernos/suavesylocas.ipynb but the maths output is not getting rendered as expected. Below is the my code and the output:
In [30]:
%load_ext sympyprinting
%pylab inline
from __future__ import division
import sympy as sym
from sympy import *
init_printing()
x,y,z=symbols("x y z")
k,m,n=symbols("k m n", integer=True)
The sympyprinting extension is already loaded. To reload it, use:
%reload_ext sympyprinting
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.
In [31]:
t = sin(2*pi*x*(k**2))/ (4*(pi**2)*(k**5)) + (x**2) / (2*k)
t
Out[31]:
2 ⎛ 2 ⎞
x sin⎝2⋅π⋅k ⋅x⎠
─── + ─────────────
2⋅k 2 5
4⋅π ⋅k
I have tried other examples also, and they are also not getting rendered properly. Where am I going wrong?
I had the same problem. Try
from sympy.interactive import printing
printing.init_printing(use_latex=True)
instead of
%load_ext sympyprinting
I am using sympy 0.7.2
I recently had the same problem, and I'm using Linux Crunchbang, which is a derivative of Redhat I think. Originally I installed sympy using
pip install sympy
However, this led to the above problem as described. So then I went to the sympy webpage and cloned the git repository to a folder. Then it can be installed (once in the local folder) by using
python setup.py install
After that everything worked fine, so I think it had something to do with the version used. For the record, the commands I used to initialize the printing in python were
import sympy
sympy.init_printing()
Import:
from sympy import *
init_printing()
Example:
x = symbols('x')
a = Integral(cos(x)*exp(x), x)
Eq(a, a.doit())
Output: