简体   繁体   English

使用Tensorflow 2创建RNN

[英]Create a RNN with Tensorflow 2

Im' trying to create a RNN with Python and Tensorflow 2a, but I'm really not sure about what I did... The prediction results are constants. 我试图用Python和Tensorflow 2a创建一个RNN,但我真的不确定我做了什么......预测结果是常数。 What do you think about the data preparation? 您如何看待数据准备?

### Create the data ###
training_data =    [[1,2], [4,5], [7,8]...] # here, input_size = 2
training_targets = [3,     6,     9...]
predict_data =     [[9,10], [12,13], [15,16]...] # predictions should be [11, 14, 17...]

### Imports ###
import numpy as np
import tensorflow as tf
from tensorflow.python import keras as tfk

### Parameters ###
batch_size = 8
time_steps = 64

### Create the model ###
model = tfk.Sequential()
model.add(tfk.layers.Bidirectional(tfk.layers.LSTM(128, return_sequences=True, input_shape=(time_steps, input_size))))
model.add(tfk.layers.Bidirectional(tfk.layers.LSTM(64, return_sequences=True)))
model.add(tfk.layers.Dropout(rate=0.05))
model.add(tfk.layers.Dense(32, activation='relu'))
model.add(tfk.layers.Dense(1, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

### Create the training dataset ###
# Separate data in time steps
data = np.array([training_data[i: i + time_steps] for i in range(len(training_data) - time_steps)])
targets = np.array([training_targets[i: i + time_steps] for i in range(len(training_argets) - time_steps)])
# Create the tensors and dataset
data = tf.convert_to_tensor(data)
targets = tf.convert_to_tensor(targets)
dataset = tf.data.Dataset.from_tensor_slices((data, targets))
# Batch data, the data shape is : (batch_size, time_steps, input_size)
dataset = dataset.batch(batch_size)

### Train the model ###
model.fit(dataset, epochs=10, validation_data=validation_dataset, shuffle=False)

### Create the predict data ###
data = np.array([predict_data[i: i + time_steps] for i in range(len(predict_data) - time_steps)])
data = tf.convert_to_tensor(data)

### Try the model ###
results = model.predict(data, steps=time_steps)

Predictions should be [11, 14, 17...] But it's like constant and in a weird shape: 预测应该是[11,14,17 ...]但它就像是不变的并且形状奇怪:

[
[[1], [1], [1], [1] ...],
[[1], [1], [1], [1] ...],
...
]

Thanks for your help! 谢谢你的帮助!

You only have one neuron in your final layer. 你的最后一层只有一个神经元。 Think about it. 想一想。 All your network can do is return a single number, but that number is meaningless because for all your training data, it never knows what you want it to do. 您的所有网络都可以返回一个数字,但这个数字毫无意义,因为对于您的所有训练数据,它永远不知道您希望它做什么。 The problem is you are framing this as a classification problem when it's much better suited to be a regression problem. 问题是,当它更适合作为回归问题时,您将此作为分类问题。 Take out the softmax on the last layer and use MSE as the loss metric. 取出最后一层的softmax,并使用MSE作为损失度量。

Also, there only seem to be 2 time steps but your code implies there are 64. That doesn't make sense to me. 此外,似乎只有2个时间步,但你的代码暗示有64个。这对我来说没有意义。

Also, where do you define 'input_size'. 此外,您在哪里定义'input_size'。 It's not included in your code above. 它不包含在上面的代码中。

Have a go at thinking about the problem a bit more, make those changes and hopefully it will work. 再考虑一下这个问题,做出那些改变,希望它会起作用。 I'd try and run it myself but I don't want to make assumptions on your training data and targets. 我会尝试自己运行它,但我不想对你的训练数据和目标做出假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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