0

I'm trying to build and train a model with keras, but I'm facing the error "ValueError: as_list() is not defined on an unknown TensorShape.". Follows the most bare bone code I could build that still shows the issue:

import numpy as np
import tensorflow as tf

def func(x):
    return x

def create_dataset(vect):
    a = [vect]
    b = [vect] 

    dataset = tf.data.Dataset.from_tensor_slices((a, b))

    py_func = lambda x, y: (tf.numpy_function(func, [x], tf.float32), y)    ##LINE 1 !                                                                            
    dataset = dataset.map(py_func)                                          ##LINE 2 !

    dataset = dataset.map(lambda data, label: (tf.expand_dims(data, axis=0), tf.expand_dims(label, axis=0))) 
    return dataset

vect = np.array([-0.18441772, -0.17321777, -0.16046143, -0.14782715, -0.13504028,
       -0.12179565, -0.10858154, -0.09503174, -0.08117676, -0.06964111],
      dtype=np.float32)

example_dataset = create_dataset(vect)
example_list = list(example_dataset)
input_shape_m = example_list[0][0].shape #which is TensorShape([1, 10])

def DeepModel(input_shape):

    X_input = tf.keras.Input(input_shape)
    X = tf.keras.layers.Dense(10)(X_input)
    model = tf.keras.Model(inputs=X_input, outputs=X)

    return model

model = MyModel(input_shape_m[1:]) 
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])

history = model.fit(example_dataset, epochs=5)

This will give me the error I've said when running the last line of code.

If instead I comment the lines of code "LINE 1" and "LINE 2", the code will run just fine and it will not give me any error. Why is that? Thank you so much already

1 Answer 1

0

If anyone is curious, I solved this issue by modifying LINE 1 as follows:

py_func = lambda x, y: (tf.ensure_shape(tf.numpy_function(func, [x], tf.float32), vect.shape), y)
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.