cuda in python issue [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have written the code below in order to discover the number of threads and blocks and send them to train_kernel function.
rows = df.shape[0]
thread_ct = (gpu.WARP_SIZE, gpu.WARP_SIZE)
block_ct = map(lambda x: int(math.ceil(float(x) / thread_ct[0])),[rows,ndims])
train_kernel[block_ct, thread_ct](Xg, yg, syn0g, syn1g, iterations)
but after execution, I face the error below:
griddim must be a sequence of integers

Although you have not stated it, you are clearly running this code in Python 3.
The semantics of map changed between Python 2 and Python 3. In Python 2 map returns a list. In Python 3 it returns an iterator. See here.
To fix this you need to do something like:
block_ct = list(map(lambda x: int(math.ceil(float(x) / thread_ct[0])),[rows,ndims]))
Alternatively you could just use a list comprehension without the lambda expression and map call:
block_ct = [ int(math.ceil(float(x) / thread_ct[0])) for x in [rows,ndims] ]
Either will yield a list with the necessary elements which should work in the CUDA kernel launch call.

Related

How can I write a function that adds the number of possitive number in a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I am trying to write a simple function that takes a list of number passed as an argument and prints how many positive number is in the list.
I can't seem to figure out what is wrong with the code here. Can someone please explain this.
You should return add instead of num. And you should initialize add outside the for loop.
lst = [1,2,3,4,-4,-3,-2,-1]
def count_positives(lst):
return sum(i > 0 for i in lst)
print(count_positives(lst))
the program above will print 4

What to use instead of * and / in Python to avoid ValueError? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm having a similar issue as to this post:
Why does this simple numpy multiply operation raise an "invalid number of arguments" error?
I have this incredibly confusing equation in my code:
(m[0]*np.power(q[0])*q[0]*r[0]*(m[0]*R)*np.power(q[0]))/(R*((m[0]*R)*np.power(q[0])+m*np.power(q[0]))**2) - a[0]*N/(b[0]+N)
and when I run it, it returns the error:
ValueError: invalid number of arguments
I assumed it could have something to do with the ** I was using to define the exponents (q[0] is an exponent), so I substituted those to np.power() to no success. Also, I changed the / that I am using to define the fractions to *(.....)**(-1), but that didn't work either. At this point, I'm assuming the issue is with the * I'm using for multiplications. But how can I write multiplications and divisions in this long expression without raising a position error?
Thank you in advance for the help!
You have to add another argument of what power you want.
For example, power means add 2
For example, cube means add 3
np.power(x1, 2)
np.power(x1, 3)
If you just want to squre with np.power(), then I have already edited the code for you:
(m[0]*np.power(q[0], 2)*q[0]*r[0]*(m[0]*R)*np.power(q[0], 2))/(R*((m[0]*R)*np.power(q[0], 2)+m*np.power(q[0], 2))**2) - a[0]*N/(b[0]+N)

Filtering even numbers in python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
just started with python and wanted to filter the even numbers from a numpy array:
>array = np.arange(2,10000)
>>print(array)
I know that the remainder of even no./2 should be 0, so part of the filtering condition should look somehow like this:
>if x%2 == 0
But no matter how, I always get an error of some kind. btw I'm using Python 3.
Thanks and Best
One Liner as pointed by Mikel:
print(np.arange(2,10000,2))
This creates an array starting from 2 ending at 10k with a step size of 2 i.e every second number.
Or if you want to use modulus you can try like this:
ar = np.arange(2,10000)
ar = ar[ar%2==0]
print(ar)
Output:
array([ 2, 4, 6, ..., 9994, 9996, 9998])
ar%2==0 creates a boolean mask to include only even numbers

How to write this for loop from C to Python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am new to Python, could someone give some pointers as to how this C code can be done in Python:
for(i=0, j=0; j<n; i++, j++){
A[i] = A2[j];
}
I gave this as a example. I am working on a web scraping project where I have to compare each word in a string given by the user to another string and should count proximity of each word and the strings I've to compare are in an array.
You are basically copying an array, equivalent to a python list. You could simply do:
A = list(A2)
In a for loop scenario (which isn't even needed due to the availability of the list call), you'd do:
for ind, val in enumerate(A2):
A[ind] = val
you really have many other options too, A2.copy(), A2[:], a list comprehension and in most recent python versions [*A2]. Python generally makes it very easy to do this.
Python supports iteration over collections/iterables (so range, for example) which are generally discrete. So, you can rewrite that as a while loop:
i = 0
j = 0
while j < n:
A[i] = A2[j]
i += 1
j += 1

Matching two comma seperated strings in Python and correct position counts [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
Hope get assistance on the below problem.
Python having more in-built sequence matcher functions. Whether the following requirement can be done through any built-in function without looping.
x = 'hu1_X','hu2_Y','hu3_H','hu4_H','hu5_H','hu7_H'
y = 'hu1_H','hu2_X','hu3_H','hu4_H','hu5_H','hu7_X'
for comparing the above string the final match count is 3.
Matches are: 'hu3_H','hu4_H','hu5_H'.
Any idea, which in-built function can use? Can we go with ZIP() in Python.
Thanks in advance.
You can use a generator expression and the builtin sum function along with zip, like this
x = 'hu1_X', 'hu2_Y', 'hu3_H', 'hu4_H', 'hu5_H', 'hu7_H'
y = 'hu1_H', 'hu2_X', 'hu3_H', 'hu4_H', 'hu5_H', 'hu7_X'
print(sum(item1 == item2 for item1, item2 in zip(x, y)))
# 3
This works because, in Python, True and False can be treated as 1 and 0 respectively. So, we can simply compare the corresponding elements and the result of that evaluation can be added together to get the total match count.

Categories