0

I am trying to build a machine learning model which predicts a single number from a series of numbers. I am using a Sequential model from the keras API of Tensorflow.

You can imagine my dataset to look something like this:

Index x data y data
0 np.ndarray(shape (1000000,) ) numpy.float32
1 np.ndarray(shape (1000000,) ) numpy.float32
2 np.ndarray(shape (1000000,) ) numpy.float32
3 np.ndarray(shape (1000000,) ) numpy.float32
... ... ...

This was my first attempt:

I tried using a numpy ndarray which contains numpy ndarrays which finally contain floats as my xdata, so something like this:

array([
    array([3.59280851, 3.60459062, 3.60459062, ..., 4.02911493]) #the inner arrays have 1000000 elements each
    array([3.54752101, 3.56740332, 3.56740332, ..., 4.02837855])
    array([3.61048168, 3.62152741, 3.62152741, ..., 4.02764217])
    ...
])

My y data is a numpy ndarray containing floats, which looks something like this

array([2.9864411, 3.0562437, ... , 2.7750807, 2.8712902], dtype=float32)

But when I tried to train the model using model.fit() it yields this error:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

You can have a closer look at my current try with this minimal example on Google Colab here.

Question: Easily said I just want my model to predict a number from a sequence of numbers. For example like this:

  • array([3.59280851, 3.60459062, 3.60459062, ...]) => 2.8989773
  • array([3.54752101, 3.56740332, 3.56740332, ...]) => 3.0893357
  • ...

How can I use a sequence of numbers to predict a single number in Tensorflow?

All in all I think that my question ist pretty general and should be easy to answer if you know how to tackle this problem, unlike me. Thanks in advance!

Luca Tatas
  • 160
  • 1
  • 14
  • Maybe [this](https://stackoverflow.com/q/58636087/14774959) can help you. I think that the problem is that an LSTM is expecting a 3D tensor as input `(batch_size, timesteps, features)`, but it is not getting that. – ClaudiaR Aug 23 '22 at 07:43

1 Answers1

0

I was able to solve my answer to some extent:

This is how I prepared my x data in the beginnning:

x_train = np.asarray(train_set["input"])

Now I changed it to this:

x_train = np.array([sub for sub in train_set["input"]]).reshape(3, 5, 1)

Because calling reshape on my first try directly did not work, I manually had to create the array from my dataset rather than calling np.asarray.

Luca Tatas
  • 160
  • 1
  • 14