I am seriously stuck with something for ages now. I need some help.
I am running a theano conv network on GPU.
The network has a loss function as such
def mse(x, t):
return T.mean((x - t) ** 2)
Here x is the predicted value of a rectified liner unit and t is the expected value.
Now for a particular learning problem I am trying to modify the function such that I want to threshold the value of x. So essentially something simple as this
x[x>ts] = ts
But I am really struggling with this. I tried so many things
ts = 0.91
Y = T.vector()
#x_update = (x, T.set_subtensor(x[(x > ts).eval()], Y))
#f = function([Y], updates=[x_update])
#v=np.empty(len((x > ts).eval()))
#v.fill(ts)
#f(v)
#x.shape.eval()
x_arr = x.flatten()
print type(x_arr)
print type(t)
print type(x)
#print T.shape(x_arr).eval()
#print x.shape.eval()
#print x_arr.shape.eval()
#print t.shape.eval()
#print x.eval()
#print x_arr.get_value()
#x_newarr = x_arr.eval()
#x_newarr[x_newarr>ts] = ts
#x = T.shared(x_newarr)
return T.mean((x - t) ** 2)
Apart from the three prints, which all print <class 'theano.tensor.var.TensorVariable' > everything else gives me error.
So I am at my wits end how to do this simple stuff.
Is it because this stuff is on GPU ?
I did test the code on local python prompt, by constructing a numpy array and converting it into a tensor shared variable. The different stuff above works.
But I am conscious that the type is theano.tensor.sharedvar.TensorSharedVariable and not theano.tensor.var.TensorVariable.
I would really appreciate if some one gives me a helping hand here.
Regards
Please find the answer to this question given by pascal at
https://groups.google.com/forum/#!topic/theano-users/cNnvw2rUHc8
The failures are correct because the input values are not being provided at the time the function is being called, since it is symbolic.
The answer is to use T.minimum(x,threshold)
Related
I don't think I fully understand the use of intermediate variables in arrays and would love some help with my code.
Along with the error this equation is posted ((-1)*((((((0.95)*(i371)))*(9))-((int_v2)*(4))))), it looks like my objective function
yh = model.Array(model.Intermediate,(10),equation=None)
for i in range(10):
yh[i] = model.Intermediate(x[i]*f[i]*0.1) #x,f are variable arrays of size 10
y1 = model.Array(model.if3, (10), x1=1, x2=0, condition=sum(yh)-d) #d is a constant array of size 10
y2 = model.Array(model.if3, (10), x1=1, x2=0, condition=-1*(sum(yh)-lb)) #lb is a constant array of size 10
model.Equation(sum(x)==10)
model.options.IMODE = 3
model.options.SOLVER = 1
m2 = model.Array(model.Intermediate,(10,10),equation=None)
for i in range(10):
for j in range(10):
m2[i][j] = model.Intermediate(m[i][j]*x[i]*0.1*y1[j]*y2[j]) #m is a 10x10 constant array, i'm trying to multiply every element in a row
#with the corresponding x value, and every element in a column with the corresponding y value
r = model.Array(model.Intermediate,(10),equation=None)
for i in range(10):
r[i]= model.Intermediate(sum(m2[j][i] for j in range(10))) #im trying to get the sum of each column
model.Obj(-1*(0.95*r*c2-x*c1)) #c1,c2 are constant arrays; x is a variable array
model.solve()
Here is a complete script that demonstrates the two issues with your current program.
from gekko import GEKKO
model = GEKKO()
x = model.Array(model.Var,10)
yh = model.Array(model.Intermediate,10,equation=None)
for i in range(10):
yh[i] = model.Intermediate(x[i]**2)
model.Equation(sum(x)==10)
model.Obj(yh)
model.solve()
The first is that you are creating an array of Intermediate types and then creating them again in your loop. This gives the error:
#error: Model Expression
*** Error in syntax of function string: Invalid element: none
Position: 1
none
?
because the first Intermediates that you create have blank equations. You can avoid this error by just defining a list of None values.
yh = [None]*10
for i in range(10):
yh[i] = model.Intermediate(x[i]**2)
The second error is because you are using an array in the objective statement (as you already noted in your answer). This gives the error:
Warning: there is insufficient data in CSV file 136.36.211.159_gk_model0.csv
#error: Model Expression
*** Error in syntax of function string: Missing operator
Position: 2
0,0,0,0,0,0,0,0,0,0
?
As you correctly noted, you can add a summation to add the terms into a single term. You can also have multiple model.Obj() functions or model.Minimize() as a more descriptive version of the same function.
from gekko import GEKKO
model = GEKKO()
x = model.Array(model.Var,10)
yh = [None]*10
for i in range(10):
yh[i] = model.Intermediate(x[i]**2)
model.Equation(sum(x)==10)
model.Minimize(model.sum(yh))
model.solve()
```model.Obj(-1*(0.95*sum(r*c2)-sum(x*c1)))```
solved the issue as the objective function returns one value now not an array
I am trying to run a specific code to find the sum of all the possible combinations of the list that is coming from a .in file. The same code, when running with relatively small files, runs perfectly and with bigger files hangs and after a bit throws MEMORY ERROR
import itertools
file = open("c_medium.in","r")
if file.mode=='r':
content = file.readlines()
maxSlices,numberOfPizza = map(int,content[0].split())
numberOfSlices = tuple(map(int,content[1].split()))
print(maxSlices)
print(numberOfSlices)
sol = []
sumOfSlices = []
for x in range(1,len(numberOfSlices)+1):
print(x)
for y in itertools.combinations(numberOfSlices,x):
if sum(y) <= maxSlices:
sumOfSlices.append(sum(y))
sumOfSlices.sort()
print(sumOfSlices)
checkSum = sumOfSlices[len(sumOfSlices)-1]
print(checkSum)
found = False
if found == False:
for x in range(1,len(numberOfSlices)+1):
print(x)
for y in itertools.combinations(numberOfSlices,x):
if found == False:
if sum(y) == checkSum:
for z in y:
sol.append(numberOfSlices.index(z))
found = True
solution = tuple(map(str,sol))
print(solution)
The number of combinations of N elements grows very, very fast with N.
Regarding your code in particular, if (sum(y) <= maxSlices) is always true, then you'll generate a list with 2^(numberOfSlices) elements. i.e., you'll overflow a 32-bit integer if numberOfSlices=32.
I'd recommend trying to solve your task without explicitly building a list. If you describe what your code is doing, maybe someone can help.
I'm currently struggling immensely with my python module and I need to "define" a function that converts an integer to a string. I'm really confused about programming in general and was wondering if anyone could help provide an analysis and description on how I would solve this problem / where I'm going wrong with my approach. I know the code is simple but i'm lost on where to even start.
x=12
def Converter():
str(x)
print (x)
def Converter(x):
return str(x)
x = 12
result = Converter(x)
print(result)
Two things: 1. You should pass the parameter to function.
2. You should print the result.
x = 12
def Converter(int_number):
return str(int_number)
print(Converter(x))
Could you give me a hint where the time consuming part of this code is?
It's my temporary solutions for the kata Generate Numbers from Digits #2 from codewars.com.
Thanks!
from collections import Counter
from itertools import permutations
def proc_arrII(arr):
length = Counter(arr).most_common()[-1][1]
b = [''.join(x) for x in list(set(permutations(arr,length)))]
max_count = [max(Counter(x).values()) for x in b]
total = 0
total_rep = 0
maximum_pandigit = 0
for i in range(len(b)):
total+=1
if max_count[i] > 1:
total_rep+=1
elif int(b[i]) > maximum_pandigit:
maximum_pandigit = int(b[i])
if maximum_pandigit == 0:
return([total])
else:
return([total,total_rep,maximum_pandigit])
When posting this,
it would have been helpful to offer example input,
or link to the original question,
or include some python -m cProfile output.
Here is a minor item, it inflates the running time very very slightly.
In the expression [''.join(x) for x in list(set(permutations(arr, length)))]
there's no need to call list( ... ).
The join just needs an iterable, and a set works fine for that.
Here is a bigger item.
permutations already makes the promise that
"if the input elements are unique, there will be no repeat values in each permutation."
Seems like you want to dedup (with set( ... )) on the way in,
rather than on the way out,
for an algorithmic win -- reduced complexity.
The rest looks nice enough.
You might try benching without the elif clause,
using the expression max(map(int, b)) instead.
If there's any gain it would only be minor,
turning O(n) into O(n) with slightly smaller coefficient.
Similarly, you should just assign total = len(b) and be done with it,
no need to increment it that many times.
For educational purposes, I am trying to build an efficient algorithm to find the Least Common Multiple. I already have a quadratic and slow implementation for that. I am trying to build a new one. My new implementation uses a math property involving the Greatest Common Divisor (GCD) and the Least Common Multiple (LCM).
Basically: For any two positive integers a and b,
LCM(a, b) * GCD(a, b) = a * b
I am using Python 3 for that.
I have a very efficient implementation for GCD (it uses another math property, but it is pointless to talk about that):
def euclidean_gcd(a,b):
if b == 0:
return a
else:
a_prime = a%b
return euclidean_gcd(b,a_prime)
My implementation for LCM is:
def lcm_fast(a,b):
return (int((a*b)/(euclidean_gcd(a,b))))
However, when I call:
lcm_fast(1023473145,226553150)
I get as an output:
46374212988031352
The correct answer would be a close number:
46374212988031350
I am a beginner (second year on the Applied Math major), why is this happening?
I am not sure if I could grasp the concept of integer overflow, but, according to my understanding above a little research I did, there is no integer overflow in Python.
I did stress testing and tried to find this mistake in a easier to understand case. However, the problem seems to happen only with really big numbers. Bellow you can check my stress testing for that:
import random
#defina a fronteira máxima dos testes randômicos
print ("insira um número para ser o final do intervalo de testes aleatórios")
bound_right = int(input())
#versão lenta, ou naive
def check_elem_in_list(list_1,list_2):
for element in list_1:
if element in list_2:
return element
else:
return False
#nested loops, vai ter comportamento quadrático
def lcm_slow(num_1,num_2):
list_of_num_1_prod = []
list_of_num_2_prod = []
max_nums = max(num_1,num_2)
end_range = max_nums +1
for i in range(1, end_range):
list_of_num_1_prod.append(i*num_1)
list_of_num_2_prod.append(i*num_2)
if check_elem_in_list(list_of_num_1_prod,list_of_num_2_prod) != False:
return (check_elem_in_list(list_of_num_1_prod,list_of_num_2_prod))
def euclidean_gcd(a,b):
if b == 0:
return a
else:
a_prime = a%b
return euclidean_gcd(b,a_prime)
def lcm_fast(a,b):
return (int((a*b)/(euclidean_gcd(a,b))))
# está dando pau com os inputs 1023473145, 226553150
# vou fazer stress testing
#primeiro, fazer função para gerar testes
a_in = random.randint(1,bound_right)
b_in = random.randint(1,bound_right)
while (lcm_slow(a_in,b_in)==lcm_fast(a_in,b_in)):
a_in = random.randint(1,bound_right)
b_in = random.randint(1,bound_right)
print (a_in,b_in,"OK",lcm_fast(a_in,b_in),lcm_slow(a_in,b_in))
if (lcm_slow(a_in,b_in)!=lcm_fast(a_in,b_in)):
print (a_in, b_in,"OPS",lcm_fast(a_in,b_in),lcm_slow(a_in,b_in))
break
#
EDITED AFTER SOME COMMENTS/ANSWERS TO THE ORIGINAL PROBLEM
Inside this problem, a new problem arrives.
I am building this for a platform. My solution is right. After the comment from Blender. I did that (which was my original solution):
def lcm_fast(a,b):
a = ((a*b)/(euclidean_gcd(a,b)))
return a
The problem is that I receive this message failing on the platform's test cases:
Failed case #1/42: Cannot check answer. Perhaps output format is wrong.
Input: 18 35 Your output: 630.0 Correct output: 630 (Time used: 0.01/5.00, memory used: 9613312/536870912.)
That's funny. If I avoid the approximation with int(), the code is right for big numbers. However, without the conversion from float to int, I am unable to provide the answer on the desired format.
You're converting the result of your division back into an integer with int() because "regular" integer division results in a float. CPython's floats have a fixed precision, so your conversion back and forth will result in lost information for sufficiently large numbers.
Avoid losing precision and perform floor division with //, which returns an integer:
def lcm_fast(a,b):
return (a * b) // euclidean_gcd(a,b)
In python 3, standard division (/) operations will automatically promote to float, while integer division (//) will always return an int.
Thus, python can handle arbitrarily large ints, at some point your data is being treated as a float rather than an int, subjecting it to floating point precision error.
(I see someone else also typed up a similar answer while I was writing this up and feel obligated to make note of this before being bashed to death for copying someone else's answer.)