y = convertor.encode_dataset(chains) split_at = len(y) - len(y) / 10 (y_train, y_val) = (y[:split_at], y[split_at:]) (i_train, i_val) = (initial_value[:split_at], initial_value[split_at:]) (X_train, X_val) = (y_train, y_val) print(i_train.shape) print(y_train.shape) 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(
y_train = np_utils.to_categorical(y_train) y_test = np_utils.to_categorical(y_test) y_train.shape, y_test.shape batch_size = 128 nb_epoch = 50 print 'X_train shape: ', x_train.shape print 'y_train shape: ', y_train.shape print 'img size: ', x_train.shape[2], x_train.shape[3] print 'batch size: ', batch_size print 'nb_epoch: ', nb_epoch # model architecture: model = Graph() model.add_input(name='n00', input_shape=(1, 48, 48)) # layer 1 model.add_node(Convolution2D(64, 1, 1, activation='relu'), name='n11', input='n00') model.add_node(Flatten(), name='n11_f', input='n11') model.add_node(Convolution2D(96, 1, 1, activation='relu'), name='n12', input='n00') model.add_node(Convolution2D(16, 1, 1, activation='relu'), name='n13',
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
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 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
PARAGRAPH_LENGTH = 50 INPUT_SHAPE = (SENTENCE_LENGTH * PARAGRAPH_LENGTH, ) global_vectors = Sequential([Embedding(input_dim=gb_global.W.shape[0], output_dim=300, weights=[gb_global.W], input_length=INPUT_SHAPE[0])]) imdb_vectors = Sequential([Embedding(input_dim=gb_imdb.W.shape[0], output_dim=300, weights=[gb_imdb.W], input_length=INPUT_SHAPE[0])]) model = Sequential() model.add(Merge([global_vectors, imdb_vectors], mode='concat')) model.add(Reshape((PARAGRAPH_LENGTH, SENTENCE_LENGTH, 2, 300))) model.add(Permute(dims=(1, 3, 2, 4))) # -- create convolution units... conv_unit = SubGraph() conv_unit = Graph() conv_unit.add_input('embeddings', input_shape=model.output_shape[1:]) for n in NGRAMS: conv_unit.add_node( TimeDistributed(Convolution2D(NFILTERS, n, 300, W_regularizer=l2(0.0001), activation='relu') ), name='conv{}gram'.format(n), input='embeddings' ) conv_unit.add_node( TimeDistributed(MaxPooling2D(pool_size=(SENTENCE_LENGTH - n + 1, 1))), name='maxpool{}gram'.format(n), input='conv{}gram'.format(n)
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