def get_predict_function(file_path): with open(file_path) as f: model_state = cPickle.load(f) # define model architecture word_embedding_layer = EmbeddingLayer.create_from_state(model_state['word_embedding_layer']) cnn_embedding_layer = DenseLayer.create_from_state(model_state['cnn_embedding_layer']) row_stack_layer = RowStackLayer.create_from_state(model_state['row_stack_layer']) embedding_scale_layer = ScaleLayer.create_from_state(model_state['embedding_scale_layer']) lstm_layer = LstmLayer.create_from_state(model_state['lstm_layer']) hidden_states_scale_layer = ScaleLayer.create_from_state(model_state['hidden_states_scale_layer']) pre_softmax_layer = DenseLayer.create_from_state(model_state['pre_softmax_layer']) softmax_layer = NonlinearityLayer.create_from_state(model_state['softmax_layer']) # define forward propagation expression for model word_indices = T.ivector() cnn_features = T.fvector() word_embedings = word_embedding_layer.get_output_expr(word_indices) cnn_embedings = cnn_embedding_layer.get_output_expr(cnn_features) embedings = row_stack_layer.get_output_expr(cnn_embedings, word_embedings) scaled_embedings = embedding_scale_layer.get_output_expr(embedings) h = lstm_layer.get_output_expr(scaled_embedings) scaled_h = hidden_states_scale_layer.get_output_expr(h[h.shape[0]-1]) unnormalized_probs = pre_softmax_layer.get_output_expr(scaled_h) probs = softmax_layer.get_output_expr(unnormalized_probs) predict_probs = theano.function(inputs=[word_indices, cnn_features], outputs=probs) return predict_probs, model_state['word_to_idx'], model_state['idx_to_word']
outputs=loss, updates=updates, givens={ cnn_features: train_data_iterator.features[cnn_features_idx], word_indices: train_data_iterator.flatten_input_captions[caption_begin:caption_end], true_dist: train_data_iterator.flatten_output_captions[caption_begin:caption_end] }) # define valid model val_word_embedding_layer = EmbeddingLayer.create_copy(word_embedding_layer) val_cnn_embedding_layer = DenseLayer.create_copy(cnn_embedding_layer) val_row_stack_layer = RowStackLayer.create_copy(row_stack_layer) val_embedding_scale_layer = ScaleLayer('embedding_scale', embedding_dropout_layer.keep_prob) val_lstm_layer = LstmLayer.create_copy(lstm_layer) val_hidden_states_scale_layer = ScaleLayer( 'hidden_states_scale', hidden_states_dropout_layer.keep_prob) val_pre_softmax_layer = DenseLayer.create_copy(pre_softmax_layer) val_softmax_layer = NonlinearityLayer.create_copy(softmax_layer) # define forward propagation expression for val model and loss function val_word_embedings = val_word_embedding_layer.get_output_expr(word_indices) val_cnn_embedings = val_cnn_embedding_layer.get_output_expr(cnn_features) val_embedings = val_row_stack_layer.get_output_expr(val_cnn_embedings, val_word_embedings) val_scaled_embedings = val_embedding_scale_layer.get_output_expr(val_embedings) val_h = val_lstm_layer.get_output_expr(val_scaled_embedings) val_scaled_h = val_hidden_states_scale_layer.get_output_expr(val_h[1:]) val_unnormalized_probs = val_pre_softmax_layer.get_output_expr(val_scaled_h)
inputs=[cnn_features_idx, caption_begin, caption_end], outputs=loss, updates=updates, givens={ cnn_features: train_data_iterator.features[cnn_features_idx], word_indices: train_data_iterator.flatten_input_captions[caption_begin:caption_end], true_dist: train_data_iterator.flatten_output_captions[caption_begin:caption_end] } ) # define valid model val_word_embedding_layer = EmbeddingLayer.create_copy(word_embedding_layer) val_cnn_embedding_layer = DenseLayer.create_copy(cnn_embedding_layer) val_row_stack_layer = RowStackLayer.create_copy(row_stack_layer) val_embedding_scale_layer = ScaleLayer('embedding_scale', embedding_dropout_layer.keep_prob) val_lstm_layer = LstmLayer.create_copy(lstm_layer) val_hidden_states_scale_layer = ScaleLayer('hidden_states_scale', hidden_states_dropout_layer.keep_prob) val_pre_softmax_layer = DenseLayer.create_copy(pre_softmax_layer) val_softmax_layer = NonlinearityLayer.create_copy(softmax_layer) # define forward propagation expression for val model and loss function val_word_embedings = val_word_embedding_layer.get_output_expr(word_indices) val_cnn_embedings = val_cnn_embedding_layer.get_output_expr(cnn_features) val_embedings = val_row_stack_layer.get_output_expr(val_cnn_embedings, val_word_embedings) val_scaled_embedings = val_embedding_scale_layer.get_output_expr(val_embedings) val_h = val_lstm_layer.get_output_expr(val_scaled_embedings) val_scaled_h = val_hidden_states_scale_layer.get_output_expr(val_h[1:]) val_unnormalized_probs = val_pre_softmax_layer.get_output_expr(val_scaled_h) val_probs = softmax_layer.get_output_expr(val_unnormalized_probs)