本文整理汇总了Python中theano_lstm.StackedCells.forward方法的典型用法代码示例。如果您正苦于以下问题:Python StackedCells.forward方法的具体用法?Python StackedCells.forward怎么用?Python StackedCells.forward使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano_lstm.StackedCells
的用法示例。
在下文中一共展示了StackedCells.forward方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Model
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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)
示例2: Model
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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)
示例3: __init__
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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.celltype=celltype
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.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
#初始第一次前传
x=T.concatenate([gfs[:,0],gfs[:,1],gfs[:,2],pm25in[:,0],pm25in[:,1],self.cnt[:,:,0]],axis=1)
if self.celltype==RNN:
init_hiddens = [(T.repeat(T.shape_padleft(create_shared(layer.hidden_size, name="RNN.initial_hidden_state")),
x.shape[0], axis=0)
if x.ndim > 1 else create_shared(layer.hidden_size, name="RNN.initial_hidden_state"))
if hasattr(layer, 'initial_hidden_state') else None
for layer in self.model.layers]
if self.celltype==LSTM:
init_hiddens = [(T.repeat(T.shape_padleft(create_shared(layer.hidden_size * 2, name="LSTM.initial_hidden_state")),
x.shape[0], axis=0)
if x.ndim > 1 else create_shared(layer.hidden_size * 2, name="LSTM.initial_hidden_state"))
if hasattr(layer, 'initial_hidden_state') else None
for layer in self.model.layers]
self.layerstatus=self.model.forward(x,init_hiddens)
#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_cost_fun (self):
self.cost = (self.predictions - self.pm25target).norm(L=2)
def create_valid_error(self):
self.valid_error=T.mean(T.abs_(self.predictions - self.pm25target),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)
示例4: __init__
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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)
示例5: __init__
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
#.........这里部分代码省略.........
# need to compile again for calculating predictions after loading lstm
self.srng = T.shared_randomstreams.RandomStreams(np.random.randint(0, 1024))
self.lstm_predictions = self.create_lstm_prediction()
self.final_predictions = self.create_final_prediction()
self.greedy_predictions = self.create_lstm_prediction(greedy=True)#can change to final
self.create_cost_fun()#create 2 cost func(lstm final)
self.lstm_lr = lr
self.turing_lr = lr#change this
self.all_lr = lr
self.create_training_function()#create 3 functions(lstm turing all)
self.create_predict_function()#create 2 predictions(lstm final)
self.lstm_ppl = self.create_lstm_ppl()
self.final_ppl = self.create_final_ppl()
self.create_ppl_function()
print "done loading model"
# print "done compile"
def stop_on(self, idx):
self._stop_word.set_value(idx)
@property
def params(self):
return self.model.params
def create_lstm_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))
示例6: Model
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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 = []
#.........这里部分代码省略.........
示例7: __init__
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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()
#.........这里部分代码省略.........
示例8: __init__
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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, 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()
'''上面几步的意思就是先把公式写好'''
@property
def params(self):
return self.model.params+self.regression.params+self.classifier.params
def create_prediction(self):#做一次predict的方法
def step(idx):
new_states=self.model.forward(idx)
output0=self.regression.activate(new_states[-1])
output1=self.classifier.activate(new_states[-1])
return [output0,output1]#不论recursive与否,会全部输出
x = self.x
num_examples = x.shape[0]
#outputs_info =[initial_state_with_taps(layer, num_examples) for layer in self.model.layers]
#outputs_info = [initial_state_with_taps(layer, num_examples) for layer in self.model.layers[1:]]
[result0,result1], _ = theano.scan(fn=step,
n_steps=self.steps,
sequences=dict(input=x.dimshuffle((1,0,2)), taps=[-0]),
)
return result0.dimshuffle((1,0,2)),result1.dimshuffle((2,0,1))
def create_cost_fun (self):
y=self.target1[:,0,0]
self.cost = (self.predictions0 - self.target0[:,:,0:1]).norm(L=2)+100*(-T.mean(T.log(self.predictions1)[T.arange(y.shape[0]),:,y]))
def create_valid_error(self):
self.valid_error0=T.mean(T.abs_(self.predictions0 - self.target0[:,:,0:1]),axis=0)
#self.valid_error1=-T.mean(T.log(self.predictions1)[T.arange(self.target1.shape[0]),:,self.target1[:,0,0]])
self.valid_error1=T.mean(T.eq(T.argmax(self.predictions1, axis=2).dimshuffle(1,0),self.target1[:,0,0]))
def create_predict_function(self):
self.pred_fun = theano.function(inputs=[self.x,self.steps],outputs =[self.predictions0,self.predictions1],allow_input_downcast=True)
def create_training_function(self):
updates, gsums, xsums, lr, max_norm = create_optimization_updates(self.cost, self.params, lr=0.01, method="adagrad")#这一步Gradient Decent!!!!
self.update_fun = theano.function(
inputs=[self.x, self.target0,self.target1,self.steps],
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.x, self.target0,self.target1,self.steps],
outputs=[self.valid_error0,self.valid_error1],
allow_input_downcast=True
)
def __call__(self, x):
return self.pred_fun(x)
示例9: __init__
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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:(就是做一次整个序列完整的进行预测,得到结果是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=list(states)
new_states=self.model.forward(idx,prev_hiddens = new_hiddens)
if greedy:
return
else:
return new_states#不论recursive与否,会全部输出
inputs = self.input_mat[:,0:-1]
num_examples = inputs.shape[0]
if greedy:
return
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)
return result[-1].transpose((2,0,1))
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)#这一步更新!!!!,idx是layer_input
#new_states是一个列表,包括了stackcells各个层的最新输出
if greedy:
new_idxes = new_states[-1]#即最后一层softmax的输出
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:]#除第0层之外,其他各层输出
# 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)
'''就是这里sequences相当于每次把inputs的一个给到idx,改动这里使符合一次给多种的pm25形式'''
'''outputs_info:就是说让scan把每回的输出重新传回fn的输入,而outputs_info就是第一回没有之前输出时,给入的值。于是output_info也暗示了这种回传的形式
Second, if there is no accumulation of results, we can set outputs_info to None. This indicates to scan that it doesn’t need to pass the prior result to fn.'''
#.........这里部分代码省略.........
示例10: Model
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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 = 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
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()
'''上面几步的意思就是先把公式写好'''
@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,7):#前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)
if self.steps > 1:
gfs_x=T.concatenate([gfs_x[:,9:],gfs[:,9]],axis=1)
pm25in_x=T.concatenate([pm25in_x[:,1:],T.shape_padright(self.results[:,-1])],axis=1)
self.layerstatus=self.model.forward(T.concatenate([gfs_x,pm25in_x,self.cnt[:,:,7]],axis=1),self.layerstatus)
self.results=T.concatenate([self.results,self.layerstatus[-1]],axis=1)
#前传之后step-2次
for i in xrange(2,self.steps):
gfs_x=T.concatenate([gfs_x[:,9:],gfs[:,i+8]],axis=1)
pm25in_x=T.concatenate([pm25in_x[:,1:],T.shape_padright(self.results[:,-1])],axis=1)
self.layerstatus=self.model.forward(T.concatenate([gfs_x,pm25in_x,self.cnt[:,:,i+6]],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 create_valid_error(self):
self.valid_error=T.mean(T.abs_(self.predictions[:,6:46] - self.pm25target[:,6:46]),axis=0)
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)
示例11: __init__
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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=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()
'''上面几步的意思就是先把公式写好'''
@property
def params(self):
return self.model.params
def create_prediction(self):#做一次predict的方法
'''x=self.x
#初始第一次前传
self.layerstatus=self.model.forward(x[:,0])
#results.shape?40*1
self.results=self.layerstatus[-1].dimshuffle((0,'x',1))
if self.steps > 1:
for i in xrange(1,self.steps):
self.layerstatus=self.model.forward(x[:,i],self.layerstatus)
#need T.shape_padright???
self.results=T.concatenate([self.results,self.layerstatus[-1].dimshuffle((0,'x',1))],axis=1)
return self.results'''
def step(idx):
new_states=self.model.forward(idx)
return new_states#不论recursive与否,会全部输出
x = self.x
num_examples = x.shape[0]
#outputs_info =[initial_state_with_taps(layer, num_examples) for layer in self.model.layers]
#outputs_info = [initial_state_with_taps(layer, num_examples) for layer in self.model.layers[1:]]
result, _ = theano.scan(fn=step,
n_steps=self.steps,
sequences=dict(input=x.dimshuffle((1,0,2)), taps=[-0]),
)
return result[-1].dimshuffle((1,0,2))
def create_prediction2(self):#做一次predict的方法
def step(idx):
new_states=self.model.forward(idx)
return new_states#不论recursive与否,会全部输出
x = self.x
num_examples = x.shape[0]
#outputs_info =[initial_state_with_taps(layer, num_examples) for layer in self.model.layers]
#outputs_info = [initial_state_with_taps(layer, num_examples) for layer in self.model.layers[1:]]
result, _ = theano.scan(fn=step,
n_steps=self.stepsin,
sequences=dict(input=x.dimshuffle((1,0,2)), taps=[-0]),
)
return result[-1].dimshuffle((1,0,2))
def create_cost_fun (self):
self.cost = (self.predictions - self.target[:,:,0:1]).norm(L=2)
def create_valid_error(self):
self.valid_error=T.mean(T.abs_(self.predictions - self.target[:,:,0:1]),axis=0)
def create_predict_function(self):
self.pred_fun = theano.function(inputs=[self.x],outputs =self.predictions,allow_input_downcast=True)
self.pred_fun2 = theano.function(inputs=[self.x,self.stepsin],outputs =self.predictions2,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.x, self.target],
outputs=self.cost,
updates=updates,
name='update_fun',
profile=False,
#.........这里部分代码省略.........
示例12: __init__
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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)
示例13: Model
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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):
# 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=40
self.gfs=T.matrix('gfs')#输入gfs数据
self.pm25in=T.matrix('pm25in')#pm25初始数据部分
self.pm25target=T.matrix('pm25target')#输出的目标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_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):
def oneStep(gfs_tm2,gfs_tm1,gfs_t,pm25_tm2,pm25_tm1,*prev_hiddens):
input_x=T.concatenate([gfs_tm2,gfs_tm1,gfs_t,pm25_tm2,pm25_tm1],axis=0)
new_states = self.model.forward(input_x, prev_hiddens)
#错位之后返回
return [new_states[-1]]+new_states[:-1]
result, updates = theano.scan(oneStep,
n_steps=self.steps,
sequences=[dict(input=self.gfs, taps=[-2,-1,-0])],
outputs_info=[dict(initial=self.pm25in, taps=[-2,-1])] + [dict(initial=layer.initial_hidden_state, taps=[-1]) for layer in self.model.layers if hasattr(layer, 'initial_hidden_state')])
#根据oneStep,result的结果list有两个元素,result[0]是new_stats[-1]即最后一层输出的array,result[1]是之前层
return result[0]
def create_cost_fun (self):
#可能改cost function,记得
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)
示例14: __init__
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
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):
# 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()
'''上面几步的意思就是先把公式写好'''
@property
def params(self):
return self.model.params
'''def create_prediction(self):
def oneStep(gfs_tm2,gfs_tm1,gfs_t,pm25_in,pm25_tm1,*hidden_states):
input_x=gfs_tm2+gfs_tm1+gfs_t+pm25_in+pm25_tm1
new_hiddens=list(hidden_states)
layers_out = self.model.forward(input_x, prev_hiddens = new_hiddens)
#这一步更新!!!!,这里input_x和previous_hidden应该是放在outputinfo里进行迭代的
y_given_x=layers_out[-1]#每一层的结果都有输出,最后一层就是输出层了,这里就是输出了下一帧pm25
hiddens=layers_out
return [y_given_x]+hiddens
#按下面三行描述规则排序,预测的那一时刻帧为0
# in sequence forecasting scenario we take everything
# up to the before last step, and predict subsequent
# steps ergo, 0 ... n - 1, hence:
gfs=self.gfs
pm25in=self.pm25in
pm250=self.pm250
hiddens0=[initial_state_with_taps(layer,1) for layer in self.model.layers]
#这个函数是自动按照scan的格式,已经把taps=-1加上了,所以之后在scan里就直接写进去了
# pass this to Theano's recurrence relation function:
# choose what gets outputted at each timestep:
outputs_info = [dict(initial=pm250, taps=[-1])]+hiddens0
result, _ = theano.scan(fn=oneStep,
sequences=[dict(input=gfs, taps=[-2,-1,0]),pm25in],
outputs_info=outputs_info,
n_steps=self.steps)
return result[0]#每一次y_given_x组成的list
# we reorder the predictions to be:
# 1. what row / example
# 2. what timestep
# 3. softmax dimension'''
def create_prediction(self):
def oneStep(gfs_tm2,gfs_tm1,gfs_t,pm25_tm2,pm25_tm1,*prev_hiddens):
input_x=gfs_tm2+gfs_tm1+gfs_t+pm25_tm2+pm25_tm1
new_states = self.model.forward(input_x, prev_hiddens)
#错位之后返回
return [new_states[-1]]+new_states[:-1]
gfs=self.gfs
initial_predict=self.pm25in
result, updates = theano.scan(oneStep,
n_steps=self.steps,
sequences=[dict(input=gfs, taps=[-2,-1,-0])],
outputs_info=[dict(initial=initial_predict, taps=[-2,-1])] + [dict(initial=layer.initial_hidden_state, taps=[-1]) for layer in self.model.layers if hasattr(layer, 'initial_hidden_state')])
return result[0]
def create_cost_fun (self):
self.cost = (self.predictions - self.pm25target).norm(L=2) / self.steps
def create_predict_function(self):
self.pred_fun = theano.function(inputs=[self.gfs,self.pm25in,self.steps],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.steps],
outputs=self.cost,
updates=updates,
allow_input_downcast=True)
def __call__(self, gfs,pm25in,steps):
return self.pred_fun(gfs,pm25in,steps)
示例15: RelativeShiftLSTMStack
# 需要导入模块: from theano_lstm import StackedCells [as 别名]
# 或者: from theano_lstm.StackedCells import forward [as 别名]
#.........这里部分代码省略.........
separated_mem = per_note_mem.reshape((n_batch, self.window_size, per_note))
# separated_mem is (batch, note, mem)
# [a b c ... x y z] shifted up 1 (+1) goes to [b c ... x y z 0]
# [a b c ... x y z] shifted down 1 (-1) goes to [0 a b c ... x y]
def _shift_step(c_mem, c_shift):
# c_mem is (note, mem)
# c_shift is an int
if self.mode=="drop":
def _clamp_w(x):
return T.maximum(0,T.minimum(x,self.window_size))
ins_at_front = T.zeros((_clamp_w(-c_shift),per_note))
ins_at_back = T.zeros((_clamp_w(c_shift),per_note))
take_part = c_mem[_clamp_w(c_shift):self.window_size-_clamp_w(-c_shift),:]
return T.concatenate([ins_at_front, take_part, ins_at_back], 0)
elif self.mode=="roll":
return T.roll(c_mem, (-c_shift)%12, axis=0)
if self.unroll_batch_num is None:
shifted_mem, _ = theano.map(_shift_step, [separated_mem, shifts])
else:
shifted_mem_parts = []
for i in range(self.unroll_batch_num):
shifted_mem_parts.append(_shift_step(separated_mem[i], shifts[i]))
shifted_mem = T.stack(shifted_mem_parts)
new_per_note_mem = shifted_mem.reshape((n_batch, self.window_size * per_note))
new_layer_hiddens = T.concatenate([indep_mem, new_per_note_mem, remaining_values], 1)
new_hiddens.append(new_layer_hiddens)
if dropout_masks == [] or not self.dropout:
masks = []
else:
masks = [None] + dropout_masks
new_states = self.cells.forward(in_data, prev_hiddens=new_hiddens, dropout=masks)
return new_states
def do_preprocess_scan(self, deterministic_dropout=False, **kwargs):
"""
Run a scan using this LSTM, preprocessing all inputs before the scan.
Parameters:
kwargs[k]: should be a theano tensor of shape (n_batch, n_time, ... )
Note that "relative_position" should be a keyword argument given here if there are relative
shifts.
deterministic_dropout: If True, apply dropout deterministically, scaling everything. If false,
sample dropout
Returns:
A theano tensor of shape (n_batch, n_time, output_size) of activations
"""
assert len(kwargs)>0, "Need at least one input argument!"
n_batch, n_time = list(kwargs.values())[0].shape[:2]
squashed_kwargs = {
k: v.reshape([n_batch*n_time] + [x for x in v.shape[2:]]) for k,v in kwargs.items()
}
full_input = T.concatenate([ part.generate(**squashed_kwargs) for part in self.input_parts ], 1)
adjusted_input = full_input.reshape([n_batch, n_time, self.input_size]).dimshuffle((1,0,2))
if "relative_position" in kwargs:
relative_position = kwargs["relative_position"]
diff_shifts = T.extra_ops.diff(relative_position, axis=1)
cat_shifts = T.concatenate([T.zeros((n_batch, 1), 'int32'), diff_shifts], 1)
shifts = cat_shifts.dimshuffle((1,0))