[英]How do I set up my x data correctly to train an LSTM model in Tensorflow?
I am trying to build a machine learning model which predicts a single number from a series of numbers.我正在尝试构建一个机器学习 model 从一系列数字中预测一个数字。 I am using a Sequential model from the keras API of Tensorflow.我正在使用 ZCB20B802A3F0255E054E4FBED281 的 keras API 中的顺序 model。
You can imagine my dataset to look something like this:你可以想象我的数据集看起来像这样:
Index指数 | x data x 数据 | y data y 数据 |
---|---|---|
0 0 | np.ndarray(shape (1000000,) ) |
numpy.float32 |
1 1 | np.ndarray(shape (1000000,) ) |
numpy.float32 |
2 2 | np.ndarray(shape (1000000,) ) |
numpy.float32 |
3 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:我尝试使用 numpy ndarray,其中包含 numpy ndarray,最终包含浮点数作为我的 xdata,所以是这样的:
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我的 y 数据是一个包含浮点数的 numpy ndarray,看起来像这样
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:但是,当我尝试使用model.fit()
训练 model 时,会产生以下错误:
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.您可以在此处通过 Google Colab 上的这个最小示例仔细查看我当前的尝试。
Question: Easily said I just want my model to predict a number from a sequence of numbers.问题:简单地说,我只想让我的 model 从一个数字序列中预测一个数字。 For example like this:例如像这样:
How can I use a sequence of numbers to predict a single number in Tensorflow?如何使用数字序列来预测 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!提前致谢!
I was able to solve my answer to some extent:我能够在某种程度上解决我的答案:
This is how I prepared my x data in the beginnning:这就是我在开始时准备 x 数据的方式:
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
.因为在我第一次尝试时直接调用 reshape 不起作用,所以我必须手动从我的数据集创建数组,而不是调用np.asarray
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.