创建 numpy 函数并转换为张量
Creating numpy functions and converting to tensor
我正在尝试使用 numpy 创建函数,例如 f=(x-a1)^2+(y-a2)^2+a3
其中a1,a2,a3是随机生成的数字,x,y是参数。
但我无法使用它,我想找到 f(0,0),其中 [0,0] 是 [x,y] 并且 [a1,a2,a3] 是之前设置的,但我的代码没有工作。
然后我想将此函数转换为 tensorflow tensor.Here 是我的代码,带有“##”的字符串不起作用。
import tensorflow as tf
from random import random, seed
import numpy as np
def mypolyval(x, min_point, min_value):
res = min_value
for i in range(len(min_point)):
res += (x[i] - min_point[i]) ** 2
return res
class FunGen:
def __init__(self, dim, n):
self.dim = dim
self.n = n
self.functions = []
self.x = []
def c2(self):
seed(10)
for _ in range(self.n):
min_point = [random() for _ in range(self.dim)]
min_value = random()
f = np.vectorize(mypolyval, excluded=['x'])
##print(f(x=np.array([0, 0]), min_point=min_point, min_value=min_value))
self.functions.append((f, min_point, min_value))
return self.functions
functions = FunGen(2, 1).c2()
for i in functions:
print(type(i[0]))
f=i[0]
## print(f(x=[0, 0], min_value=i[1], min_point=i[2]))
##a=tf.convert_to_tensor(f,dtype=np.float32)
要从 numpy 函数创建 TensorFlow 函数,您应该使用 tf.py_func
:
Wraps a python function and uses it as a TensorFlow op.
来自 TensorFlow API:
def my_func(x):
# x will be a numpy array with the contents of the placeholder below
return np.sinh(x)
inp = tf.placeholder(tf.float32)
y = tf.py_func(my_func, [inp], tf.float32)
问题与tensorflow无关。行
min_point = [random() for _ in range(self.dim)]
创建一个列表,列表没有 .size()
属性。
你可以使用 min_point = np.array([random() for _ in range(self.dim)]))
将它变成一个 numpy 数组,然后 .size()
就可以了。
或者,如果您想坚持使用列表,请使用 for i in range(len(min_point))
,它会计算列表的长度。
您还需要将 min_point
和 min_value
添加到排除列表中:
from random import random, seed
import numpy as np
def mypolyval(x, min_point, min_value):
print('x', x)
print('min_point', min_point)
print('min_value', min_value)
res = min_value
for i in range(len(min_point)):
res += (x[i] - min_point[i]) ** 2
return res
class FunGen:
def __init__(self, dim, n):
self.dim = dim
self.n = n
self.functions = []
self.x = []
def c2(self):
seed(10)
for _ in range(self.n):
min_point = [random() for _ in range(self.dim)]
min_value = random()
f = np.vectorize(mypolyval, excluded=['x', 'min_point', 'min_value'])
#print(f(x=[0, 0], min_value=min_value, min_point=min_point))
self.functions.append((f, min_point, min_value))
return self.functions
functions = FunGen(2, 1).c2()
for i in functions:
print(type(i[0]))
print(i)
f=i[0]
print(f(x=[0, 0], min_value=i[2], min_point=i[1]))
我正在尝试使用 numpy 创建函数,例如 f=(x-a1)^2+(y-a2)^2+a3
其中a1,a2,a3是随机生成的数字,x,y是参数。
但我无法使用它,我想找到 f(0,0),其中 [0,0] 是 [x,y] 并且 [a1,a2,a3] 是之前设置的,但我的代码没有工作。 然后我想将此函数转换为 tensorflow tensor.Here 是我的代码,带有“##”的字符串不起作用。
import tensorflow as tf
from random import random, seed
import numpy as np
def mypolyval(x, min_point, min_value):
res = min_value
for i in range(len(min_point)):
res += (x[i] - min_point[i]) ** 2
return res
class FunGen:
def __init__(self, dim, n):
self.dim = dim
self.n = n
self.functions = []
self.x = []
def c2(self):
seed(10)
for _ in range(self.n):
min_point = [random() for _ in range(self.dim)]
min_value = random()
f = np.vectorize(mypolyval, excluded=['x'])
##print(f(x=np.array([0, 0]), min_point=min_point, min_value=min_value))
self.functions.append((f, min_point, min_value))
return self.functions
functions = FunGen(2, 1).c2()
for i in functions:
print(type(i[0]))
f=i[0]
## print(f(x=[0, 0], min_value=i[1], min_point=i[2]))
##a=tf.convert_to_tensor(f,dtype=np.float32)
要从 numpy 函数创建 TensorFlow 函数,您应该使用 tf.py_func
:
Wraps a python function and uses it as a TensorFlow op.
来自 TensorFlow API:
def my_func(x):
# x will be a numpy array with the contents of the placeholder below
return np.sinh(x)
inp = tf.placeholder(tf.float32)
y = tf.py_func(my_func, [inp], tf.float32)
问题与tensorflow无关。行
min_point = [random() for _ in range(self.dim)]
创建一个列表,列表没有 .size()
属性。
你可以使用 min_point = np.array([random() for _ in range(self.dim)]))
将它变成一个 numpy 数组,然后 .size()
就可以了。
或者,如果您想坚持使用列表,请使用 for i in range(len(min_point))
,它会计算列表的长度。
您还需要将 min_point
和 min_value
添加到排除列表中:
from random import random, seed
import numpy as np
def mypolyval(x, min_point, min_value):
print('x', x)
print('min_point', min_point)
print('min_value', min_value)
res = min_value
for i in range(len(min_point)):
res += (x[i] - min_point[i]) ** 2
return res
class FunGen:
def __init__(self, dim, n):
self.dim = dim
self.n = n
self.functions = []
self.x = []
def c2(self):
seed(10)
for _ in range(self.n):
min_point = [random() for _ in range(self.dim)]
min_value = random()
f = np.vectorize(mypolyval, excluded=['x', 'min_point', 'min_value'])
#print(f(x=[0, 0], min_value=min_value, min_point=min_point))
self.functions.append((f, min_point, min_value))
return self.functions
functions = FunGen(2, 1).c2()
for i in functions:
print(type(i[0]))
print(i)
f=i[0]
print(f(x=[0, 0], min_value=i[2], min_point=i[1]))