构造函数(也即\__init__
)的使命是搭建舞台
(该神经网络一共多少层,这些层的输入与输出,激励函数,每一层的属性),构造整个Architecture
,它不负责运算,优化事宜。
import numpy
import theano
from theano.tensor.shared_randomstreams import RandomStreams
numpy_rng = numpy.random.RandomState(23455)
theano_rng = RandomStreams(numpy_rng.randint(2**30))
T.grad(cost, wrt)
第一个参数表示代价函数(是一个符号型函数,而非一个标量),第二个参数(with respect to)表示函数所关于的参数。
def get_cost_update(self, corruption_level, eta):
tilde_x = self.get_corrupted_input(self.x, corruption_level)
y = self.get_hidden_values(tilde_x)
z = self.get_reconstructed_input(y)
L = -T.sum(self.x*T.log(z)+(1-self.x)*T.log(1-z), axis=1)
cost = T.mean(L)
gparams = T.grad(cost, self.params)
# self.params = [self.W, self.b, self.b_prime]
updates = [(param, param-eta*gparam) for param, gparam in zip(self.params, gparams)]
return (cost, updates)
def load_data(dataset):
with gzip.open(dataset, 'rb') as fp:
train_set, valid_set, test_set = pickle(fp, encoding='latin1')
def shared_datd(data_xy, borrow=True):
data_x, data_y = data_xy
shared_x = theano.shared(
numpy.asarray(data_x, dtype=theano.config.floatX), borrow=borrow
)
shared_y = theano.shared(
numpy.asarray(data_y, dtype=theano.config.floatX), borrow=borrow
)
return shared_x, T.cast(shared_y, 'int32')
train_set_x, train_set_y = shared_data(train_set)
valid_set_x, valid_set_y = shared_data(valid_set)
test_set_x, test_set_y = shared_data(test_set)
rval = [(train_set_x, train_set_y), (valid_set_x, valid_set_y), (test_set_x, test_set_y)]
return rval
for i in range(self.nlayers):
if i == 0:
input_size = nins
else:
input_size = hidden_layers_size[i-1]
简单的二分支的判断,自然可以使用python下的三目运算符进行简化,也即:
for i in range(self.nlayers):
input_size = nins if i == 0 else hidden_layers_size[i-1]
这段代码见于SdA
self.sigmoid_layers = []
self.params = []
self.dA_layers = []
for i in range(self.nlayers):
input_size = nins if i == 0 else hidden_layers_size[i-1]
layer_input = self.x if i == 0 else self.sigmoid_layers[-1].output
sigmoid_layer = HiddenLayer(...)
self.sigmoid_layers.append(sigmoid_layer)
# 一段精彩的逻辑
self.params.extend(sigmoid_layer.params)
# self.params += sigmoid_layer.params
dA_layer = dA(...)
self.dA_layers.append(dA_layer)