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)
train_model = theano.function( 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)