本文整理汇总了Python中chainer.FunctionSet.encoder方法的典型用法代码示例。如果您正苦于以下问题:Python FunctionSet.encoder方法的具体用法?Python FunctionSet.encoder怎么用?Python FunctionSet.encoder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.FunctionSet
的用法示例。
在下文中一共展示了FunctionSet.encoder方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import encoder [as 别名]
class DenoisingAutoencoder:
def __init__(
self,
n_input,
n_hidden,
tied=True,
noise=None,
ratio=None,
optimizer=optimizers.Adam(),
loss_function=F.sigmoid_cross_entropy,
activation_function=F.sigmoid,
):
self.model = FunctionSet(encoder=F.Linear(n_input, n_hidden), decoder=F.Linear(n_hidden, n_input))
if tied:
self.model.decoder.W = self.model.encoder.W.T
self.noise = noise
self.ratio = ratio
self.optimizer = optimizer
self.optimizer.setup(self.model.collect_parameters())
self.loss_function = loss_function
self.activation_function = activation_function
def train(self, x_data):
self.optimizer.zero_grads()
loss = self.autoencode(x_data, train=True)
loss.backward()
self.optimizer.update()
return loss
def test(self, x_data):
return self.autoencode(x_data, train=False)
def autoencode(self, x_data, train=True):
x = Variable(x_data)
if self.noise and train:
nx = Variable(self.noise.noise(x_data))
else:
nx = Variable(x_data)
if self.ratio:
h = F.dropout(self.encode(nx), ratio=self.ratio, train=train)
else:
h = self.encode(nx)
y = self.decode(h)
return self.loss_function(y, x)
def encode(self, x):
return self.activation_function(self.model.encoder(x))
def decode(self, x):
return self.activation_function(self.model.decoder(x))
示例2: DA
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import encoder [as 别名]
class DA(object):
def __init__(
self,
rng,
data,
n_inputs=784,
n_hidden=784,
corruption_level=0.3,
optimizer=optimizers.AdaDelta,
gpu=-1
):
"""
Denoising AutoEncoder
data: data for train
n_inputs: a number of units of input layer and output layer
n_hidden: a number of units of hidden layer
corruption_level: a ratio of masking noise
"""
self.model = FunctionSet(
encoder=F.Linear(n_inputs, n_hidden),
decoder=F.Linear(n_hidden, n_inputs)
)
if gpu >= 0:
self.model.to_gpu()
self.gpu = gpu
self.x_train, self.x_test = data
self.n_train = len(self.x_train)
self.n_test = len(self.x_test)
self.n_inputs = n_inputs
self.n_hidden = n_hidden
self.optimizer = optimizer()
self.optimizer.setup(self.model)
self.corruption_level = corruption_level
self.rng = rng
self.train_losses = []
self.test_losses = []
@property
def xp(self):
return cuda.cupy if self.gpu >= 0 else numpy
def forward(self, x_data, train=True):
y_data = x_data
# add noise (masking noise)
x_data = self.get_corrupted_inputs(x_data, train=train)
x, t = Variable(x_data), Variable(y_data)
# encode
h = self.encode(x)
# decode
y = self.decode(h)
# compute loss
loss = F.mean_squared_error(y, t)
return loss
def compute_hidden(self, x_data):
# x_data = self.xp.asarray(x_data)
x = Variable(x_data)
h = self.encode(x)
# return cuda.to_cpu(h.data)
return h.data
def predict(self, x_data):
x = Variable(x_data)
# encode
h = self.encode(x)
# decode
y = self.decode(h)
return cuda.to_cpu(y.data)
def encode(self, x):
return F.relu(self.model.encoder(x))
def decode(self, h):
return F.relu(self.model.decoder(h))
def encoder(self):
initialW = self.model.encoder.W
initial_bias = self.model.encoder.b
return F.Linear(self.n_inputs,
self.n_hidden,
initialW=initialW,
initial_bias=initial_bias)
def decoder(self):
return self.model.decoder
def to_cpu(self):
self.model.to_cpu()
self.xp = np
#.........这里部分代码省略.........