RNN与Keras中的GRU

时间:2018-10-26 10:32:47

标签: 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 = 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,)

2 个答案:

答案 0 :(得分:0)

在这里,您提到的输入矢量形状为1000。

model.add(fv(units=42, activation='tanh', input_shape= (1000,42),return_sequences=True)) #i want to send Batches to train

但是,您的训练数据(X_tr)的形状为1-D 检查您的X_tr变量,输入层的尺寸是否相同。

答案 1 :(得分:0)

如果仔细阅读错误,您会发现提供的标签形状(None, 1)与模型输出形状(None, 1)之间存在形状不匹配:< / p>

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

因此,您需要使它们保持一致。您只需要将最后一层的单位数更改为1,因为每个输入样本有一个输出:

model.add(tf.keras.layers.Dense(1, activation='softsign')) # 1 unit in the output