def __init__(self, n_inputs):
"""Constructs a net with a given number of inputs and no layers."""
assert isposint(n_inputs), 'Number of inputs must be a positive integer.'
self.n_inputs = n_inputs
self.n_outputs = n_inputs
self.n_units = [n_inputs]
self.n_layers = 0
self.n_params = 0
self.Ws = []
self.bs = []
self.hs = [tt.matrix('x')]
self.parms = self.Ws + self.bs
self.input = self.hs[0]
self.output = self.hs[-1]
self.eval_f = None
python类matrix()的实例源码
def squareError(x):
"""Square error loss function."""
if x.ndim == 1:
y = tt.vector('y')
L = tt.mean((x - y) ** 2)
elif x.ndim == 2:
y = tt.matrix('y')
L = tt.mean(tt.sum((x - y) ** 2, axis=1))
else:
raise ValueError('x must be either a vector or a matrix.')
L.name = 'loss'
return y, L
def crossEntropy(x):
"""Cross entropy loss function. Only works for networks with one output."""
if x.ndim == 1:
pass
elif x.ndim == 2:
x = x[:, 0]
else:
raise ValueError('x must be either a vector or a matrix.')
y = tt.vector('y')
L = -tt.mean(y * tt.log(x) + (1-y) * tt.log(1-x))
L.name = 'loss'
return y, L
def savetofile(self, outfile):
"""Save model parameters to file."""
# Pickle non-matrix params into bytestring, then convert to numpy byte array
pklbytes = pickle.dumps({'hyper': self.hyper, 'epoch': self.epoch, 'pos': self.pos},
protocol=pickle.HIGHEST_PROTOCOL)
p = np.fromstring(pklbytes, dtype=np.uint8)
# Gather parameter matrices and names
pvalues = { n:m.get_value() for n, m in self.params.items() }
# Now save params and matrices to file
try:
np.savez_compressed(outfile, p=p, **pvalues)
except OSError as e:
raise e
else:
if isinstance(outfile, str):
stdout.write("Saved model parameters to {0}\n".format(outfile))
def load_data(dataset):
print('... loading data')
# Load the dataset
with gzip.open(dataset, 'rb') as f:
try:
train_set, valid_set, test_set = pickle.load(f, encoding='latin1')
except:
train_set, valid_set, test_set = pickle.load(f)
# train_set, valid_set, test_set format: tuple(input, target)
# input is a numpy.ndarray of 2 dimensions (a matrix)
# where each row corresponds to an example. target is a
# numpy.ndarray of 1 dimension (vector) that has the same length as
# the number of rows in the input. It should give the target
# to the example with the same index in the input.
test_set_x, test_set_y = shared_dataset(test_set)
valid_set_x, valid_set_y = shared_dataset(valid_set)
train_set_x, train_set_y = shared_dataset(train_set)
rval = [(train_set_x, train_set_y), (valid_set_x, valid_set_y),
(test_set_x, test_set_y)]
return rval
def times_diag(input, n_hidden, diag, swap_re_im):
# input is a Ix2n_hidden matrix, where I is number
# of training examples
# diag is a n_hidden-dimensional real vector, which creates
# the 2n_hidden x 2n_hidden complex diagonal matrix using
# e.^{j.*diag}=cos(diag)+j.*sin(diag)
d = T.concatenate([diag, -diag]) #d is 2n_hidden
Re = T.cos(d).dimshuffle('x',0)
Im = T.sin(d).dimshuffle('x',0)
input_times_Re = input * Re
input_times_Im = input * Im
output = input_times_Re + input_times_Im[:, swap_re_im]
return output
def times_unitary(x,n,swap_re_im,Wparams,Wimpl):
# multiply tensor x on the right by the unitary matrix W parameterized by Wparams
if (Wimpl == 'adhoc'):
theta=Wparams[0]
reflection=Wparams[1]
index_permute_long=Wparams[2]
step1 = times_diag(x, n, theta[0,:], swap_re_im)
step2 = do_fft(step1, n)
step3 = times_reflection(step2, n, reflection[0,:])
step4 = vec_permutation(step3, index_permute_long)
step5 = times_diag(step4, n, theta[1,:], swap_re_im)
step6 = do_ifft(step5, n)
step7 = times_reflection(step6, n, reflection[1,:])
step8 = times_diag(step7, n, theta[2,:], swap_re_im)
y = step8
elif (Wimpl == 'full'):
Waug=Wparams[0]
y = T.dot(x,Waug)
return y
def make_idx_data_cv(revs, word_idx_map, cv, max_l=51, k=100, filter_h=5):
"""
Transforms sentences into a 2-d matrix.
"""
train, test = [], []
for rev in revs:
sent = get_idx_from_sent(rev["text"], word_idx_map, max_l, k, filter_h)
sent.append(rev["y"])
if rev["split"]==cv:
test.append(sent)
else:
train.append(sent)
train = np.array(train,dtype="int")
train = np.random.permutation(train)[:10000]
test = np.array(test,dtype="int")
test = np.random.permutation(test)[:10000]
return [train, test]
def word_features(table):
"""
Extract word features into a normalized matrix
"""
features = numpy.zeros((len(table), 620), dtype='float32')
keys = table.keys()
for i in range(len(table)):
f = table[keys[i]]
features[i] = f / norm(f)
return features
def build_encoder(tparams, options):
"""
build an encoder, given pre-computed word embeddings
"""
# word embedding (source)
embedding = tensor.tensor3('embedding', dtype='float32')
x_mask = tensor.matrix('x_mask', dtype='float32')
# encoder
proj = get_layer(options['encoder'])[1](tparams, embedding, options,
prefix='encoder',
mask=x_mask)
ctx = proj[0][-1]
return embedding, x_mask, ctx
def word_features(table):
"""
Extract word features into a normalized matrix
"""
features = numpy.zeros((len(table), 620), dtype='float32')
keys = table.keys()
for i in range(len(table)):
f = table[keys[i]]
features[i] = f / norm(f)
return features
def build_encoder(tparams, options):
"""
build an encoder, given pre-computed word embeddings
"""
# word embedding (source)
embedding = tensor.tensor3('embedding', dtype='float32')
x_mask = tensor.matrix('x_mask', dtype='float32')
# encoder
proj = get_layer(options['encoder'])[1](tparams, embedding, options,
prefix='encoder',
mask=x_mask)
ctx = proj[0][-1]
return embedding, x_mask, ctx
def ndim_tensor(ndim):
if ndim == 1:
return T.vector()
elif ndim == 2:
return T.matrix()
elif ndim == 3:
return T.tensor3()
elif ndim == 4:
return T.tensor4()
return T.matrix()
# get int32 tensor
def init_func(self, img_value, scene_value):
if self._init_func is None:
img = T.matrix()
init_state = self.proj_mlp.compute(img)
self._init_func = theano.function([img], init_state)
self._scene_shared.set_value(scene_value)
return self._init_func(img_value)
def step_func(self, state_value, w_value):
if self._step_func is None:
w = T.ivector()
state = T.matrix()
new_state, p = self.compute(state, w, self._scene_shared)
self._step_func = theano.function([state, w], [new_state, T.log(p)])
return self._step_func(state_value, w_value)
def __init__(self, name='scene_mlp', layer_sizes=(2048, 1024, 1024, 80), model_file=None):
self.name = name
if model_file is not None:
with h5py.File(model_file, 'r') as f:
layer_sizes = f.attrs['layer_sizes']
self.config = {'layer_sizes': layer_sizes}
# define inputs
x = T.matrix('x')
y = T.matrix('y')
self.inputs = [x, y]
# define computation graph
self.mlp = MLP(layer_sizes=layer_sizes, name='mlp', output_type='softmax')
self.proba = self.mlp.compute(x)
self.log_proba = T.log(self.proba)
# define costs
def kl_divergence(p, q):
kl = T.mean(T.sum(p * T.log((p+1e-30)/(q+1e-30)), axis=1))
kl += T.mean(T.sum(q * T.log((q+1e-30)/(p+1e-30)), axis=1))
return kl
kl = kl_divergence(self.proba, y)
acc = T.mean(T.eq(self.proba.argmax(axis=1), y.argmax(axis=1)))
self.costs = [kl, acc]
# layers and parameters
self.layers = [self.mlp]
self.params = sum([l.params for l in self.layers], [])
# load weights from file, if model_file is not None
if model_file is not None:
self.load_weights(model_file)
def step_func(self, state_value, w_value):
if self._step_func is None:
w = T.ivector()
state = T.matrix()
new_state, p = self.compute(state, w)
self._step_func = theano.function([state, w], [new_state, T.log(p)])
return self._step_func(state_value, w_value)
def step_func(self, state_value, w_value):
if self._step_func is None:
w = T.ivector()
state = T.matrix()
new_state, p, _ = self.compute(state, w, self._feat_shared, self._scene_shared)
self._step_func = theano.function([state, w], [new_state, T.log(p)])
return self._step_func(state_value, w_value)
def step_func(self, state_value, w_value):
if self._step_func is None:
w = T.ivector()
state = T.matrix()
new_state, p, _ = self.compute(state, w, self._feat_shared)
self._step_func = theano.function([state, w], [new_state, T.log(p)])
return self._step_func(state_value, w_value)
def sharedX_mtx(mtx, name=None, borrow=None, dtype=None):
"""Share a matrix value with type theano.confgig.floatX.
Parameters:
value: matrix array
name: variable name (str)
borrow: boolean
dtype: the type of the value when shared. default: theano.config.floatX
"""
if dtype is None:
dtype = theano.config.floatX
return theano.shared(
np.array(mtx, dtype=dtype), name=name, borrow=borrow)