2015-12-17 20 views
8

tôi đã được đào tạo một mô hình đơn giản dài trí nhớ ngắn hạn (lstm) trong lasagne sau recipie đây: https://github.com/Lasagne/Recipes/blob/master/examples/lstm_text_generation.pyPickle mô hình python lasagne

Dưới đây là kiến ​​trúc:

l_in = lasagne.layers.InputLayer(shape=(None, None, vocab_size)) 

# We now build the LSTM layer which takes l_in as the input layer 
# We clip the gradients at GRAD_CLIP to prevent the problem of exploding gradients. 

l_forward_1 = lasagne.layers.LSTMLayer(
    l_in, N_HIDDEN, grad_clipping=GRAD_CLIP, 
    nonlinearity=lasagne.nonlinearities.tanh) 

l_forward_2 = lasagne.layers.LSTMLayer(
    l_forward_1, N_HIDDEN, grad_clipping=GRAD_CLIP, 
    nonlinearity=lasagne.nonlinearities.tanh) 

# The l_forward layer creates an output of dimension (batch_size, SEQ_LENGTH, N_HIDDEN) 
# Since we are only interested in the final prediction, we isolate that quantity and feed it to the next layer. 
# The output of the sliced layer will then be of size (batch_size, N_HIDDEN) 
l_forward_slice = lasagne.layers.SliceLayer(l_forward_2, -1, 1) 

# The sliced output is then passed through the softmax nonlinearity to create probability distribution of the prediction 
# The output of this stage is (batch_size, vocab_size) 
l_out = lasagne.layers.DenseLayer(l_forward_slice, num_units=vocab_size, W = lasagne.init.Normal(), nonlinearity=lasagne.nonlinearities.softmax) 

# Theano tensor for the targets 
target_values = T.ivector('target_output') 

# lasagne.layers.get_output produces a variable for the output of the net 
network_output = lasagne.layers.get_output(l_out) 

# The loss function is calculated as the mean of the (categorical) cross-entropy between the prediction and target. 
cost = T.nnet.categorical_crossentropy(network_output,target_values).mean() 

# Retrieve all parameters from the network 
all_params = lasagne.layers.get_all_params(l_out) 

# Compute AdaGrad updates for training 
print("Computing updates ...") 
updates = lasagne.updates.adagrad(cost, all_params, LEARNING_RATE) 

# Theano functions for training and computing cost 
print("Compiling functions ...") 
train = theano.function([l_in.input_var, target_values], cost, updates=updates, allow_input_downcast=True) 
compute_cost = theano.function([l_in.input_var, target_values], cost, allow_input_downcast=True) 

# In order to generate text from the network, we need the probability distribution of the next character given 
# the state of the network and the input (a seed). 
# In order to produce the probability distribution of the prediction, we compile a function called probs. 

probs = theano.function([l_in.input_var],network_output,allow_input_downcast=True) 

và mô hình được đào tạo qua:

for it in xrange(data_size * num_epochs/BATCH_SIZE): 
     try_it_out() # Generate text using the p^th character as the start. 

     avg_cost = 0; 
     for _ in range(PRINT_FREQ): 
      x,y = gen_data(p) 

      #print(p) 
      p += SEQ_LENGTH + BATCH_SIZE - 1 
      if(p+BATCH_SIZE+SEQ_LENGTH >= data_size): 
       print('Carriage Return') 
       p = 0; 


      avg_cost += train(x, y) 
     print("Epoch {} average loss = {}".format(it*1.0*PRINT_FREQ/data_size*BATCH_SIZE, avg_cost/PRINT_FREQ)) 

Làm cách nào để lưu mô hình để tôi không cần phải đào tạo lại? Với scikit tôi thường chỉ cần chọn đối tượng mô hình. Tuy nhiên tôi không rõ ràng về quá trình tương tự với Theano/lasagne.

Trả lời

14

Bạn có thể lưu các trọng với NumPy:

np.savez('model.npz', *lasagne.layers.get_all_param_values(network_output)) 

Và tải chúng một lần nữa sau này như thế này:

with np.load('model.npz') as f: 
    param_values = [f['arr_%d' % i] for i in range(len(f.files))] 
lasagne.layers.set_all_param_values(network_output, param_values) 

Nguồn: https://github.com/Lasagne/Lasagne/blob/master/examples/mnist.py

Đối với các định nghĩa mô hình riêng của mình: Một tùy chọn chắc chắn là giữ mã và tạo lại mạng, trước khi thiết lập các trọng số giả định.

+0

Có, phần cuối cùng đáng chú ý. Bạn cần phải bảo tồn cách mà mô hình được xây dựng. Tôi khuyên bạn nên làm điều này trong các tệp riêng biệt mà bạn có thể tải/nhập. – ComputerScientist

0

Tôi đã thành công sử dụng dill kết hợp với numpy.savez chức năng:

import dill as pickle 

... 
np.savez('model.npz', *lasagne.layers.get_all_param_values(network)) 
with open('model.dpkl','wb') as p_output: 
    pickle.dump(network, p_output) 

Để nhập các mô hình ngâm:

with open('model.dpkl', 'rb') as p_input: 
    network = pickle.load(p_input) 

with np.load('model.npz') as f: 
    param_values = [f['arr_%d' % i] for i in range(len(f.files))] 
lasagne.layers.set_all_param_values(network, param_values) 
1

Bạn có thể lưu các thông số mô hình và mô hình bởi Pickle

import cPickle as pickle 
import os 
#save the network and its parameters as a dictionary 
netInfo = {'network': network, 'params': lasagne.layers.get_all_param_values(network)} 
Net_FileName = 'LSTM.pkl' 
# save the dictionary as a .pkl file 
pickle.dump(netInfo, open(os.path.join(/path/to/a/folder/, Net_FileName), 'wb'),protocol=pickle.HIGHEST_PROTOCOL) 

Sau khi lưu mô hình của bạn, nó có thể được truy xuất bởi pickle.load:

net = pickle.load(open(os.path.join(/path/to/a/folder/,Net_FileName),'rb')) 
all_params = net['params'] 
lasagne.layers.set_all_param_values(net['network'], all_params) 
Các vấn đề liên quan