简体   繁体   English

重塑Keras的1D numpy阵列

[英]Reshape 1D Numpy Array for Keras

I'm using keras, and when I try model.fit it throws an error because the X_Train and Y_Train inputs have incompatible shapes. 我正在使用keras,当我尝试model.fit时会抛出错误,因为X_Train和Y_Train输入的形状不兼容。

The data I have is a system of 10 inputs and 1 output. 我拥有的数据是10个输入和1个输出的系统。 And I'm using 9 iterations of the data as a test, so I have a list of 9 vectors with shape [10, 1] so, understandable, X_Train.shape = [9, 10, 1]. 而且我正在使用9次数据迭代作为测试,因此我有9个形状为[10,1]的向量的列表,因此,X_Train.shape = [9,10,1]是可以理解的。 My output is a list of 9 values which makes Y_Train.shape = [9,1]. 我的输出是9个值的列表,这些值使Y_Train.shape = [9,1]。 Yet I get this error: 但是我得到这个错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [9,1] vs. [9,10,1]

I asume the correct shape of the Y_Train vector must be [9, 1, 1] but cannot find a way to shape it so. 我假设Y_Train向量的正确形状必须为[9,1,1],但找不到找到它的形状的方法。

Based on this I have two questions: Is [9, 1, 1] the correct expected shape according to my description of the problem? 基于此,我有两个问题:[9,1,1]根据我对问题的描述是正确的预期形状吗? and how do I make it conform to that expected shape? 以及如何使其符合预期形状?

The input shape that passes through a computation graph in keras is of the type: 通过keras中的计算图传递的输入形状为以下类型:

(?, x.shape[1], x.shape[2], ....)   #As seen in model.summary()

The first ? 首先 ? is the channel for passing your samples (rows in your dataset). 是传递样本(数据集中的行)的通道。 You can pass them in batches, so thats something that you define while fitting the model itself. 您可以批量传递它们,以便在拟合模型本身时定义它们。

When setting the shape of the layers however, you set it as 但是,当设置图层的形状时,将其设置为

(x.shape[1], x.shape[2], ....)

Keras automatically adds the first channel at the start for the batches. Keras会在批次开始时自动添加第一个通道。 So, if each row in your dataset is a 1-D array of length 10. Then, 因此,如果数据集中的每一行都是长度为10的一维数组。那么,

## For keras functional API
inp = Input((10,))

## For keras sequential API
model = Sequential([
    Dense(32, input_shape=(10,))
])

If you are working with say a 3-D dataset, where each 'row' or sample in your dataset is a 2-D array of (10,10) shape: 如果您使用3-D数据集,则数据集中的每个“行”或样本都是(10,10)形状的二维数组:

## For keras functional API
inp = Input((10,10))

## For keras sequential API
model = Sequential([
    Dense(32, input_shape=(10,10))
])

Specific to your question, since you have a list of 9 arrays of the shape (10,1). 特定于您的问题,因为您有9个形状为(10,1)的数组的列表。 You should simply ignore the 9, since that is what gets pass on the first channel as (?, 10, 1). 您应该简单地忽略9,因为在第一个通道上传递的是(?,10,1)。 So define your input shapes as just (10,) or (10,1) 因此,将输入形状定义为(10,)或(10,1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM