你最讨厌哪门课程?有没有想过,用AI来拯救你最讨厌课程的作业?好比,替你写论文?看完本文,你可能会掌握这一技术哦。RNN神经网络一直在努力解决的一个问题是顺序数据和时态数据。传统的神经网络需要具有牢固的输入和输出巨细。
就像你看到一个关于有人从楼梯上掉下来的视频,你想训练一个神经网络来分类视频中发生的事情。一个普通的神经网络可能会告诉谁人人站在视频的第一帧。然后它可能能够看到它们之间的帧,直到它到达视频的最后一帧而且它看到一个已经跌倒的人。
可是一旦它到达那里,人们就会忘记这小我私家是否站在了第一位。回归神经网络因其事情原理而有助于解决这类问题。在较高的层面上,RNN通过相互循环来事情。
神经网络执行前向通报然后在第二次前向通报中,它从第一次迭代中获取一些信息以获得更多上下文以举行第二次预测。显示“展开的”RNN图表,其中信息在迭代之间传输。从一开始的信息基本上可以在整个网络中传输。
现在我们的网络可以明白是否有人真的摔倒在了楼梯!RNN的类型RNN很酷,因为它们不需要牢固的输入或输出,可以接受输入并发生序列的输出。1. 一对一:这基本上是一个通例的神经网络。它接纳牢固输入并提供牢固输出。2. 一对多:这种RNN吸收一个输入并提供多个输出。
你可以做的是将第一个输出反馈回神经网络以生成另一个输出,将效果反馈回神经网络等。这称为对神经网络举行采样,通过这样做,你可以生玉成新的序列。
3. 多对一:RNN吸收多个输入并提供单个输出。这用于情感分析等应用法式,你可以在其中为神经网络提供一段文本并预测其情绪或情绪。输出序列可以是句子中的单词,输出可以是神经网络对情绪的预测。
4. 多对多:这种类型的RNN接纳多个输入序列并发生多个输出。实际上有两种类型的多对多RNN。
当两个序列纷歧定具有相同长度时,使用第一种类型。对于机械翻译等应用,输入可以是英语或任何其他语言的句子,输出可以是法语或其他语言的句子。在这种情况下,输入和输出的字数可能差别,使得这种类型的多对多网络变得有用。
输入和输出同步时使用第二种类型。如果我们标志视频的每一帧,我们可以输入一个帧,输出一个标签,然后继续浏览视频的其余部门。RNN的问题到现在为止,RNN似乎很完美,对吧?我们使用这种新型网络以比传统网络更好的方式操作数据序列。
但RNN的一个问题是消失/爆炸梯度问题。消失梯度的图像效果当你使用反向流传更新模型并盘算损失的梯度(模型有何等错误)时,渐变越来越小,在网络中获得的距离越远。这基本上意味着网络中的层数越多,培训效率就越低。爆炸梯度基本上是相反的,如果梯度很是大,它反向流传就像雪崩一样,而且由于RNN履历了许多序列和迭代,因此存在消失/爆炸梯度的问题。
恒久短期影象该问题的一个解决方案是使用LSTM,是非期存储器单元。它包罗一系列数学公式,可资助RNN解决消失的梯度问题,并使预测更准确。在使用LSTM时,请思量有四条信息:恒久影象、短期影象、事件和输出。
LSTM单元基于事件提供输出,并在举行预测时思量恒久影象和短期影象。该图显示了如何在LSTM单元中更新信息的示例从观点上讲,LSTM包罗四个门:遗忘之门,学习之门,影象之门和使用之门。恒久影象被通报到忘记之门,忘记不需要的信息,短期影象和事件进入学习之门,生存有用信息。
LTM,STM和事件加入影象之门,然后存储在更新的LTM中,而且三条信息也通报到使用之门,在那里举行预测(STM)。显示出LSTM单元中的栅极的结构和部署的图固然,这都是超级简化的。
但主要的问题应该是LSTM在使用RNN时很是有用。运用RNN撰写论文现在我们对RNN的事情方式有了基本的相识,让我们回到最初的问题:如何从你不喜欢的作业中解脱出来?我们可以通过使用一对多RNN来实现这一目的。可以在一堆差别的论文上训练神经网络,并在模型上举行采样,以生成以前从未见过的新论文!让我们使用Keras,一个用于开发深度学习模型的高级API。这遵循Keras Github repo中的示例代码,用于使用LSTM生成文本。
我们将首先导入所有必须的库和模块。from keras.callbacks importLambdaCallbackfrom keras.models importSequentialfrom keras.layers import Densefrom keras.layers import LSTMfrom keras.optimizers importAdamfrom keras.utils.data_utilsimport get_fileimport numpy as npimport randomimport sysimport io任何文本都可以使用。为了举例,我们将使用尼采的著作。
导入文本文件,举行一些预处置惩罚,并对值举行矢量化。path = get_file( 'nietzsche.txt', origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt')with io.open(path,encoding='utf-8') as f: text = f.read().lower()print('corpus length:',len(text)) chars = sorted(list(set(text)))print('total chars:', len(chars))char_indices = dict((c, i) fori, c in enumerate(chars))indices_char = dict((i, c) fori, c in enumerate(chars)) # cut the text in semi-redundantsequences of maxlen charactersmaxlen = 40step = 3sentences = []next_chars = []for i in range(0, len(text) -maxlen, step): sentences.append(text[i: i + maxlen]) next_chars.append(text[i + maxlen])print('nb sequences:',len(sentences)) print('Vectorization...')x = np.zeros((len(sentences),maxlen, len(chars)), dtype=np.bool)y = np.zeros((len(sentences),len(chars)), dtype=np.bool)for i, sentence inenumerate(sentences): for t, char in enumerate(sentence): x[i, t, char_indices[char]] = 1 y[i, char_indices[next_chars[i]]] = 1现在是实际操作模型的时候了。它由单个LSTM组成,并使用Adam优化器。model = Sequential()model.add(LSTM(128,input_shape=(maxlen, len(chars))))model.add(Dense(len(chars),activation='softmax')) optimizer = Adam(lr=0.01)model.compile(loss='categorical_crossentropy',optimizer=optimizer)现在界说两个函数,样本(sample):一个从概率数组中采样索引的辅助函数,以及在每个样本的末端处(on_epoch_end):这是一个在每个纪元竣事时挪用的函数,并打印由模型生成的文本。
def sample(preds,temperature=1.0): preds = np.asarray(preds).astype('float64') preds = np.log(preds) / temperature exp_preds = np.exp(preds) preds = exp_preds / np.sum(exp_preds) probas = np.random.multinomial(1, preds, 1) return np.argmax(probas) def on_epoch_end(epoch, _): print() print('----- Generating text after Epoch:%d' % epoch) start_index = random.randint(0, len(text) -maxlen - 1) for diversity in [0.2, 0.5, 1.0, 1.2]: print('----- diversity:', diversity) generated = '' sentence = text[start_index:start_index + maxlen] generated += sentence print('----- Generating with seed:"' + sentence + '"') sys.stdout.write(generated) for i in range(400): x_pred = np.zeros((1, maxlen,len(chars))) for t, char in enumerate(sentence): x_pred[0, t,char_indices[char]] = 1. preds = model.predict(x_pred,verbose=0)[0] next_index = sample(preds,diversity) next_char =indices_char[next_index] generated += next_char sentence = sentence[1:] + next_char sys.stdout.write(next_char) sys.stdout.flush() print()我们快乐成了!最后,我们将编写回调函数,我们将在其中看到文本输出并使模型适合数据。print_callback = LambdaCallback(on_epoch_end=on_epoch_end) model.fit(x, y, batch_size=128, epochs=60, callbacks=[print_callback])就是这样。在电脑上训练所有这些花了约莫三个小时,你还可以使用GPU运行时在Google Colab上训练模型。
从理论上讲,生成的文本应该在约莫30个时期之后开始连贯,所以让我们看一下模型的输出。“patienceare first developed—our sense, that the world of theprocess as in the same form to the reverence, and the master of the synthesionand the same tempo of the sensues it and and as the all tame and the except ofthe free of the contradition of existe not that the morality and want of theplace to all man of the tempope” of all moral exclive the soul to the good hasalways to his stands has of the chrison to danger of the super” “could regard even the emotions of hatred, ordo the conduct, and an instances of the has one wele be enterer of the state ofthe religion in the soul, the represens and according and can be the feelingsof the above all religions and a struggle of the senses well entiblent standsand can all suffett of the conduct of the soutes of his subject, and dependersfor the religion that is not as a feelings of whom suphriated the sense of thest”嗯......这个输入效果看起来并不是特别好。但这些都是由神经网络发生的,这不是很酷吗?毫无疑问,通过更好的架构和更广泛的数据,你可以获得可能与人类写的内容不分上下的效果。关键结论自然语言处置惩罚现在正在取得令人敬畏的突破,文本生成实际上是很是有趣的。
你应该记着以下几个关键要点:1.递归神经网络与传统神经网络的差别之处在于它们能够保留先前迭代的数据以举行更好的预测。2.有许多差别类型的RNN——包罗一对一,一对多,多对一,多对多,每种都适合特定任务。
3.是非期影象(LSTM)门用于解决RNN中的消失梯度问题,而且还用于毗连神经网络中的恒久信息。
本文关键词:想,脱离,作业,苦海,用,来,替你,完成,论文,吧,亚搏体育官网入口
本文来源:亚搏体育官网-www.nbmeifu.com