Tensor variable indexing - python

I'm trying to make some symbolic calculations using indexing of symbolic variable.
X = T.matrix('X')
y = T.matrix('y')
z = T.dot(T.dot(X,y[0]),y[1]).norm(L=2)
callable_function = theano.function([y,X], z)
callable_function(np.array([np.array([[3],[5]]),np.array([[4,1]])]),np.array([1,2]))
And I'm getting
AttributeError: ('Bad input argument to theano function with name "C:/Users/LIKAN/PycharmProjects/deepEEG/test.py:17" at index 0(0-based)', "'float' object has no attribute 'dtype'")
How to use symbolic variable indexing correctly?

You declare both y and X as matrices but your inputs to the compiled Theano function are an object array and a vector.
np.array([np.array([[3],[5]]),np.array([[4,1]])]) is an object array because it is constructed as an array of numpy arrays. Note that np.array([np.array([[3],[5]]),np.array([[4,1]])]).dtype == object.
To create a matrix, just use a multi-dimensional array in the numpy array construction. You don't even need to create numpy arrays, just pass vanilla Python lists. Since your second argument (for X) is a vector I've assumed the input value is correct and the symbolic variable declaration is incorrect. With these changes, the following code runs:
import numpy as np
import theano
import theano.tensor as T
X = T.vector('X')
y = T.matrix('y')
z = T.dot(T.dot(X,y[0]),y[1]).norm(L=2)
callable_function = theano.function([y,X], z)
print callable_function([[3,5],[4,1]], [1,2])

Related

extract data from numpy array of shape ()

