I have numpy code for a project and wanted to convert it to tensorflow.
I have a 2D Tensor like x => [[0,1],[1,2],[2,3]] etc. and I want to slice a 3D tensor y using this. e.g. y[x[:,0], x[:,1], :] but it doesn't work. Following is error:
ValueError: Shape must be rank - but is rank - for 'strided_slice_?' (op: 'StridedSlice') with input shapes: [-], [-], [-], [-].
Can anyone please help!
Thanks
You need scalars to index into y, not tensors of rank 1+.
Try y[x[0, 0], x[0, 1], :] for a quick test.
Related
Suppose I have two images with dimensions of 32x32x3 (number of channels=3). I want to multiply them (like "matmul" function) on the first and the second dimensions for each of these 3 channels in Tensorflow to get a new 32x32x3 image.
Can someone help me with this?
Something like this loop:
#x.shape=(32,32,3)
#y.shape=(32,32,3)
a = np.zeros((x.shape[-3], x.shape[-2], x.shape[-1],), dtype='float32')
for i in range(a.shape[-1]):
a[:, :, i] = tf.matmul(x[:, :, i], y[:, :, i])
a = tf.convert_to_tensor(a, dtype=tf.float32)
but I was wondering there is a more efficient way to do this?
Actually, I found the answer.
The matmul works also for 3d arrays. However, the features (channels) need to be first in matmul function. So we need to use tf.transpose if channels are placed in the last dimension as follow:
x=tf.transpose(x, perm=[2, 0, 1])
y=tf.transpose(y, perm=[2, 0, 1])
a=tf.matmul(x,y)
a=tf.transpose(a, perm=[1, 2, 0])
It gives the same result as the loop I wrote above.
I would like to replace values under condition.
NumPy version would go like this
intensity=np.where(
np.abs(intensity)<1e-4,
1e-4,
intensity)
But TensorFlow has a bit different usage for tf.where()
When I tried this
intensity=tf.where(
tf.math.abs(intensity)<1e-4,
1e-4,
intensity)
I got this error
ValueError: Shapes must be equal rank, but are 0 and 4 for 'Select' (op: 'Select') with input shapes: [?,512,512,1], [], [?,512,512,1].
Does that mean I should 4 dimensional tensor for 1e-4?
Following code passed the error
# Create an array which has small value (1e-4),
# whose shape is (2,512,512,1)
small_val=np.full((2,512,512,1),1e-4).astype("float32")
# Convert numpy array to tf.constant
small_val=tf.constant(small_val)
# Use tf.where()
intensity=tf.where(
tf.math.abs(intensity)<1e-4,
small_val,
intensity)
# Error doesn't occur
print(intensity.shape)
# (2, 512, 512, 1)
I want to know why this error occured.
The input is image files (24*375*3(width, height, channels)images, *.png) and output is labeled file(.csv) which has Boolean (0 or 1) label.
Here is my github
https://github.com/dldudwo0805/DeepLearningPractice
Plz. Give me advice.
The error code is -
The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.
y_data = tf.reshape(y_data, [50, 1])
y_data is a tensor. Try np.reshape rather than tf.reshape
Given a 3D source tensor t (batch_size, height, width) and another 2D tensor l indicating the length. I want to do something like:
rslt = tf.dynamic_slice(t, l)
such that rslt[0] = t[:l[0][0], :l[0][1]], rslt[1] = t[:l[1][0], :l[1][1]] and so on. But as I know, tf.slice can not do this. Could you tell me how can I do the dynamic slicing in tensorflow? Thanks a lot!
I am trying something similar to code below
datax=theano.shared(value=rng.rand(5,500,45))
x=T.dmatrix('x')
i=T.lscalar('i')
W=theano.shared(value=rng.rand(90,45,500))
Hb=theano.shared(value=np.zeros(90))
w_v_bias=T.dot(W,x).sum(axis=2).sum(axis=1)+Hb
z=theano.function([i],w_v_bias,givens={x:datax[i*5:(i+1)*5]})
z(0)
Theano is giving me a TypeError with msg:
Cannot convert Type TensorType(float64, 3D) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float64, matrix). You can try to manually convert Subtensor{int64:int64:}.0 into a TensorType(float64, matrix)
What I am doing wrong here?
Edit
As mentioned by daniel changing x to dtensor3 will result in another error.
ValueError: Input dimension mis-match. (input[0].shape[1] = 5, input[1].shape[1] = 90)
Apply node that caused the error: Elemwise{add,no_inplace}(Sum{axis=[1], acc_dtype=float64}.0, DimShuffle{x,0}.0)
Another way is to modify my train function but then I won't be able to do batch learning.
z=theano.function([x],w_v_bias)
z(datax[0])
I am trying to implement RBM with integer values for visible units.
The problem is that datax is a 3D tensor and datax[index*5:(index+1)*5] is also a 3D tensor but you're trying to assign that to x which is a 2D tensor (i.e. a matrix).
Changing
x = T.dmatrix('x')
to
x = T.dtensor3('x')
solves this problem but creates a new one because the dimensions of W and x don't match up to perform the dot product. It's unclear what the desired outcome is.
Solved it after few hit and trials.
What I needed was to change
x=T.dmatrix('x')
w_v_bias=T.dot(W,x).sum(axis=2).sum(axis=1)+Hb
to
x=T.dtensor3('x')
w_v_bias=T.dot(x,W).sum(axis=3).sum(axis=1)+Hb
Now it produces (5,90) array after adding Hb elementwise to each of the five vectors of dot product.