def from_config(cls, config, layer_cache=None): '''Supports legacy formats ''' from keras.utils.layer_utils import layer_from_config from keras.layers import Merge assert type(config) is list if not layer_cache: layer_cache = {} def normalize_legacy_config(conf): if 'class_name' not in conf: class_name = conf['name'] name = conf.get('custom_name') conf['name'] = name new_config = { 'class_name': class_name, 'config': conf, } return new_config return conf # the model we will return model = cls() def get_or_create_layer(layer_data): if layer_data['class_name'] == 'Sequential': return Sequential.from_config(layer_data['config'], layer_cache=layer_cache) name = layer_data['config'].get('name') if name in layer_cache: return layer_cache[name] layer = layer_from_config(layer_data) layer_cache[name] = layer return layer first_layer = config[0] first_layer = normalize_legacy_config(first_layer) if first_layer['class_name'] == 'Merge': merge_inputs = [] first_layer_config = first_layer['config'] for merge_input_config in first_layer_config.pop('layers'): merge_input = layer_from_config(merge_input_config) merge_inputs.append(merge_input) first_layer_config['layers'] = merge_inputs merge = Merge.from_config(first_layer_config) model.add(merge) else: layer = get_or_create_layer(first_layer) model.add(layer) for conf in config[1:]: conf = normalize_legacy_config(conf) layer = get_or_create_layer(conf) model.add(layer) return model
def from_config(cls, config, layer_cache=None): """Supports legacy formats """ from keras.utils.layer_utils import layer_from_config from keras.layers import Merge assert type(config) is list if not layer_cache: layer_cache = {} def normalize_legacy_config(conf): if "class_name" not in conf: class_name = conf["name"] name = conf.get("custom_name") conf["name"] = name new_config = {"class_name": class_name, "config": conf} return new_config return conf # the model we will return model = cls() def get_or_create_layer(layer_data): if layer_data["class_name"] == "Sequential": return Sequential.from_config(layer_data["config"], layer_cache=layer_cache) name = layer_data["config"].get("name") if name in layer_cache: return layer_cache[name] layer = layer_from_config(layer_data) layer_cache[name] = layer return layer first_layer = config[0] first_layer = normalize_legacy_config(first_layer) if first_layer["class_name"] == "Merge": merge_inputs = [] first_layer_config = first_layer["config"] for merge_input_config in first_layer_config.pop("layers"): merge_input = layer_from_config(merge_input_config) merge_inputs.append(merge_input) first_layer_config["layers"] = merge_inputs merge = Merge.from_config(first_layer_config) model.add(merge) else: layer = get_or_create_layer(first_layer) model.add(layer) for conf in config[1:]: conf = normalize_legacy_config(conf) layer = get_or_create_layer(conf) model.add(layer) return model
def from_config(cls, config): '''Supports legacy formats ''' from keras.utils.layer_utils import layer_from_config from keras.layers import Merge assert type(config) is list def normalize_legacy_config(conf): if 'class_name' not in conf: class_name = conf['name'] name = conf.get('custom_name') conf['name'] = name new_config = { 'class_name': class_name, 'config': conf, } return new_config return conf model = cls() first_layer = config[0] first_layer = normalize_legacy_config(first_layer) if first_layer['class_name'] == 'Merge': merge_inputs = [] first_layer_config = first_layer['config'] for merge_input_config in first_layer_config.pop('layers'): merge_input = layer_from_config(merge_input_config) merge_inputs.append(merge_input) first_layer_config['layers'] = merge_inputs merge = Merge.from_config(first_layer_config) model.add(merge) else: layer = layer_from_config(first_layer) model.add(layer) for conf in config[1:]: conf = normalize_legacy_config(conf) layer = layer_from_config(conf) model.add(layer) return model
# question_net.add(Dense(EMBEDDING_DIM)) # question_net.add(Bidirectional(GRU(EMBEDDING_DIM))) # question_net.add(Activation('tanh')) # question_net.add(Dropout(0.1)) question_net.add(Bidirectional(LSTM(EMBEDDING_DIM, return_sequences=True))) # question_net.add(GRU(EMBEDDING_DIM, return_sequences=True)) # question_net.add(LSTM(EMBEDDING_DIM, return_sequences=True, consume_less='mem')) print("question layer shape:", question_net.layers[-1].output_shape) # question_net.add(Dense(1, activation='sigmoid')) plot(question_net, to_file='question_net.png', show_shapes=True) # multiply passage by the dot product # add softmax here c_to_q = Merge([passage_net, question_net], mode='dot') cont_to_query = Sequential() cont_to_query.add(c_to_q) cont_to_query.add(Dense(200, activation='softmax')) # cont_to_query.add(Permute((2, 1))) # cont_to_query.add(AveragePooling1D(pool_length=6, stride=None, border_mode='valid')) # cont_to_query.add(Permute((2, 1))) merged = Merge([passage_net, cont_to_query], mode='dot') model = Sequential() model.add(merged) model.add(Permute((2, 1))) model.add(AveragePooling1D(pool_length=50, stride=None, border_mode='valid')) model.add(Permute((2, 1)))
X_rev[i, t, char_indices[char]] = 1 return X, X_rev, y # build the model: a single LSTM print('Build model...') left = Sequential() left.add(LSTM(128, return_sequences=True, input_shape=(maxlen, len(chars)))) right = Sequential() right.add( LSTM(128, go_backwards=True, return_sequences=True, input_shape=(maxlen, len(chars)))) model = Sequential() model.add(Merge([left, right], mode='concat')) model.add(Dropout(0.2)) left = Sequential() right = Sequential() left.add(model) right.add(model) left.add(LSTM(128, return_sequences=False, input_shape=(maxlen, len(chars)))) right.add( LSTM(128, go_backwards=True, return_sequences=False, input_shape=(maxlen, len(chars)))) model = Sequential() model.add(Merge([left, right], mode='concat'))
branch_4.add(MaxPooling1D(pool_size=2)) branch_4.add(Dropout(0.5)) branch_4.add(BatchNormalization()) branch_4.add(LSTM(100)) branch_5 = Sequential() branch_5.add(input_layer) branch_5.add(Conv1D(filters=32, kernel_size=5, padding='same')) branch_5.add(Activation('relu')) branch_5.add(MaxPooling1D(pool_size=2)) branch_5.add(Dropout(0.5)) branch_5.add(BatchNormalization()) branch_5.add(LSTM(100)) model = Sequential() model.add(Merge([branch_3,branch_4,branch_5], mode='concat')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print("") # -+-+-+-+-+-+-+- TRAINING MODEL -+-+-+-+-+-+-+- print("RUNNING MODEL") extra_hist = ExtraHistory() start_time = time.time() hist = model.fit(np.vstack((X_train,X_test)), np.hstack((y_train,y_test)), validation_split=0.5, epochs=inputs.num_epochs, batch_size=64, callbacks=[extra_hist]) end_time = time.time() print_time(start_time, end_time) print("")
def main_func(input_question): question=input_question # question="Jane had 4 apples. She gave 1 to Umesh. How many apples does jane hav now?" RNN = recurrent.LSTM EMBED_HIDDEN_SIZE = 50 SENT_HIDDEN_SIZE = 100 QUERY_HIDDEN_SIZE = 100 BATCH_SIZE = 32 EPOCHS = 5 #print('RNN / Embed / Sent / Query = {}, {}, {}, {}'.format(RNN,EMBED_HIDDEN_SIZE, SENT_HIDDEN_SIZE, QUERY_HIDDEN_SIZE)) train = get_stories(open("DATA/train_LSTM_26112016", 'r',encoding='utf-8')) test = get_stories(open("DATA/test_LSTM_26112016", 'r',encoding='utf-8')) story,query=chunck_question(question) #print(story) new_story=[] new_query=[] for i in story: x=word_tokenize(i) for j in x: new_story.append(str(j)) new_query=word_tokenize(query) n_query=list(map(str,new_query)) vocab = sorted(reduce(lambda x, y: x | y, (set(story + q + [answer]) for story, q, answer in train + test))) for i in n_query: vocab.append(i) for i in new_story: vocab.append(i) vocab_size = len(vocab) + 1 vocab_answer_set = set() for story, q, answer in train + test: for item in answer.split(): if re.search('\+|\-|\*|/', item): vocab_answer_set.add(item) vocab_answer = list(vocab_answer_set) vocab_answer_size = len(vocab_answer) word_idx = OrderedDict((c, i + 1) for i, c in enumerate(vocab)) word_idx_answer = OrderedDict((c, i) for i, c in enumerate(vocab_answer)) word_idx_operator_reverse = OrderedDict((i, c) for i, c in enumerate(vocab_answer)) story_maxlen = max(map(len, (x for x, _, _ in train + test))) query_maxlen = max(map(len, (x for _, x, _ in train + test))) X, Xq, Y = vectorize_stories(train, word_idx, word_idx_answer, story_maxlen, query_maxlen) tX, tXq, tY = vectorize_stories(test, word_idx, word_idx_answer, story_maxlen, query_maxlen) #print("erer"+str(n_query)) xp,xqp=vectorize(new_story,n_query,word_idx,word_idx_answer,story_maxlen,query_maxlen) #print('Build model...') #print(vocab_size, vocab_answer_size) sentrnn = Sequential() sentrnn.add(Embedding(vocab_size, EMBED_HIDDEN_SIZE, input_length=story_maxlen)) sentrnn.add(Dropout(0.3)) qrnn = Sequential() qrnn.add(Embedding(vocab_size, EMBED_HIDDEN_SIZE, input_length=query_maxlen)) qrnn.add(Dropout(0.3)) qrnn.add(RNN(EMBED_HIDDEN_SIZE, return_sequences=False)) qrnn.add(RepeatVector(story_maxlen)) model = Sequential() model.add(Merge([sentrnn, qrnn], mode='sum')) model.add(RNN(EMBED_HIDDEN_SIZE, return_sequences=False)) model.add(Dropout(0.3)) model.add(Dense(vocab_answer_size, activation='softmax')) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) #print('Training') model.fit([X, Xq], Y, batch_size=BATCH_SIZE, nb_epoch=EPOCHS, validation_split=0.05) loss, acc = model.evaluate([tX, tXq], tY, batch_size=BATCH_SIZE) #print("Testing") #print('Test loss / test accuracy = {:.4f} / {:.4f}'.format(loss, acc)) goldLabels = list() predictedLabels = list() for pr in model.predict([xp, xqp]): predictedLabels.append(word_idx_operator_reverse[np.argsort(pr)[-1]]) print(predictedLabels) numlist=[] numlist=list(re.findall(r"[-+]?\d*\.\d+|\d+", input_question)) answer=find_answer(predictedLabels[0],numlist) print(answer) return predictedLabels, answer
def _build_model(self): # Neural Net for Deep-Q learning Model # input image dimensions img_rows, img_cols = 224, 320 # number of convolutional filters to use nb_filters = 32 # size of pooling area for max pooling nb_pool = 2 # convolution kernel size nb_conv = 3 nb_classes = 4 # model = Sequential() img_input = Input(shape=(224, 320, 3)) x = Conv2D(32, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D(32, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block3_conv4')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) # x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) # x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block4_conv4')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 # x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) # x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) # x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) # x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv4')(x) # x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) x = Flatten(name='flatten')(x) # x = Dense(64, activation='relu', name='fc1')(x) x = Dense(64, activation='relu', name='fc2')(x) # x = Dense(nb_classes, activation='softmax', name='predictions')(x) left_branch = Model(img_input, x, name='vgg19') info_input = Input(shape=(5, )) x2 = Dense(12, activation='relu', name='ifc1')(info_input) x2 = Dense(12, activation='relu', name='ifc2')(x2) right_branch = Model(info_input, x2, name='info_net') merged = Merge([left_branch, right_branch], mode='concat') final_model = Sequential() final_model.add(merged) final_model.add(Dense(24, activation='relu')) final_model.add(Dense(nb_classes, activation='softmax')) final_model.compile(loss='mse', optimizer='adam', metrics=['accuracy']) # print(final_model.inputs) # exit() return final_model
plt.show() print(np.mean(ratings['rating'])) # 第一个小网络,处理用户嵌入层 model1 = Sequential() model1.add(Embedding(n_users + 1, k, input_length=1)) model1.add(core.Reshape((k, ))) # keras.layers.core.Reshape # 第二个小网络,处理电影嵌入层 model2 = Sequential() model2.add(Embedding(n_movies + 1, k, input_length=1)) model2.add(core.Reshape((k, ))) # 第三个小网络,在第一,二个网络基础上把用户和电影向量结合在一起 model = Sequential() model.add(Merge([model1, model2], mode='concat')) # 然后加入Dropout和relu这两个非线性变换项,构造多层深度模型 model.add(Dropout(0.2)) model.add(Dense(k, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(int(k / 4), activation='relu')) model.add(Dropout(0.5)) model.add(Dense(int(k / 16), activation='relu')) model.add(Dropout(0.5)) # 因为是预测连续变量评分,最后一层直接上线性变化。(当然,可以尝试分类问题,使用softmax去模拟每个评分类别的概率) model.add(Dense(1, activation='linear')) # 输出层和最后评分作对比,后向传播更新网络参数 model.compile(loss='mse', optimizer='adam')
print("Output shape of 1st convolution (3d):", model_3d.output_shape) model_3d.add( Convolution3D(nb_filters + 10, kernel_size_3d[0], kernel_size_3d[1], kernel_size_3d[2], border_mode='valid')) model_3d.add(Activation('relu')) print("Output shape of 2nd convolution (3d):", model_3d.output_shape) model_3d.add(AveragePooling3D(pool_size=pool_size_3d)) #model_3d.add(Dropout(0.25)) print("Output shape after max pooling (3d):", model_3d.output_shape) model_3d.add(Flatten()) print("Output shape after flatten (3d):", model_3d.output_shape) merged = Merge([model_x, model_y, model_z, model_3d], mode='concat') final_model = Sequential() final_model.add(merged) # print("Output shape after merge:", final_model.output_shape) # final_model.add(Dense(1024)) # final_model.add(Activation('relu')) # final_model.add(Dropout(0.5)) print("Output shape after merge:", final_model.output_shape) final_model.add(Dense(128)) final_model.add(Activation('relu')) print("Output shape after dully connected:", final_model.output_shape) final_model.add(Dense(nb_classes)) final_model.add(Activation('softmax')) print("Output shape after softmax (2 classes):", final_model.output_shape)
input_length=max_seq_length, trainable=False) # Embedded version of the inputs encoded_left = cpp_embedding_layer(left_input) encoded_right = java_embedding_layer(right_input) # Since this is a siamese network, both sides share the same LSTM shared_lstm = LSTM(n_hidden) left_output = shared_lstm(encoded_left) right_output = shared_lstm(encoded_right) # Calculates the distance as defined by the MaLSTM model malstm_distance = Merge( mode=lambda x: exponent_neg_manhattan_distance(x[0], x[1]), output_shape=lambda x: (x[0][0], 1))([left_output, right_output]) # Pack it all up into a model malstm = Model([left_input, right_input], [malstm_distance]) # Adadelta optimizer, with gradient clipping by norm optimizer = Adadelta(clipnorm=gradient_clipping_norm) malstm.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['accuracy']) # Start training training_start_time = time()
def cnnModel(): embedding_layer = Embedding( MAX_NB_WORDS + 1, EMBEDDING_DIM, embeddings_initializer=initializers.he_uniform(20), #weights=[embedding_matrix], input_length=INPUT_LEN, trainable=True) # #trainable=False)) model1 = Sequential() model1.add(embedding_layer) model1.add(Convolution1D(128, 4, padding='same', init='he_normal')) model1.add(BatchNormalization()) model1.add(Activation('relu')) model1.add( Convolution1D(128, 4, padding='same', activation='relu', init='he_normal')) model1.add(GlobalMaxPooling1D()) model2 = Sequential() model2.add(embedding_layer) model2.add(Convolution1D(128, 3, padding='same', init='he_normal')) model2.add(BatchNormalization()) model2.add(Activation('relu')) model2.add( Convolution1D(128, 3, padding='same', activation='relu', init='he_normal')) model2.add(GlobalMaxPooling1D()) model3 = Sequential() model3.add(embedding_layer) model3.add(Convolution1D(128, 5, padding='same', init='he_normal')) model3.add(BatchNormalization()) model3.add(Activation('relu')) model3.add( Convolution1D(128, 5, padding='same', activation='relu', init='he_normal')) model3.add(GlobalMaxPooling1D()) model4 = Sequential() model4.add(embedding_layer) model4.add(Convolution1D(128, 7, padding='same', init='he_normal')) model4.add(BatchNormalization()) model4.add(Activation('relu')) model4.add( Convolution1D(128, 7, padding='same', activation='relu', init='he_normal')) model4.add(GlobalMaxPooling1D()) model = Sequential() model.add( Merge([model1, model2, model3, model4], mode='concat', concat_axis=1)) #model.add(GRU(128, dropout=0.2, recurrent_dropout=0.1)) model.add(Dropout(0.3)) model.add(Dense(128, activation='relu', init='he_normal')) model.add(Dense(LAW_COUNT, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adamax', metrics=[Jaccard_Sim]) model.summary() return model
branch3 = Sequential([ Embedding(nb_words + 1, EMBEDDING_DIM, weights=[embedding_matrix], input_length=MAX_SEQUENCE_LENGTH, trainable=False, name='embedding'), LSTM(output_dim=num_hidden_units_lstm, return_sequences=False, input_shape=(MAX_SEQUENCE_LENGTH, EMBEDDING_DIM), name='LSTM3') ]) model = Sequential([ Merge([branch1, branch2, branch3], mode='mul'), Dense(nb_hidden_units, init='uniform', activation='tanh'), Dense(nb_hidden_units, init='uniform', activation='tanh'), Dropout(0.5), Dense(2, init='uniform', activation='softmax'), ]) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) model.fit([x1_train, x2_train, x3_train], y_train, validation_data=([x1_val, x2_val, x3_val], y_val), nb_epoch=50, batch_size=32)
model.add(LSTM(32, input_length=10, input_dim=64)) ### # Merge layer # Multiple Sequential instances can be merged into a single output via a Merge layer. The output is a layer that can be added as first layer in a new Sequential model: from keras.layers import Merge left_branch = Sequential() left_branch.add(Dense(32, input_dim=784)) right_branch = Sequential() right_branch.add(Dense(32, input_dim=784)) merged = Merge([left_branch, right_branch], mode='concat') final_model = Sequential() final_model.add(merged) # first layer in new Sequential model final_model.add(Dense(10, activation='softmax')) # This two-branch model can then be trained via, eg: final_model.compile(optimizer='rmsprop', loss='categorical_crossentropy') final_model.fit([input_data_1, input_data_2], targets) # pass one data array per model input # Now you know enough to be able to define almost any model with Keras. For complex models that cannot be expressed via Sequential and Merge, you can use the functional API. ### # Compilation
score_table.to_csv('E:/lf/score_table.csv',index = None) n_users=np.max(score_table['new_id']) n_movices=int(np.max(score_table['fid'])) print([n_users,n_movices,len(score_table)]) score_table.to_csv('E:/scoretable.csv',index = None) model1=Sequential() model1.add(Embedding(n_users+1,k,input_length=1)) model1.add(Reshape((k,))) model2=Sequential() model2.add(Embedding(n_movices+1,k,input_length=1)) model2.add(Reshape((k,))) model=Sequential() model.add(Merge([model1,model2],mode='concat'))#然后加入Dropout 和relu 这个非线性变换项,构造多层深度模型。 #model.add(Concatenate([model1, model2])) #x = concatenate([a, b], axis=-1) model.add(Dropout(0.5)) model.add(Dense(k, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(int(k / 4), activation='relu')) model.add(Dropout(0.5)) model.add(Dense(int(k / 16), activation='relu')) model.add(Dropout(0.5)) model.add(Dense(5, activation = 'softmax'))#因为是预测连续变量评分,最后一层直接上线性变化 model.compile(loss='categorical_crossentropy',optimizer='adam')
model = load_model(MODEL_PATH) else: board_model = Sequential([ Reshape([8, 8, 1], input_shape=[8, 8], name="Reshape"), LocallyConnected2D(64, 3, 3, border_mode='valid', name="LC1"), # 6x6 ELU(name="ELU1"), BatchNormalization(name="BN1"), LocallyConnected2D(256, 3, 3, border_mode='valid', name="LC2"), # 4x4 ELU(name="ELU2"), BatchNormalization(name="BN2"), Flatten(name="Flatten") ]) color_input = InputLayer([1]) model = Sequential([ Merge([board_model, color_input], mode="concat", name="MERGE"), Dense(512, name="Dense1"), ELU(name="ELU2_1"), Dense(128, name="Dense2"), ELU(name="ELU2_2"), Dense(3, activation="softmax", name="Dense3") # [DRAW, BLACK, WHITE] ]) adam = Adam(lr=1e-6, beta_1=0.5) model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) records = glob.glob(RECORD_PATH) print("{0} records".format(len(records)))
## Certo é 256 recog=Sequential() recog.add(Dense(64,activation='relu',input_shape=(784,),init='glorot_uniform')) recog_left=recog recog_left.add(Dense(64,input_shape=(64,),activation='relu')) recog_right=recog recog_right.add(Dense(64,input_shape=(64,),activation='relu')) recog_right.add(Lambda(lambda x: x + K.exp(x / 2) * K.random_normal(shape=(1, 64), mean=0., std=epsilon_std), output_shape=(64,))) recog_right.add(Highway()) recog_right.add(Activation('sigmoid')) recog1=Sequential() recog1.add(Merge([recog_left,recog_right],mode = 'ave')) recog1.add(Dense(64,init='glorot_uniform')) recog1.add(Dense(784, activation='sigmoid',init='glorot_uniform')) recog1.compile(loss='mean_squared_error', optimizer=sgd,metrics = ['mae']) recog1.fit(x_train[0].reshape((1,784)), x_train_orig[0].reshape((1,784)), nb_epoch=150, batch_size=30,verbose=1) a=recog1.predict(x_train[0].reshape(1,784),verbose=1) plt.figure(figsize=(10, 10)) ax = plt.subplot(1, 2, 1) plt.imshow(x_train[0].reshape(28, 28))
subsample_length=1, W_regularizer=l2(1e-5)) ''' # Build promoter branch promoter_branch = Sequential() promoter_branch.add(promoter_conv_layer) promoter_branch.add(Activation("relu")) #promoter_branch.add(promoter_conv_layer_slim) #promoter_branch.add(Activation("relu")) promoter_branch.add(promoter_max_pool_layer) # Define main model layers # Concatenate outputs of enhancer and promoter convolutional layers merge_layer = Merge([enhancer_branch, promoter_branch], mode='concat', concat_axis=1) ''' # Bidirectional LSTM to extract combinations of motifs biLSTM_layer = Bidirectional(LSTM(input_dim=n_kernels, output_dim=LSTM_out_dim, return_sequences=True)) ''' # Dense layer to allow nonlinearities dense_layer = Dense(output_dim=dense_layer_size, init="glorot_uniform", W_regularizer=l2(1e-6)) # Logistic regression layer to make final binary prediction LR_classifier_layer = Dense(output_dim=1)
# Convolutional model submodels = [] for kw in size_filters: submodel = Sequential() submodel.add( Conv1D(num_filters, kw, padding='valid', kernel_initializer=initializers.RandomNormal(np.sqrt(2 / kw)), input_shape=(tam_fijo, embedding_vecor_length))) submodel.add(advanced_activations.PReLU(initializers.Constant(value=0.25))) submodel.add(GlobalMaxPooling1D()) submodels.append(submodel) model = Sequential() model.add(Merge(submodels, mode="concat")) model.add(Dropout(dropout)) model.add(Dense(3, activation='softmax')) # Log to tensorboard tensorBoardCallback = TensorBoard(log_dir='./logs22', write_graph=True) adadelta = optimizers.Adadelta(lr=alpha) model.compile(loss='categorical_crossentropy', optimizer=adadelta, metrics=['accuracy', 'mse']) model.fit([X_train] * len(size_filters), y_train, epochs=n_iter, callbacks=[tensorBoardCallback], batch_size=size_batch,
def bugDuplicate_new_input(df, cluster_id): coll_name = "BugDupsTrainSet_" + str(cluster_id)+ "_cluster" #"_complete" collection = db[coll_name] e = load_data(db, collection, True) df = e.copy() b1 = df.iloc[143389]#df[df['IDENTIFIER'] == 'CSCur69644'] print(b1['IDENTIFIER']) stops = set(stopwords.words('english')) max_seq_length = 150 text_cols = ['wrdEmbSet', 'DUP_wrdEmbSet'] embedding_dim = 150 cut_off = 90 #/data/csap_models/bugDups #/auto/vgapps-cstg02-vapps/analytics/csap/models/files/bugDups/ filename = '/data/csap_models/bugDups/w2vmodel_' + str(cluster_id) + '.bin' w2vmodel = Word2Vec.load(filename) f = '/data/csap_models/bugDups/vocab_model_' + str(cluster_id) + '.json' vocabulary = json.load(open(f, 'r')) thefile = '/data/csap_models/bugDups/inv_vocab_model_' + str(cluster_id) + '.json' with open (thefile, 'rb') as fp: inverse_vocabulary = pickle.load(fp) words = list(w2vmodel.wv.vocab) thefile = "/data/csap_models/bugDups/embeddings_model_" + str(cluster_id) + '.json' with open (thefile, 'rb') as fp: embeddings = pickle.load(fp) n_hidden = 50 gradient_clipping_norm = 1.25 batch_size = 64 n_epoch = 1 def exponent_neg_manhattan_distance(left, right): return K.exp(-K.sum(K.abs(left-right), axis=1, keepdims=True)) left_input = Input(shape=(max_seq_length,), dtype='int32') right_input = Input(shape=(max_seq_length,), dtype='int32') embedding_layer = Embedding(len(embeddings), embedding_dim, weights = [embeddings], input_length=max_seq_length, trainable=False) encoded_left = embedding_layer(left_input) encoded_right = embedding_layer(right_input) shared_lstm = LSTM(n_hidden) left_output = shared_lstm(encoded_left) right_output = shared_lstm(encoded_right) malstm_distance = Merge(mode=lambda x: exponent_neg_manhattan_distance(x[0], x[1]), output_shape=lambda x: (x[0][0], 1))([left_output, right_output]) malstm = Model([left_input, right_input], [malstm_distance]) optimizer = Adadelta(clipnorm=gradient_clipping_norm) malstm.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['accuracy']) filename = '/data/csap_models/bugDups/text_model_' + str(cluster_id) + '.h5' malstm.load_weights(filename) df = df[(df['PRODUCT'] == b1['PRODUCT']) & (df['PROJECT'] == b1['PROJECT'])] df1 = get_test_data(df, b1, w2vmodel, words, vocabulary, inverse_vocabulary, cluster_id, stops) print(df1[df1['DUPLICATE_OF'] == 'CSCur69644']) text_predictions = test_model_text(malstm, embeddings, vocabulary, w2vmodel, words, df1, cluster_id, stops, max_seq_length, text_cols) result = pd.DataFrame() result['DUPLICATE_OF'] = df1['DUPLICATE_OF'] p = [] for i in text_predictions: p.append(i[0]*100) result['pred_text'] = p result = result.drop_duplicates(subset='DUPLICATE_OF', keep="last") result = result.sort_values(['pred_text'], ascending=[0]) #print(result[result['DUPLICATE_OF'] == 'CSCzv37475']) result = result[result['pred_text'] > cut_off] if(result.shape[0] > 10): v = 10 else: v = result.shape[0] if(v != 0): df2 = pd.DataFrame() df2['id'] = [1] df2['DUPLICATE_LIST'] = ' '.join(list(result.iloc[0:v]['DUPLICATE_OF'])) df2['PROBABILITIES'] = ' '.join(str(x) for x in list(result.iloc[0:v]['pred_text'])) else: return pd.DataFrame() return df2
model0.add(Flatten()) model1 = Sequential() model1.add( Conv3D(32, (3, 3, 3), activation='relu', input_shape=[dim_y, dim_x, dim_t, 1])) model1.add(MaxPooling3D(pool_size=(2, 2, 1), strides=(2, 2, 1))) model1.add(Conv3D(64, (3, 3, 3), activation='relu')) model1.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2))) model1.add(Conv3D(128, (3, 3, 3), activation='relu')) model1.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2))) model1.add(Flatten()) model = Sequential() model.add(Merge([model0, model1], mode='concat')) model.add(Dense(16)) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) sgd = SGD(lr=0.001, decay=1e-1, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) fname_log = "Testlog_%d.log" % (num_ex_per_class) csv_logger = CSVLogger(fname_log) history = model.fit([Train_data, Train_feat], Train_data_label, epochs=100, batch_size=10,
conv_layer = Conv1D(filters=128, kernel_size=3, padding='valid', activation='relu') sequence_1_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32') embedded_sequences_1 = embedding_layer(sequence_1_input) sequence_2_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32') embedded_sequences_2 = embedding_layer(sequence_2_input) filter_sizes = [3, 4, 5] convs1 = [] for fsz in filter_sizes: con_1 = conv_layer(embedded_sequences_1) con_1 = MaxPooling1D(2)(con_1) convs1.append(con_1) con_1 = Merge(mode='concat', concat_axis=2)(convs1) con_1 = lstm_layer(con_1) convs1 = [] for fsz in filter_sizes: con_2 = conv_layer(embedded_sequences_1) con_2 = MaxPooling1D(2)(con_2) convs1.append(con_2) con_2 = Merge(mode='concat', concat_axis=2)(convs1) con_2 = lstm_layer(con_2) merged = concatenate([con_1, con_2, leaks_dense]) merged = BatchNormalization()(merged) merged = Dropout(rate_drop_dense)(merged)
third_branch = Sequential() third_branch.add(Embedding(max_features, 256)) third_branch.add(SimpleRNN(128, activation='tanh', return_sequences=True)) third_branch.add(Dropout(0.6)) #third_branch.add(Dense(1)) #third_branch.add(Activation('tanh')) fourth_branch = Sequential() fourth_branch.add(Embedding(max_features, 256)) fourth_branch.add(SimpleRNN(128, activation='tanh', return_sequences=True)) fourth_branch.add(Dropout(0.6)) #fourth_branch.add(Dense(1)) #fourth_branch.add(Activation('tanh')) merged1 = Merge([first_branch, second_branch, third_branch, fourth_branch], mode='concat') first_branch2 = Sequential() first_branch2.add(Embedding(max_features, 256)) first_branch2.add(SimpleRNN(128, activation='tanh', return_sequences=True)) first_branch2.add(Dropout(0.6)) #first_branch.add(Dense(1)) #first_branch.add(Activation('tanh')) second_branch2 = Sequential() second_branch2.add(Embedding(max_features, 256)) second_branch2.add(SimpleRNN(128, activation='tanh', return_sequences=True)) second_branch2.add(Dropout(0.6)) #second_branch.add(Dense(1)) #second_branch.add(Activation('tanh'))
image_model = Sequential() image_model.add(base_model) image_model.add(Dense(EMBEDDING_SIZE, activation='relu')) image_model.add(RepeatVector(SENTENCE_MAX_LENGTH)) # we use an Embedding layer to generate a good representation for captions. language_model = Sequential() # language_model.add(Embedding(voc_size, EMBEDDING_SIZE, input_length=SENTENCE_MAX_LENGTH)) language_model.add( LSTM(128, input_shape=(SENTENCE_MAX_LENGTH, 12503), return_sequences=True)) language_model.add(TimeDistributed(Dense(128))) # after merging CNN feature (image) and embedded vector (caption), we feed them into a LSTM model # at its end, we use a fully connected layer with softmax activation to convert the output into probability model = Sequential() model.add(Merge([image_model, language_model], mode='concat')) # model.add(Concatenate([image_model, language_model])) model.add(LSTM(1000, return_sequences=True)) # model.add(Dense(voc_size, activation='softmax', name='final_output')) model.add(TimeDistributed(Dense(12503, activation='softmax'))) # draw the model and save it to a file. # plot_model(model, to_file='model.pdf', show_shapes=True) # model = model model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) model.load_weights('**********') # my_generator = Data_generator()
def create_model(): models = [] model_ps_ind_02_cat = Sequential() model_ps_ind_02_cat.add(Embedding(5, 3, input_length=1)) model_ps_ind_02_cat.add(Reshape(target_shape=(3, ))) models.append(model_ps_ind_02_cat) model_ps_ind_04_cat = Sequential() model_ps_ind_04_cat.add(Embedding(3, 2, input_length=1)) model_ps_ind_04_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_ind_04_cat) model_ps_ind_05_cat = Sequential() model_ps_ind_05_cat.add(Embedding(8, 5, input_length=1)) model_ps_ind_05_cat.add(Reshape(target_shape=(5, ))) models.append(model_ps_ind_05_cat) model_ps_car_01_cat = Sequential() model_ps_car_01_cat.add(Embedding(13, 7, input_length=1)) model_ps_car_01_cat.add(Reshape(target_shape=(7, ))) models.append(model_ps_car_01_cat) model_ps_car_02_cat = Sequential() model_ps_car_02_cat.add(Embedding(3, 2, input_length=1)) model_ps_car_02_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_car_02_cat) model_ps_car_03_cat = Sequential() model_ps_car_03_cat.add(Embedding(3, 2, input_length=1)) model_ps_car_03_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_car_03_cat) model_ps_car_04_cat = Sequential() model_ps_car_04_cat.add(Embedding(10, 5, input_length=1)) model_ps_car_04_cat.add(Reshape(target_shape=(5, ))) models.append(model_ps_car_04_cat) model_ps_car_05_cat = Sequential() model_ps_car_05_cat.add(Embedding(3, 2, input_length=1)) model_ps_car_05_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_car_05_cat) model_ps_car_06_cat = Sequential() model_ps_car_06_cat.add(Embedding(18, 8, input_length=1)) model_ps_car_06_cat.add(Reshape(target_shape=(8, ))) models.append(model_ps_car_06_cat) model_ps_car_07_cat = Sequential() model_ps_car_07_cat.add(Embedding(3, 2, input_length=1)) model_ps_car_07_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_car_07_cat) model_ps_car_09_cat = Sequential() model_ps_car_09_cat.add(Embedding(6, 3, input_length=1)) model_ps_car_09_cat.add(Reshape(target_shape=(3, ))) models.append(model_ps_car_09_cat) model_ps_car_10_cat = Sequential() model_ps_car_10_cat.add(Embedding(3, 2, input_length=1)) model_ps_car_10_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_car_10_cat) model_ps_car_11_cat = Sequential() model_ps_car_11_cat.add(Embedding(104, 10, input_length=1)) model_ps_car_11_cat.add(Reshape(target_shape=(10, ))) models.append(model_ps_car_11_cat) model_rest = Sequential() model_rest.add(Dense(16, input_dim=24)) models.append(model_rest) model = Sequential() model.add(Merge(models, mode='concat')) model.add(Dense(80)) model.add(Activation('relu')) model.add(Dropout(.35)) model.add(Dense(20)) model.add(Activation('relu')) model.add(Dropout(.15)) model.add(Dense(10)) model.add(Activation('relu')) model.add(Dropout(.15)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam') return model
image_model.layers.pop() for layer in image_model.layers: layer.trainable = False language_model = Sequential() language_model.add(Embedding(vocab_size, 256, input_length=max_caption_len)) language_model.add(LSTM(output_dim=128, return_sequences=True)) language_model.add(TimeDistributed(Dense(128))) # let's repeat the image vector to turn it into a sequence. print("Repeat model loading") image_model.add(RepeatVector(max_caption_len)) print("Repeat model loaded") # the output of both models will be tensors of shape (samples, max_caption_len, 128). # let's concatenate these 2 vector sequences. print("Merging") model = Sequential() model.add(Merge([image_model, language_model], mode='concat', concat_axis=-1)) # let's encode this vector sequence into a single vector model.add(LSTM(256, return_sequences=False)) # which will be used to compute a probability # distribution over what the next word in the caption should be! model.add(Dense(vocab_size)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') print("Merged") model.fit([images, captions], next_words, batch_size=1, epochs=5) model.save_weights('image_caption_weights.h5')
def test_sequential_regression(): from keras.models import Sequential, Model from keras.layers import Merge, Embedding, BatchNormalization, LSTM, InputLayer, Input # start with a basic example of using a Sequential model # inside the functional API seq = Sequential() seq.add(Dense(input_dim=10, output_dim=10)) x = Input(shape=(10, )) y = seq(x) model = Model(x, y) model.compile('rmsprop', 'mse') weights = model.get_weights() # test serialization config = model.get_config() model = Model.from_config(config) model.compile('rmsprop', 'mse') model.set_weights(weights) # more advanced model with multiple branches branch_1 = Sequential(name='branch_1') branch_1.add( Embedding(input_dim=100, output_dim=10, input_length=2, name='embed_1')) branch_1.add(LSTM(32, name='lstm_1')) branch_1.add(BatchNormalization()) branch_2 = Sequential(name='branch_2') branch_2.add(Dense(32, input_shape=(8, ), name='dense_2')) branch_3 = Sequential(name='branch_3') branch_3.add(Dense(32, input_shape=(6, ), name='dense_3')) branch_1_2 = Sequential([Merge([branch_1, branch_2], mode='concat')], name='branch_1_2') branch_1_2.add(Dense(16, name='dense_1_2-0')) # test whether impromtu input_shape breaks the model branch_1_2.add(Dense(16, input_shape=(16, ), name='dense_1_2-1')) model = Sequential([Merge([branch_1_2, branch_3], mode='concat')], name='final') model.add(Dense(16, name='dense_final')) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.summary() x = (100 * np.random.random((100, 2))).astype('int32') y = np.random.random((100, 8)) z = np.random.random((100, 6)) labels = np.random.random((100, 16)) model.fit([x, y, z], labels, nb_epoch=1) # test if Sequential can be called in the functional API a = Input(shape=(2, ), dtype='int32') b = Input(shape=(8, )) c = Input(shape=(6, )) o = model([a, b, c]) outer_model = Model([a, b, c], o) outer_model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) outer_model.fit([x, y, z], labels, nb_epoch=1) # test serialization config = outer_model.get_config() outer_model = Model.from_config(config) outer_model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) outer_model.fit([x, y, z], labels, nb_epoch=1)
def build_model(top_words, embedding_vecor_length, max_review_length, show_summaries=False): input_layer = Embedding(top_words, embedding_vecor_length, input_length=max_review_length) # --- 2 --- branch_2 = Sequential() branch_2.add(input_layer) branch_2.add( Conv1D(filters=512, kernel_size=2, padding='same', kernel_regularizer=l2(.01))) branch_2.add(Activation('relu')) branch_2.add(MaxPooling1D(pool_size=32)) branch_2.add(BatchNormalization()) branch_2.add(LSTM(128)) # --- 3 --- branch_3 = Sequential() branch_3.add(input_layer) branch_3.add( Conv1D(filters=512, kernel_size=3, padding='same', kernel_regularizer=l2(.01))) branch_3.add(Activation('relu')) branch_3.add(MaxPooling1D(pool_size=32)) branch_3.add(BatchNormalization()) branch_3.add(LSTM(128)) # --- 4 --- branch_4 = Sequential() branch_4.add(input_layer) branch_4.add( Conv1D(filters=512, kernel_size=4, padding='same', kernel_regularizer=l2(.01))) branch_4.add(Activation('relu')) branch_4.add(MaxPooling1D(pool_size=32)) branch_4.add(BatchNormalization()) branch_4.add(LSTM(128)) # --- 5 --- branch_5 = Sequential() branch_5.add(input_layer) branch_5.add( Conv1D(filters=512, kernel_size=5, padding='same', kernel_regularizer=l2(.01))) branch_5.add(Activation('relu')) branch_5.add(MaxPooling1D(pool_size=32)) branch_5.add(BatchNormalization()) branch_5.add(LSTM(128)) # --- 6 --- branch_6 = Sequential() branch_6.add(input_layer) branch_6.add( Conv1D(filters=512, kernel_size=6, padding='same', kernel_regularizer=l2(.01))) branch_6.add(Activation('relu')) branch_6.add(MaxPooling1D(pool_size=32)) branch_6.add(BatchNormalization()) branch_6.add(LSTM(128)) # --- 7 --- branch_7 = Sequential() branch_7.add(input_layer) branch_7.add( Conv1D(filters=512, kernel_size=7, padding='same', kernel_regularizer=l2(.01))) branch_7.add(Activation('relu')) branch_7.add(MaxPooling1D(pool_size=32)) branch_7.add(BatchNormalization()) branch_7.add(LSTM(128)) model = Sequential() model.add( Merge([branch_2, branch_3, branch_4, branch_5, branch_6, branch_7], mode='concat')) model.add(Dropout(0.8)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) if show_summaries: print(branch_3.summary()) print(branch_4.summary()) print(branch_5.summary()) print(model.summary()) return model
trainable=True) # applying a more complex convolutional approach convs = [] filter_sizes = [3, 4, 5] sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32') embedded_sequences = embedding_layer(sequence_input) for fsz in filter_sizes: l_conv = Conv1D(nb_filter=128, filter_length=fsz, activation='relu')(embedded_sequences) l_pool = MaxPooling1D(5)(l_conv) convs.append(l_pool) l_merge = Merge(mode='concat', concat_axis=1)(convs) l_cov1 = Conv1D(128, 5, activation='relu')(l_merge) l_pool1 = MaxPooling1D(5)(l_cov1) l_cov2 = Conv1D(128, 5, activation='relu')(l_pool1) l_pool2 = MaxPooling1D(30)(l_cov2) l_flat = Flatten()(l_pool2) l_dense = Dense(128, activation='relu')(l_flat) preds = Dense(2, activation='softmax')(l_dense) model = Model(sequence_input, preds) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc']) print("model fitting - more complex convolutional neural network") model.summary()
def build_merged_fn(self, input_dim=12, input_shape=(1, 8, 8), kernel_size=(1,1), # convolution kernel size pool_size=(1, 1), # size of pooling area for max pooling nlayers=3, list_of_nneurons=[50, 50, 50, 50, 50, 50, 50, 50, 50, 50], dense_layer_sizes=1, nb_filters = 32, # number of convolutional filters to use dropout_rate=0.01, l2_norm=0.001, learning_rate=1e-3, batch_norm=False, activation='relu', kernel_initializer='lecun_normal', optimizer='adam', metric='accuracy', loss='binary_crossentropy'): # ========= Mixed deep learning model right branch (DNN) ======================== branch_right = Sequential() # Add fully connected layer with an activation function (input layer) branch_right.add(Dense(units=list_of_nneurons[0], input_dim=self.input_dim, kernel_initializer=kernel_initializer, activation=activation, kernel_regularizer=l2(l2_norm))) branch_right.add(Dropout(dropout_rate)) # Indicate the number of hidden layers for index, layer in enumerate(range(self.nlayers-1)): self.model.add(Dense(units=list_of_nneurons[index+1], kernel_initializer=kernel_initializer, activation=activation, kernel_regularizer=l2(l2_norm))) # Add dropout layer branch_right.add(Dropout(dropout_rate)) if batch_norm == True: branch_right.add(BatchNormalization()) # ========= Mixed deep learning model right branch (CNN) ======================== branch_left = Sequential() branch_left.add(Conv2D(filters=nb_filters, # Number of convolutional filters to use (output feature map) kernel_size=kernel_size, # Column and Row size of kernel used for convolution padding='valid', #'same' input_shape=self.input_shape, # 8x8 imagine with 1 channel dim_ordering='tf' )) #branch_left.add(Conv2D(filters=nb_filters, # kernel_size = kernel_size, # )) #branch_left.add(Activation(activation)) branch_left.add(Conv2D(filters=nb_filters, # Number of output feature map kernel_size=kernel_size, dim_ordering='tf' )) branch_left.add(Activation(activation)) branch_left.add(MaxPooling2D(pool_size=pool_size)) # Size of pooling area for max pooling branch_left.add(Flatten()) # Required since merging layer needs matching size #branch_left.add(Dense(16, activation='sigmoid')) branch_left.add(Dense(128, activation='relu')) branch_left.add(Dropout(0.5)) if batch_norm == True: branch_left.add(BatchNormalization()) # Merged model model = Sequential() model.add(Merge([branch_right, branch_left], mode = 'concat')) # HANDLES the network merging model.add(Dense(1, activation="sigmoid", kernel_initializer="normal")) #optimizer = SGD(lr = 0.1, momentum = 0.9, decay = 0, nesterov = False) model.compile(loss=loss, optimizer='adam', metrics=[metric]) return model
print(model_variation) data_in = Input(shape=(sequence_length, embedding_dim)) convs = [] # convolutional layers in parallel with one input and one output for filts in filter_sizes: conv = Convolution1D(nb_filter=num_filters, filter_length=filts, border_mode='valid', activation='relu', subsample_length=1)(data_in) pool = MaxPooling1D(pool_length=2)(conv) flatten = Flatten()(pool) convs.append(flatten) if len(filter_sizes) > 1: out = Merge(mode='concat')(convs) else: out = convs[0] graph = Model(input=data_in, output=out) model = Sequential() if model_variation == 'CNN-non-static' or model_variation == "CNN-rand": model.add( Embedding(len(vocab), embedding_dim, input_length=sequence_length, weights=embedding_weights)) model.add( Dropout(dropout_prob[0], input_shape=(sequence_length, embedding_dim))) model.add(graph)
weights=init_vectors)) branch.add( Convolution1D(nb_filter=cfg.getint('cnn', 'filters'), filter_length=int(filter_len), border_mode='valid', activation='relu', subsample_length=1)) branch.add(MaxPooling1D(pool_length=2)) branch.add(Flatten()) branches.append(branch) train_xs.append(train_x) test_xs.append(test_x) model = Sequential() model.add(Merge(branches, mode='concat')) model.add(Dropout(cfg.getfloat('cnn', 'dropout'))) model.add(Dense(cfg.getint('cnn', 'hidden'))) model.add(Activation('relu')) model.add(Dropout(cfg.getfloat('cnn', 'dropout'))) model.add(Dense(classes)) model.add(Activation('softmax')) optimizer = RMSprop(lr=cfg.getfloat('cnn', 'learnrt'), rho=0.9, epsilon=1e-08) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
input_encoder_m = Sequential() input_encoder_m.add( Embedding(input_dim=vocab_size, output_dim=64, input_length=story_maxlen)) input_encoder_m.add(Dropout(0.3)) # output: (samples, story_maxlen, embedding_dim) # embed the question into a sequence of vectors question_encoder = Sequential() question_encoder.add( Embedding(input_dim=vocab_size, output_dim=64, input_length=query_maxlen)) question_encoder.add(Dropout(0.3)) # output: (samples, query_maxlen, embedding_dim) # compute a 'match' between input sequence elements (which are vectors) # and the question vector sequence match = Sequential() match.add( Merge([input_encoder_m, question_encoder], mode='dot', dot_axes=[2, 2])) match.add(Activation('softmax')) # output: (samples, story_maxlen, query_maxlen) # embed the input into a single vector with size = story_maxlen: input_encoder_c = Sequential() input_encoder_c.add( Embedding(input_dim=vocab_size, output_dim=query_maxlen, input_length=story_maxlen)) input_encoder_c.add(Dropout(0.3)) # output: (samples, story_maxlen, query_maxlen) # sum the match vector with the input vector: response = Sequential() response.add(Merge([match, input_encoder_c], mode='sum')) # output: (samples, story_maxlen, query_maxlen) response.add(Permute((2, 1))) # output: (samples, query_maxlen, story_maxlen)