简体   繁体   中英

How to build a one-to-many RNN in Tensorflow

I am new to RNNs and I want to build a one-to-many RNN using Tensorflow. The "one" input in my model is a vector of 3 coordinates(something like [x0, y0, z0]) and the "many" output I wish to achieve is a sequence of 50 numbers, 1 for each time step.

For example, I give this single point in 3d space like P = [1, 2, 3] and I wish to predict a sequence of 50 numbers, representing the temperature at this point for 50 consecutive time steps.

I searched a lot about some sample codes but I could not find a good one-to-many RNN on the web. I appreciate your help with this.

If I understand your question correctly, you want a RNN where you input 3 features ([x0, y0, z0]) and output a vector of 50 numbers representing temperature over 50 time steps?

In that case, you need to input coordinates for each timestep you want to predict, as the whole idea of RNN's is that they can learn to predict time series by seeing examples of such series. Your data needs to be in the following structure:

Your coordinate data needs to be in a format where you have the x,y,z data for each timestep you wish to predict. If we assume you have 10000 data points, your input data would need to have this shape:

[ 10000 , 50 , 3 ]

Here 10000 is the amount of data points, 50 is the timestep for each xyz coordinate, and 3 is each of the coordinates in a given timestep.

Your temperature data then needs to have the following shape:

[ 10000 , 50 ]

Here 10000 is each collection of temperatures over the 50-time steps, and 50 is each of the temperature readings in the timesteps.

In the case that you only want to use 1 coordinate set to predict the 50 timesteps, you could just reshape your data to have the same 50 coordinates in each of the timesteps.

Assuming you have this dataset, you could construct a simple RNN like this:

# ----- Define dummy data -----
coordinates = np.random.random([10000, 50, 3]).astype(np.float32)
temps = np.random.rand(10000,50)

# ----- Define RNN model -----
rnn_model = tf.keras.Sequential()
rnn_model.add(tf.keras.layers.SimpleRNN(128))
rnn_model.add(tf.keras.layers.Dense(50))

# ----- Compile model -----
rnn_model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(1e-4))

# ----- Train model -----
history = rnn_model.fit(x=coordinates, y=temps, batch_size=32,epochs=50)

You would of course need to change your.network structure depending on the task, and you could also experiment with other recurrent layers such as a LSTM layer, but this should give you an idea of the basics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM