How to concatenate like List comprehension - pytorch

I want to do without define input_list and output_list befor for like "List comprehension"
Please tell me how to do in this case?
Thank you.
e.g.
inputs = torch.rand(1,5)
weights = torch.rand(5,5)
def training(self,inputs,weights,steps=50):
input_list=torch.empty(1,5)
output_list=torch.empty(5,5)
for i in range(steps):
outputs = torch.matmul(inputs,weights)
input_list = torch.cat([input_list,inputs], axis=0)
output_list = torch.cat([output_list,outputs], axis=0)
inputs = outputs
return inputs,input_list,output_list
them
input_list is
tensor([[1.8367e-36, 0.0000e+00, 3.3631e-44, 0.0000e+00, nan],
[3.9109e-01, 8.9431e-01, 6.8890e-01, 8.3894e-01, 1.7802e-01]])
I dont need the first Row. How to concatenate in this case?

This problem was solved by professional programer.
like this;
input_list=torch.tensor([])
output_list=torch.tensor([])
Reference;
https://teratail.com/questions/298576

Related

python count ocurrences in a tuple of tuples

I have a tuple with tuples inside like this:
tup = ((1,2,3,'Joe'),(3,4,5,'Kevin'),(6,7,8,'Joe'),(10,11,12,'Donald'))
This goes on and on and the numbers don't matter here. The only data that matters are the names. What I need is to count how many times a given name occurs in the tuple and return a list where each item is a list and the number of times it occurs, like this:
list_that_i_want = [['Joe',2],['Kevin',1],['Donald',1]]
I don't want to use any modules or collections like Counter. I want to hard code this.
I actually wanted to hardcode the full solution and not even use the '.count()' method.
So far what I got is this:
def create_list(tuples):
new_list= list()
cont = 0
for tup in tuples:
for name in tup:
name = tup[3]
cont = tup.count(name)
if name not in new_list:
new_list.append(name)
new_list.append(cont)
return new_list
list_that_i_want = create_list(tup)
print(list_that_i_want)
And the output that I am been given is:
['Joe',1,'Kevin',1,'Donald',1]
Any help? Python newbie here.
You could. create a dictionary first and find the counts. Then convert the dictionary to a list of list.
tup = ((1,2,3,'Joe'),(3,4,5,'Kevin'),(6,7,8,'Joe'),(10,11,12,'Donald'))
dx = {}
for _,_,_,nm in tup:
if nm in dx: dx[nm] +=1
else: dx[nm] = 1
list_i_want = [[k,v] for k,v in dx.items()]
print (list_i_want)
You can replace the for_loop and the if statement section to this one line:
for _,_,_,nm in tup: dx[nm] = dx.get(nm, 0) + 1
The output will be
[['Joe', 2], ['Kevin', 1], ['Donald', 1]]
The updated code will be:
tup = ((1,2,3,'Joe'),(3,4,5,'Kevin'),(6,7,8,'Joe'),(10,11,12,'Donald'))
dx = {}
for _,_,_,nm in tup: dx[nm] = dx.get(nm, 0) + 1
list_i_want = [[k,v] for k,v in dx.items()]
print (list_i_want)
Output:
[['Joe', 2], ['Kevin', 1], ['Donald', 1]]
Using an intermediary dict:
def create_list(tuple_of_tuples):
results = {}
for tup in tuple_of_tuples:
name = tup[3]
if name not in results:
results[name] = 0
results[name] += 1
return list(results.items())
Of course, using defaultdict, or even Counter, would be the more Pythonic solution.
You can try with this approach:
tuples = ((1,2,3,'Joe'),(3,4,5,'Kevin'),(6,7,8,'Joe'),(10,11,12,'Donald'))
results = {}
for tup in tuples:
if tup[-1] not in results:
results[tup[-1]] = 1
else:
results[tup[-1]] += 1
new_list = [[key,val] for key,val in results.items()]
Here, a no-counter solution:
results = {}
for t in tup:
results[t[-1]] = results[t[-1]]+1 if (t[-1] in results) else 1
results.items()
#dict_items([('Joe', 2), ('Kevin', 1), ('Donald', 1)])

What is the difference between list1 and list1[:] in Python?

