def sentence_embedding(sentence_len, wv_params, wv_size, input_name='sentence_embedding', output_name='vector_embedding'): ''' Creates an embedding of word vectors into a sentence image. Args: ----- sentence_len: length of sentences to be passed wv_params: a dict of the following format wv_params = { 'fixed_wv' : { 'vocab_size' : 1000, 'init' : None, 'fixed' : True }, 'floating_wv' : { 'vocab_size' : 1000, 'init' : None, 'fixed' : False } } the keys of the dictionary are the names in the keras graph model, and you can have any number of word vector layers encoded. input_name: the name of the input node for the graph output_name: the name of the output node for the graph Returns: -------- a keras container that takes as input an integer array with shape (n_samples, n_words), and returns shape (n_samples, wv_channels, len_sentence, wv_dim)! ''' # -- output is (n_samples, n_channels, n_words, wv_dim) g = SubGraph() if KERAS_BACKEND: g.add_input(input_name, (sentence_len, ), dtype='int') else: g.add_input(input_name, (-1, ), dtype='int') for name, params in wv_params.iteritems(): # g.add_input(params['input_name'], (-1, ), dtype='int') g.add_node(make_embedding(wv_size=wv_size, **params), name=name, input=input_name) g.add_node(Reshape((sentence_len, len(wv_params), wv_size)), name='reshape', inputs=wv_params.keys(), merge_mode='concat') g.add_node(Permute(dims=(2, 1, 3)), name='permute', input='reshape') # -- output is of shape (nb_samples, nb_wv_channels, len_sentence, wv_dim) g.add_output(name=output_name, input='permute') return g
def incept(input_shape, nm): inception = Graph() input_nm = nm + '_input' inception.add_input(name = input_nm, input_shape = input_shape) inception.add_node(Convolution2D(nb_filters, 1, 1,activation = 'relu', border_mode='same'), input = input_nm, name = nm + '11') inception.add_node(Convolution2D(nb_filters, 1, 1, activation = 'relu', border_mode='same'), input = input_nm, name = nm + '1133') inception.add_node(Convolution2D(nb_filters, 1, 1, activation = 'relu', border_mode='same'), input = input_nm, name = nm + '1155') inception.add_node(Convolution2D(nb_filters, 1, 1, activation = 'relu', border_mode='same'), input = input_nm, name = nm + '1177') inception.add_node(MaxPooling2D(pool_size = (3,3), strides = (1,1), border_mode='same'), input = input_nm, name = nm + 'max11') inception.add_node(Convolution2D(nb_filters, 3, 3, activation = 'relu', border_mode='same'), input = nm + '1133', name = nm + '33') inception.add_node(Convolution2D(nb_filters, 5, 5, activation = 'relu', border_mode='same'), input = nm + '1155', name = nm + '55') inception.add_node(Convolution2D(nb_filters, 7, 7, activation = 'relu', border_mode='same'), input = nm + '1177', name = nm + '77') inception.add_node(Convolution2D(nb_filters, 1, 1, activation ='relu', border_mode='same'), input = nm + 'max11', name = nm + '11out') inception.add_output(name = nm + "_out", inputs = [nm + '11', nm + '33', nm + '55', nm + '77', nm + '11out'], concat_axis = 1) return inception
print("Build model...") HIDDEN_SIZE = 128 BATCH_SIZE = 50 MAXLEN = len(chars) - 1 input_dim = convertor.get_dim() rnn_layer = SimpleRNN(HIDDEN_SIZE, input_shape=(MAXLEN, convertor.get_dim()), return_sequences=True) shift_layer = Shift(rnn_layer, initial_value) model = Graph() model.add_input(name="initial_value", input_shape=(1, input_dim)) model.add_input(name="sequence_input", input_shape=(MAXLEN, input_dim)) model.add_node(shift_layer, name="shift", input="sequence_input") model.add_node(rnn_layer, name="rnn", input="shift") model.add_node(TimeDistributedDense(input_dim), name="tdd", input="rnn") model.add_node(Activation("softmax"), name="softmax", input="tdd") model.add_output(name="output", input="softmax") model.compile(loss={"output": "categorical_crossentropy"}, optimizer="adam") for iteration in range(1, 200): print() print("-" * 50) print("Iteration", iteration) model.fit( {"initial_value": i_train, "sequence_input": X_train, "output": y_train}, batch_size=BATCH_SIZE, nb_epoch=1, validation_data={"initial_value": i_val, "sequence_input": X_val}, show_accuracy=True, )
name='n23', input='n13') model.add_node(Flatten(), name='n23_f', input='n23') model.add_node(Convolution2D(32, 1, 1, activation='relu'), name='n24', input='n14') model.add_node(Flatten(), name='n24_f', input='n24') # output layer model.add_node(Dense(1024, activation='relu'), name='layer4', inputs=['n11_f', 'n22_f', 'n23_f', 'n24_f'], merge_mode='concat') model.add_node(Dense(10, activation='softmax'), name='layer5', input='layer4') model.add_output(name='output1', input='layer5') print 'Training....' model.compile(loss={'output1': 'categorical_crossentropy'}, optimizer='adam', metrics=['accuracy']) model.fit({ 'n00': x_train, 'output1': y_train }, nb_epoch=nb_epoch, batch_size=batch_size, validation_split=0.3, shuffle=True, verbose=1)
def paragraph_embedding(paragraph_len, sentence_len, wv_params, wv_size, input_name='paragraph_embedding', output_name='vector_embedding'): ''' Creates an embedding of word vectors into a sequence of sentence images. Args: ----- sentence_len: length of sentences to be passed wv_params: a dict of the following format wv_params = { 'fixed_wv' : { 'vocab_size' : 1000, 'init' : None, 'fixed' : True }, 'floating_wv' : { 'vocab_size' : 1000, 'init' : None, 'fixed' : False } } the keys of the dictionary are the names in the keras graph model, and you can have any number of word vector layers encoded. input_name: the name of the input node for the graph output_name: the name of the output node for the graph Returns: -------- a keras container that takes as input an integer array with shape (n_samples, n_words), where n_words is the number of words in the paragraph. ***NOTE*** that n_words should be organized such that n_words = n_sentences * len_sentence! I.e., if you have a len_sentence of 3, and your input is [2, 1, 5, 0, 9, 3, 2, 4, 5], it WILL ASSUME that [[2, 1, 5], [0, 9, 3], [2, 4, 5]] are all sentences in a paragraph. This returns a shape (n_samples, n_sentences, wv_channels, len_sentence, wv_dim)! ''' # -- output is (n_samples, n_sentences, n_channels, n_words, wv_dim) g = SubGraph() # -- fix for new keras backend if KERAS_BACKEND: g.add_input(input_name, (sentence_len * paragraph_len, ), dtype='int') else: g.add_input(input_name, (-1, ), dtype='int') for name, params in wv_params.iteritems(): # g.add_input(params['input_name'], (-1, ), dtype='int') g.add_node(make_embedding(wv_size=wv_size, **params), name=name, input=input_name) if len(wv_params.keys()) > 1: # -- removal for backend compatibility if KERAS_BACKEND: g.add_node(Reshape( (paragraph_len, sentence_len, len(wv_params), wv_size)), name='reshape', inputs=wv_params.keys(), merge_mode='concat') else: g.add_node(Reshape((-1, sentence_len, len(wv_params), wv_size)), name='reshape', inputs=wv_params.keys(), merge_mode='concat') else: # -- removal for backend compatibility if KERAS_BACKEND: g.add_node(Reshape( (paragraph_len, sentence_len, len(wv_params), wv_size)), name='reshape', input=wv_params.keys()[0]) else: g.add_node(Reshape((-1, sentence_len, len(wv_params), wv_size)), name='reshape', inputs=wv_params.keys(), merge_mode='concat') g.add_node(Permute(dims=(1, 3, 2, 4)), name='permute', input='reshape') # -- output is of shape (nb_samples, nb_wv_channels, len_sentence, wv_dim) g.add_output(name=output_name, input='permute') return g
def paragraph_embedding(paragraph_len, sentence_len, wv_params, wv_size, input_name='paragraph_embedding', output_name='vector_embedding'): ''' Creates an embedding of word vectors into a sequence of sentence images. Args: ----- sentence_len: length of sentences to be passed wv_params: a dict of the following format wv_params = { 'fixed_wv' : { 'vocab_size' : 1000, 'init' : None, 'fixed' : True }, 'floating_wv' : { 'vocab_size' : 1000, 'init' : None, 'fixed' : False } } the keys of the dictionary are the names in the keras graph model, and you can have any number of word vector layers encoded. input_name: the name of the input node for the graph output_name: the name of the output node for the graph Returns: -------- a keras container that takes as input an integer array with shape (n_samples, n_words), where n_words is the number of words in the paragraph. ***NOTE*** that n_words should be organized such that n_words = n_sentences * len_sentence! I.e., if you have a len_sentence of 3, and your input is [2, 1, 5, 0, 9, 3, 2, 4, 5], it WILL ASSUME that [[2, 1, 5], [0, 9, 3], [2, 4, 5]] are all sentences in a paragraph. This returns a shape (n_samples, n_sentences, wv_channels, len_sentence, wv_dim)! ''' # -- output is (n_samples, n_sentences, n_channels, n_words, wv_dim) g = SubGraph() # -- fix for new keras backend if KERAS_BACKEND: g.add_input(input_name, (sentence_len * paragraph_len, ), dtype='int') else: g.add_input(input_name, (-1, ), dtype='int') for name, params in wv_params.iteritems(): # g.add_input(params['input_name'], (-1, ), dtype='int') g.add_node(make_embedding(wv_size=wv_size, **params), name=name, input=input_name) if len(wv_params.keys()) > 1: # -- removal for backend compatibility if KERAS_BACKEND: g.add_node(Reshape((paragraph_len, sentence_len, len(wv_params), wv_size)), name='reshape', inputs=wv_params.keys(), merge_mode='concat') else: g.add_node(Reshape((-1, sentence_len, len(wv_params), wv_size)), name='reshape', inputs=wv_params.keys(), merge_mode='concat') else: # -- removal for backend compatibility if KERAS_BACKEND: g.add_node(Reshape((paragraph_len, sentence_len, len(wv_params), wv_size)), name='reshape', input=wv_params.keys()[0]) else: g.add_node(Reshape((-1, sentence_len, len(wv_params), wv_size)), name='reshape', inputs=wv_params.keys(), merge_mode='concat') g.add_node(Permute(dims=(1, 3, 2, 4)), name='permute', input='reshape') # -- output is of shape (nb_samples, nb_wv_channels, len_sentence, wv_dim) g.add_output(name=output_name, input='permute') return g