a=[1,2,3];
context_var = autograd.Variable(torch.LongTensor(a))
This is giving an error
RuntimeError: tried to construct a tensor from a int sequence, but found an item of type numpy.int32 at index
I am not able to figure out how to get over this.
Your code works perfectly fine in the recent version of pytorch. But for older versions, you can convert the numpy array to list using .tolist() method as follows to get rid of the error.
a=[1,2,3];
context_var = autograd.Variable(torch.LongTensor(a.tolist()))
Works fine for me:
a=[1,2,3]
print(torch.autograd.Variable(torch.LongTensor(a)))
b = np.array(a)
print(torch.autograd.Variable(torch.LongTensor(b)))
outputs:
Variable containing:
1
2
3
[torch.LongTensor of size 3]
Variable containing:
1
2
3
[torch.LongTensor of size 3]
I'm using Python 3.6.2, torch 0.2.0.post3, and numpy 1.13.3.
Related
Code:
a=training_dataset.map(lambda x,y: (tf.pad(x,tf.constant([[13-int(tf.shape(x)[0]),0],[0,0]])),y))
gives the following error:
TypeError: in user code:
<ipython-input-32-b25101c2110a>:1 None *
a=training_dataset.map(lambda x,y: (tf.pad(tensor=x,paddings=tf.constant([[13-int(tf.shape(x)[0]),0],[0,0]]),mode="CONSTANT"),y))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:264 constant **
allow_broadcast=True)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:282 _constant_impl
allow_broadcast=allow_broadcast))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py:456 make_tensor_proto
_AssertCompatible(values, dtype)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py:333 _AssertCompatible
raise TypeError("Expected any non-tensor type, got a tensor instead.")
TypeError: Expected any non-tensor type, got a tensor instead.
However, when I use:
a=training_dataset.map(lambda x,y: (tf.pad(x,tf.constant([[1,0],[0,0]])),y))
Above code works fine.
This brings me to the conclusion that something is wrong with: 13-tf.shape(x)[0] but cannot understand what.
I tried converting the tf.shape(x)[0] to int(tf.shape(x)[0]) and still got the same error.
What I want the code to do:
I have a tf.data.Dataset object having variable length sequences of size (None,128) where the first dimension(None) is less than 13. I want to pad the sequences such that the size of every collection is 13 i.e (13,128).
Is there any alternate way (if the above problem cannot be solved)?
A solution that works:
using:
paddings = tf.concat(([[13-tf.shape(x)[0],0]], [[0,0]]), axis=0)
instead of using:
paddings = tf.constant([[13-tf.shape(x)[0],0],[0,0]])
works for me.
However, I still cannot figure out why the latter one did not work.
I have 3 arrays and one list:
array1.shape = (1000,5,5,7)
array2.shape = (1000,)
array3.shape = (1000,)
len(list1) = (1000)
I want to save all of these to a numpy file. When I used to run in Python 2.7, the command I used to use was:
np.save(filename,[array1, array2, array3, list1])
And everything worked great, including loading all of the data with np.load. However, when I try this command in Python 3.6 I get an error:
could not broadcast input array from shape (1000,5,5,7) into shape (1000)
How am I able to save the 3 arrays as well as the list into a single numpy array in Python 3.6?
My pytorch code below keeps getting jit tracer warning (in pytorch 1.1.0 environment) complaining that "Pytorch 1.0 Tracer Warning: Converting a tensor to a Python index might ..."
Is there a way to implement the code line marked (A) below without using python indexing?
N,C,H,W = input.size()
Cout=4*C
Hout=H//2
Wout=W//2
downsampled=torch.zeros([N,Cout,Hout,Wout], dtype= torch.FloatTensor)
downsampled[:,1:Cout:4,:,:]=input[:,:,0::2,1::2] ---- (A)
I confirmed that jit tracer no longer complains the python indexing in Pytorch 1.2 (as Umang Gupta commented).
BTW, I came up with an implementation with no slicing (but still using indexing) as follow:
import torch
input=torch.arange(100)
input=input.view(10,10)
input=input[None, None, ...].expand(2,3,10,10) #torch.Size([2,3,10,10])
N,C,H,W=input.size()
Cout=4*C
Hout=H//2
Wout=W//2
downsampled=torch.zeros([N,Cout,Hout,Wout],dtype=torch.int8) #torch.Size([2,12,5,5])
dim2_idx=torch.tensor([k for k in range(0,H,2)])
dim3_idx=torch.tensor([k for k in range(1,W,2)])
sliced_input=input.index_select(2,dim2_idx).index_select(3,dim3_idx) #torch.Size([2,3,5,5])
#downsampled.index_select(1,torch.tensor([k for k in range(1,Cout,4)]))=temp <---Error: Can't assign to function call
for idx in range(1,Cout,4):
downsampled[:,idx,:,:]=sliced_input[:,idx//4,:,:]
I'd like to build a tensorflow graph in a separate function get_graph(), and to print out a simple ops a in the main function. It turns out that I can print out the value of a if I return a from get_graph(). However, if I use get_operation_by_name() to retrieve a, it print out None. I wonder what I did wrong here? Any suggestion to fix it? Thank you!
import tensorflow as tf
def get_graph():
graph = tf.Graph()
with graph.as_default():
a = tf.constant(5.0, name='a')
return graph, a
if __name__ == '__main__':
graph, a = get_graph()
with tf.Session(graph=graph) as sess:
print(sess.run(a))
a = sess.graph.get_operation_by_name('a')
print(sess.run(a))
it prints out
5.0
None
p.s. I'm using python 3.4 and tensorflow 1.2.
Naming conventions in tensorflow are subtle and a bit offsetting at first.
The thing is, when you write
a = tf.constant(5.0, name='a')
a is not the constant op, but its output. Names of op outputs derive from the op name by adding a number corresponding to its rank. Here, constant has only one output, so its name is
print(a.name)
# `a:0`
When you run sess.graph.get_operation_by_name('a') you do get the constant op. But what you actually wanted is to get 'a:0', the tensor that is the output of this operation, and whose evaluation returns an array.
a = sess.graph.get_tensor_by_name('a:0')
print(sess.run(a))
# 5
I am following this tutorial: graph tutorial on my own data file. but I am getting stuck on the last bit. the code seems to throw:
(most recent call last)<ipython-input-193-7227d35394c0> in <module>()
1 for node in links: #loops through each link and changes each dictionary to a tuple so networkx can read in the information
2 edges = node.items()
----> 3 G.add_edge(*edges[0]) #takes the tuple from the list and unpacks the tuples
4
5 nx.draw(G)
TypeError: 'dict_items' object does not support indexing
is there a fix? I'm sort of stuck.
This is probably python-2 only compatible code.
In python 2, dict.items() returns a list of tuples (key, value) as opposed to dict.iteritems()
In python 3, dict.iteritems() has been removed, and dict.items() returns an iterator.
So you have to explicitly convert it to list to get access to the elements by index:
edges = list(node.items())