Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为Keras(tensorflow)中的LSTM正确塑造2D输入和输出?将输入设置为3D并将输出设置为热编码时生成错误_Python_Tensorflow_Keras_Lstm_Recurrent Neural Network - Fatal编程技术网

Python 如何为Keras(tensorflow)中的LSTM正确塑造2D输入和输出?将输入设置为3D并将输出设置为热编码时生成错误

Python 如何为Keras(tensorflow)中的LSTM正确塑造2D输入和输出?将输入设置为3D并将输出设置为热编码时生成错误,python,tensorflow,keras,lstm,recurrent-neural-network,Python,Tensorflow,Keras,Lstm,Recurrent Neural Network,对于类项目,我们必须获取2D数据集并使用LSTM NN进行预测。我们将其与简单的DNN进行比较 我需要重塑我的数据,使其与NN一起工作,我很难找到输入和输出的正确形状。我可能也设置了错误的NN——但它解决了我在处理3D数据时遇到的另一个问题 我的x\u列车形状是(2340590),我的y形状是(2340,) 我将x_列车改造为(12340590) I一个热编码的y——称为binary\u标签 二进制标签。形状是(2340,2) 总结相关输入形状: x_train.shape = (1, 2340

对于类项目,我们必须获取2D数据集并使用LSTM NN进行预测。我们将其与简单的DNN进行比较

我需要重塑我的数据,使其与NN一起工作,我很难找到输入和输出的正确形状。我可能也设置了错误的NN——但它解决了我在处理3D数据时遇到的另一个问题

我的
x\u列车形状
(2340590)
,我的
y
形状是
(2340,)

我将
x_列车
改造为
(12340590)

I一个热编码的
y
——称为
binary\u标签

二进制标签。形状
(2340,2)

总结相关输入形状:

x_train.shape = (1, 2340, 590)

binary_labels.shape = (2340, 2)
> --------------------------------------------------------------------------- ValueError                                Traceback (most recent call
> last) <ipython-input-224-36b1852bd7a7> in <module>
>      11           metrics=['accuracy'])
>      12 
> ---> 13 rnn_model.fit(X_train, binary_labels, epochs=5, validation_split = 0.2)
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py
> in fit(self, x, y, batch_size, epochs, verbose, callbacks,
> validation_split, validation_data, shuffle, class_weight,
> sample_weight, initial_epoch, steps_per_epoch, validation_steps,
> validation_freq, max_queue_size, workers, use_multiprocessing,
> **kwargs)
>     641             `tf.data` dataset or a dataset iterator, and 'steps_per_epoch'
>     642             is None, the epoch will run until the input dataset is exhausted.
> --> 643         validation_steps: Only relevant if `validation_data` is provided and
>     644             is a dataset or dataset iterator. Total number of steps (batches of
>     645             samples) to draw before stopping when performing validation
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py
> in fit(self, model, x, y, batch_size, epochs, verbose, callbacks,
> validation_split, validation_data, shuffle, class_weight,
> sample_weight, initial_epoch, steps_per_epoch, validation_steps,
> validation_freq, **kwargs)
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py
> in _standardize_user_data(self, x, y, sample_weight, class_weight,
> batch_size, check_steps, steps_name, steps, validation_split, shuffle,
> extract_tensors_from_dataset)    2463     if not self.inputs:    2464 
> # We need to use `x_input` to set the model inputs.
> -> 2465     2466       # If input data is a dataset iterator in graph mode or if it is an eager    2467       # iterator and only one batch
> of samples is required, we fetch the data
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py
> in check_array_lengths(inputs, targets, weights)
>     617   """
>     618   batch_count = int(len(index_array) / batch_size)
> --> 619   # to reshape we need to be cleanly divisible by batch size
>     620   # we stash extra items and reappend them after shuffling
>     621   last_batch = index_array[batch_count * batch_size:]
> 
> ValueError: Input arrays should have the same number of samples as
> target arrays. Found 1 input samples and 2340 target samples
问题:

x_train.shape = (1, 2340, 590)

binary_labels.shape = (2340, 2)
> --------------------------------------------------------------------------- ValueError                                Traceback (most recent call
> last) <ipython-input-224-36b1852bd7a7> in <module>
>      11           metrics=['accuracy'])
>      12 
> ---> 13 rnn_model.fit(X_train, binary_labels, epochs=5, validation_split = 0.2)
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py
> in fit(self, x, y, batch_size, epochs, verbose, callbacks,
> validation_split, validation_data, shuffle, class_weight,
> sample_weight, initial_epoch, steps_per_epoch, validation_steps,
> validation_freq, max_queue_size, workers, use_multiprocessing,
> **kwargs)
>     641             `tf.data` dataset or a dataset iterator, and 'steps_per_epoch'
>     642             is None, the epoch will run until the input dataset is exhausted.
> --> 643         validation_steps: Only relevant if `validation_data` is provided and
>     644             is a dataset or dataset iterator. Total number of steps (batches of
>     645             samples) to draw before stopping when performing validation
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py
> in fit(self, model, x, y, batch_size, epochs, verbose, callbacks,
> validation_split, validation_data, shuffle, class_weight,
> sample_weight, initial_epoch, steps_per_epoch, validation_steps,
> validation_freq, **kwargs)
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py
> in _standardize_user_data(self, x, y, sample_weight, class_weight,
> batch_size, check_steps, steps_name, steps, validation_split, shuffle,
> extract_tensors_from_dataset)    2463     if not self.inputs:    2464 
> # We need to use `x_input` to set the model inputs.
> -> 2465     2466       # If input data is a dataset iterator in graph mode or if it is an eager    2467       # iterator and only one batch
> of samples is required, we fetch the data
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py
> in check_array_lengths(inputs, targets, weights)
>     617   """
>     618   batch_count = int(len(index_array) / batch_size)
> --> 619   # to reshape we need to be cleanly divisible by batch size
>     620   # we stash extra items and reappend them after shuffling
>     621   last_batch = index_array[batch_count * batch_size:]
> 
> ValueError: Input arrays should have the same number of samples as
> target arrays. Found 1 input samples and 2340 target samples
运行模型会生成一个错误,即输入数组应具有相同数量的样本

