Python 在Keras中带有GRU的RNN

Python 在Keras中带有GRU的RNN,python,machine-learning,keras,rnn,gated-recurrent-unit,Python,Machine Learning,Keras,Rnn,Gated Recurrent Unit,我想在python中使用Keras用GRU实现递归神经网络。我在运行代码时遇到了问题,我越来越多地更改变量,但它不起作用。你有解决这个问题的办法吗 inputs = 42 #number of columns input num_hidden =50 #number of neurons in the layer outputs = 1 #number of columns output num_epochs = 50 batch_size

我想在python中使用Keras用GRU实现递归神经网络。我在运行代码时遇到了问题,我越来越多地更改变量,但它不起作用。你有解决这个问题的办法吗

inputs = 42          #number of columns input  
num_hidden =50      #number of neurons in the layer
outputs = 1           #number of columns output  
num_epochs = 50
batch_size = 1000
learning_rate = 0.05
#train       (125973, 42)  125973 Rows and 42 Features
#Labels  (125973,1) is True Results
model = tf.contrib.keras.models.Sequential()
fv=tf.contrib.keras.layers.GRU
model.add(fv(units=42, activation='tanh', input_shape= (1000,42),return_sequences=True))  #i want to send Batches to train


#model.add(tf.keras.layers.Dropout(0.15))  # Dropout overfitting

#model.add(fv((1,42),activation='tanh', return_sequences=True))
#model.add(Dropout(0.2))  # Dropout overfitting

model.add(fv(42, activation='tanh'))
model.add(tf.keras.layers.Dropout(0.15))  # Dropout overfitting

model.add(tf.keras.layers.Dense(1000,activation='softsign'))
#model.add(tf.keras.layers.Activation("softsign"))


start = time.time()
# sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
# model.compile(loss="mse", optimizer=sgd)
model.compile(loss="mse", optimizer="Adam") 
inp = np.array(train)
oup = np.array(labels)
X_tr = inp[:batch_size].reshape(-1, batch_size, inputs)
model.fit(X_tr,labels,epochs=20, batch_size=batch_size)
但是,我得到以下错误:

ValueError: Error when checking target: expected dense to have shape (1000,) but got array with shape (1,)

这里,您已经提到输入向量形状为1000


model.add(fv(units=42,activation='tanh',input_shape=(1000,42),return_sequences=True))#我想将批次发送到train

然而,训练数据的形状(X_tr)是一维的
检查X_tr变量并使输入层具有相同的尺寸。

如果仔细阅读错误,您会发现您提供的标签形状之间存在形状不匹配,即
(无,1)
,以及模型输出形状之间存在形状不匹配,即
(无,1)


您能告诉我如何更改它吗?如果下面的答案之一解决了您的问题,请通过单击答案旁边的复选标记将其标记为“已回答”来接受它-参见X_tr is(1,1000,42)我不知道我真的很困惑我更改了它,但错误accure ValueError:输入数组的样本数应与目标数组的样本数相同。找到1个输入样本和125973个目标样本。@Mahdi.m确保输入数据(
X_-tr
)的形状为
(num_-samples,num_-timesteps,num_-features)
,标签数组(
labels
)的形状为
(num_-samples,)
(num_-samples,1)
.X_-tr.shape,label.shape((1000,1,42),(1000,1))ValueError:检查输入时出错:预期gru_4_输入具有形状(1000,42),但获得具有形状(1,42)的数组@Mahdi.m将gru层的输入形状更改为
(1,42)
。其工作!!!谢谢,我有一个问题,我如何发送批次来训练它?我应该使用loop for吗?你能告诉我吗?
ValueError: Error when checking target:  <--- This means the output shapes
expected dense to have shape (1000,)     <--- output shape of model  
but got array with shape (1,)            <--- the shape of labels you give when training
model.add(tf.keras.layers.Dense(1, activation='softsign')) # 1 unit in the output