def test_2o_1i_weights(self): print('test a non-sequential graph with 1 input and 2 outputs') graph = Graph() graph.add_input(name='input1', ndim=2) graph.add_node(Dense(32, 16), name='dense1', input='input1') graph.add_node(Dense(32, 4), name='dense2', input='input1') graph.add_node(Dense(16, 1), name='dense3', input='dense1') graph.add_output(name='output1', input='dense2') graph.add_output(name='output2', input='dense3') graph.compile('rmsprop', {'output1': 'mse', 'output2': 'mse'}) history = graph.fit( { 'input1': X_train, 'output1': y_train, 'output2': y2_train }, nb_epoch=10) out = graph.predict({'input1': X_test}) assert (type(out == dict)) assert (len(out) == 2) loss = graph.test_on_batch({ 'input1': X_test, 'output1': y_test, 'output2': y2_test }) loss = graph.train_on_batch({ 'input1': X_test, 'output1': y_test, 'output2': y2_test }) loss = graph.evaluate({ 'input1': X_test, 'output1': y_test, 'output2': y2_test }) print(loss) assert (loss < 4.) print('test weight saving') graph.save_weights('temp.h5', overwrite=True) graph = Graph() graph.add_input(name='input1', ndim=2) graph.add_node(Dense(32, 16), name='dense1', input='input1') graph.add_node(Dense(32, 4), name='dense2', input='input1') graph.add_node(Dense(16, 1), name='dense3', input='dense1') graph.add_output(name='output1', input='dense2') graph.add_output(name='output2', input='dense3') graph.compile('rmsprop', {'output1': 'mse', 'output2': 'mse'}) graph.load_weights('temp.h5') nloss = graph.evaluate({ 'input1': X_test, 'output1': y_test, 'output2': y2_test }) print(nloss) assert (loss == nloss)
def get_graph(num_items, latent_dim, number_of_timestamps, number_of_channels): batch_input_shape = (1, number_of_timestamps, number_of_channels) # batch_input_shape = None magic_num = 90 model = Graph() for i in range(magic_num): model.add_input('positive_item_input_{}'.format(i), input_shape=(number_of_timestamps, number_of_channels), batch_input_shape=batch_input_shape) model.add_shared_node( get_item_subgraph((num_items, ), latent_dim), name='item_latent', inputs=["positive_item_input_{}".format(i) for i in range(magic_num)], merge_mode='join') # Compute loss model.add_node(Lambda(per_char_loss), name='triplet_loss', input='item_latent') # Add output model.add_output(name='triplet_loss', input='triplet_loss') model.compile(loss={'triplet_loss': identity_loss}, optimizer='sgd') # Adagrad(lr=0.1, epsilon=1e-06)) return model
def prep_model(glove, vocab, dropout=1/2, dropout_w=0, dropout_in=4/5, l2reg=1e-4, cnnact='tanh', cnninit='glorot_uniform', cdim={1: 1, 2: 1/2, 3: 1/2, 4: 1/2, 5: 1/2}, project=True, pdim=2.5, ptscorer=B.mlp_ptscorer, mlpsum='sum', Ddim=1, oact='sigmoid'): model = Graph() N = B.embedding(model, glove, vocab, s0pad, s1pad, dropout, dropout_w) if dropout_in is None: dropout_in = dropout Nc = B.cnnsum_input(model, N, s0pad, dropout=dropout_in, l2reg=l2reg, cnninit=cnninit, cnnact=cnnact, cdim=cdim) # Projection if project: model.add_shared_node(name='proj', inputs=['e0s_', 'e1s_'], outputs=['e0p', 'e1p'], layer=Dense(input_dim=Nc, output_dim=int(N*pdim), W_regularizer=l2(l2reg))) # This dropout is controversial; it might be harmful to apply, # or at least isn't a clear win. # model.add_shared_node(name='projdrop', inputs=['e0p', 'e1p'], outputs=['e0p_', 'e1p_'], # layer=Dropout(dropout_in, input_shape=(N,))) # final_outputs = ['e0p_', 'e1p_'] final_outputs = ['e0p', 'e1p'] else: final_outputs = ['e0s_', 'e1s_'] # Measurement kwargs = dict() if ptscorer == B.mlp_ptscorer: kwargs['sum_mode'] = mlpsum model.add_node(name='scoreS', input=ptscorer(model, final_outputs, Ddim, N, l2reg, **kwargs), layer=Activation(oact)) model.add_output(name='score', input='scoreS') return model
def test_recursive(): # test layer-like API graph = Graph() graph.add_input(name='input1', input_shape=(32, )) graph.add_node(Dense(16), name='dense1', input='input1') graph.add_node(Dense(4), name='dense2', input='input1') graph.add_node(Dense(4), name='dense3', input='dense1') graph.add_output(name='output1', inputs=['dense2', 'dense3'], merge_mode='sum') seq = Sequential() seq.add(Dense(32, input_shape=(32, ))) seq.add(graph) seq.add(Dense(4)) seq.compile('rmsprop', 'mse') seq.fit(X_train_graph, y_train_graph, batch_size=10, nb_epoch=10) loss = seq.evaluate(X_test_graph, y_test_graph) # test serialization config = seq.get_config() new_graph = Sequential.from_config(config) seq.summary() json_str = seq.to_json() new_graph = model_from_json(json_str) yaml_str = seq.to_yaml() new_graph = model_from_yaml(yaml_str)
def conv(input_shape): graph = Graph() graph.add_input("input", input_shape) graph.add_node(Convolution2D(32, 3, 3, subsample=(2, 2), activation=activation_function()), name="conv1", input="input") graph.add_node(Convolution2D(32, 3, 3, activation=activation_function()), name="conv2", input="conv1") graph.add_node(Convolution2D(64, 3, 3, activation=activation_function()), name="conv3", input="conv2") graph.add_node(MaxPooling2D((3, 3), stride=(2, 2)), name="pool4", input="conv3") graph.add_node(Convolution2D(80, 3, 3, activation=activation_function()), name="conv5", input="pool4") graph.add_node(Convolution2D(192, 3, 3, subsample=(2, 2), activation=activation_function()), name="conv6", input="conv5") graph.add_node(Convolution2D(288, 3, 3, activation=activation_function()), name="conv7", input="conv6") graph.add_output("output", input="conv7") return graph
def VGG_like_convnet_graph(data_shape, opt): # create a network model = Graph() model.add_input(name='data_in', input_shape=(data_shape[0], data_shape[1], data_shape[2])) model.add_node(Convolution2D(32, 3, 3, border_mode='valid'), name='conv1', input='data_in') model.add_node(Activation('relu'), name='relu1', input='conv1') model.add_node(Convolution2D(32, 3, 3), name='conv2', input='relu1') model.add_node(Activation('relu'), name='relu2', input='conv2') model.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool1', input='relu2') # model.add(Dropout(0.25)) model.add_node(Convolution2D(64, 3, 3, border_mode='valid'), name='conv3', input='pool1') model.add_node(Activation('relu'), name='relu3', input='conv3') model.add_node(Convolution2D(64, 3, 3), name='conv4', input='relu3') model.add_node(Activation('relu'), name='relu4', input='conv4') model.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool2', input='relu4') # model.add(Dropout(0.25)) model.add_node(Flatten(), name='flatten', input='pool2') model.add_node(Dense(256), name='dense1', input='flatten') model.add_node(Activation('relu'), name='relu5', input='dense1') model.add_node(Dense(2), name='dense2', input='relu5') model.add_node(Activation('softmax'), name='softmax_out', input='dense2') model.add_output(name='class_out', input='softmax_out') print('VGG_like_convnet_graph... nb params: {}'.format( model.count_params())) model.compile(loss={'class_out': 'categorical_crossentropy'}, optimizer=opt) return model
def build_residual_block(name, input_shape, n_hidden, n_skip=2): """ Rough sketch of building blocks of layers for residual learning. See http://arxiv.org/abs/1512.03385 for motivation. """ block = Graph() input_name = 'x' block.add_input(input_name, input_shape=input_shape) # The current keras graph implementation doesn't allow you to connect # an input node to an output node. Use Identity to work around that. block.add_node(Identity(), name=name + 'identity', input=input_name) prev_output = input_name for i in range(n_skip): layer_name = 'h' + str(i) l = Dense(n_hidden, activation='relu') block.add_node(l, name=layer_name, input=prev_output) prev_output = layer_name if i < n_skip: block.add_node(Dropout(0.5), name=layer_name + 'do', input=layer_name) prev_output = layer_name + 'do' block.add_output(name=name + 'output', inputs=[name + 'identity', prev_output], merge_mode='sum') return block
def test_siamese_5(): graph = Graph() graph.add_input(name='input1', input_shape=(32,)) graph.add_input(name='input2', input_shape=(32,)) graph.add_shared_node(Dense(16), name='shared1', inputs=['input1', 'input2']) graph.add_shared_node(Dense(4), name='shared2', inputs=['shared1']) graph.add_shared_node(Dense(4), name='shared3', inputs=['shared2'], outputs=['shared_output1','shared_output2']) graph.add_node(Dense(4), name='dense1', input='shared_output1') graph.add_node(Dense(4), name='dense2', input='shared_output2') graph.add_output(name='output1', inputs=['dense1', 'dense2'], merge_mode='sum') graph.compile('rmsprop', {'output1': 'mse'}) graph.fit({'input1': X_train_graph, 'input2': X2_train_graph, 'output1': y_train_graph}, nb_epoch=10) out = graph.predict({'input1': X_test_graph, 'input2': X2_test_graph}) assert(type(out == dict)) assert(len(out) == 1) loss = graph.test_on_batch({'input1': X_test_graph, 'input2': X2_test_graph, 'output1': y_test_graph}) loss = graph.train_on_batch({'input1': X_test_graph, 'input2': X2_test_graph, 'output1': y_test_graph}) loss = graph.evaluate({'input1': X_test_graph, 'input2': X2_test_graph, 'output1': y_test_graph}) assert(loss < 3.0) graph.get_config(verbose=1)
def test_2o_1i_sample_weights(): # test a non-sequential graph with 1 input and 2 outputs with sample weights graph = Graph() graph.add_input(name='input1', input_shape=(32,)) graph.add_node(Dense(16), name='dense1', input='input1') graph.add_node(Dense(4), name='dense2', input='input1') graph.add_node(Dense(1), name='dense3', input='dense1') graph.add_output(name='output1', input='dense2') graph.add_output(name='output2', input='dense3') weights1 = np.random.uniform(size=y_train_graph.shape[0]) weights2 = np.random.uniform(size=y2_train_graph.shape[0]) weights1_test = np.random.uniform(size=y_test_graph.shape[0]) weights2_test = np.random.uniform(size=y2_test_graph.shape[0]) graph.compile('rmsprop', {'output1': 'mse', 'output2': 'mse'}) graph.fit({'input1': X_train_graph, 'output1': y_train_graph, 'output2': y2_train_graph}, nb_epoch=10, sample_weight={'output1': weights1, 'output2': weights2}) out = graph.predict({'input1': X_test_graph}) assert(type(out == dict)) assert(len(out) == 2) loss = graph.test_on_batch({'input1': X_test_graph, 'output1': y_test_graph, 'output2': y2_test_graph}, sample_weight={'output1': weights1_test, 'output2': weights2_test}) loss = graph.train_on_batch({'input1': X_train_graph, 'output1': y_train_graph, 'output2': y2_train_graph}, sample_weight={'output1': weights1, 'output2': weights2}) loss = graph.evaluate({'input1': X_train_graph, 'output1': y_train_graph, 'output2': y2_train_graph}, sample_weight={'output1': weights1, 'output2': weights2})
def test_1o_1i_2(): # test a more complex non-sequential graph with 1 input and 1 output graph = Graph() graph.add_input(name='input1', input_shape=(32,)) graph.add_node(Dense(16), name='dense1', input='input1') graph.add_node(Dense(4), name='dense2-0', input='input1') graph.add_node(Activation('relu'), name='dense2', input='dense2-0') graph.add_node(Dense(16), name='dense3', input='dense2') graph.add_node(Dense(4), name='dense4', inputs=['dense1', 'dense3'], merge_mode='sum') graph.add_output(name='output1', inputs=['dense2', 'dense4'], merge_mode='sum') graph.compile('rmsprop', {'output1': 'mse'}) graph.fit({'input1': X_train_graph, 'output1': y_train_graph}, nb_epoch=10) out = graph.predict({'input1': X_train_graph}) assert(type(out == dict)) assert(len(out) == 1) loss = graph.test_on_batch({'input1': X_test_graph, 'output1': y_test_graph}) loss = graph.train_on_batch({'input1': X_test_graph, 'output1': y_test_graph}) loss = graph.evaluate({'input1': X_test_graph, 'output1': y_test_graph}) assert(loss < 2.5) graph.get_config(verbose=1) graph.summary()
def test_1o_2i(): # test a non-sequential graph with 2 inputs and 1 output graph = Graph() graph.add_input(name='input1', input_shape=(32,)) graph.add_input(name='input2', input_shape=(32,)) graph.add_node(Dense(16), name='dense1', input='input1') graph.add_node(Dense(4), name='dense2', input='input2') graph.add_node(Dense(4), name='dense3', input='dense1') graph.add_output(name='output1', inputs=['dense2', 'dense3'], merge_mode='sum') graph.compile('rmsprop', {'output1': 'mse'}) graph.fit({'input1': X_train_graph, 'input2': X2_train_graph, 'output1': y_train_graph}, nb_epoch=10) out = graph.predict({'input1': X_test_graph, 'input2': X2_test_graph}) assert(type(out == dict)) assert(len(out) == 1) loss = graph.test_on_batch({'input1': X_test_graph, 'input2': X2_test_graph, 'output1': y_test_graph}) loss = graph.train_on_batch({'input1': X_test_graph, 'input2': X2_test_graph, 'output1': y_test_graph}) loss = graph.evaluate({'input1': X_test_graph, 'input2': X2_test_graph, 'output1': y_test_graph}) assert(loss < 3.0) graph.get_config(verbose=1)
def test_1o_1i(): # test a non-sequential graph with 1 input and 1 output np.random.seed(1337) graph = Graph() graph.add_input(name='input1', input_shape=(32,)) graph.add_node(Dense(16), name='dense1', input='input1') graph.add_node(Dense(4), name='dense2', input='input1') graph.add_node(Dense(4), name='dense3', input='dense1') graph.add_output(name='output1', inputs=['dense2', 'dense3'], merge_mode='sum') graph.compile('rmsprop', {'output1': 'mse'}) graph.fit({'input1': X_train_graph, 'output1': y_train_graph}, nb_epoch=10) out = graph.predict({'input1': X_test_graph}) assert(type(out == dict)) assert(len(out) == 1) loss = graph.test_on_batch({'input1': X_test_graph, 'output1': y_test_graph}) loss = graph.train_on_batch({'input1': X_test_graph, 'output1': y_test_graph}) loss = graph.evaluate({'input1': X_test_graph, 'output1': y_test_graph}, verbose=0) assert(loss < 2.5) # test validation split graph.fit({'input1': X_train_graph, 'output1': y_train_graph}, validation_split=0.2, nb_epoch=1) # test validation data graph.fit({'input1': X_train_graph, 'output1': y_train_graph}, validation_data={'input1': X_train_graph, 'output1': y_train_graph}, nb_epoch=1)
def test_graph_fit_generator(): def data_generator_graph(train): while 1: if train: yield {'input1': X_train_graph, 'output1': y_train_graph} else: yield {'input1': X_test_graph, 'output1': y_test_graph} graph = Graph() graph.add_input(name='input1', input_shape=(32,)) graph.add_node(Dense(16), name='dense1', input='input1') graph.add_node(Dense(4), name='dense2', input='input1') graph.add_node(Dense(4), name='dense3', input='dense1') graph.add_output(name='output1', inputs=['dense2', 'dense3'], merge_mode='sum') graph.compile('rmsprop', {'output1': 'mse'}) graph.fit_generator(data_generator_graph(True), 1000, nb_epoch=4) graph.fit_generator(data_generator_graph(True), 1000, nb_epoch=4) graph.fit_generator(data_generator_graph(True), 1000, nb_epoch=4, validation_data={'input1': X_test_graph, 'output1': y_test_graph}) graph.fit_generator(data_generator_graph(True), 1000, nb_epoch=4, validation_data={'input1': X_test_graph, 'output1': y_test_graph}) loss = graph.evaluate({'input1': X_test_graph, 'output1': y_test_graph}, verbose=0) assert(loss < 3.)
def get_graph(num_users, num_items, latent_dim): model = Graph() model.add_input(name='user_input', input_shape=(num_users, )) model.add_input(name='positive_item_input', input_shape=(num_items, )) model.add_input(name='negative_item_input', input_shape=(num_items, )) model.add_node(layer=Dense(latent_dim, input_shape=(num_users, )), name='user_latent', input='user_input') model.add_shared_node( layer=Dense(latent_dim, input_shape=(num_items, )), name='item_latent', inputs=['positive_item_input', 'negative_item_input'], merge_mode=None, outputs=['positive_item_latent', 'negative_item_latent']) model.add_node(layer=Activation('linear'), name='user_pos', inputs=['user_latent', 'positive_item_latent'], merge_mode='dot', dot_axes=1) model.add_node(layer=Activation('linear'), name='user_neg', inputs=['user_latent', 'negative_item_latent'], merge_mode='dot', dot_axes=1) model.add_output(name='triplet_loss_out', inputs=['user_pos', 'user_neg']) model.compile(loss={'triplet_loss_out': ranking_loss}, optimizer=Adam()) #Adagrad(lr=0.1, epsilon=1e-06)) return model
def qualitative_CNN(self, vocab_size, emb_dim, max_len, nb_filters): self.max_len = max_len max_features = vocab_size filter_lengths = [3, 4, 5] print("Build model...") self.qual_model = Graph() self.qual_conv_set = {} '''Embedding Layer''' self.qual_model.add_input( name='input', input_shape=(max_len,), dtype=int) self.qual_model.add_node(Embedding(max_features, emb_dim, input_length=max_len, weights=self.model.nodes['sentence_embeddings'].get_weights()), name='sentence_embeddings', input='input') '''Convolution Layer & Max Pooling Layer''' for i in filter_lengths: model_internal = Sequential() model_internal.add( Reshape(dims=(1, max_len, emb_dim), input_shape=(max_len, emb_dim))) self.qual_conv_set[i] = Convolution2D(nb_filters, i, emb_dim, activation="relu", weights=self.model.nodes[ 'unit_' + str(i)].layers[1].get_weights()) model_internal.add(self.qual_conv_set[i]) model_internal.add(MaxPooling2D(pool_size=(max_len - i + 1, 1))) model_internal.add(Flatten()) self.qual_model.add_node( model_internal, name='unit_' + str(i), input='sentence_embeddings') self.qual_model.add_output( name='output_' + str(i), input='unit_' + str(i)) self.qual_model.compile( 'rmsprop', {'output_3': 'mse', 'output_4': 'mse', 'output_5': 'mse'})
def block3(input_shape, nb_filter1, nb_filter2, is_edge): zerop = (1, 1) subsample = (1, 1) if is_edge: zerop = (2, 2) subsample = (2, 2) g = Graph() g.add_input("input", input_shape) zero = Zero(input_shape, zerop) conv1 = ConvB(cinput_shape(zero), nb_filter1, 1, 1, subsample=subsample) conv2 = ConvB(cinput_shape(conv1), nb_filter1, 3, 3) conv3 = ConvB(cinput_shape(conv2), nb_filter2, 1, 1) shortcut = ConvB(input_shape, nb_filter2, 1, 1, subsample=subsample) g.add_node(zero, "zero", "input") g.add_node(conv1, "conv1", "zero") g.add_node(conv2, "conv2", "conv1") g.add_node(conv3, "conv3", "conv2") g.add_node(shortcut, "shortcut", "input") g.add_output("output", inputs=["conv3", "shortcut"], merge_mode="sum") return g
def get_model(inputdim, outputdim, regularization_strength=0.01, lr=0.000, cosine=False, **kwargs): transformation = Dense(inputdim, init='identity', W_constraint=Orthogonal()) model = Graph() model.add_input(name='embeddings1', input_shape=(inputdim,)) model.add_input(name='embeddings2', input_shape=(inputdim,)) model.add_shared_node(transformation, name='transformation', inputs=['embeddings1', 'embeddings2'], outputs=['transformed1', 'transformed2']) model.add_node(Lambda(lambda x: x[:, :outputdim]), input='transformed1', name='projected1') model.add_node(Lambda(lambda x: -x[:, :outputdim]), input='transformed2', name='negprojected2') if cosine: model.add_node(Lambda(lambda x: x / K.reshape(K.sqrt(K.sum(x * x, axis=1)), (x.shape[0], 1))), name='normalized1', input='projected1') model.add_node(Lambda(lambda x: x / K.reshape(K.sqrt(K.sum(x * x, axis=1)), (x.shape[0], 1))), name='negnormalized2', input='negprojected2') model.add_node(Lambda(lambda x: K.reshape(K.sum(x, axis=1), (x.shape[0], 1))), name='distances', inputs=['normalized1', 'negnormalized2'], merge_mode='mul') else: model.add_node(Lambda(lambda x: K.reshape(K.sqrt(K.sum(x * x, axis=1)), (x.shape[0], 1))), name='distances', inputs=['projected1', 'negprojected2'], merge_mode='sum') model.add_output(name='y', input='distances') model.compile(loss={'y': lambda y, d: K.mean(y * d)}, optimizer=SimpleSGD()) return model
def createModel(): model = Graph() model.add_input(name="cards", input_shape=(52*9,)) model.add_input(name="state", input_shape=(81+8*8,)) model.add_input(name="bet", input_shape=(1,)) model.add_node(layer=Dense(1024), name="cards1", input="cards") model.add_node(layer=Activation("relu"), name="cards1Act", input="cards1") model.add_node(layer=BatchNormalization(), name='card1sNorm', input='cards1Act') model.add_node(layer=Dense(1024), name="cards2", input="cards1Norm") model.add_node(layer=Activation("relu"), name="cards2Act", input="cards2") model.add_node(layer=BatchNormalization(), name='cards2Norm', input='cards2Act') model.add_node(layer=Dense(1024), name="sum", inputs=["cards2Norm", "state", "bet"]) model.add_node(layer=Activation("relu"), name="sumAct", input="sum") model.add_node(layer=BatchNormalization(), name='sumNorm', input='sumAct') model.add_node(layer=Dense(1), name="res", input="sumNorm") model.add_node(layer=Activation("linear"), name="resAct", input="res") model.add_output(name='output', input='resAct') model.compile('adam', {'output':'mse'}) return model
def prep_model(glove, vocab, module_prep_model, c, oact, s0pad, s1pad): # Input embedding and encoding model = Graph() N = B.embedding(model, glove, vocab, s0pad, s1pad, c['inp_e_dropout'], c['inp_w_dropout'], add_flags=c['e_add_flags']) # Sentence-aggregate embeddings final_outputs = module_prep_model(model, N, s0pad, s1pad, c) # Measurement if c['ptscorer'] == '1': # special scoring mode just based on the answer # (assuming that the question match is carried over to the answer # via attention or another mechanism) ptscorer = B.cat_ptscorer final_outputs = final_outputs[1] else: ptscorer = c['ptscorer'] kwargs = dict() if ptscorer == B.mlp_ptscorer: kwargs['sum_mode'] = c['mlpsum'] model.add_node(name='scoreS', input=ptscorer(model, final_outputs, c['Ddim'], N, c['l2reg'], **kwargs), layer=Activation(oact)) model.add_output(name='score', input='scoreS') return model
def get_model(in_shape, out_shape): def get_conv_network(in_shape, out_shape): model = Sequential() model.add(Lambda(lambda x: x / 255. - 0.5, input_shape=in_shape)) model.add(Convolution2D(32, 3, 3, border_mode='same')) model.add(Activation('relu')) model.add(Convolution2D(out_shape[0], 3, 3, border_mode='same')) return model #conv_net = get_conv_network(in_shape, out_shape) g = Graph() g.add_input(name='input', input_shape=in_shape) g.add_node(Lambda(lambda x: x / 255. - 0.5), name='normalize', input='input') g.add_node(Convolution2D(out_shape[0], 3, 3, border_mode='same'), name='conv', input='normalize') g.add_output(name='output', input='conv') g.compile( loss={'output': 'mean_squared_error'}, optimizer=Adam( lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, ), ) return g
def build_model(nb_filters=32, nb_pool=2, nb_conv=3): C_1 = 64 C_2 = 32 C_3 = 16 c = Convolution2D(C_1, nb_conv, nb_conv, border_mode='same', input_shape=(3, 32, 32)) mp = MaxPooling2D(pool_size=(nb_pool, nb_pool)) c2 = Convolution2D(C_2, nb_conv, nb_conv, border_mode='same', input_shape=(3, 32, 32)) mp2 = MaxPooling2D(pool_size=(nb_pool, nb_pool)) d = Dense(100) encoder = get_encoder(c, c2, d, mp, mp2) decoder = get_decoder(C_1, C_2, C_3, c, c2, d, mp, mp2, nb_pool) graph = Graph() graph.add_input(name='input', input_shape=(3, 32, 32)) graph.add_node(encoder, name='encoder', input='input') graph.add_node(decoder, name='decoder', input='encoder') graph.add_output(name='autoencoder_feedback', input='decoder') graph.compile('rmsprop', {'autoencoder_feedback': 'mean_squared_error'}) return graph
def test_1o_1i_2(self): print('test a more complex non-sequential graph with 1 input and 1 output') graph = Graph() graph.add_input(name='input1', ndim=2) graph.add_node(Dense(32, 16), name='dense1', input='input1') graph.add_node(Dense(32, 4), name='dense2-0', input='input1') graph.add_node(Activation('relu'), name='dense2', input='dense2-0') graph.add_node(Dense(4, 16), name='dense3', input='dense2') graph.add_node(Dense(16, 4), name='dense4', inputs=['dense1', 'dense3'], merge_mode='sum') graph.add_output(name='output1', inputs=['dense2', 'dense4'], merge_mode='sum') graph.compile('rmsprop', {'output1':'mse'}) history = graph.fit({'input1':X_train, 'output1':y_train}, nb_epoch=10) out = graph.predict({'input1':X_train}) assert(type(out == dict)) assert(len(out) == 1) loss = graph.test_on_batch({'input1':X_test, 'output1':y_test}) loss = graph.train_on_batch({'input1':X_test, 'output1':y_test}) loss = graph.evaluate({'input1':X_test, 'output1':y_test}) print(loss) assert(loss < 1.4) graph.get_config(verbose=1)
def inception5(input_shape): graph = Graph() graph.add_input("input", input_shape) graph.add_node(Convolution2D(16, 1, 1, activation=activation_function()), "conv1_1", "input") graph.add_node(ZeroPadding2D(padding=(2, 2)), "zero1_2", "conv1_1") graph.add_node(Convolution2D(32, 3, 3, activation=activation_function()), "conv1_3", "zero1_2") graph.add_node(Convolution2D(32, 3, 3, activation=activation_function()), "conv1_4", "conv1_3") graph.add_node(Convolution2D(96, 1, 1, activation=activation_function()), "conv2_1", "input") graph.add_node(ZeroPadding2D(padding=(1, 1)), "zero2_2", "conv2_1") graph.add_node(Convolution2D(128, 3, 3, activation=activation_function()), "conv2_3", "zero2_2") graph.add_node(ZeroPadding2D(padding=(1, 1)), "zero3_1", "input") graph.add_node(MaxPooling2D((3, 3), stride=(1, 1)), "pool3_2", "zero3_1") graph.add_node(Convolution2D(32, 1, 1, activation=activation_function()), "conv3_3", "pool3_2") graph.add_node(Convolution2D(64, 1, 1, activation=activation_function()), "conv4_1", "input") graph.add_output("output", inputs=["conv1_4", "conv2_3", "conv3_3", "conv4_1"], merge_mode="concat", concat_axis=1) return graph
def prep_model(self, module_prep_model, oact='sigmoid'): # Input embedding and encoding model = Graph() N = B.embedding(model, self.emb, self.vocab, self.s0pad, self.s1pad, self.c['inp_e_dropout'], self.c['inp_w_dropout'], add_flags=self.c['e_add_flags']) # Sentence-aggregate embeddings final_outputs = module_prep_model(model, N, self.s0pad, self.s1pad, self.c) # Measurement if self.c['ptscorer'] == '1': # special scoring mode just based on the answer # (assuming that the question match is carried over to the answer # via attention or another mechanism) ptscorer = B.cat_ptscorer final_outputs = final_outputs[1] else: ptscorer = self.c['ptscorer'] kwargs = dict() if ptscorer == B.mlp_ptscorer: kwargs['sum_mode'] = self.c['mlpsum'] if self.c['f_add_kw']: model.add_input('kw', input_shape=(1,)) model.add_input('akw', input_shape=(1,)) kwargs['extra_inp'] = ['kw', 'akw'] model.add_node(name='scoreS', input=ptscorer(model, final_outputs, self.c['Ddim'], N, self.c['l2reg'], **kwargs), layer=Activation(oact)) model.add_output(name='score', input='scoreS') return model
def test_create_output(self): print('test create_output argument') graph = Graph() graph.add_input(name='input1', ndim=2) graph.add_node(Dense(32, 16), name='dense1', input='input1') graph.add_node(Dense(32, 4), name='dense2', input='input1') graph.add_node(Dense(16, 4), name='dense3', input='dense1') graph.add_node(Dense(4, 4), name='output1', inputs=['dense2', 'dense3'], merge_mode='sum', create_output=True) graph.compile('rmsprop', {'output1': 'mse'}) history = graph.fit({ 'input1': X_train, 'output1': y_train }, nb_epoch=10) out = graph.predict({'input1': X_test}) assert (type(out == dict)) assert (len(out) == 1) loss = graph.test_on_batch({'input1': X_test, 'output1': y_test}) loss = graph.train_on_batch({'input1': X_test, 'output1': y_test}) loss = graph.evaluate({'input1': X_test, 'output1': y_test}) print(loss) assert (loss < 2.5)
def build_keras_model_sg( index_size, vector_size, context_size, #code_dim, sub_batch_size=256, learn_vectors=True, learn_hidden=True, model=None): kerasmodel = Graph() kerasmodel.add_input(name='point', input_shape=(1, ), dtype=int) kerasmodel.add_input(name='index', input_shape=(1, ), dtype=int) kerasmodel.add_node(Embedding(index_size, vector_size, input_length=sub_batch_size, weights=[model.syn0]), name='embedding', input='index') kerasmodel.add_node(Embedding(context_size, vector_size, input_length=sub_batch_size, weights=[model.keras_syn1]), name='embedpoint', input='point') kerasmodel.add_node(Lambda(lambda x: x.sum(2)), name='merge', inputs=['embedding', 'embedpoint'], merge_mode='mul') kerasmodel.add_node(Activation('sigmoid'), name='sigmoid', input='merge') kerasmodel.add_output(name='code', input='sigmoid') kerasmodel.compile('rmsprop', {'code': 'mse'}) return kerasmodel
def test_count_params(): # test count params nb_units = 100 nb_classes = 2 graph = Graph() graph.add_input(name='input1', input_shape=(32, )) graph.add_input(name='input2', input_shape=(32, )) graph.add_node(Dense(nb_units), name='dense1', input='input1') graph.add_node(Dense(nb_classes), name='dense2', input='input2') graph.add_node(Dense(nb_classes), name='dense3', input='dense1') graph.add_output(name='output', inputs=['dense2', 'dense3'], merge_mode='sum') n = 32 * nb_units + nb_units n += 32 * nb_classes + nb_classes n += nb_units * nb_classes + nb_classes assert (n == graph.count_params()) graph.compile('rmsprop', {'output': 'binary_crossentropy'}) assert (n == graph.count_params())
def prep_model(glove, dropout=0, l2reg=1e-4): model = Graph() # Process sentence embeddings model.add_input(name='e0', input_shape=(glove.N, )) model.add_input(name='e1', input_shape=(glove.N, )) model.add_node(name='e0_', input='e0', layer=Dropout(dropout)) model.add_node(name='e1_', input='e1', layer=Dropout(dropout)) # Generate element-wise features from the pair # (the Activation is a nop, merge_mode is the important part) model.add_node(name='sum', inputs=['e0_', 'e1_'], layer=Activation('linear'), merge_mode='sum') model.add_node(name='mul', inputs=['e0_', 'e1_'], layer=Activation('linear'), merge_mode='mul') # Use MLP to generate classes model.add_node(name='hidden', inputs=['sum', 'mul'], merge_mode='concat', layer=Dense(50, W_regularizer=l2(l2reg))) model.add_node(name='hiddenS', input='hidden', layer=Activation('sigmoid')) model.add_node(name='out', input='hiddenS', layer=Dense(6, W_regularizer=l2(l2reg))) model.add_node(name='outS', input='out', layer=Activation('softmax')) model.add_output(name='classes', input='outS') return model
def save_model_arch(): graph = Graph() graph.add_input(name='input', input_shape=(3, 40, 40)) graph.add_node(Convolution2D(32, 9, 9, activation='relu'), name='conv1', input='input') graph.add_node(Convolution2D(32, 9, 9, activation='relu'), name='conv2', input='conv1') graph.add_node(MaxPooling2D(pool_size=(2, 2)), name='pool', input='conv2') graph.add_node(Dropout(0.25), name='drop', input='pool') graph.add_node(Flatten(), name='flatten', input='drop') graph.add_node(Dense(640, activation='relu'), name='ip', input='flatten') graph.add_node(Dropout(0.5), name='drop_out', input='ip') graph.add_node(Dense(19, activation='softmax'), name='result', input='drop_out') graph.add_output(name='out', input='result') print '... compiling' graph.compile(optimizer='adadelta', loss={ 'out': 'categorical_crossentropy', }) graph_json = graph.to_json() return graph_json
def build_graph(Q, js, X_width, conv1_channels, conv1_height, conv1_width, pool1_height, pool1_width, conv2_channels, conv2_height, conv2_width, pool2_height, pool2_width, dense1_channels, dense2_channels, alpha): graph = Graph() # Input X_height = (js[1] - js[0]) * Q X_shape = (1, X_height, X_width) graph.add_input(name="X", input_shape=X_shape) # Shared layers init = "he_normal" conv1 = Convolution2D(conv1_channels, conv1_height, conv1_width, border_mode="valid", init=init) graph.add_node(conv1, name="conv1", input="X") relu1 = LeakyReLU(alpha=alpha) graph.add_node(relu1, name="relu1", input="conv1") pool1 = MaxPooling2D(pool_size=(pool1_height, pool1_width)) graph.add_node(pool1, name="pool1", input="relu1") conv2 = Convolution2D(conv2_channels, conv2_height, conv2_width, border_mode="valid", init=init) graph.add_node(conv2, name="conv2", input="pool1") relu2 = LeakyReLU(alpha=alpha) graph.add_node(relu2, name="relu2", input="conv2") pool2 = MaxPooling2D(pool_size=(pool2_height, pool2_width)) graph.add_node(pool2, name="pool2", input="relu2") flatten = Flatten() graph.add_node(flatten, name="flatten", input="pool2") drop1 = Dropout(0.5) graph.add_node(drop1, name="drop1", input="flatten") dense1 = Dense(dense1_channels, init="lecun_uniform") graph.add_node(dense1, name="dense1", input="drop1") relu3 = LeakyReLU(alpha=alpha) graph.add_node(relu3, name="relu3", input="dense1") drop2 = Dropout(0.5) graph.add_node(drop2, name="drop2", input="relu3") dense2 = Dense(dense2_channels, init="lecun_uniform", activation="softmax") graph.add_node(dense2, name="dense2", input="drop2") # Outputs graph.add_output(name="Y", input="dense2") return graph