我尝试将
二进制标签重新设置为
(12340,2)
-但运行NN时,我得到
值错误

空的训练数据

rnn\u模型=keras.Sequential([
keras.layers.LSTM(2,输入_-shape=(X_-train.shape[1],X_-train.shape[2]),返回_-sequences=True),
keras.layers.LSTM(590,返回序列=True,激活=tf.nn.relu),
keras.layers.LSTM(590,返回序列=True,激活=tf.nn.relu),
keras.layers.LSTM(590,返回序列=True,激活=tf.nn.relu),
keras。层。致密(2,激活='sigmoid')
])
rnn_model.compile(optimizer='adam',
损失='binary\u交叉熵',
指标=[‘准确度’])
rnn_模型拟合(X_序列,二进制标签,历元=5,验证_分割=0.2)
我希望模型能够运行!相反,我收到一条无法解决的错误消息

有人知道我如何解决这个问题吗

错误消息的相关部分:
ValueError:输入数组的样本数应与目标数组的样本数相同。找到1个输入样本和2340个目标样本

整个错误消息:

x_train.shape = (1, 2340, 590)

binary_labels.shape = (2340, 2)
> --------------------------------------------------------------------------- ValueError                                Traceback (most recent call
> last) <ipython-input-224-36b1852bd7a7> in <module>
>      11           metrics=['accuracy'])
>      12 
> ---> 13 rnn_model.fit(X_train, binary_labels, epochs=5, validation_split = 0.2)
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py
> in fit(self, x, y, batch_size, epochs, verbose, callbacks,
> validation_split, validation_data, shuffle, class_weight,
> sample_weight, initial_epoch, steps_per_epoch, validation_steps,
> validation_freq, max_queue_size, workers, use_multiprocessing,
> **kwargs)
>     641             `tf.data` dataset or a dataset iterator, and 'steps_per_epoch'
>     642             is None, the epoch will run until the input dataset is exhausted.
> --> 643         validation_steps: Only relevant if `validation_data` is provided and
>     644             is a dataset or dataset iterator. Total number of steps (batches of
>     645             samples) to draw before stopping when performing validation
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py
> in fit(self, model, x, y, batch_size, epochs, verbose, callbacks,
> validation_split, validation_data, shuffle, class_weight,
> sample_weight, initial_epoch, steps_per_epoch, validation_steps,
> validation_freq, **kwargs)
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py
> in _standardize_user_data(self, x, y, sample_weight, class_weight,
> batch_size, check_steps, steps_name, steps, validation_split, shuffle,
> extract_tensors_from_dataset)    2463     if not self.inputs:    2464 
> # We need to use `x_input` to set the model inputs.
> -> 2465     2466       # If input data is a dataset iterator in graph mode or if it is an eager    2467       # iterator and only one batch
> of samples is required, we fetch the data
> 
> ~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py
> in check_array_lengths(inputs, targets, weights)
>     617   """
>     618   batch_count = int(len(index_array) / batch_size)
> --> 619   # to reshape we need to be cleanly divisible by batch size
>     620   # we stash extra items and reappend them after shuffling
>     621   last_batch = index_array[batch_count * batch_size:]
> 
> ValueError: Input arrays should have the same number of samples as
> target arrays. Found 1 input samples and 2340 target samples
--------------------------------------------------------------------------------------ValueError回溯(最近的调用)
>最后)在
>11个指标=[‘准确度’])
>      12 
>-->13 rnn_模型.fit(X_序列,二进制标签,历元=5,验证_分割=0.2)
> 
>~/Desktop/Program_下载/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py
>适合(自我、x、y、批量大小、时代、详细、回调、,
>验证分割、验证数据、洗牌、等级权重、,
>样本重量、初始历元、每历元步骤、验证步骤、,
>验证频率、最大队列大小、工人、使用多处理、,
>**kwargs)
>641`tf.data`dataset或dataset迭代器,以及'steps_per_epoch'
>642为“无”,将运行历元,直到输入数据集耗尽。
>-->643验证步骤:仅当提供了“验证数据”且
>644是数据集或数据集迭代器。步骤总数(批数)
>645个样本)在执行验证时停止之前提取
> 
>~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py
>适合(自我、型号、x、y、批量大小、年代、详细、回调、,
>验证分割、验证数据、洗牌、等级权重、,
>样本重量、初始历元、每历元步骤、验证步骤、,
>验证频率,**kwargs)
> 
>~/Desktop/Program_下载/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py
>标准化用户数据(自身、x、y、样本重量、类别重量、,
>批量大小、检查步骤、步骤名称、步骤、验证、拆分、洗牌、,
>从_数据集中提取_张量_)2463如果不是自输入:2464
>#我们需要使用'x_input'来设置模型输入。
>->2465 2466#如果输入数据是图形模式下的数据集迭代器,或者如果它是一个渴望的2467#迭代器,并且只有一批
>如果需要大量样本,我们将获取数据
> 
>~/Desktop/Program_Downloads/anaconda3/envs/uwdatasci420/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py
>检查数组长度(输入、目标、权重)
>     617   """
>618批次计数=整数(len(索引数组)/批次大小)
>-->619#要重塑,我们需要用批量大小清楚地划分
>620#我们把多余的物品藏起来,洗牌后重新出售
>621最后一批=索引数组[批次计数*批次大小:]
> 
>ValueError:输入数组的样本数应与
>目标数组。找到1个输入样本和2340个目标样本

如果我很理解你的问题,问题是:

x\u train.shape
(2340590)
所以您有2340个大小(590,)的样本
如果您像以前那样重塑数据:
(12340590)
,您将只输入一个大小为
(2340590)
,因为keras模型输入形状的定义如下:
(批次大小,大小1,大小2)

因此,为了使模型正常工作,您只需对数据进行如下重塑:

x_train = np.expand_dims(x_train, -1) #new shape = (2340, 590, 1)

试试看,告诉我是否更好!

我认为将最后的
致密层包装进去会为您解决问题

#。。。
keras.layers.LSTM(590,返回序列=True,激活=tf.nn.relu),
keras.layers.TimeDistributed(keras.layers.Dense(2,激活='sigmoid'))
]
也就是说,如果要对序列中的每个时间步进行分类,则可能不希望返回上一个LSTM中的序列,而只希望在看到整个输入序列后,根据LSTM的最终状态进行分类: