我有经过数据集培训的RNN模型:
train = tf.data.Dataset.from_tensor_slices((data_x[:train_size],
data_y[:train_size])).batch(batch_size).repeat()
模型:
model = tf.keras.Sequential()
model.add(tf.keras.layers.GRU(units=lstm_num_units,
return_sequences=True,
kernel_initializer='random_uniform',
recurrent_initializer='random_uniform',
bias_initializer='random_uniform',
batch_size=batch_size,
input_shape = [seq_len, num_features]))
model.add(tf.keras.layers.LSTM(units=lstm_num_units,
batch_size=batch_size,
return_sequences=True,
input_shape = [seq_len, num_features]))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(units=dence_units))
model.add(tf.keras.layers.Dropout(drop_flat))
model.add(tf.keras.layers.Dense(units=out_units))
model.add(tf.keras.layers.Softmax())
model.compile(loss="sparse_categorical_crossentropy",
optimizer=tf.train.RMSPropOptimizer(opt),
metrics=['accuracy'])
model.fit(train, epochs=EPOCHS,
steps_per_epoch=repeat_size_train,
validation_data=validate,
validation_steps=repeat_size_validate,
verbose=1,
shuffle=True)
callbacks=[tensorboard, cp_callback])
我需要对seq-len的单个输入进行预测,但看起来我的输入必须是一个批大小:
ar = np.random.randint(98, size=[batch_size, seq_len])
ar = np.reshape(ar, [batch_size, seq_len, 1])
prediction = model.m.predict(ar)
有没有一种方法可以使它在形状[1,序列长度,1]的单个输入上工作?