我正致力于LSTM神经网络的实施,用于序列分类.我想用以下参数设计一个网络:
n热矢量序列.我需要在CNTK中实现它,但我很难,因为它的文档编写得不是很好.有人可以帮助我吗?
machine-learning neural-network lstm recurrent-neural-network cntk
我正在尝试这个笔记本:https: //github.com/sjchoi86/Tensorflow-101/blob/master/notebooks/char_rnn_sample_tutorial.ipynb
我对[6]中的这一行有疑问:
outputs, final_state = seq2seq.rnn_decoder(inputs, istate, cell, loop_function=None, scope='rnnlm')
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
NameError: name 'seq2seq' is not defined
Run Code Online (Sandbox Code Playgroud)
我正在使用tensorflow 1.0.1.我试过了
tf.contrib.seq2seq
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
AttributeError: 'module' object has no attribute 'rnn_decoder'
Run Code Online (Sandbox Code Playgroud)
我认为在tensorflow 1.0.1中新的rnn网络实现是一个问题,但我不知道如何修复它.
python attributeerror lstm tensorflow recurrent-neural-network
我正在用 Keras 编写一个序列到序列模型。由于某种原因,当我尝试在下面的函数中定义模型时:
def define_GRU_models(encoder_input_dim,
output_dim,
activation,
n_units):
# define training encoder #
###########################
# layer 1
encoder_inputs = Input(shape=encoder_input_dim)
l1_encoder = GRU(n_units,
name='l1_encoder',
return_sequences=True,
return_state=True)
l1_encoder_outputs, l1_encoder_state = l1_encoder(encoder_inputs)
# layer 2
l2_encoder = GRU(n_units,
name='l2_encoder',
return_state=True)
l2_encoder_outputs, l2_encoder_state = l2_encoder(l1_encoder_outputs)
# define training decoder #
###########################
# layer 1
decoder_inputs = Input(shape=(None, output_dim))
l1_decoder_gru = GRU(int(n_units/2),
name='l1_decoder_gru',
return_sequences=True,
return_state=False)
l1_decoder_outputs, _ = l1_decoder_gru(decoder_inputs)
# layer 2
l2_decoder_gru = GRU(n_units,
name='l2_decoder_gru',
return_sequences=True,
return_state=False)
l2_decoder_outputs, _ = l2_decoder_gru(l1_decoder_outputs, initial_state=l1_encoder_state)
# …Run Code Online (Sandbox Code Playgroud) python machine-learning keras tensorflow recurrent-neural-network
我想用 Keras 创建一个带有两个 LSTM 层的模型。但是,以下代码会生成错误:
from keras.models import Sequential
from keras.layers import LSTM, Dropout, Activation
from keras.callbacks import ModelCheckpoint
from keras.utils import to_categorical
model = Sequential()
model.add(LSTM(5, activation="softmax"))
model.add(LSTM(5, activation="softmax"))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['categorical_accuracy'])
# These values are to be predicted.
directions = [-2, -1, 0, 1, 2]
# Sample data. We have three time steps, one
# feature per timestep, and one resulting value.
data = [[[[1], [2], [3]], -1],
[[[3], [2], [1]], 2],
[[[4], [5], [7]], 1],
[[[1], [-1], …Run Code Online (Sandbox Code Playgroud) 我使用 keras 来训练 seq2seq 模型(keras.models.Model)。模型的 X 和 y 是 [X_encoder, X_decoder] , y 即编码器和解码器输入和标签的列表(请注意,解码器输入 X_decoder 是 \xe2\x80\x98y\xe2\x80\x99 ,前面有一个位置比实际的 y 基本上是老师强迫的)。
\n\n所以我现在的问题是在训练之后,当涉及到实际预测时,我没有任何标签,如何为我的输入提供 \xe2\x80\x98X_decoder\xe2\x80\x99 ?还是我要训练别的东西?
\n\n这是模型定义的片段(如果有帮助的话):)
\n\n# Encoder\nencoder_inputs = Input(batch_shape=(batch_size, max_len,), dtype='int32')\nencoder_embedding = embedding_layer(encoder_inputs)\nencoder_LSTM = CuDNNLSTM(hidden_dim, return_state=True, stateful=True)\nencoder_outputs, state_h, state_c = encoder_LSTM(encoder_embedding)\n\n# Decoder\ndecoder_inputs = Input(shape=(max_len,), dtype='int32')\ndecoder_embedding = embedding_layer(decoder_inputs)\ndecoder_LSTM = CuDNNLSTM(hidden_dim, return_state=True, return_sequences=True)\ndecoder_outputs, _, _ = decoder_LSTM(decoder_embedding, initial_state=[state_h, state_c])\n\n# Output\noutputs = TimeDistributed(Dense(vocab_size, activation='softmax'))(decoder_outputs)\nmodel = Model([encoder_inputs, decoder_inputs], outputs)\n\n# model fitting:\nmodel.fit([X_encoder, X_decoder], y, steps_per_epoch=int(number_of_train_samples/batch_size),\nepochs=epochs)\nRun Code Online (Sandbox Code Playgroud)\n 在 Keras 文档中, 和stateful都unroll设置为False。那么如果这两者都不是的话,Keras 中的循环是如何完成的呢?
Keras RNN 文档
我检查了Keras中RNN的源代码,似乎默认操作是在每个时间步初始化LSTM。我累了吗?
if initial_state is not None:
pass
elif self.stateful:
initial_state = self.states
else:
initial_state = self.get_initial_state(inputs)
Run Code Online (Sandbox Code Playgroud)
如果我是正确的,是否意味着对于时间序列分析,设置 会更好unroll=True?
machine-learning deep-learning lstm keras recurrent-neural-network
我正在使用一些训练 lstm 生成序列的代码。训练模型后,调用 lstm() 方法:
x = some_input
lstm_output, (h_n, c_n) = lstm(x, hc)
funcc = nn.Linear(in_features=lstm_num_hidden,
output_features=vocab_size,
bias=True)
func_output = func(lstm_output)
Run Code Online (Sandbox Code Playgroud)
我已经查看了文档,nn.Linear()但我仍然不明白这个转换正在做什么以及为什么它是必要的。如果 lstm 已经经过训练,那么它给出的输出应该已经具有预先建立的维度。该输出(lstm_output)将是生成的序列,或者在我的例子中是向量数组。我在这里错过了什么吗?
我的数据框是每小时一次(我的 df 的索引),我想预测 y。
> df.head()
Date y
2019-10-03 00:00:00 343
2019-10-03 01:00:00 101
2019-10-03 02:00:00 70
2019-10-03 03:00:00 67
2019-10-03 04:00:00 122
Run Code Online (Sandbox Code Playgroud)
我现在将导入库并训练模型:
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
min_max_scaler = MinMaxScaler()
prediction_hours = 24
df_train= df[:len(df)-prediction_hours]
df_test= df[len(df)-prediction_hours:]
print(df_train.head())
print('/////////////////////////////////////////')
print (df_test.head())
training_set = df_train.values
training_set = min_max_scaler.fit_transform(training_set)
x_train = training_set[0:len(training_set)-1]
y_train = training_set[1:len(training_set)]
x_train = np.reshape(x_train, (len(x_train), 1, 1))
num_units = 2
activation_function = 'sigmoid'
optimizer = …Run Code Online (Sandbox Code Playgroud) 我尝试从tensorflow.keras.layers导入 CuDnnLSTM以提高训练速度,但出现此错误。我知道 Keras 2.0.8 和 python 3.5 的用户也提出了类似的问题。
我的配置是tensorflow版本2.0.0-beta1和Python 3.6.10。
这就是我尝试过的: from tensorflow.keras.layers import CuDNNLSTM
我收到此错误, ImportError:无法导入名称“CuDNNLSTM”
请问有人知道如何修复此错误吗?提前致谢!
python-3.x deep-learning tensorflow recurrent-neural-network keras-layer
我有网络:
Tensor("input_1:0", shape=(?, 5, 1), dtype=float32)
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 5, 1) 0
_________________________________________________________________
bidirectional_1 (Bidirection (None, 5, 64) 2176
_________________________________________________________________
activation_1 (Activation) (None, 5, 64) 0
_________________________________________________________________
bidirectional_2 (Bidirection (None, 5, 128) 16512
_________________________________________________________________
activation_2 (Activation) (None, 5, 128) 0
_________________________________________________________________
bidirectional_3 (Bidirection (None, 1024) 656384
_________________________________________________________________
activation_3 (Activation) (None, 1024) 0
_________________________________________________________________
dense_1 (Dense) (None, 1) 1025
_________________________________________________________________
p_re_lu_1 (PReLU) (None, 1) 1
=================================================================
Total params: 676,098
Trainable params: 676,098
Non-trainable …Run Code Online (Sandbox Code Playgroud) python deep-learning keras tensorflow recurrent-neural-network
lstm ×7
keras ×5
python ×5
tensorflow ×4
cntk ×1
forecasting ×1
keras-layer ×1
nlp ×1
python-3.x ×1
pytorch ×1
seq2seq ×1