-1

我想在 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,)
4

2 回答 2

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 变量并且输入层具有相同的维度。

于 2018-10-26T10:39:29.950 回答
0

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

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
于 2018-10-26T13:42:57.280 回答