0

I am trying to use a function (from another module) inside tensorflow. The function accepts a numpy array and returns the changepoints. My main goal is to deploy this model on tensorflow serving. I am running into error

AttributeError: 'DType' object has no attribute 'type'

There are 2 functions, one is create_data() that creates a numpy array and returns it, another is change() which accepts numpy array and uses the before mentioned function to return changepoints. I have created a placeholder to accept input data, an operation to execute the function. Problem is, if i try to send data through placeholder, i run into error. If i send the data directly into the function, it runs. Following is my code.

def create_data():
    np.random.seed(0)
    size = 100
    mean_a = 0.0
    mean_b = 10.0
    mean_c = 0
    var = 0.1
    data_a = np.random.normal(mean_a, var, size)
    data_b = np.random.normal(mean_b, var, size)
    data_c = np.random.normal(mean_c, var, size)
    data = np.concatenate([data_a, data_b, data_c])
    return data

def change(data):
    # what else i tried
    # data = np.array(data, dtype=np.float)
    # above line gives another error mentioned after code 
    cpts = (pelt(normal_mean(x, np.var(x)), len(x)))
    return cpts

sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[300, ], name="myInput")
y = tf.convert_to_tensor(change(x),np.float32,name="myOutput")
z = sess.run(y,feed_dict={x:create_data()})

If i try the code data = np.array(data, dtype=np.float) in the function change(), it gives me error

ValueError: setting an array element with a sequence.

I also tried data = np.hstack((data)).astype(np.float) and data = np.vstack((data)).astype(np.float) but it runs into a separate error that says use tf.map_fn. I also tried to use tf.eval() to convert the numbers but i couldn't get them to run inside a function with placeholders.

But if i send in the output directly,

y = tf.convert_to_tensor(change(create_data()),np.float32,name="myOutput")

It works.

How should i send in the input to make it work?

EDIT: The function in question is this if anyone wants to know.

1 Answer 1

4

This error is raised when you try to pass a Tensor into a numpy function

You need to use tf.py_func to include python function into tensorflow graph

(also, your change() functin uses data as argument instead of x)

Here is the code that worked for me

import numpy as np
import tensorflow as tf
from changepy import pelt
from changepy.costs import normal_mean

def create_data():
    np.random.seed(0)
    size = 100
    mean_a = 0.0
    mean_b = 10.0
    mean_c = 0
    var = 0.1
    data_a = np.random.normal(mean_a, var, size)
    data_b = np.random.normal(mean_b, var, size)
    data_c = np.random.normal(mean_c, var, size)
    data = np.concatenate([data_a, data_b, data_c])
    return data

def change(x):
    # what else i tried
    # data = np.array(data, dtype=np.float)
    # above line gives another error mentioned after code 

    cpts = (pelt(normal_mean(x, np.var(x)), len(x)))
    return cpts


sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[300, ], name="myInput")
y = tf.convert_to_tensor(tf.compat.v1.py_func(change, [x], 3*[tf.int64]),np.float32,name="myOutput")
z = sess.run(y,feed_dict={x:create_data()})
print(z)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.