本文整理汇总了Python中chainer.FunctionSet.to_gpu方法的典型用法代码示例。如果您正苦于以下问题:Python FunctionSet.to_gpu方法的具体用法?Python FunctionSet.to_gpu怎么用?Python FunctionSet.to_gpu使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.FunctionSet
的用法示例。
在下文中一共展示了FunctionSet.to_gpu方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
def main():
if P.use_mean_var:
conv6_output = 126
else:
conv6_output = 128
if P.model_name is None:
model = FunctionSet(
conv1 = F.Convolution2D( 1, 128, 3, stride=1),
conv2 = F.Convolution2D(128, 128, 3, stride=1),
conv3 = F.Convolution2D(128, 128, 3, stride=1),
conv4 = F.Convolution2D(128, 128, 3, stride=1),
conv5 = F.Convolution2D(128, 128, 3, stride=1),
conv6 = F.Convolution2D(128, conv6_output, 3, stride=1),
conv7 = F.Convolution2D(128, 128, 1, stride=1),
conv8 = F.Convolution2D(128, 1, 1, stride=1)
)
if P.gpu >= 0:
cuda.init(P.gpu)
model.to_gpu()
else:
if P.gpu >= 0:
cuda.init(P.gpu)
model = pickle.load(open(os.path.join(P.model_dir, P.model_name), 'rb'))
optimizer = optimizers.MomentumSGD(lr=P.lr, momentum=P.momentum)
optimizer.setup(model.collect_parameters())
train(model, optimizer)
return
示例2: TestFunctionSet
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class TestFunctionSet(TestCase):
def setUp(self):
self.fs = FunctionSet(
a = Linear(3, 2),
b = Linear(3, 2)
)
def test_get_sorted_funcs(self):
self.assertItemsEqual([k for (k, v) in self.fs._get_sorted_funcs()], ('a', 'b'))
def check_equal_fs(self, fs1, fs2):
self.assertTrue((fs1.a.W == fs2.a.W).all())
self.assertTrue((fs1.a.b == fs2.a.b).all())
self.assertTrue((fs1.b.W == fs2.b.W).all())
self.assertTrue((fs1.b.b == fs2.b.b).all())
def test_pickle_cpu(self):
s = pickle.dumps(self.fs)
fs2 = pickle.loads(s)
self.check_equal_fs(self.fs, fs2)
@attr.gpu
def test_pickle_gpu(self):
self.fs.to_gpu()
s = pickle.dumps(self.fs)
fs2 = pickle.loads(s)
self.fs.to_cpu()
fs2.to_cpu()
self.check_equal_fs(self.fs, fs2)
示例3: TestFunctionSet
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class TestFunctionSet(TestCase):
def setUp(self):
self.fs = FunctionSet(
a = Linear(3, 2),
b = Linear(3, 2)
)
def check_equal_fs(self, fs1, fs2):
self.assertTrue((fs1.a.W == fs2.a.W).all())
self.assertTrue((fs1.a.b == fs2.a.b).all())
self.assertTrue((fs1.b.W == fs2.b.W).all())
self.assertTrue((fs1.b.b == fs2.b.b).all())
def test_pickle_cpu(self):
s = pickle.dumps(self.fs)
fs2 = pickle.loads(s)
self.check_equal_fs(self.fs, fs2)
def test_pickle_gpu(self):
self.fs.to_gpu()
s = pickle.dumps(self.fs)
fs2 = pickle.loads(s)
self.fs.to_cpu()
fs2.to_cpu()
self.check_equal_fs(self.fs, fs2)
示例4: __init__
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class FTCS_Y:
def __init__(self):
self.model0 = FunctionSet(l=F.Convolution2D(1, 1, 3, pad=1, nobias=True))
self.model0.l.W[0,0,:,:] = np.array([[0,-1,0],[0,0,0],[0,1,0]]).astype(np.float32)/2
self.model0.to_gpu()
def forward(self, x_data):
y0 = self.model0.l(x_data)
return y0
示例5: ConvolutionalDenoisingAutoencoder
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class ConvolutionalDenoisingAutoencoder():
def __init__(self, imgsize, n_in_channels, n_out_channels, ksize, stride=1, pad=0, use_cuda=False):
self.model = FunctionSet(
encode=F.Convolution2D(n_in_channels, n_out_channels, ksize, stride, pad),
decode=F.Linear(n_out_channels*(math.floor((imgsize+2*pad-ksize)/stride)+1)**2, n_in_channels*imgsize**2)
)
self.use_cuda = use_cuda
if self.use_cuda:
self.model.to_gpu()
self.optimizer = optimizers.Adam()
self.optimizer.setup(self.model.collect_parameters())
def encode(self, x_var):
return F.sigmoid(self.model.encode(x_var))
def decode(self, x_var):
return self.model.decode(x_var)
def predict(self, x_data):
if self.use_cuda:
x_data = cuda.to_gpu(x_data)
x = Variable(x_data)
p = self.encode(x)
if self.use_cuda:
return cuda.to_cpu(p.data)
else:
return p.data
def cost(self, x_data):
x = Variable(x_data)
t = Variable(x_data.reshape(x_data.shape[0], x_data.shape[1]*x_data.shape[2]*x_data.shape[3]))
h = F.dropout(x)
h = self.encode(h)
y = self.decode(h)
return F.mean_squared_error(y, t)
def train(self, x_data):
if self.use_cuda:
x_data = cuda.to_gpu(x_data)
self.optimizer.zero_grads()
loss = self.cost(x_data)
loss.backward()
self.optimizer.update()
if self.use_cuda:
return float(cuda.to_cpu(loss.data))
else:
return loss.data
def test(self, x_data):
if self.use_cuda:
x_data = cuda.to_gpu(x_data)
loss = self.cost(x_data)
return float(cuda.to_cpu(loss.data))
示例6: init_model
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
def init_model(model_params):
wscale1 = model_params.wscale1 # math.sqrt(5 * 5 * 3) * 0.0001
wscale2 = model_params.wscale2 # math.sqrt(5 * 5 * 32) * 0.01
wscale3 = model_params.wscale3 # math.sqrt(5 * 5 * 32) * 0.01
wscale4 = model_params.wscale4 # math.sqrt(576) * 0.1
wscale5 = model_params.wscale5 # math.sqrt(64) * 0.1
# wscale1, wscale2, wscale3, wscale4, wscale5 = [math.sqrt(2)] * 5
model = FunctionSet(conv1=F.Convolution2D(3, 32, 5, wscale=wscale1, stride=1, pad=2),
conv2=F.Convolution2D(32, 32, 5, wscale=wscale2, stride=1, pad=2),
conv3=F.Convolution2D(32, 64, 5, wscale=wscale3, stride=1, pad=2),
fl4=F.Linear(576, 64, wscale=wscale4),
fl5=F.Linear(64, 10, wscale=wscale5))
if params.gpu_flag:
model.to_gpu()
return model
示例7: TestNestedFunctionSet
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class TestNestedFunctionSet(TestCase):
def setUp(self):
self.fs1 = FunctionSet(
a = MockFunction((1, 2)))
self.fs2 = FunctionSet(
fs1 = self.fs1,
b = MockFunction((3, 4)))
def test_get_sorted_funcs(self):
self.assertItemsEqual([k for (k, v) in self.fs2._get_sorted_funcs()], ('b', 'fs1'))
def test_collect_parameters(self):
p_b = np.zeros((3, 4)).astype(np.float32)
p_a = np.zeros((1, 2)).astype(np.float32)
gp_b = np.ones((3, 4)).astype(np.float32)
gp_a = np.ones((1, 2)).astype(np.float32)
actual = self.fs2.collect_parameters()
self.assertTrue(map(len, actual) == [2, 2])
self.assertTrue((actual[0][0] == p_b).all())
self.assertTrue((actual[0][1] == p_a).all())
self.assertTrue((actual[1][0] == gp_b).all())
self.assertTrue((actual[1][1] == gp_a).all())
def test_pickle_cpu(self):
fs2_serialized = pickle.dumps(self.fs2)
fs2_loaded = pickle.loads(fs2_serialized)
self.assertTrue((self.fs2.b.p == fs2_loaded.b.p).all())
self.assertTrue((self.fs2.fs1.a.p == fs2_loaded.fs1.a.p).all())
@attr.gpu
def test_pickle_gpu(self):
self.fs2.to_gpu()
fs2_serialized = pickle.dumps(self.fs2)
fs2_loaded = pickle.loads(fs2_serialized)
fs2_loaded.to_cpu()
self.fs2.to_cpu()
self.assertTrue((self.fs2.b.p == fs2_loaded.b.p).all())
self.assertTrue((self.fs2.fs1.a.p == fs2_loaded.fs1.a.p).all())
示例8: getResult
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
def getResult(self, data, batch_size=100):
"""
入力データをネットワークに与え、結果を取得する。
batch_size は一度にネットワークに投げるデータ数。マシン性能により調整。
"""
self.logger.info("Get result start.")
### Model 設定
model = FunctionSet()
for num, f_layer in enumerate(self.f_layers, 1):
name = "l_f{0}".format(num)
model.__setattr__(name, f_layer)
if self.use_gpu:
model = model.to_gpu()
self.optimizer.setup(model)
### forward 処理設定
def forward(x_data):
x = Variable(x_data)
t = Variable(x_data)
h = x
for num in xrange(1, len(self.f_layers)):
h = self.activation(model.__getitem__("l_f{0}".format(num))(h))
y = model.__getitem__("l_f{0}".format(num + 1))(h)
return y.data
### 結果取得
test_data = data
test_size = len(test_data)
batch_max = int(math.ceil(test_size / float(batch_size)))
y_data = np.zeros((test_size, self.layer_sizes[len(self.layer_sizes) - 1]), dtype=test_data.dtype)
for i in xrange(batch_max):
start = i * batch_size
end = (i + 1) * batch_size
x_batch = test_data[start:end]
self.logger.debug("Index {0} => {1}, data count = {2}".format(start, end, len(x_batch)))
if self.use_gpu:
x_batch = cuda.to_gpu(x_batch)
y_batch = forward(x_batch)
if self.use_gpu:
y_batch = cuda.to_cpu(y_batch)
y_data[start:end] = y_batch
self.logger.info("Complete get result.")
return y_data
示例9: InceptionBN
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class InceptionBN(Function):
"""Inception module in new GoogLeNet with BN."""
def __init__(self, in_channels, out1, proj3, out3, proj33, out33,
pooltype, proj_pool=None, stride=1):
if out1 > 0:
assert stride == 1
assert proj_pool is not None
self.f = FunctionSet(
proj3 = F.Convolution2D(in_channels, proj3, 1, nobias=True),
conv3 = F.Convolution2D( proj3, out3, 3, pad=1, stride=stride, nobias=True),
proj33 = F.Convolution2D(in_channels, proj33, 1, nobias=True),
conv33a = F.Convolution2D( proj33, out33, 3, pad=1, nobias=True),
conv33b = F.Convolution2D( out33, out33, 3, pad=1, stride=stride, nobias=True),
proj3n = F.BatchNormalization(proj3),
conv3n = F.BatchNormalization(out3),
proj33n = F.BatchNormalization(proj33),
conv33an = F.BatchNormalization(out33),
conv33bn = F.BatchNormalization(out33),
)
if out1 > 0:
self.f.conv1 = F.Convolution2D(in_channels, out1, 1, stride=stride, nobias=True)
self.f.conv1n = F.BatchNormalization(out1)
if proj_pool is not None:
self.f.poolp = F.Convolution2D(in_channels, proj_pool, 1, nobias=True)
self.f.poolpn = F.BatchNormalization(proj_pool)
if pooltype == 'max':
self.f.pool = MaxPooling2D(3, stride=stride, pad=1)
elif pooltype == 'avg':
self.f.pool = AveragePooling2D(3, stride=stride, pad=1)
else:
raise NotImplementedError()
def forward(self, x):
f = self.f
self.x = Variable(x[0])
outs = []
if hasattr(f, 'conv1'):
h1 = f.conv1(self.x)
h1 = f.conv1n(h1)
h1 = F.relu(h1)
outs.append(h1)
h3 = F.relu(f.proj3n(f.proj3(self.x)))
h3 = F.relu(f.conv3n(f.conv3(h3)))
outs.append(h3)
h33 = F.relu(f.proj33n(f.proj33(self.x)))
h33 = F.relu(f.conv33an(f.conv33a(h33)))
h33 = F.relu(f.conv33bn(f.conv33b(h33)))
outs.append(h33)
p = f.pool(self.x)
if hasattr(f, 'poolp'):
p = F.relu(f.poolpn(f.poolp(p)))
outs.append(p)
self.y = F.concat(outs, axis=1)
return self.y.data,
def backward(self, x, gy):
self.y.grad = gy[0]
self.y.backward()
return self.x.grad,
def to_gpu(self, device=None):
super(InceptionBN, self).to_gpu(device)
self.f.to_gpu(device)
@property
def parameters(self):
return self.f.parameters
@parameters.setter
def parameters(self, params):
self.f.parameters = params
@property
def gradients(self):
return self.f.gradients
@gradients.setter
def gradients(self, grads):
self.f.gradients = grads
示例10: MLP
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class MLP(Base):
def __init__(self, data=None, target=None, n_inputs=784, n_hidden=784, n_outputs=10, gpu=-1):
self.excludes.append('xp')
self.model = FunctionSet(l1=F.Linear(n_inputs, n_hidden),
l2=F.Linear(n_hidden, n_hidden),
l3=F.Linear(n_hidden, n_outputs))
if gpu >= 0:
self.model.to_gpu()
self.xp = cuda.cupy
else:
self.xp = np
if not data is None:
self.x_train, self.x_test = data
else:
self.x_train, self.y_test = None, None
if not target is None:
self.y_train, self.y_test = target
self.n_train = len(self.y_train)
self.n_test = len(self.y_test)
else:
self.y_train, self.y_test = None, None
self.n_train = 0
self.n_test = 0
self.gpu = gpu
self.optimizer = optimizers.Adam()
self.optimizer.setup(self.model)
def forward(self, x_data, y_data, train=True):
x, t = Variable(x_data), Variable(y_data)
h1 = F.dropout(F.relu(self.model.l1(x)), train=train)
h2 = F.dropout(F.relu(self.model.l2(h1)), train=train)
y = self.model.l3(h2)
return F.softmax_cross_entropy(y, t), F.accuracy(y, t)
def train_and_test(self, n_epoch=20, batchsize=100):
for epoch in xrange(1, n_epoch+1):
print 'epoch', epoch
perm = np.random.permutation(self.n_train)
sum_accuracy = 0
sum_loss = 0
for i in xrange(0, self.n_train, batchsize):
x_batch = self.xp.asarray(self.x_train[perm[i:i+batchsize]])
y_batch = self.xp.asarray(self.y_train[perm[i:i+batchsize]])
real_batchsize = len(x_batch)
self.optimizer.zero_grads()
loss, acc = self.forward(x_batch, y_batch)
loss.backward()
self.optimizer.update()
sum_loss += float(cuda.to_cpu(loss.data)) * real_batchsize
sum_accuracy += float(cuda.to_cpu(acc.data)) * real_batchsize
print 'train mean loss={}, accuracy={}'.format(sum_loss/self.n_train, sum_accuracy/self.n_train)
# evalation
sum_accuracy = 0
sum_loss = 0
for i in xrange(0, self.n_test, batchsize):
x_batch = self.xp.asarray(self.x_test[i:i+batchsize])
y_batch = self.xp.asarray(self.y_test[i:i+batchsize])
real_batchsize = len(x_batch)
loss, acc = self.forward(x_batch, y_batch, train=False)
sum_loss += float(cuda.to_cpu(loss.data)) * real_batchsize
sum_accuracy += float(cuda.to_cpu(acc.data)) * real_batchsize
print 'test mean loss={}, accuracy={}'.format(sum_loss/self.n_test, sum_accuracy/self.n_test)
示例11: SDA
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class SDA(object):
def __init__(
self,
rng,
data,
target,
n_inputs=784,
n_hidden=[784,784,784],
n_outputs=10,
corruption_levels=[0.1,0.2,0.3],
gpu=-1):
self.model = FunctionSet(
l1=F.Linear(n_inputs, n_hidden[0]),
l2=F.Linear(n_hidden[0], n_hidden[1]),
l3=F.Linear(n_hidden[1], n_hidden[2]),
l4=F.Linear(n_hidden[2], n_outputs)
)
if gpu >= 0:
self.model.to_gpu()
self.rng = rng
self.gpu = gpu
self.data = data
self.target = target
self.x_train, self.x_test = data
self.y_train, self.y_test = target
self.n_train = len(self.y_train)
self.n_test = len(self.y_test)
self.corruption_levels = corruption_levels
self.n_inputs = n_inputs
self.n_hidden = n_hidden
self.n_outputs = n_outputs
self.dae1 = None
self.dae2 = None
self.dae3 = None
self.optimizer = None
self.setup_optimizer()
self.train_accuracies = []
self.train_losses = []
self.test_accuracies = []
self.test_losses = []
def setup_optimizer(self):
self.optimizer = optimizers.AdaDelta()
self.optimizer.setup(self.model)
@property
def xp(self):
return cuda.cupy if self.gpu >= 0 else numpy
def pre_train(self, n_epoch=20, batchsize=100):
first_inputs = self.data
# initialize first dAE
self.dae1 = DA(self.rng,
data=first_inputs,
n_inputs=self.n_inputs,
n_hidden=self.n_hidden[0],
corruption_level=self.corruption_levels[0],
gpu=self.gpu)
# train first dAE
logging.info("--------First DA training has started!--------")
self.dae1.train_and_test(n_epoch=n_epoch, batchsize=batchsize)
self.dae1.to_cpu()
# compute second iputs for second dAE
tmp1 = self.dae1.compute_hidden(first_inputs[0])
tmp2 = self.dae1.compute_hidden(first_inputs[1])
if self.gpu >= 0:
self.dae1.to_gpu()
second_inputs = [tmp1, tmp2]
# initialize second dAE
self.dae2 = DA(
self.rng,
data=second_inputs,
n_inputs=self.n_hidden[0],
n_hidden=self.n_hidden[1],
corruption_level=self.corruption_levels[1],
gpu=self.gpu
)
# train second dAE
logging.info("--------Second DA training has started!--------")
self.dae2.train_and_test(n_epoch=n_epoch, batchsize=batchsize)
self.dae2.to_cpu()
# compute third inputs for third dAE
tmp1 = self.dae2.compute_hidden(second_inputs[0])
tmp2 = self.dae2.compute_hidden(second_inputs[1])
if self.gpu >= 0:
self.dae2.to_gpu()
third_inputs = [tmp1, tmp2]
# initialize third dAE
#.........这里部分代码省略.........
示例12: setattr
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
fixed_layer = F.Linear(n_unitslist[layer_id], n_unitslist[layer_id + 1])
fixed_layer.W = encoded_Ws[layer_id]
fixed_layer.b = encoded_bs[layer_id]
setattr(ae_fix_model, "l" + str(layer_id + 1), fixed_layer)
setattr(
ae_model,
"l" + str(training_layer_id + 1),
F.Linear(n_unitslist[training_layer_id], n_unitslist[training_layer_id + 1]),
)
setattr(
ae_model,
"r" + str(training_layer_id + 1),
F.Linear(n_unitslist[training_layer_id + 1], n_unitslist[training_layer_id]),
)
ae_fix_model = ae_fix_model.to_gpu()
ae_model = ae_model.to_gpu()
ae_optimizer = optimizers.Adam()
ae_optimizer.setup(ae_model)
# training
perm = np.random.permutation(n_train_batchset)
sum_loss = 0
for i in range(0, n_train_batchset, batchsize):
x_batch = cuda.to_gpu(x_train[perm[i : i + batchsize]])
ae_optimizer.zero_grads()
x = Variable(x_batch)
acc = x
for layer_id in range(0, training_layer_id):
示例13: MLP
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class MLP(object):
def __init__(
self,
data,
target,
n_inputs=784,
n_hidden=784,
n_outputs=10,
gpu=-1
):
self.model = FunctionSet(
l1=F.Linear(n_inputs, n_hidden),
l2=F.Linear(n_hidden, n_hidden),
l3=F.Linear(n_hidden, n_outputs)
)
if gpu >= 0:
self.model.to_gpu()
self.x_train, self.x_test = data
self.y_train, self.y_test = target
self.n_train = len(self.y_train)
self.n_test = len(self.y_test)
self.gpu = gpu
self.optimizer = optimizers.Adam()
self.optimizer.setup(self.model)
self.train_accuracies = []
self.train_losses = []
self.test_accuracies = []
self.test_losses = []
@property
def xp(self):
return cuda.cupy if self.gpu >= 0 else numpy
def forward(self, x_data, y_data, train=True):
x, t = Variable(x_data), Variable(y_data)
h1 = F.dropout(F.relu(self.model.l1(x)), train=train)
h2 = F.dropout(F.relu(self.model.l2(h1)), train=train)
y = self.model.l3(h2)
return F.softmax_cross_entropy(y, t), F.accuracy(y, t)
def train_and_test(self, n_epoch=20, batchsize=100):
for epoch in xrange(1, n_epoch + 1):
logging.info('epoch {}'.format(epoch))
perm = numpy.random.permutation(self.n_train)
sum_accuracy = 0
sum_loss = 0
for i in xrange(0, self.n_train, batchsize):
x_batch = self.xp.asarray(self.x_train[perm[i:i+batchsize]])
y_batch = self.xp.asarray(self.y_train[perm[i:i+batchsize]])
real_batchsize = len(x_batch)
self.optimizer.zero_grads()
loss, acc = self.forward(x_batch, y_batch)
loss.backward()
self.optimizer.update()
sum_loss += float(cuda.to_cpu(loss.data)) * real_batchsize
sum_accuracy += float(cuda.to_cpu(acc.data)) * real_batchsize
self.train_accuracies.append(sum_accuracy / self.n_train)
self.train_losses.append(sum_loss / self.n_train)
logging.info(
'train mean loss={}, accuracy={}'.format(
sum_loss / self.n_train,
sum_accuracy / self.n_train
)
)
# evalation
sum_accuracy = 0
sum_loss = 0
for i in xrange(0, self.n_test, batchsize):
x_batch = self.xp.asarray(self.x_test[i:i+batchsize])
y_batch = self.xp.asarray(self.y_test[i:i+batchsize])
real_batchsize = len(x_batch)
loss, acc = self.forward(x_batch, y_batch, train=False)
sum_loss += float(cuda.to_cpu(loss.data)) * real_batchsize
sum_accuracy += float(cuda.to_cpu(acc.data)) * real_batchsize
self.test_accuracies.append(sum_accuracy / self.n_test)
self.test_accuracies.append(sum_loss / self.n_test)
logging.info(
'test mean loss={}, accuracy={}'.format(
sum_loss / self.n_test,
sum_accuracy / self.n_test
)
)
示例14: dA
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class dA(BaseEstimator):
"""
Denoising Autoencoder
reference:
http://deeplearning.net/tutorial/dA.html
https://github.com/pfnet/chainer/blob/master/examples/mnist/train_mnist.py
"""
def __init__(self, n_visible, n_hidden, noise_level=0.0, dropout_ratio=0.3,
batch_size=100, n_epoch=20, optimizer=optimizers.Adam(),
activation_func=F.relu, verbose=False, gpu=-1):
self.n_visible = n_visible
self.n_hidden = n_hidden
self.noise_level = noise_level
self.dropout_ratio = dropout_ratio
self.batch_size = batch_size
self.n_epoch = n_epoch
# construct model and setup optimizer
self.model = FunctionSet(
encoder=F.Linear(n_visible, n_hidden),
decoder=F.Linear(n_hidden, n_visible)
)
self.optimizer = optimizer
self.optimizer.setup(self.model)
self.activation_func = activation_func
self.verbose = verbose
# set gpu
self.gpu = gpu
if self.gpu >= 0:
cuda.get_device(self.gpu).use()
self.model.to_gpu()
def fit(self, X):
xp = cuda.cupy if self.gpu >= 0 else np
for epoch in range(self.n_epoch):
utils.disp('epoch: {}'.format(epoch + 1), self.verbose)
perm = np.random.permutation(len(X))
sum_loss = 0
for i in range(0, len(X), self.batch_size):
X_batch = xp.asarray(X[perm[i: i + self.batch_size]])
loss = self._fit(X_batch)
sum_loss += float(loss.data) * len(X_batch)
utils.disp('train mean loss={}'.format(sum_loss / len(X)), self.verbose)
return self
def _fit(self, X):
self.optimizer.zero_grads()
y_var = self._forward(X, train=True)
loss = F.mean_squared_error(y_var, Variable(X.copy()))
loss.backward()
self.optimizer.update()
return loss
def _forward(self, X, train):
X_var = Variable(dA.add_noise(X, self.noise_level, train))
h1 = self.encode(X_var, train)
y_var = self.model.decoder(h1)
return y_var
def predict(self, X):
return self._forward(X, train=False).data
@property
def encoder(self):
return self.model.encoder
def encode(self, X_var, train):
return F.dropout(self.activation_func(self.encoder(X_var)), ratio=self.dropout_ratio, train=train)
@staticmethod
def add_noise(X, noise_level, train):
return (np.random.binomial(size=X.shape, n=1, p=1 - (noise_level if train else 0.0)) * X).astype(np.float32)
示例15: Replay
# 需要导入模块: from chainer import FunctionSet [as 别名]
# 或者: from chainer.FunctionSet import to_gpu [as 别名]
class DQN_class:
# Hyper-Parameters
gamma = 0.99 # Discount factor
initial_exploration = 100#10**4 # Initial exploratoin. original: 5x10^4
replay_size = 32 # Replay (batch) size
target_model_update_freq = 10**4 # Target update frequancy. original: 10^4
data_size = 10**5 #10**5 # Data size of history. original: 10^6
def __init__(self, enable_controller=[0, 3, 4]):
self.num_of_actions = len(enable_controller)
self.enable_controller = enable_controller # Default setting : "Pong"
print "Initializing DQN..."
print "Model Building"
self.CNN_model = FunctionSet(
l1=F.Convolution2D(4, 32, ksize=8, stride=4, nobias=False, wscale=np.sqrt(2)),
l2=F.Convolution2D(32, 64, ksize=4, stride=2, nobias=False, wscale=np.sqrt(2)),
l3=F.Convolution2D(64, 64, ksize=3, stride=1, nobias=False, wscale=np.sqrt(2)),
)
self.model = FunctionSet(
l4=F.Linear(3136, 512, wscale=np.sqrt(2)),
q_value=F.Linear(512, self.num_of_actions,
initialW=np.zeros((self.num_of_actions, 512),
dtype=np.float32))
).to_gpu()
d = 'elite/'
self.CNN_model.l1.W.data = np.load(d+'l1_W.npy')#.astype(np.float32)
self.CNN_model.l1.b.data = np.load(d+'l1_b.npy')#.astype(np.float32)
self.CNN_model.l2.W.data = np.load(d+'l2_W.npy')#.astype(np.float32)
self.CNN_model.l2.b.data = np.load(d+'l2_b.npy')#.astype(np.float32)
self.CNN_model.l3.W.data = np.load(d+'l3_W.npy')#.astype(np.float32)
self.CNN_model.l3.b.data = np.load(d+'l3_b.npy')#.astype(np.float32)
self.CNN_model = self.CNN_model.to_gpu()
self.CNN_model_target = copy.deepcopy(self.CNN_model)
self.model_target = copy.deepcopy(self.model)
print "Initizlizing Optimizer"
self.optimizer = optimizers.RMSpropGraves(lr=0.00025, alpha=0.95, momentum=0.95, eps=0.0001)
self.optimizer.setup(self.model.collect_parameters())
# History Data : D=[s, a, r, s_dash, end_episode_flag]
self.D = [np.zeros((self.data_size, 4, 84, 84), dtype=np.uint8),
np.zeros(self.data_size, dtype=np.uint8),
np.zeros((self.data_size, 1), dtype=np.int8),
np.zeros((self.data_size, 4, 84, 84), dtype=np.uint8),
np.zeros((self.data_size, 1), dtype=np.bool),
np.zeros((self.data_size, 1), dtype=np.uint8)]
def forward(self, state, action, Reward, state_dash, episode_end):
num_of_batch = state.shape[0]
s = Variable(state)
s_dash = Variable(state_dash)
Q = self.Q_func(s) # Get Q-value
# Generate Target Signals
tmp = self.Q_func_target(s_dash) # Q(s',*)
tmp = list(map(np.max, tmp.data.get())) # max_a Q(s',a)
max_Q_dash = np.asanyarray(tmp, dtype=np.float32)
target = np.asanyarray(Q.data.get(), dtype=np.float32)
for i in xrange(num_of_batch):
if not episode_end[i][0]:
tmp_ = np.sign(Reward[i]) + self.gamma * max_Q_dash[i]
else:
tmp_ = np.sign(Reward[i])
action_index = self.action_to_index(action[i])
target[i, action_index] = tmp_
# TD-error clipping
td = Variable(cuda.to_gpu(target)) - Q # TD error
td_tmp = td.data + 1000.0 * (abs(td.data) <= 1) # Avoid zero division
td_clip = td * (abs(td.data) <= 1) + td/abs(td_tmp) * (abs(td.data) > 1)
zero_val = Variable(cuda.to_gpu(np.zeros((self.replay_size, self.num_of_actions), dtype=np.float32)))
loss = F.mean_squared_error(td_clip, zero_val)
return loss, Q
def stockExperience(self, time,
state, action, lstm_reward, state_dash,
episode_end_flag, ale_reward):
data_index = time % self.data_size
if episode_end_flag is True:
self.D[0][data_index] = state
self.D[1][data_index] = action
self.D[2][data_index] = lstm_reward
self.D[5][data_index] = ale_reward
else:
self.D[0][data_index] = state
#.........这里部分代码省略.........