简体   繁体   English

如何正确设置我的 x 数据以在 Tensorflow 中训练 LSTM model?

[英]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:例如像这样:

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

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.

相关问题 Tensorflow:如何将预训练 model 已经嵌入的数据输入到 LSTM model 中? - Tensorflow: How to input data already embedded by pre-train model into a LSTM model? 如何在一维序列上训练我的LSTM? - How do I train my LSTM on 1-dimensional sequences? 如何从生成器编写数据集以替换 tensorflow 中切片中的数据集,以获取具有 X_train 和 y_train 的表格数据集 - How do I write a Dataset from generator to replace Dataset from slices in tensorflow for a tabular data set with X_train and y_train 我如何 select 训练 LSTM 网络训练的数据 - How do I select train data for LSTM network training 如何正确使用 LSTM model 进行预测? - How do I correctly use LSTM model to make prediction? 如何在 Tensorflow 中手动设置 LSTM 层的权重 - How do I manually set the weights of an LSTM layer in Tensorflow 如何使用 Tensorflow 设置分类标签并将数据拆分为训练、测试和开发拆分? - How do i set the categorical labels and split the data into train,test and dev splits using Tensorflow? 如何在 Tensorflow 中获得 LSTM 的测试准确度 - How do I get the Test Accuracy for my LSTM in Tensorflow 我如何训练我的DNNClassifier模型(在tensorflow中),以从新的训练案例中学习? 我无权访问初始CSV文件 - How do I train my DNNClassifier model (in tensorflow), to learn from new training cases? I do not have access to the initial CSV file 我如何为我的 NN 模型正确地塑造我的数据? - How Do I Correctly Shape my Data for my NN Model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM