日记大全

日记大全 > 句子大全

想脱离作业苦海(用AI来替你完成论文吧)

句子大全 2023-05-23 06:59:01
相关推荐

你最讨厌哪门课程?有没有想过,用AI来拯救你最讨厌课程的作业?比如,替你写论文?看完本文,你可能会掌握这一技能哦。

RNN

神经网络一直在努力解决的一个问题是顺序数据和时态数据。传统的神经网络需要具有固定的输入和输出大小。

就像你看到一个关于有人从楼梯上掉下来的视频,你想训练一个神经网络来分类视频中发生的事情。一个普通的神经网络可能会告诉那个人站在视频的第一帧。然后它可能能够看到它们之间的帧,直到它到达视频的最后一帧并且它看到一个已经跌倒的人。但是一旦它到达那里,人们就会忘记这个人是否站在了第一位。

回归神经网络因其工作原理而有助于解决这类问题。在较高的层面上,RNN通过相互循环来工作。神经网络执行前向传递然后在第二次前向传递中,它从第一次迭代中获取一些信息以获得更多上下文以进行第二次预测。

显示“展开的”RNN图表,其中信息在迭代之间传输。

从一开始的信息基本上可以在整个网络中传输。现在我们的网络可以理解是否有人真的摔倒在了楼梯!

RNN的类型

RNN很酷,因为它们不需要固定的输入或输出,可以接受输入并产生序列的输出。

1. 一对一:这基本上是一个常规的神经网络。它采用固定输入并提供固定输出。

2. 一对多:这种RNN接收一个输入并提供多个输出。你可以做的是将第一个输出反馈回神经网络以生成另一个输出,将结果反馈回神经网络等。这称为对神经网络进行采样,通过这样做,你可以生成全新的序列。

3. 多对一:RNN接收多个输入并提供单个输出。这用于情感分析等应用程序,你可以在其中为神经网络提供一段文本并预测其情绪或情绪。输出序列可以是句子中的单词,输出可以是神经网络对情绪的预测。

4. 多对多:这种类型的RNN采用多个输入序列并产生多个输出。实际上有两种类型的多对多RNN。当两个序列不一定具有相同长度时,使用第一种类型。对于机器翻译等应用,输入可以是英语或任何其他语言的句子,输出可以是法语或其他语言的句子。在这种情况下,输入和输出的字数可能不同,使得这种类型的多对多网络变得有用。输入和输出同步时使用第二种类型。如果我们标记视频的每一帧,我们可以输入一个帧,输出一个标签,然后继续浏览视频的其余部分。

RNN的问题

到目前为止,RNN似乎很完美,对吧?我们使用这种新型网络以比传统网络更好的方式操作数据序列。但RNN的一个问题是消失/爆炸梯度问题。

消失梯度的图像结果

当你使用反向传播更新模型并计算损失的梯度(模型有多么错误)时,渐变越来越小,在网络中得到的距离越远。这基本上意味着网络中的层数越多,培训效率就越低。爆炸梯度基本上是相反的,如果梯度非常大,它反向传播就像雪崩一样,并且由于RNN经历了许多序列和迭代,因此存在消失/爆炸梯度的问题。

长期短期记忆

该问题的一个解决方案是使用LSTM,长短期存储器单元。它包含一系列数学公式,可帮助RNN解决消失的梯度问题,并使预测更准确。在使用LSTM时,请考虑有四条信息:长期记忆、短期记忆、事件和输出。LSTM单元基于事件提供输出,并在进行预测时考虑长期记忆和短期记忆。

从概念上讲,LSTM包含四个门:遗忘之门,学习之门,记忆之门和使用之门。长期记忆被传递到忘记之门,忘记不需要的信息,短期记忆和事件进入学习之门,保存有用信息。LTM,STM和事件加入记忆之门,然后存储在更新的LTM中,并且三条信息也传递到使用之门,在那里进行预测(STM)。

当然,这都是超级简化的。但主要的问题应该是LSTM在使用RNN时非常有用。

运用RNN撰写论文

现在我们对RNN的工作方式有了基本的了解,让我们回到最初的问题:如何从你不喜欢的作业中解脱出来?

我们可以通过使用一对多RNN来实现这一目标。可以在一堆不同的论文上训练神经网络,并在模型上进行采样,以生成以前从未见过的新论文!

让我们使用Keras,一个用于开发深度学习模型的高级API。这遵循Keras Github repo中的示例代码,用于使用LSTM生成文本。

我们将首先导入所有必需的库和模块。

from keras.callbacks importLambdaCallback

from keras.models importSequential

from keras.layers import Dense

from keras.layers import LSTM

from keras.optimizers importAdam

from keras.utils.data_utilsimport get_file

import numpy as np

import random

import sys

import 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 characters

maxlen = 40

step = 3

sentences = []

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中的消失梯度问题,并且还用于连接神经网络中的长期信息。

阅读剩余内容
网友评论
相关内容
拓展阅读
最近更新