# OpenAI开发可拓展元学习算法Reptile，能快速学习

MAML元学习算法：

http://bair.berkeley.edu/blog/2017/07/18/learning-to-learn/

Reptile的工作原理

Reptile要求k>1，也就是说，参数更新要依赖于损失函数的高阶导数实现，此时算法的表现和k=1（联合训练）时是完全不同的。

https://en.wikipedia.org/wiki/Taylor_series

Omniglot：

https://github.com/brendenlake/omniglot

Mini-ImageNet：

https://arxiv.org/abs/1606.04080

X坐标：外循环迭代次数

Y坐标:Omniglot对比5种方式的

5次分类的准确度

GitHub：

https://github.com/openai/supervised-reptile

https://github.com/openai/supervised-reptile/tree/master/web

importnumpy as np

importtorch

from torchimportnn, autograd as ag

importmatplotlib.pyplot as plt

from copyimportdeepcopy

seed =

plot = True

innerstepsize =0.02# stepsize in inner SGD

innerepochs =1# number of epochs of each inner SGD

outerstepsize0 =0.1# stepsize of outer optimization, i.e., meta-optimization

niterations =30000# number of outer updates; each iteration we sample one task and update on it

rng = np.random.RandomState(seed)

torch.manual_seed(seed)

# Define task distribution

x_all = np.linspace(-5,5,50)[:,None] # All of the x points

ntrain =10# Size of training minibatches

"Generate classification problem"

phase = rng.uniform(low=, high=2*np.pi)

ampl = rng.uniform(0.1,5)

f_randomsine = lambda x : np.sin(x + phase) * ampl

returnf_randomsine

# Define model. Reptile paper uses ReLU, but Tanh gives slightly better results

model = nn.Sequential(

nn.Linear(1,64),

nn.Tanh(),

nn.Linear(64,64),

nn.Tanh(),

nn.Linear(64,1),

)

def totorch(x):

returnag.Variable(torch.Tensor(x))

def train_on_batch(x, y):

x = totorch(x)

y = totorch(y)

ypred = model(x)

loss = (ypred - y).pow(2).mean()

loss.backward()

forparam in model.parameters():

param.data -= innerstepsize * param.grad.data

def predict(x):

x = totorch(x)

returnmodel(x).data.numpy()

# Choose a fixed task and minibatchforvisualization

xtrain_plot = x_all[rng.choice(len(x_all), size=ntrain)]

# Reptile training loop

foriteration in range(niterations):

weights_before = deepcopy(model.state_dict())

y_all = f(x_all)

# Do SGD onthistask

inds = rng.permutation(len(x_all))

for_ in range(innerepochs):

forstart in range(, len(x_all), ntrain):

mbinds = inds[start:start+ntrain]

train_on_batch(x_all[mbinds], y_all[mbinds])

# Interpolate between current weights and trained weights fromthistask

# I.e. (weights_before - weights_after) is the meta-gradient

weights_after = model.state_dict()

outerstepsize = outerstepsize0 * (1- iteration / niterations) # linear schedule

# Periodically plot the results on a particular task and minibatch

ifplot and iteration==or (iteration+1) %1000==:

plt.cla()

f = f_plot

weights_before = deepcopy(model.state_dict()) # save snapshot before evaluation

plt.plot(x_all, predict(x_all), label="pred after 0", color=(,,1))

forinneriter in range(32):

train_on_batch(xtrain_plot, f(xtrain_plot))

if(inneriter+1) %8==:

frac = (inneriter+1) /32

plt.plot(x_all, predict(x_all), label="pred after %i"%(inneriter+1), color=(frac,,1-frac))

plt.plot(x_all, f(x_all), label="true", color=(,1,))

lossval = np.square(predict(x_all) - f(x_all)).mean()

plt.plot(xtrain_plot, f(xtrain_plot),"x", label="train", color="k")

plt.ylim(-4,4)

plt.legend(loc="lower right")

plt.pause(0.01)

model.load_state_dict(weights_before) # restore from snapshot

print(f"-----------------------------")

print(f"iteration ")

print(f"loss on plotted curve ") # would be better to average loss over asetof examples, butthisis optimizedforbrevity

https://arxiv.org/abs/1803.02999

https://github.com/openai/supervised-reptile

https://blog.openai.com/reptile/

【今日机器学习概念】

Have a Great Definition

• 发表于:
• 原文链接http://kuaibao.qq.com/s/20180411A0UQBF00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2022-05-18

2022-05-18

2022-05-18

2018-04-04

2022-05-18

2018-03-26

2018-07-04

2018-04-26

2018-03-29

2022-05-18

2022-05-18

2022-05-18