I was trying to implement a backtracking solution to a Leetcode problem (https://leetcode.com/problems/subsets/) and found out an unexpected bug in my code. In the first solution I do out_list.append(curr_array) in line 8 and it outputs me an empty output list.
class Solution:
def subsets(self, nums):
def backtrack(curr_array, curr_idx):
if len(curr_array) > 0:
out_list.append(curr_array)
for idx in range(curr_idx, len(nums)):
curr_array.append(nums[idx])
backtrack(curr_array, idx + 1)
curr_array.pop()
out_list = []
backtrack([], 0)
return out_list
Whereas when I do out_list.append(curr_array[:]), I get the correct answer as output.
class Solution:
def subsets(self, nums):
def backtrack(curr_array, curr_idx):
if len(curr_array) > 0:
out_list.append(curr_array[:])
for idx in range(curr_idx, len(nums)):
curr_array.append(nums[idx])
backtrack(curr_array, idx + 1)
curr_array.pop()
out_list = []
backtrack([], 0)
return out_list
I've been under the impression that list1[:] is the same thing as list1. Can you tell me what am I missing here?
cur_array is a reference to the original list. When you append cur_array to out_list and then later modify cur_array, out_list changes as well.
cur_array[:] is a copy of cur_array (same as cur_array.copy()). When you append cur_array[:] to out_list and then later modify cur_array, out_list does not change because it has its own copy of cur_array.
It looks like you make a misunderstand on list.
>>> a = [] # Here we announce a list
>>> id(a)
82700416
>>> id(a[:])
82700544
As you can see, slice will make a copy of list.

How to create a DataFrame with the word2ve vectors as data, and the terms as row labels?

I tried to follow this documentation:
nbviewer.jupyter.org/github/skipgram/modern-nlp-in-python/blob/master/executable/Modern_NLP_in_Python.ipynb
Where I have the following code snippet:
ordered_vocab = [(term, voc.index, voc.count)
for term, voc in food2vec.vocab.iteritems()]
ordered_vocab = sorted(ordered_vocab, key=lambda (term, index, count): -count)
ordered_terms, term_indices, term_counts = zip(*ordered_vocab)
word_vectors = pd.DataFrame(food2vec.syn0norm[term_indices, :],
index=ordered_terms
To get it to run i have change it to following:
ordered_vocab = [(term, voc.index, voc.count)
for term, voc in word2vecda.wv.vocab.items()]
ordered_vocab = sorted(ordered_vocab)
ordered_terms, term_indices, term_counts = zip(*ordered_vocab)
word_vectorsda = pd.DataFrame(word2vecda.wv.syn0norm[term_indices,],index=ordered_terms)
word_vectorsda [:20]
But the last line before I print the DataFrame give me an error I cannot get my head around. It keeps return that the noneType object cannot be in this line. To me, it looks like it is Term_indices there tracking it, but I do not get why?
TypeError: 'NoneType' object is not subscriptable
Can any help me with this? Any inputs are most welcome
Best Niels
Use the following code:
ordered_vocab = [(term, voc.index, voc.count) for term, voc in model.wv.vocab.items()]
ordered_vocab = sorted(ordered_vocab, key=lambda k: k[2])
ordered_terms, term_indices, term_counts = zip(*ordered_vocab)
word_vectors = pd.DataFrame(model.wv.syn0[term_indices, :], index=ordered_terms)
Replace model with food2vec.
Working on python 3.6.1, gensim '3.0.0'

How to print the values of tensors inside a while loop?

I'm very new to tensorflow, and I couldn't figure this one out.
I have this while loop:
def process_tree_tf(n_child, reprs, weights, bias, embed_dim, activation = tf.nn.relu):
n_child, reprs = n_child, reprs
parent_idxs = generate_parents_numpy(n_child)
loop_idx = reprs.shape[0] - 1
loop_vars = loop_idx, reprs, parent_idxs, weights, embed_dim
def loop_condition(loop_ind, *_):
return tf.greater(0, loop_idx)
def loop_body(loop_ind, reprs, parent_idxs, weights, embed_dim):
x = reprs[loop_ind]
x_expanded = tf.expand_dims(x, axis=-1)
w = weights
out = tf.squeeze(tf.add(tf.matmul(x_expanded,w,transpose_a=True), bias))
activated = activation(out)
par_idx = parent_idxs[loop_ind]
reprs = update_parent(reprs, par_idx, embed_dim, activated)
reprs = tf.Print(reprs, [reprs]) #This doesn't work
loop_ind = loop_ind-1
return loop_ind, reprs, parent_idxs, weights, embed_dim
return tf.while_loop(loop_condition, loop_body, loop_vars)
And I'm evaluating it this way:
embed_dim = 2
hidden_dim = 2
n_nodes = 4
batch = 2
reprs = np.ones((n_nodes, embed_dim+hidden_dim))
n_child = np.array([1, 1, 1, 0])
weights = np.ones((embed_dim+hidden_dim, hidden_dim))
bias = np.ones(hidden_dim)
with tf.Session() as sess:
_, r, *_ = process_tree_tf(n_child, reprs, weights, bias, embed_dim, activation=tf.nn.relu)
print(r.eval())
I want to check the value of reprs inside the while loop, but tf.Print doesn't seem to work, and print just tells me it's a tensor and gives me its shape.
How do I go about doing this?
Thank you so much!
Take a look at this webpage: https://www.tensorflow.org/api_docs/python/tf/Print
You can see that tf.Print is an identity operator with the side effect of printing data when evaluating. You should therefore use this line to print:
reprs = tf.Print(reprs, [reprs])
Hope this helps, and good luck!
The approach suggested by rmeertens is the one I think is correct. I would just add (as a response to your comments) that if something is printing "Tensor("while/update_parent:0, ...... " then that implies that that value in the graph is not being evaluated.
You are likely seeing that as the output of your "print(r.eval())" statement, NOT the tf.Print() statement.
Note that the output of tf.Print() appears in PyCharm (the IDE I am using) in red, while the output of a normal python print operation appears in black. So the tf.Print() output looks like a warning message. It could be that it is indeed printing out, but you are simply overlooking it.

Translating dictionaries and a list of strings into variables to solve ODE

say I have a list of strings representing equations
['', '-protein_0*Kdeg_protein_0+mRNA_0*Ktrans_0',
'-mRNA_0*Kdeg_mRNA_0+gene_0*Kprod_0', '+mRNA_0*Kdeg_mRNA_0+protein_0*Kdeg_protein_0']
a dictionary with parameter values as floats
{'Kdeg_protein_0': 0.5865674906323503, 'Kdeg_mRNA_0': 0.873345564768431, 'Kprod_0': 13.403565061372824, 'Ktrans_0': 10.37622098808632}
and a dictionary of states
{'Bin': 'y[3]', 'gene_0': 'y[0]', 'mRNA_0': 'y[2]', 'protein_0': 'y[1]'}
I want to get it into the form such that it can be solved by scipy integrate.odeint something like
def ODEs(y,t,input_1,input_2,input_3):
Equations = input_1
Parameters = input_2
States = input_3
for key,value in Parameters.items():
exec(key + '=value')
for key,value in States.items():
exec(key + '=value')
for i in range(len(Equations)):
Equations[i] = eval(Equations[i])
return Equations
def main():
t = numpy.linspace(0,24,24*60)
y0 = [10,0,0,0]
y = integrate.odeint(ODEs,y0,t,(GG,PP,LL),)
print y
I havent been able to work it out, any suggestions to solve this or another approach, it is imperative that the initial data is in list or dictionary form containing strings
at the moment im getting this error:
bad operand type for unary +: 'str'
for the equations being evaluated
for some reason it is tricky to translate your strings to variables whilst trying to build a function for an ODE solver, can only think of replacing everything seperately with floats and matrix indices first and plugging that into a model building function that builds a model that can be solved by odeint
def Equation_Builder(input_1,input_2,input_3):
Equations = input_1
Parameters = input_2
States = input_3
for key,value in Parameters.items():
exec('{} = {}'.format(key,value))
print Parameters
for i in range(len(Equations)):
for key,value in States.items():
Equations[i] = Equations[i].replace(key,value)
for i in range(len(Equations)):
for key,value in Parameters.items():
Equations[i] = Equations[i].replace(key,value)
for i in range(len(Equations)):
if Equations[i] == '':
Equations[i] ='0'
return Equations
def Model_Builder(input_1):
Equations = input_1
def model(y,t):
dydt = numpy.zeros(len(Equations))
for i in range(len(Equations)):
dydt[i] = eval(''.join(Equations[i]))
return dydt
return model
def main():
t = numpy.linspace(0,24,24*60)
y0 = [10,0,0,0]
model = Model_Builder(Equations)
y = integrate.odeint(model,y0,t)
print y
EDIT: Made some wrong assumptions about the parameters; a regular expression is needed to guarantee that the state variable names are not contained inside the parameter names.
Your problem is that your equations as written contain undefined variables, you need to substitute the state variables with y[0], y[1] etc. I assume that the equations will always contain valid python syntax?
Your solution should work if you add
import re
to the top of the file, and replace
for key,value in Parameters.items():
exec(key + '=value')
for key,value in States.items():
exec(key + '=value')
with
for key,value in Parameters.items():
exec('{} = {}'.format(key,value))
for key,value in States.items():
for i in range(len(Equations)):
Equations[i] = re.sub(r"\b%s\b" % key, value, Equations[i])
and sort out the empty string case as you did in your answer. The regex makes sure that a whole word is matched, rather than a substring.

Resources