本文整理汇总了Python中theano_lstm.StackedCells类的典型用法代码示例。如果您正苦于以下问题:Python StackedCells类的具体用法?Python StackedCells怎么用?Python StackedCells使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StackedCells类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, t_layer_sizes, p_layer_sizes, dropout=0):
self.t_layer_sizes = t_layer_sizes
self.p_layer_sizes = p_layer_sizes
# From our architecture definition, size of the notewise input
self.t_input_size = 80
# time network maps from notewise input size to various hidden sizes
self.time_model = StackedCells( self.t_input_size, celltype=LSTM, layers = t_layer_sizes)
self.time_model.layers.append(PassthroughLayer())
# pitch network takes last layer of time model and state of last note, moving upward
# and eventually ends with a two-element sigmoid layer
p_input_size = t_layer_sizes[-1] + 2
self.pitch_model = StackedCells( p_input_size, celltype=LSTM, layers = p_layer_sizes)
self.pitch_model.layers.append(Layer(p_layer_sizes[-1], 2, activation = T.nnet.sigmoid))
self.dropout = dropout
self.conservativity = T.fscalar()
self.srng = T.shared_randomstreams.RandomStreams(np.random.randint(0, 1024))
print "model-setup::Trace-1"
self.setup_train()
print "model-setup::Trace-2"
self.setup_predict()
print "model-setup::Trace-3"
self.setup_slow_walk()
示例2: Model
class Model(object):
"""
Simple predictive model for forecasting words from
sequence using LSTMs. Choose how many LSTMs to stack
what size their memory should be, and how many
words can be predicted.
"""
def __init__(self, hidden_size, input_size, output_size, stack_size=1, celltype=RNN,steps=40):
# declare model
self.model = StackedCells(input_size, celltype=celltype, layers =[hidden_size] * stack_size)
# add a classifier:
self.model.layers.append(Layer(hidden_size, output_size, activation = T.tanh))
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self.steps=steps
self.gfs=T.tensor3('gfs')#输入gfs数据
self.pm25in=T.tensor3('pm25in')#pm25初始数据部分
self.layerstatus=None
self.results=None
self.cnt = T.tensor3('cnt')
# create symbolic variables for prediction:(就是做一次整个序列完整的进行预测,得到结果是prediction)
self.predictions = self.create_prediction()
self.create_predict_function()
'''上面几步的意思就是先把公式写好'''
@property
def params(self):
return self.model.params
def create_prediction(self):#做一次predict的方法
gfs=self.gfs
pm25in=self.pm25in
#初始第一次前传
self.layerstatus=self.model.forward(T.concatenate([gfs[:,0],gfs[:,1],gfs[:,2],pm25in[:,0],pm25in[:,1],self.cnt[:,:,0]],axis=1))
#results.shape?40*1
self.results=self.layerstatus[-1]
if self.steps > 1:
self.layerstatus=self.model.forward(T.concatenate([gfs[:,1],gfs[:,2],gfs[:,3],pm25in[:,1],self.results,self.cnt[:,:,1]],axis=1),self.layerstatus)
self.results=T.concatenate([self.results,self.layerstatus[-1]],axis=1)
#前传之后step-2次
for i in xrange(2,self.steps):
self.layerstatus=self.model.forward(T.concatenate([gfs[:,i],gfs[:,i+1],gfs[:,i+2],T.shape_padright(self.results[:,i-2]),T.shape_padright(self.results[:,i-1]),self.cnt[:,:,i]],axis=1),self.layerstatus)
#need T.shape_padright???
self.results=T.concatenate([self.results,self.layerstatus[-1]],axis=1)
return self.results
def create_predict_function(self):
self.pred_fun = theano.function(inputs=[self.gfs,self.pm25in,self.cnt],outputs =self.predictions,allow_input_downcast=True)
def __call__(self, gfs,pm25in):
return self.pred_fun(gfs,pm25in)
示例3: Model
class Model(object):
"""
Simple predictive model for forecasting words from
sequence using LSTMs. Choose how many LSTMs to stack
what size their memory should be, and how many
words can be predicted.
"""
def __init__(self, hidden_size, input_size, output_size, stack_size, celltype=RNN,steps=40):
# declare model
self.model = StackedCells(input_size, celltype=celltype, layers =[hidden_size]*stack_size)
# add a classifier:
self.model.layers.append(Layer(hidden_size, output_size, activation = lambda x:x))
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self.steps=steps
self.gfs=T.tensor3('gfs')#输入gfs数据
self.pm25in=T.tensor3('pm25in')#pm25初始数据部分
self.layerstatus=None
self.results=None
# create symbolic variables for prediction:(就是做一次整个序列完整的进行预测,得到结果是prediction)
self.predictions = self.create_prediction()
self.create_predict_function()
@property
def params(self):
return self.model.params
def create_prediction(self):#做一次predict的方法
gfs=self.gfs
pm25in=self.pm25in
#初始第一次前传
gfs_x=T.concatenate([gfs[:,0],gfs[:,1],gfs[:,2]],axis=1)
pm25in_x=T.concatenate([pm25in[:,0],pm25in[:,1]],axis=1)
self.layerstatus=self.model.forward(T.concatenate([gfs_x,pm25in_x],axis=1))
self.results=self.layerstatus[-1]
pm25next=pm25in[:,1]-self.results
if self.steps > 1:
for i in xrange(1,self.steps):
gfs_x=T.concatenate([gfs_x[:,9:],gfs[:,i+2]],axis=1)
pm25in_x=T.concatenate([pm25in_x[:,1:],pm25next],axis=1)
self.layerstatus=self.model.forward(T.concatenate([gfs_x,pm25in_x],axis=1),self.layerstatus)
self.results=T.concatenate([self.results,self.layerstatus[-1]],axis=1)
pm25next=pm25next-self.layerstatus[-1]
return self.results
def create_predict_function(self):
self.pred_fun = theano.function(inputs=[self.gfs,self.pm25in],outputs =self.predictions,allow_input_downcast=True)
def __call__(self, gfs,pm25in):
return self.pred_fun(gfs,pm25in)
示例4: __init__
def __init__(self, hidden_size, input_size, n_components, stack_size=1,
celltype=LSTM):
# declare model
self.model = StackedCells(input_size, celltype=celltype,
layers=[hidden_size] * stack_size)
# add an embedding
self.model.layers.insert(0, Embedding(vocab_size, input_size))
# add a classifier:
self.model.layers.append(Layer(hidden_size, vocab_size,
activation=linear))
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self._stop_word = theano.shared(np.int32(999999999), name="stop word")
self.for_how_long = T.ivector()
self.input_mat = T.imatrix()
self.priming_word = T.iscalar()
self.srng = T.shared_randomstreams.RandomStreams(np.random.randint(0, 1024))
# create symbolic variables for prediction:
self.predictions = self.create_prediction()
# create symbolic variable for greedy search:
self.greedy_predictions = self.create_prediction(greedy=True)
# create gradient training functions:
self.create_cost_fun()
self.create_training_function()
self.create_predict_function()
示例5: __init__
def __init__(self, input_parts, layer_sizes, output_size, window_size=0, dropout=0, mode="drop", unroll_batch_num=None):
"""
Parameters:
input_parts: A list of InputParts
layer_sizes: A list of the form [ (indep, per_note), ... ] where
indep is the number of non-shifted cells to have, and
per_note is the number of cells to have per window note, which shift as the
network moves
Alternately can just be [ indep, ... ]
output_size: An integer, the width of the desired output
dropout: How much dropout to apply.
mode: Either "drop" or "roll". If drop, discard memory that goes out of range. If roll, roll it instead
"""
self.input_parts = input_parts
self.window_size = window_size
layer_sizes = [x if isinstance(x,tuple) else (x,0) for x in layer_sizes]
self.layer_sizes = layer_sizes
self.tot_layer_sizes = [(indep + per_note*self.window_size) for indep, per_note in layer_sizes]
self.output_size = output_size
self.dropout = dropout
self.input_size = sum(part.PART_WIDTH for part in input_parts)
self.cells = StackedCells( self.input_size, celltype=LSTM, activation=T.tanh, layers = self.tot_layer_sizes )
self.cells.layers.append(Layer(self.tot_layer_sizes[-1], self.output_size, activation = lambda x:x))
assert mode in ("drop", "roll"), "Must specify either drop or roll mode"
self.mode = mode
self.unroll_batch_num = unroll_batch_num
示例6: __init__
def __init__(self, hidden_size, input_size, output_size, stack_size, celltype=RNN,steps=40):
# declare model
self.model = StackedCells(input_size, celltype=celltype, layers =[hidden_size]*stack_size)
# add a classifier:
self.model.layers.append(Layer(hidden_size, output_size, activation = lambda x:x))
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self.steps=steps
self.gfs=T.tensor3('gfs')#输入gfs数据
self.pm25in=T.tensor3('pm25in')#pm25初始数据部分
self.layerstatus=None
self.results=None
# create symbolic variables for prediction:(就是做一次整个序列完整的进行预测,得到结果是prediction)
self.predictions = self.create_prediction()
self.create_predict_function()
示例7: __init__
def __init__(self, hidden_size, input_size, vocab_size, entropy_reg = 0.001, key_entropy_reg = 0.001, stack_size=1, celltype=LSTM):
# core layer in RNN/LSTM
self.model = StackedCells(input_size, celltype=celltype, layers =[hidden_size] * stack_size)
# add an embedding
self.model.layers.insert(0, Embedding(vocab_size, input_size))
# add a classifier:
self.model.layers.append(Layer(hidden_size, vocab_size, activation = softmax))
self.entropy_reg = entropy_reg
self.key_entropy_reg = key_entropy_reg
self.turing_params = Parameters()
#init turing machine model
self.turing_updates , self.turing_predict = turing_model.build(self.turing_params , hidden_size , vocab_size)
self.hidden_size = hidden_size
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self._stop_word = theano.shared(np.int32(999999999), name="stop word")
self.for_how_long = T.ivector()
self.mask_matrix = T.imatrix()
self.input_mat = T.imatrix()
self.priming_word = T.iscalar()
self.srng = T.shared_randomstreams.RandomStreams(np.random.randint(0, 1024))
# create symbolic variables for prediction:
#change by darong #issue : what is greedy
self.lstm_predictions = self.create_lstm_prediction()
self.final_predictions,self.entropy,self.key_entropy = self.create_final_prediction()
# create symbolic variable for greedy search:
self.greedy_predictions = self.create_lstm_prediction(greedy=True)
# create gradient training functions:
self.create_cost_fun()#create 2 cost func(lstm final)
self.lstm_lr = 0.01
self.turing_lr = 0.01
self.all_lr = 0.01
self.create_training_function()#create 3 functions(lstm turing all)
self.create_predict_function()#create 2 predictions(lstm final)
# create ppl
self.lstm_ppl = self.create_lstm_ppl()
self.final_ppl = self.create_final_ppl()
self.create_ppl_function()
示例8: __init__
def __init__(self, hidden_size, input_size, output_size, stack_size=1, celltype=RNN):
# declare model
self.model = StackedCells(input_size, celltype=celltype, layers =[hidden_size] * stack_size)
# add a classifier:
self.model.layers.append(Layer(hidden_size, output_size, activation = T.tanh))
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self.steps=T.iscalar()
self.gfs=T.matrix()#输入gfs数据
self.pm25in=T.matrix()#pm25初始数据部分
self.pm25target=T.matrix()#输出的目标target
self.srng = T.shared_randomstreams.RandomStreams(np.random.randint(0, 1024))
# create symbolic variables for prediction:(就是做一次整个序列完整的进行预测,得到结果是prediction)
self.predictions = self.create_prediction()
# create gradient training functions:
self.create_cost_fun()
self.create_training_function()
self.create_predict_function()
'''上面几步的意思就是先把公式写好'''
示例9: __init__
def __init__(self, hidden_size, input_size, output_size, stack_size=1, celltype=RNN,steps=40):
# declare model
self.model = StackedCells(input_size, celltype=celltype, layers =[hidden_size] * stack_size)
# add a classifier:
self.model.layers.append(Layer(hidden_size, output_size, activation = T.tanh))
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self.steps=steps
self.gfs=T.tensor3('gfs')#输入gfs数据
self.pm25in=T.tensor3('pm25in')#pm25初始数据部分
self.layerstatus=None
self.results=None
self.cnt = T.tensor3('cnt')
# create symbolic variables for prediction:(就是做一次整个序列完整的进行预测,得到结果是prediction)
self.predictions = self.create_prediction()
self.create_predict_function()
self.pm25target=T.matrix('pm25target')#输出的目标target,这一版把target维度改了
self.create_valid_error()
self.create_validate_function()
'''上面几步的意思就是先把公式写好'''
示例10: __init__
def __init__(self, hidden_size, input_size, output_size, celltype=Layer):
# declare model
self.model = StackedCells(input_size, celltype=celltype, layers =hidden_size)
# add a classifier:
self.regression=Layer(hidden_size[-1], output_size[0], activation = T.tanh)
self.classifier=Layer(hidden_size[-1], output_size[1], activation = softmax)
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self.steps=T.iscalar('steps')
self.x=T.tensor3('x')#输入gfs数据
self.target0=T.tensor3('target0')#输出的目标target,这一版把target维度改了
self.target1=T.itensor3('target1')
self.layerstatus=None
self.results=None
# create symbolic variables for prediction:(就是做一次整个序列完整的进行预测,得到结果是prediction)
self.predictions0,self.predictions1 = self.create_prediction()
# create gradient training functions:
#self.create_cost_fun()
#self.create_valid_error()
#self.create_training_function()
self.create_predict_function()
#self.create_validate_function()
'''上面几步的意思就是先把公式写好'''
示例11: __init__
def __init__(self, hidden_size, input_size, output_size, stack_size=1, celltype=Layer,steps=40):
# declare model
self.model = StackedCells(input_size, celltype=celltype, layers =[hidden_size] * stack_size)
# add a classifier:
self.model.layers.append(Layer(hidden_size, output_size, activation = T.tanh))
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self.steps=steps
self.stepsin=T.iscalar('stepsin')
self.x=T.tensor3('x')#输入gfs数据
self.target=T.tensor3('target')#输出的目标target,这一版把target维度改了
self.layerstatus=None
self.results=None
# create symbolic variables for prediction:(就是做一次整个序列完整的进行预测,得到结果是prediction)
self.predictions = self.create_prediction()
self.predictions2 = self.create_prediction2()
# create gradient training functions:
self.create_cost_fun()
self.create_valid_error()
self.create_training_function()
self.create_predict_function()
self.create_validate_function()
'''上面几步的意思就是先把公式写好'''
示例12: Model
class Model(object):
def __init__(self, t_layer_sizes, p_layer_sizes, dropout=0):
self.t_layer_sizes = t_layer_sizes
self.p_layer_sizes = p_layer_sizes
# From our architecture definition, size of the notewise input
self.t_input_size = 80
# time network maps from notewise input size to various hidden sizes
self.time_model = StackedCells( self.t_input_size, celltype=LSTM, layers = t_layer_sizes)
self.time_model.layers.append(PassthroughLayer())
# pitch network takes last layer of time model and state of last note, moving upward
# and eventually ends with a two-element sigmoid layer
p_input_size = t_layer_sizes[-1] + 2
self.pitch_model = StackedCells( p_input_size, celltype=LSTM, layers = p_layer_sizes)
self.pitch_model.layers.append(Layer(p_layer_sizes[-1], 2, activation = T.nnet.sigmoid))
self.dropout = dropout
self.conservativity = T.fscalar()
self.srng = T.shared_randomstreams.RandomStreams(np.random.randint(0, 1024))
print "model-setup::Trace-1"
self.setup_train()
print "model-setup::Trace-2"
self.setup_predict()
print "model-setup::Trace-3"
self.setup_slow_walk()
@property
def params(self):
return self.time_model.params + self.pitch_model.params
@params.setter
def params(self, param_list):
ntimeparams = len(self.time_model.params)
self.time_model.params = param_list[:ntimeparams]
self.pitch_model.params = param_list[ntimeparams:]
@property
def learned_config(self):
return [self.time_model.params, self.pitch_model.params, [l.initial_hidden_state for mod in (self.time_model, self.pitch_model) for l in mod.layers if has_hidden(l)]]
@learned_config.setter
def learned_config(self, learned_list):
self.time_model.params = learned_list[0]
self.pitch_model.params = learned_list[1]
for l, val in zip((l for mod in (self.time_model, self.pitch_model) for l in mod.layers if has_hidden(l)), learned_list[2]):
l.initial_hidden_state.set_value(val.get_value())
def setup_train(self):
# dimensions: (batch, time, notes, input_data) with input_data as in architecture
self.input_mat = T.btensor4()
# dimensions: (batch, time, notes, onOrArtic) with 0:on, 1:artic
self.output_mat = T.btensor4()
self.epsilon = np.spacing(np.float32(1.0))
print "model-setup-train::Trace-1"
def step_time(in_data, *other):
other = list(other)
split = -len(self.t_layer_sizes) if self.dropout else len(other)
hiddens = other[:split]
masks = [None] + other[split:] if self.dropout else []
new_states = self.time_model.forward(in_data, prev_hiddens=hiddens, dropout=masks)
return new_states
def step_note(in_data, *other):
other = list(other)
split = -len(self.p_layer_sizes) if self.dropout else len(other)
hiddens = other[:split]
masks = [None] + other[split:] if self.dropout else []
new_states = self.pitch_model.forward(in_data, prev_hiddens=hiddens, dropout=masks)
return new_states
# We generate an output for each input, so it doesn't make sense to use the last output as an input.
# Note that we assume the sentinel start value is already present
# TEMP CHANGE: NO SENTINEL
print "model-setup-train::Trace-2"
input_slice = self.input_mat[:,0:-1]
n_batch, n_time, n_note, n_ipn = input_slice.shape
# time_inputs is a matrix (time, batch/note, input_per_note)
time_inputs = input_slice.transpose((1,0,2,3)).reshape((n_time,n_batch*n_note,n_ipn))
num_time_parallel = time_inputs.shape[1]
# apply dropout
if self.dropout > 0:
time_masks = MultiDropout( [(num_time_parallel, shape) for shape in self.t_layer_sizes], self.dropout)
else:
time_masks = []
#.........这里部分代码省略.........
示例13: __init__
class Model:
"""
Simple predictive model for forecasting words from
sequence using LSTMs. Choose how many LSTMs to stack
what size their memory should be, and how many
words can be predicted.
"""
def __init__(self, hidden_size, input_size, vocab_size, stack_size=1, celltype=LSTM):
# declare model
self.model = StackedCells(input_size, celltype=celltype, layers =[hidden_size] * stack_size)
# add an embedding
self.model.layers.insert(0, Embedding(vocab_size, input_size))
# add a classifier:
self.model.layers.append(Layer(hidden_size, vocab_size, activation = softmax))
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self._stop_word = theano.shared(np.int32(999999999), name="stop word")
self.for_how_long = T.ivector()
self.input_mat = T.imatrix()
self.priming_word = T.iscalar()
self.srng = T.shared_randomstreams.RandomStreams(np.random.randint(0, 1024))
# create symbolic variables for prediction:
self.predictions = self.create_prediction()
# create symbolic variable for greedy search:
self.greedy_predictions = self.create_prediction(greedy=True)
# create gradient training functions:
self.create_cost_fun()
self.create_training_function()
self.create_predict_function()
def stop_on(self, idx):
self._stop_word.set_value(idx)
@property
def params(self):
return self.model.params
def create_prediction(self, greedy=False):
def step(idx, *states):
# new hiddens are the states we need to pass to LSTMs
# from past. Because the StackedCells also include
# the embeddings, and those have no state, we pass
# a "None" instead:
new_hiddens = [None] + list(states)
new_states = self.model.forward(idx, prev_hiddens = new_hiddens)
if greedy:
new_idxes = new_states[-1]
new_idx = new_idxes.argmax()
# provide a stopping condition for greedy search:
return ([new_idx.astype(self.priming_word.dtype)] + new_states[1:-1]), theano.scan_module.until(T.eq(new_idx,self._stop_word))
else:
return new_states[1:]
# in sequence forecasting scenario we take everything
# up to the before last step, and predict subsequent
# steps ergo, 0 ... n - 1, hence:
inputs = self.input_mat[:, 0:-1]
num_examples = inputs.shape[0]
# pass this to Theano's recurrence relation function:
# choose what gets outputted at each timestep:
if greedy:
outputs_info = [dict(initial=self.priming_word, taps=[-1])] + [initial_state_with_taps(layer) for layer in self.model.layers[1:-1]]
result, _ = theano.scan(fn=step,
n_steps=200,
outputs_info=outputs_info)
else:
outputs_info = [initial_state_with_taps(layer, num_examples) for layer in self.model.layers[1:]]
result, _ = theano.scan(fn=step,
sequences=[inputs.T],
outputs_info=outputs_info)
if greedy:
return result[0]
# softmaxes are the last layer of our network,
# and are at the end of our results list:
return result[-1].transpose((2,0,1))
# we reorder the predictions to be:
# 1. what row / example
# 2. what timestep
# 3. softmax dimension
def create_cost_fun (self):
# create a cost function that
# takes each prediction at every timestep
# and guesses next timestep's value:
what_to_predict = self.input_mat[:, 1:]
# because some sentences are shorter, we
# place masks where the sentences end:
# (for how long is zero indexed, e.g. an example going from `[2,3)`)
# has this value set 0 (here we substract by 1):
for_how_long = self.for_how_long - 1
# all sentences start at T=0:
starting_when = T.zeros_like(self.for_how_long)
self.cost = masked_loss(self.predictions,
what_to_predict,
for_how_long,
starting_when).sum()
#.........这里部分代码省略.........
示例14: __init__
class Model:
"""
Simple predictive model for forecasting words from
sequence using LSTMs. Choose how many LSTMs to stack
what size their memory should be, and how many
words can be predicted.
"""
def __init__(self, hidden_size, input_size, output_size, stack_size=1, celltype=RNN,steps=40):
# declare model
self.model = StackedCells(input_size, celltype=celltype, layers =[hidden_size] * stack_size)
# add a classifier:
self.model.layers.append(Layer(hidden_size, output_size, activation = lambda x:x))
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self.steps=steps
self.gfs=T.tensor3('gfs')#输入gfs数据
self.pm25in=T.tensor3('pm25in')#pm25初始数据部分
self.pm25target=T.matrix('pm25target')#输出的目标target,这一版把target维度改了
self.layerstatus=None
self.results=None
self.cnt = T.tensor3('cnt')
# create symbolic variables for prediction:(就是做一次整个序列完整的进行预测,得到结果是prediction)
self.predictions = self.create_prediction()
# create gradient training functions:
self.create_cost_fun()
self.create_valid_error()
self.create_training_function()
self.create_predict_function()
self.create_validate_function()
'''上面几步的意思就是先把公式写好'''
@property
def params(self):
return self.model.params
def create_prediction(self):#做一次predict的方法
gfs=self.gfs
pm25in=self.pm25in
#初始第一次前传
gfs_x=T.concatenate([gfs[:,0],gfs[:,1],gfs[:,2]],axis=1)
pm25in_x=T.concatenate([pm25in[:,0],pm25in[:,1]],axis=1)
self.layerstatus=self.model.forward(T.concatenate([gfs_x,pm25in_x,self.cnt[:,:,0]],axis=1))
self.results=self.layerstatus[-1]
for i in xrange(1,46):#前6次(0-5),输出之前的先做的6个frame,之后第7次是第1个输出
gfs_x=T.concatenate([gfs_x[:,9:],gfs[:,i+2]],axis=1)
pm25in_x=T.concatenate([pm25in_x[:,1:],pm25in[:,i+1]],axis=1)
self.layerstatus=self.model.forward(T.concatenate([gfs_x,pm25in_x,self.cnt[:,:,i]],axis=1),self.layerstatus)
self.results=T.concatenate([self.results,self.layerstatus[-1]],axis=1)
return self.results
def create_cost_fun (self):
self.cost = (self.predictions[:,6:46] - self.pm25target[:,6:46]).norm(L=2)
def create_valid_error(self):
self.valid_error=T.mean(T.abs_(self.predictions[:,6:46] - self.pm25target[:,6:46]),axis=0)
def create_predict_function(self):
self.pred_fun = theano.function(inputs=[self.gfs,self.pm25in,self.cnt],outputs =self.predictions,allow_input_downcast=True)
def create_training_function(self):
updates, gsums, xsums, lr, max_norm = create_optimization_updates(self.cost, self.params, method="adadelta")#这一步Gradient Decent!!!!
self.update_fun = theano.function(
inputs=[self.gfs,self.pm25in, self.pm25target,self.cnt],
outputs=self.cost,
updates=updates,
name='update_fun',
profile=False,
allow_input_downcast=True)
def create_validate_function(self):
self.valid_fun = theano.function(
inputs=[self.gfs,self.pm25in, self.pm25target,self.cnt],
outputs=self.valid_error,
allow_input_downcast=True
)
def __call__(self, gfs,pm25in):
return self.pred_fun(gfs,pm25in)
示例15: __init__
class Model:
"""
Simple predictive model for forecasting words from
sequence using LSTMs. Choose how many LSTMs to stack
what size their memory should be, and how many
words can be predicted.
"""
def __init__(self, hidden_size, input_size, output_size, stack_size=1, celltype=RNN,steps=40):
# declare model
self.model = StackedCells(input_size, celltype=celltype, layers =[hidden_size] * stack_size)
# add a classifier:
self.model.layers.append(Layer(hidden_size, output_size, activation = T.tanh))
# inputs are matrices of indices,
# each row is a sentence, each column a timestep
self.steps=steps
self.gfs=T.matrix()#输入gfs数据
self.pm25in=T.matrix()#pm25初始数据部分
self.pm25target=T.matrix()#输出的目标target
self.layerstatus=None
self.results=None
self.srng = T.shared_randomstreams.RandomStreams(np.random.randint(0, 1024))
# create symbolic variables for prediction:(就是做一次整个序列完整的进行预测,得到结果是prediction)
self.predictions = self.create_prediction()
# create gradient training functions:
self.create_cost_fun()
self.create_valid_error()
self.create_training_function()
self.create_predict_function()
self.create_validate_function()
'''上面几步的意思就是先把公式写好'''
@property
def params(self):
return self.model.params
def create_prediction(self):#做一次predict的方法
gfs=self.gfs
pm25in=self.pm25in
#初始第一次前传
self.layerstatus=self.model.forward(T.concatenate([gfs[0],gfs[1],gfs[2],pm25in[0],pm25in[1]],axis=0))
self.results=T.shape_padright(self.layerstatus[-1])
if self.steps > 1:
self.layerstatus=self.model.forward(T.concatenate([gfs[1],gfs[2],gfs[3],pm25in[1],self.results[0]],axis=0),self.layerstatus)
self.results=T.concatenate([self.results,T.shape_padright(self.layerstatus[-1])],axis=0)
#前传之后step-2次
for i in xrange(2,self.steps):
self.layerstatus=self.model.forward(T.concatenate([gfs[i],gfs[i+1],gfs[i+2],self.results[i-2],self.results[i-1]],axis=0),self.layerstatus)
#need T.shape_padright???
self.results=T.concatenate([self.results,T.shape_padright(self.layerstatus[-1])],axis=0)
return self.results
def create_cost_fun (self):
self.cost = (self.predictions - self.pm25target).norm(L=2) / self.steps
def create_valid_error(self):
self.valid_error=T.abs_(self.predictions - self.pm25target)
def create_predict_function(self):
self.pred_fun = theano.function(inputs=[self.gfs,self.pm25in],outputs =self.predictions,allow_input_downcast=True)
def create_training_function(self):
updates, gsums, xsums, lr, max_norm = create_optimization_updates(self.cost, self.params, method="adadelta")#这一步Gradient Decent!!!!
self.update_fun = theano.function(
inputs=[self.gfs,self.pm25in, self.pm25target],
outputs=self.cost,
updates=updates,
name='update_fun',
profile=True,
allow_input_downcast=True)
def create_validate_function(self):
self.valid_fun = theano.function(
inputs=[self.gfs,self.pm25in, self.pm25target],
outputs=self.valid_error,
allow_input_downcast=True
)
def __call__(self, gfs,pm25in):
return self.pred_fun(gfs,pm25in)