I have a numpy array of shape () which for all intents and purposes is a scalar. If it contains a floating point number, I can simply float(arr) to get a float back. (That isn't ideal because of the implicit conversion though.)
How to extract the data if it's a more complicated (object) data type? E.g.:
import numpy
import sympy
x = sympy.Symbol("x")
val = numpy.array(2 * x)
print(val, val.shape, val.dtype)
2*x () object
val.item() or val[()] should work.

Reading h5py files into tensors

So I have a training set and a test set both in h5py format. I also have a data_load function that loads the files and returns NumPy arrays. The main problem is I don't need NumPy as I am working with Tensors. I am expecting to have an x&y tensor of size N(batch size) and D_in(input size for each image) and D_out(Output size of each tensor).
The problem:
x&y do not get converted to tensors of dimensions mentioned below.If anything their types remain to be numpy.ndarray. Any help is appreciated.
def load_data(train_file, test_file):
# Load the training data
train_dataset =h5py.File(train_file, 'r')
# Separate features(x) and labels(y) for training set
train_set_x_orig =np.array(train_dataset["train_set_x"][:])
train_set_y_orig =np.array(train_dataset["train_set_y"][:])
# Load the test data
test_dataset =h5py.File(test_file,'r')
# Separate features(x) and labels(y) for training set
test_set_x_orig =np.array(test_dataset["test_set_x"][:])
test_set_y_orig =np.array(test_dataset["test_set_y"][:])
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
train_set_y_orig = torch.from_numpy(train_set_y_orig.reshape((1, train_set_y_orig.shape[0])))
test_set_y_orig = torch.from_numpy(test_set_y_orig.reshape((1, test_set_y_orig.shape[0])))
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
x = torch.Tensor(N, D_in)
y = torch.Tensor(N, D_out)
train_file="data/train_catvnoncat.h5"
test_file="data/test_catvnoncat.h5"
x,y,_,_,_=load_data(train_file,test_file)
Because you did not convert train_set_x_orig to a torch tensor before returning.
Either use torch.from_numpy() on train_set_x_orig before returning as you do with train_set_y_orig or cast it to a tensor before assigning to x.
However, y should be of type torch.tensor.
Below is a demonstration that explains the issue:
# some sample tensor
In [27]: x = torch.Tensor(3, 2)
# check its type
In [28]: type(x)
Out[28]: torch.Tensor
# some sample ndarray
In [29]: arrx = np.arange(6).reshape(3, -1)
# assign array to tensor
# note that now the object `x` refers to the numpy array object
In [30]: x = arrx
# see that the type() of `x` is now numpy ndarray
In [31]: type(x)
Out[31]: numpy.ndarray
Also, as hpaulj pointed out in the comments, there is no need to wrap the sliced objects from h5py in np.array() since the sliced objects are already of type numpy ndarrays. So, you can just get rid of them and the code will look more cleaner!

Newton method in python for multivariables (system of equations)

My code is running fine for first iteration but after that it outputs the following error:
ValueError: matrix must be 2-dimensional
To the best of my knowledge (which is not much in python), my code is correct. but I don't know, why it is not running correctly for all given iterations. Could anyone help me in this problem.
from __future__ import division
import numpy as np
import math
import matplotlib.pylab as plt
import sympy as sp
from numpy.linalg import inv
#initial guesses
x = -2
y = -2.5
i1 = 0
while i1<5:
F= np.matrix([[(x**2)+(x*y**3)-9],[(3*y*x**2)-(y**3)-4]])
theta = np.sum(F)
J = np.matrix([[(2*x)+y**3, 3*x*y**2],[6*x*y, (3*x**2)-(3*y**2)]])
Jinv = inv(J)
xn = np.array([[x],[y]])
xn_1 = xn - (Jinv*F)
x = xn_1[0]
y = xn_1[1]
#~ print theta
print xn
i1 = i1+1
I believe xn_1 is a 2D matrix. Try printing it you and you will see [[something], [something]]
Therefore to get the x and y, you need to use multidimensional indexing. Here is what I did
x = xn_1[0,0]
y = xn_1[1,0]
This works because within the 2D matrix xn_1 are two single element arrays. Therefore we need to further index 0 to get that single element.
Edit: To clarify, xn_1[1,0] means to index 1 and then take that subarray and index 0 on that. And although according to Scipy it may seem that it should be functionally equivalent to xn_1[1][0], that only applies to the general np.array type and not the np.matrix type. Here is an excellent thread on SO that explains this.
So you should use the xn_1[1,0] way to get the element you want.
xn_1 is a numpy matrix, so it's elements are accessed with the item() method, not like an array. (with []s)
So just change
x = xn_1[0]
y = xn_1[1]
to
x = xn_1.item(0)
y = xn_1.item(1)

How to pass float argument in predict function of scikit linear regression?

I am using scikit linear regression - single variable to predict y from x. The argument is in float datatype. How can i transform the float into numpy array to predict the output ?
import matplotlib.pyplot as plt
import pandas
import numpy as np
from sklearn import linear_model
import sys
colnames = ['charge_time', 'running_time']
data = pandas.read_csv('trainingdata.txt', names=colnames)
data = data[data.running_time < 8]
x = np.array(list(data.charge_time))
y = np.array(list(data.running_time))
clf = linear_model.LinearRegression() # Creating a Linear Regression Modal
clf.fit(x[:,np.newaxis], y) # Fitting x and y array as training set
data = float(sys.stdin.readline()) # Input is Float e.g. 4.8
print clf.predict(data[:,np.newaxis]) # As per my understanding parameter should be in 1-D array.
First of all, a suggestion not directly related to your question:
You don't need to do x = np.array(list(data.charge_time)), you can directly call x = np.array(data.charge_time) or, even better, x = data.charge_time.values which directly returns the underlying ndarray.
It is also not clear to me why you're adding a dimension to the input arrays using np.newaxis.
Regarding your question, predict expects an array-like parameters: that can be a list, a numpy array, or other.
So you should be able to just do data = np.array([float(sys.stdin.readline())]). Putting the float value in a list ([]) is needed because without it numpy would create a 0-d array (i.e. a single value, which is not sliceable) instead of a 1-d array.

Returning the index of a value in Theano vector

What is the procedure in Theano for returning the index of a particular value in a Vector? In NumPy, this would be numpy.where(my_array==x). Theano's Tensor.where is a switch statement.
There is 2 behavior of numpy.where(condition, [x ,y]). Theano always support you provide 3 parameter to where(). As said in NumPy doc[1], numpy.where(cond) is equivalent to nonzero().
You can do it like this in Theano:
import theano
import numpy as np
v = np.arange(10)
var = theano.tensor.vector()
out = theano.tensor.eq(var, 2).nonzero()[0]
print out.eval({var: v})
Check line 5. NumPy nonzero() return a tuple. Theano do the same. There is one vector in that tuple per dimensions in the input of nonzero().
[1] http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html

Categories