我正在尝试构建一个小型 LSTM,它可以通过在现有 Python 代码上进行训练来学习编写代码(即使是垃圾代码)。我已将数百个文件中的数千行代码连接到一个文件中,每个文件以<eos>
表示“序列结束”。
例如,我的训练文件如下所示:
setup(name='Keras',
...
],
packages=find_packages())
<eos>
import pyux
...
with open('api.json', 'w') as f:
json.dump(sign, f)
<eos>
我正在用以下单词创建令牌:
file = open(self.textfile, 'r')
filecontents = file.read()
file.close()
filecontents = filecontents.replace("\n\n", "\n")
filecontents = filecontents.replace('\n', ' \n ')
filecontents = filecontents.replace(' ', ' \t ')
text_in_words = [w for w in filecontents.split(' ') if w != '']
self._words = set(text_in_words)
STEP = 1
self._codelines = []
self._next_words = []
for i in range(0, len(text_in_words) - self.seq_length, STEP):
self._codelines.append(text_in_words[i: i + self.seq_length])
self._next_words.append(text_in_words[i + self.seq_length])
My keras
型号是:
model = Sequential()
model.add(Embedding(input_dim=len(self._words), output_dim=1024))
model.add(Bidirectional(
LSTM(128), input_shape=(self.seq_length, len(self._words))))
model.add(Dropout(rate=0.5))
model.add(Dense(len(self._words)))
model.add(Activation('softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer="adam", metrics=['accuracy'])
但无论我如何训练它,该模型似乎永远不会生成<eos>
甚至\n
。我想这可能是因为我的 LSTM 大小是128
and my seq_length
是 200,但这不太有意义?我有什么遗漏的吗?