使用keras库指定密集 [英] Specifying Dense using keras library

查看:81
本文介绍了使用keras库指定密集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点误解了如何为我的数据创建一个简单的序列.

I slightly misunderstand how to create a simple Sequence for my data.

数据具有以下维度:

X_train.shape
(2369, 12)

y_train.shape
(2369,)

X_test.shape
(592, 12)

y_test.shape
(592,)

这是我创建模型的方式:

This is how I create the model:

batch_size = 128
nb_epoch = 20
in_out_neurons = X_train.shape[1] 
dimof_middle = 100

model = Sequential()
model.add(Dense(batch_size, batch_input_shape=(None, in_out_neurons)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(batch_size))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(in_out_neurons))
model.add(Activation('linear'))

# I am solving the regression problem, not the classification one
model.compile(loss="mean_squared_error", optimizer="rmsprop") 

history = model.fit(X_train, y_train,
                    batch_size=batch_size, nb_epoch=nb_epoch,
                    verbose=1, validation_data=(X_test, y_test))

错误消息:

异常:检查模型输入时出错:预期的density_input_14为 形状为(None,1),但数组的形状为(2369,12)ç

Exception: Error when checking model input: expected dense_input_14 to have shape (None, 1) but got array with shape (2369, 12)ç

错误是:

检查模型目标时发生错误:预期activation_42具有形状 (无,12),但数组的形状为(2369,1)

Error when checking model target: expected activation_42 to have shape (None, 12) but got array with shape (2369, 1)

此错误发生在行:

model.add(Dense(in_out_neurons))

如何更改Dense使其起作用?

另一个问题是如何添加一个简单的自动编码器以初始化ANN的权重?

Another question is how to add a simple autoencoder in order to initialize weights of ANN?

推荐答案

您的问题之一是您似乎误解了批次. 批处理是一次计算的训练样本数,因此您可以一次使用100个,而不是一次从X_train计算一个训练样本.这里重要的一点是,这与您的模型无关.

One of your problems is that you seem to misunderstand what a batch is. A batch is the number of training samples computed at a time, so instead of computing one training sample from X_train at a time you use, for example, 100 at a time. The important bit here is that this has nothing to do with your model.

所以当你写

model.add(Dense(batch_size, batch_input_shape=(None, in_out_neurons)))

然后创建一个输出大小为一批的完全连接的图层.那没有任何意义.

then you create a fully connected layer with an output size of one batch. That does not make a lot of sense.

另一个问题是,模型的输出为12个神经元,而Y的输出仅为一个值/神经元.您的模型如下所示:

Another problem is that your model's output is 12 neurons while your Y is only one value/neuron. Your model looks like this:

  |
  v
[128]
[128]
[ 12]
  |
  v

然后,fit()所做的是,将形状为(128, 12)((batch size, X_train.shape[1]))的矩阵输入模型,并尝试将形状(128,12)的输出从最后一层与相应的Y值进行比较批次(形状(128,1))

Then what fit() does is, it inputs a matrix of shape (128, 12) ((batch size, X_train.shape[1])) into the model and attempts to compare the output of shape (128,12) from the last layer to the corresponding Y values of the batch (shape (128,1)).

这篇关于使用keras库指定密集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