I have the following piece of code:
import theano
import theano.tensor as T
import numpy as np
x = theano.shared(np.asarray([1, 2, 3], dtype=theano.config.floatX), borrow=True)
y = T.cast(x, 'int32')
print 'type of y: ', type(y)
print 'type of y.owner.inputs[0]: ', type(y.owner.inputs[0])
print 'value of y: ', y.owner.inputs[0].get_value(borrow=True)
Run with CPU
$ THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python test_share.py
type of y: <class 'theano.tensor.var.TensorVariable'>
type of y.owner.inputs[0]: <class 'theano.tensor.sharedvar.TensorSharedVariable'>
value of y: [ 1. 2. 3.]
Run with GPU
$ THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python test_share.py
Using gpu device 0: GeForce 310M
type of y: <class 'theano.tensor.var.TensorVariable'>
type of y.owner.inputs[0]: <class 'theano.tensor.var.TensorVariable'>
value of y:
Traceback (most recent call last):
File "test_share.py", line 10, in <module>
print 'value of y: ', y.owner.inputs[0].get_value(borrow=True)
AttributeError: 'TensorVariable' object has no attribute 'get_value'
How can I get the same results as CPU?
The method you are using to access child nodes in the computation graph, .owner.inputs[0], is not appropriate for cross-platform code in general.
You call the shared variable x so the correct way to access x's value is to use x.get_value().
This code should run the same on CPU and GPU:
import theano
import theano.tensor as T
import numpy as np
x = theano.shared(np.asarray([1, 2, 3], dtype=theano.config.floatX), borrow=True)
y = T.cast(x, 'int32')
print 'type of y: ', type(y)
print 'type of x: ', type(x)
print 'value of x: ', x.get_value(borrow=True)
If you want to see the result of applying the symbolic cast operation to x, the symbolic result of which you call y, then you could do this:
print 'value of y: ', y.eval()
Related
I have been doing some calculations using numpy arrays and have arrived at the question of what is the difference between:
<class 'numpy.ndarray'> and numpy.ndarray
I have noticed that the following operation only works on <class 'numpy.ndarray'>:
(arrray == 1).sum()
Why is that so?
There's no difference; they're identical.
numpy.ndarray is the actual type of numpy arrays; <class 'numpy.ndarray'> is the string represention ot numpy.ndarray:
>>> import numpy as np
>>> a = np.array([1, 2, 3])
array([1, 2, 3])
>>> print(type(a) == np.ndarray)
True
>>> np.ndarray
<class 'numpy.ndarray'>
>>> print(type(a))
<class 'numpy.ndarray'>
>>> str(type(a))
"<class 'numpy.ndarray'>"
>>> repr(type(a))
"<class 'numpy.ndarray'>"
Python interpreters such as IPython and Jupyter (which underneath are actually the same thing) will trim of the <class '...' > part and only show the type itself when you enter the type the into interpreter, e.g. ipython:
$ ipython
Python 3.9.9 (main, Nov 21 2021, 03:23:44)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.1.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import numpy as np
In [2]: np.ndarray
Out[2]: numpy.ndarray
...versus python (the builtin interpreter):
$ python3
Python 3.9.9 (main, Nov 21 2021, 03:23:44)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.ndarray
<class 'numpy.ndarray'>
...but they're the exact same type.
I wonder if you are confusing numpy arrays and python lists. I/we often talk about a numpy array, meaning actually an object of class/type np.ndarray.
In [144]: a = [1, 2, 3] # a list
In [145]: b = np.array(a) # an array
In [146]: type(a), type(b)
Out[146]: (list, numpy.ndarray)
Your expression works with the array, but not the list:
In [147]: (b == 1).sum()
Out[147]: 1
In [148]: (a == 1).sum()
Traceback (most recent call last):
Input In [148] in <module>
(a == 1).sum()
AttributeError: 'bool' object has no attribute 'sum'
In [149]: b == 1
Out[149]: array([ True, False, False])
In [150]: a == 1
Out[150]: False
Note that I created b with np.array(). There is a np.ndarray function, but we don't usually use it - it's a low level creator that most of us don't need. A useful starting page:
https://numpy.org/doc/1.22/user/basics.creation.html
I have such a problem, and it happened on the latest version of tensorflow. I hope somebody can give me some suggestions.
my code as below:
%tensorflow_version 2.x
import tensorflow as tf
import numpy as np
import h5py
import t3f
import matplotlib.pyplot as plt
filename = "./video.h5"
np.random.seed(0)
with h5py.File(filename, "r") as f:
print("Keys: %s" % f.keys())
a_group_key = list(f.keys())[0]
data = list(f[a_group_key])
data_np = np.array(data)
data_tensor = tf.convert_to_tensor(data_np)
shape = [107]+[60]+[80]+[3]
# A is large tt-ranks tensor
A = t3f.to_tt_tensor(data_tensor)
# Create an X variable.
init_X = t3f.random_tensor(shape, tt_rank=3)
X = t3f.get_variable('X', initializer=init_X)
def step():
gradF = X - A
riemannian_grad = t3f.riemannian.project(gradF, X)
alpha = 1.0
t3f.assign(X, t3f.round(X - alpha * riemannian_grad, max_tt_rank=2))
return 0.5 * t3f.frobenius_norm_squared(X - A)
log = []
for i in range(1000):
F = step()
if i % 10 == 0:
print(F)
log.append(F.numpy())
Exit:
ValueError Traceback (most recent call last)
1 log = []
2 for i in range(1000):
----> 3 F = step()
4 if i % 10 == 0:
5 print(F)
4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py
in assert_is_compatible_with(self, other) 1115 """ 1116
if not self.is_compatible_with(other):
-> 1117 raise ValueError("Shapes %s and %s are incompatible" % (self, other)) 1118 1119 def
most_specific_compatible_shape(self, other):
ValueError: Shapes (1, 107, 3) and (1, 107, 2) are incompatible
But the data_tensor's shape is [107]+[60]+[80]+[3], it's same with A or X, I'm confused.
I runned this code on google colab with tf2 and python3.6.
In this link you would recurrence my question link
I have solved this issue! It's caused by the rank be different between tensor.So,I modified one of tensor rank in order to match another.
I have two numerical arrays of shape (N, M). I'd like to compute a row-wise dot product. I.e. produce an array of shape (N,) such that the nth row is the dot product of the nth row from each array.
I'm aware of numpy's inner1d method. What would the best way be to do this with jax? jax has jax.numpy.inner, but this does something else.
You can define your own jit-compiled version of inner1d in a few lines of jax code:
import jax
#jax.jit
def inner1d(X, Y):
return (X * Y).sum(-1)
Testing it out:
import jax.numpy as jnp
import numpy as np
from numpy.core import umath_tests
X = np.random.rand(5, 10)
Y = np.random.rand(5, 10)
print(umath_tests.inner1d(X, Y))
print(inner1d(jnp.array(X), jnp.array(Y)))
# [2.23219571 2.1013316 2.70353783 2.14094973 2.62582531]
# [2.2321959 2.1013315 2.703538 2.1409497 2.6258256]
You can try jax.numpy.einsum. Here the implementaion using numpy einsum
import numpy as np
from numpy.core.umath_tests import inner1d
arr1 = np.random.randint(0,10,[5,5])
arr2 = np.random.randint(0,10,[5,5])
arr = np.inner1d(arr1,arr2)
arr
array([ 87, 200, 229, 81, 53])
np.einsum('...i,...i->...',arr1,arr2)
array([ 87, 200, 229, 81, 53])
In this following example where it is trying to curve fit a sigmoid function to data I don't understand what does * in *ppot in line 11 mean
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x, Beta_1, Beta_2):
y = 1 / (1 + np.exp(-Beta_1*(x-Beta_2)))
return y
popt, pcov = curve_fit(sigmoid, xdata, ydata)
x = np.linspace(1960, 2015, 55)
x = x/max(x)
plt.figure(figsize=(8,5))
y = sigmoid(x, *popt)
plt.plot(xdata, ydata, 'ro', label='data')
plt.plot(x,y, linewidth=3.0, label='fit')
plt.legend(loc='best')
plt.ylabel('GDP')
plt.xlabel('Year')
plt.show()
thank you in advance.
The curve_fit method returns popt as a list of values, in this case, a list of 2 values (optimal values for the parameters).
Adding the * before a list splits the list into its values each assigned to a parameter of the function.
Example
>>> # Sample list
>>> lst = [1, 2, 3]
>>> lst
[1, 2, 3]
>>> # Creating a function that requires 3 parameters
>>> def add(x, y, z):
... return x + y + z
...
>>> add(*lst)
6
>>> add(lst)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: add() missing 2 required positional arguments: 'y' and 'z'
I am trying to replace a for loop with map_fn, since the latter seems to help improving the loop efficiency.
The question is that, if the fn in map_fn calls get_variable() to create a new variable, how can I set reuse to True for the rest of the loop? Or is the get_variable() only called once in map_fn?
def fn(x):
y = tf.get_variable('y', [])
return x * x
squares = tf.map_fn(fn, np.array([1, 2, 3, 4 ,5 ,6]))
# Out: [array([ 1, 4, 9, 16, 25, 36])]
sess.run([squares])
In [2]: def fn(x):
y = tf.get_variable('y', [])
print(y.name)
return x * x
In [4]: import numpy as np
In [5]: squares = tf.map_fn(fn, np.array([1, 2, 3, 4 ,5 ,6]))
y:0
As we can see that if a print is inserted in the fn and when it is called it prints only once.