0

I am trying to create function using numpy something like f=(x-a1)^2+(y-a2)^2+a3

Where a1,a2,a3 are random generated numbers and x,y are parameters.

But I cant work with it, I want to find f(0,0) where [0,0] is [x,y] and [a1,a2,a3] were set before,but my code doesnt work. And then I want to convert this function to tensorflow tensor.Here is my code, string with "##" dont work.

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)

2 Answers2

0

To create a TensorFlow function from a numpy function, you should use tf.py_func:

Wraps a python function and uses it as a TensorFlow op.

From the 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)
pfm
  • 6,210
  • 4
  • 39
  • 44
0

The problem is not related to tensorflow. The line

min_point = [random() for _ in range(self.dim)]

creates a list and lists don't have the .size() property.

You can turn it into a numpy array using min_point = np.array([random() for _ in range(self.dim)])), then the .size() will work.

Or if you want to stick to the list use for i in range(len(min_point)), which calculates the length of a list.

Also you need to add min_point and min_value them to the excluded list:

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]))
Joe
  • 6,758
  • 2
  • 26
  • 47