python类merge()的实例源码

p3_cnn.py 文件源码 项目:DeepLearn 作者: GauravBh1010tt 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def trainCNN(obj, dataset_headLines, dataset_body):
    embedding_dim = 300
    LSTM_neurons = 50
    dense_neuron = 16
    dimx = 100
    dimy = 200
    lamda = 0.0
    nb_filter = 100
    filter_length = 4
    vocab_size = 10000
    batch_size = 50
    epochs = 5
    ntn_out = 16
    ntn_in = nb_filter 
    state = False


    train_head,train_body,embedding_matrix = obj.process_data(sent_Q=dataset_headLines,
                                                     sent_A=dataset_body,dimx=dimx,dimy=dimy,
                                                     wordVec_model = wordVec_model)    
    inpx = Input(shape=(dimx,),dtype='int32',name='inpx')
    #x = Embedding(output_dim=embedding_dim, input_dim=vocab_size, input_length=dimx)(inpx)
    x = word2vec_embedding_layer(embedding_matrix)(inpx)  
    inpy = Input(shape=(dimy,),dtype='int32',name='inpy')
    #y = Embedding(output_dim=embedding_dim, input_dim=vocab_size, input_length=dimy)(inpy)
    y = word2vec_embedding_layer(embedding_matrix)(inpy)
    ques = Convolution1D(nb_filter=nb_filter, filter_length=filter_length,
                         border_mode='valid', activation='relu',
                         subsample_length=1)(x)

    ans = Convolution1D(nb_filter=nb_filter, filter_length=filter_length,
                        border_mode='valid', activation='relu',
                        subsample_length=1)(y)

    #hx = Lambda(max_1d, output_shape=(nb_filter,))(ques)
    #hy = Lambda(max_1d, output_shape=(nb_filter,))(ans)
    hx = GlobalMaxPooling1D()(ques)
    hy = GlobalMaxPooling1D()(ans)
    #wordVec_model = []
    #h =  Merge(mode="concat",name='h')([hx,hy])

    h1 = Multiply()([hx,hy])
    h2 = Abs()([hx,hy])

    h =  Merge(mode="concat",name='h')([h1,h2])
    #h = NeuralTensorLayer(output_dim=1,input_dim=ntn_in)([hx,hy])
    #h = ntn_layer(ntn_in,ntn_out,activation=None)([hx,hy])
    #score = h
    wrap = Dense(dense_neuron, activation='relu',name='wrap')(h)
    #score = Dense(1,activation='sigmoid',name='score')(h)
    #wrap = Dense(dense_neuron,activation='relu',name='wrap')(h)
    score = Dense(4,activation='softmax',name='score')(wrap)

    #score=K.clip(score,1e-7,1.0-1e-7)
    #corr = CorrelationRegularization(-lamda)([hx,hy])
    #model = Model( [inpx,inpy],[score,corr])
    model = Model( [inpx,inpy],score)
    model.compile( loss='categorical_crossentropy',optimizer="adadelta",metrics=['accuracy'])    
    return model,train_head,train_body
DeepLearn_cornet.py 文件源码 项目:DeepLearn 作者: GauravBh1010tt 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def buildModel(loss_type,lamda):

    inpx = Input(shape=(dimx,))
    inpy = Input(shape=(dimy,))

    hx = Dense(hdim_deep,activation='sigmoid')(inpx)
    hx = Dense(hdim_deep2, activation='sigmoid',name='hid_l1')(hx)
    hx = Dense(hdim, activation='sigmoid',name='hid_l')(hx)

    hy = Dense(hdim_deep,activation='sigmoid')(inpy)
    hy = Dense(hdim_deep2, activation='sigmoid',name='hid_r1')(hy)
    hy = Dense(hdim, activation='sigmoid',name='hid_r')(hy)

    #h = Activation("sigmoid")( Merge(mode="sum")([hx,hy]) )
    h =  Merge(mode="sum")([hx,hy]) 

    #recx = Dense(hdim_deep,activation='sigmoid')(h)
    recx = Dense(dimx)(h)
    #recy = Dense(hdim_deep,activation='sigmoid')(h)
    recy = Dense(dimy)(h)

    branchModel = Model( [inpx,inpy],[recx,recy,h])

    #inpx = Input(shape=(dimx,))
    #inpy = Input(shape=(dimy,))

    [recx1,recy1,h1] = branchModel( [inpx, ZeroPadding()(inpy)])
    [recx2,recy2,h2] = branchModel( [ZeroPadding()(inpx), inpy ])

    #you may probably add a reconstruction from combined
    [recx3,recy3,h] = branchModel([inpx, inpy])

    corr=CorrnetCost(-lamda)([h1,h2])

    if loss_type == 1:
        model = Model( [inpx,inpy],[recy1,recx2,recx3,recx1,recy2,recy3,corr])
        model.compile( loss=["mse","mse","mse","mse","mse","mse",corr_loss],optimizer="rmsprop")
    elif loss_type == 2:
        model = Model( [inpx,inpy],[recy1,recx2,recx1,recy2,corr])
        model.compile( loss=["mse","mse","mse","mse",corr_loss],optimizer="rmsprop")
    elif loss_type == 3:
        model = Model( [inpx,inpy],[recy1,recx2,recx3,recx1,recy2,recy3])
        model.compile( loss=["mse","mse","mse","mse","mse","mse"],optimizer="rmsprop")
    elif loss_type == 4:
        model = Model( [inpx,inpy],[recy1,recx2,recx1,recy2])
        model.compile( loss=["mse","mse","mse","mse"],optimizer="rmsprop")

    return model, branchModel
MemNN_classifier.py 文件源码 项目:ParseLawDocuments 作者: FanhuaandLuomu 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def Mem_Model2(story_maxlen,query_maxlen,vocab_size):
    input_encoder_m = Sequential()
    input_encoder_m.add(Embedding(input_dim=vocab_size,
                                  output_dim=128,
                                  input_length=story_maxlen))
    input_encoder_m.add(Dropout(0.5))
    # 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=128,
                                   input_length=query_maxlen))
    question_encoder.add(Dropout(0.5))
    # 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'))

    plot(match,to_file='model_1.png')

    # 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(Embedding(input_dim=vocab_size,
                                  output_dim=query_maxlen,
                                  input_length=story_maxlen))
    input_encoder_c.add(Dropout(0.5))
    # 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)

    plot(response,to_file='model_2.png')

    # concatenate the match vector with the question vector,
    # and do logistic regression on top
    answer = Sequential()
    answer.add(Merge([response, question_encoder], mode='concat', concat_axis=-1))
    # the original paper uses a matrix multiplication for this reduction step.
    # we choose to use a RNN instead.
    answer.add(LSTM(64))
    # one regularization layer -- more would probably be needed.
    answer.add(Dropout(0.5))
    answer.add(Dense(50))
    # we output a probability distribution over the vocabulary
    answer.add(Activation('sigmoid'))

    return answer

# ??????? ?????k???1
wavenet.py 文件源码 项目:wavenet 作者: basveeling 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def build_model(fragment_length, nb_filters, nb_output_bins, dilation_depth, nb_stacks, use_skip_connections,
                learn_all_outputs, _log, desired_sample_rate, use_bias, res_l2, final_l2):
    def residual_block(x):
        original_x = x
        # TODO: initalization, regularization?
        # Note: The AtrousConvolution1D with the 'causal' flag is implemented in github.com/basveeling/keras#@wavenet.
        tanh_out = CausalAtrousConvolution1D(nb_filters, 2, atrous_rate=2 ** i, border_mode='valid', causal=True,
                                             bias=use_bias,
                                             name='dilated_conv_%d_tanh_s%d' % (2 ** i, s), activation='tanh',
                                             W_regularizer=l2(res_l2))(x)
        sigm_out = CausalAtrousConvolution1D(nb_filters, 2, atrous_rate=2 ** i, border_mode='valid', causal=True,
                                             bias=use_bias,
                                             name='dilated_conv_%d_sigm_s%d' % (2 ** i, s), activation='sigmoid',
                                             W_regularizer=l2(res_l2))(x)
        x = layers.Merge(mode='mul', name='gated_activation_%d_s%d' % (i, s))([tanh_out, sigm_out])

        res_x = layers.Convolution1D(nb_filters, 1, border_mode='same', bias=use_bias,
                                     W_regularizer=l2(res_l2))(x)
        skip_x = layers.Convolution1D(nb_filters, 1, border_mode='same', bias=use_bias,
                                      W_regularizer=l2(res_l2))(x)
        res_x = layers.Merge(mode='sum')([original_x, res_x])
        return res_x, skip_x

    input = Input(shape=(fragment_length, nb_output_bins), name='input_part')
    out = input
    skip_connections = []
    out = CausalAtrousConvolution1D(nb_filters, 2, atrous_rate=1, border_mode='valid', causal=True,
                                    name='initial_causal_conv')(out)
    for s in range(nb_stacks):
        for i in range(0, dilation_depth + 1):
            out, skip_out = residual_block(out)
            skip_connections.append(skip_out)

    if use_skip_connections:
        out = layers.Merge(mode='sum')(skip_connections)
    out = layers.Activation('relu')(out)
    out = layers.Convolution1D(nb_output_bins, 1, border_mode='same',
                               W_regularizer=l2(final_l2))(out)
    out = layers.Activation('relu')(out)
    out = layers.Convolution1D(nb_output_bins, 1, border_mode='same')(out)

    if not learn_all_outputs:
        raise DeprecationWarning('Learning on just all outputs is wasteful, now learning only inside receptive field.')
        out = layers.Lambda(lambda x: x[:, -1, :], output_shape=(out._keras_shape[-1],))(
            out)  # Based on gif in deepmind blog: take last output?

    out = layers.Activation('softmax', name="output_softmax")(out)
    model = Model(input, out)

    receptive_field, receptive_field_ms = compute_receptive_field()

    _log.info('Receptive Field: %d (%dms)' % (receptive_field, int(receptive_field_ms)))
    return model
models.py 文件源码 项目:tartarus 作者: sergiooramas 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def get_model_4(params):
    embedding_weights = pickle.load(open(common.TRAINDATA_DIR+"/embedding_weights_w2v_%s.pk" % params['embeddings_suffix'],"rb"))
    graph_in = Input(shape=(params['sequence_length'], params['embedding_dim']))
    convs = []
    for fsz in params['filter_sizes']:
        conv = Convolution1D(nb_filter=params['num_filters'],
                             filter_length=fsz,
                             border_mode='valid',
                             activation='relu',
                             subsample_length=1)
        x = conv(graph_in)
        logging.debug("Filter size: %s" % fsz)
        logging.debug("Output CNN: %s" % str(conv.output_shape))

        pool = GlobalMaxPooling1D()
        x = pool(x)
        logging.debug("Output Pooling: %s" % str(pool.output_shape))
        convs.append(x)

    if len(params['filter_sizes'])>1:
        merge = Merge(mode='concat')
        out = merge(convs)
        logging.debug("Merge: %s" % str(merge.output_shape))
    else:
        out = convs[0]

    graph = Model(input=graph_in, output=out)

    # main sequential model
    model = Sequential()
    if not params['model_variation']=='CNN-static':
        model.add(Embedding(len(embedding_weights[0]), params['embedding_dim'], input_length=params['sequence_length'],
                            weights=embedding_weights))
    model.add(Dropout(params['dropout_prob'][0], input_shape=(params['sequence_length'], params['embedding_dim'])))
    model.add(graph)
    model.add(Dense(params['n_dense']))
    model.add(Dropout(params['dropout_prob'][1]))
    model.add(Activation('relu'))

    model.add(Dense(output_dim=params["n_out"], init="uniform"))
    model.add(Activation(params['final_activation']))
    logging.debug("Output CNN: %s" % str(model.output_shape))

    if params['final_activation'] == 'linear':
        model.add(Lambda(lambda x :K.l2_normalize(x, axis=1)))

    return model

# word2vec ARCH with LSTM
model.py 文件源码 项目:CNN-Sentence-Classifier 作者: shagunsodhani 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def _predefined_model(args, embedding_matrix):
    '''function to use one of the predefined models (based on the paper)'''
    (filtersize_list, number_of_filters_per_filtersize, pool_length_list,
     dropout_list, optimizer, use_embeddings, embeddings_trainable) \
        = _param_selector(args)

    if (use_embeddings):
        embedding_layer = Embedding(args.nb_words + 1,
                                    args.embedding_dim,
                                    weights=[embedding_matrix],
                                    input_length=args.max_sequence_len,
                                    trainable=embeddings_trainable)
    else:
        embedding_layer = Embedding(args.nb_words + 1,
                                    args.embedding_dim,
                                    weights=None,
                                    input_length=args.max_sequence_len,
                                    trainable=embeddings_trainable)

    print('Defining model.')

    input_node = Input(shape=(args.max_sequence_len, args.embedding_dim))
    conv_list = []
    for index, filtersize in enumerate(filtersize_list):
        nb_filter = number_of_filters_per_filtersize[index]
        pool_length = pool_length_list[index]
        conv = Conv1D(nb_filter=nb_filter, filter_length=filtersize, activation='relu')(input_node)
        pool = MaxPooling1D(pool_length=pool_length)(conv)
        flatten = Flatten()(pool)
        conv_list.append(flatten)

    if (len(filtersize_list) > 1):
        out = Merge(mode='concat')(conv_list)
    else:
        out = conv_list[0]

    graph = Model(input=input_node, output=out)

    model = Sequential()
    model.add(embedding_layer)
    model.add(Dropout(dropout_list[0], input_shape=(args.max_sequence_len, args.embedding_dim)))
    model.add(graph)
    model.add(Dense(150))
    model.add(Dropout(dropout_list[1]))
    model.add(Activation('relu'))
    model.add(Dense(args.len_labels_index, activation='softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['acc'])
    return model
combined_merge_predict.py 文件源码 项目:video-action-recognition 作者: ap916 项目源码 文件源码 阅读 14 收藏 0 点赞 0 评论 0
def CNN():
    input_frames=10
    batch_size=10
    nb_classes = 101
    nb_epoch = 10
    img_rows, img_cols = 150,150
    img_channels = 2*input_frames
    chunk_size=8
    requiredLines = 1000
    total_predictions = 0
    correct_predictions = 0

    print 'Loading dictionary...'
    with open('../dataset/temporal_test_data.pickle','rb') as f1:
        temporal_test_data=pickle.load(f1)

    t_model = prepareTemporalModel(img_channels,img_rows,img_cols,nb_classes)
    f_model = prepareFeaturesModel(nb_classes,requiredLines)

    merged_layer = Merge([t_model, f_model], mode='ave')
    model = Sequential()
    model.add(merged_layer)
    model.add(Dense(nb_classes, W_regularizer=l2(0.01)))
    model.add(Activation('softmax'))
    model.load_weights('combined_merge_model.h5')

    print 'Compiling model...'
    gc.collect()
    sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True,clipnorm=0.1)
    model.compile(loss='hinge',optimizer=sgd, metrics=['accuracy'])

    keys=temporal_test_data.keys()
    random.shuffle(keys)

    # Starting the training of the final model.
    for chunk in chunks(keys,chunk_size):

        tX_test,tY_test=t_getTrainData(chunk,nb_classes,img_rows,img_cols)
        fX_test,fY_test=f_getTrainData(chunk,nb_classes,requiredLines)
        if (tX_test is not None and fX_test is not None):
                preds = model.predict([tX_test,fX_test])
                print (preds)
                print ('-'*40)
                print (tY_test)

                total_predictions += fX_test.shape[0]
                correct_predictions += totalCorrectPred(preds,tY_test)

                comparisons=[]
                maximum=np.argmax(tY_test,axis=1)
                for i,j in enumerate(maximum):
                    comparisons.append(preds[i][j])
                with open('compare.txt','a') as f1:
                    f1.write(str(comparisons))
                    f1.write('\n\n')
    print "\nThe accuracy was found out to be: ",str(correct_predictions*100/total_predictions)
combined_average_predict.py 文件源码 项目:video-action-recognition 作者: ap916 项目源码 文件源码 阅读 14 收藏 0 点赞 0 评论 0
def CNN():
    input_frames=10
    batch_size=10
    nb_classes = 101
    nb_epoch = 10
    img_rows, img_cols = 150,150
    img_channels = 2*input_frames
    chunk_size=8
    requiredLines = 1000
    total_predictions = 0
    correct_predictions = 0

    print 'Loading dictionary...'
    with open('../dataset/temporal_test_data.pickle','rb') as f1:
        temporal_test_data=pickle.load(f1)

    t_model = prepareTemporalModel(img_channels,img_rows,img_cols,nb_classes)
    f_model = prepareFeaturesModel(nb_classes,requiredLines)

    merged_layer = Merge([t_model, f_model], mode='ave')
    model = Sequential()
    model.add(merged_layer)
    model.load_weights('combined_average_model.h5')
    print 'Compiling model...'
    gc.collect()
    sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True,clipnorm=0.1)
    model.compile(loss='categorical_crossentropy',optimizer=sgd, metrics=['accuracy'])

    keys=temporal_test_data.keys()
    random.shuffle(keys)

    # Starting the training of the final model.
    for chunk in chunks(keys,chunk_size):

        tX_test,tY_test=t_getTrainData(chunk,nb_classes,img_rows,img_cols)
        fX_test,fY_test=f_getTrainData(chunk,nb_classes,requiredLines)
        if (tX_test is not None and fX_test is not None):
                preds = model.predict([tX_test,fX_test])
                print (preds)
                print ('-'*40)
                print (tY_test)

                total_predictions += fX_test.shape[0]
                correct_predictions += totalCorrectPred(preds,tY_test)

                comparisons=[]
                maximum=np.argmax(tY_test,axis=1)
                for i,j in enumerate(maximum):
                    comparisons.append(preds[i][j])
                with open('compare.txt','a') as f1:
                    f1.write(str(comparisons))
                    f1.write('\n\n')
    print "\nThe accuracy was found out to be: ",str(correct_predictions*100/total_predictions)
dual_encoder_lstm.py 文件源码 项目:snli_dual_encoder_lstm 作者: hist0613 项目源码 文件源码 阅读 14 收藏 0 点赞 0 评论 0
def load_model():
    if not os.path.exists(TRAINED_CLASSIFIER_PATH):
        print("No pre-trained model...")
        print("Start building model...")

        print("Now loading SNLI data...")
        X_train_1, X_train_2, Y_train, X_test_1, X_test_2, Y_test, X_dev_1, X_dev_2, Y_dev = load_data()

        print("Now loading embedding matrix...")
        embedding_matrix = load_embedding_matrix()

        print("Now building dual encoder lstm model...")
        # define lstm for sentence1
        branch1 = Sequential()
        branch1.add(Embedding(output_dim=EMBEDDING_DIM,
                              input_dim=MAX_NB_WORDS,
                              input_length=MAX_SEQUENCE_LENGTH,
                              weights=[embedding_matrix],
                              mask_zero=True,
                              trainable=False))
        branch1.add(LSTM(output_dim=LSTM_DIM))

        # define lstm for sentence2
        branch2 = Sequential()
        branch2.add(Embedding(output_dim=EMBEDDING_DIM,
                              input_dim=MAX_NB_WORDS,
                              input_length=MAX_SEQUENCE_LENGTH,
                              weights=[embedding_matrix],
                              mask_zero=True,
                              trainable=False))
        branch2.add(LSTM(output_dim=LSTM_DIM))

        # define classifier model
        model = Sequential()
        # Merge layer holds a weight matrix of itself
        model.add(Merge([branch1, branch2], mode='mul'))
        model.add(Dense(3))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy',
                      optimizer=OPTIMIZER,
                      metrics=['accuracy'])

        print("Now training the model...")
        print("\tbatch_size={}, nb_epoch={}".format(BATCH_SIZE, NB_EPOCH))
        model.fit([X_train_1, X_train_2], Y_train,
                  batch_size=BATCH_SIZE, nb_epoch=NB_EPOCH,
                  validation_data=([X_test_1, X_test_2], Y_test))

        print("Now saving the model... at {}".format(TRAINED_CLASSIFIER_PATH))
        model.save(TRAINED_CLASSIFIER_PATH)

    else:
        print("Found pre-trained model...")
        model = K_load_model(TRAINED_CLASSIFIER_PATH)

    return model
convNet.py 文件源码 项目:semeval2017-scienceie 作者: UKPLab 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def build_cnn_char_threeModels(input_dim, output_dim,nb_filter,filter_size=3):
    left = Sequential()
    left.add(Embedding(input_dim,
             32, # character embedding size
             input_length=L,
             dropout=0.2))
    left.add(Convolution1D(nb_filter=nb_filter,
                          filter_length=filter_size,border_mode="valid",activation="relu",subsample_length=1))
    left.add(GlobalMaxPooling1D())
    left.add(Dense(100))
    left.add(Dropout(0.2))
    left.add(Activation("tanh"))

    center = Sequential()
    center.add(Embedding(input_dim,
             32, # character embedding size
             input_length=M,
             dropout=0.2))
    center.add(Convolution1D(nb_filter=nb_filter,
                          filter_length=filter_size,border_mode="valid",activation="relu",subsample_length=1))
    center.add(GlobalMaxPooling1D())
    center.add(Dense(100))
    center.add(Dropout(0.2))
    center.add(Activation("tanh"))

    right = Sequential()
    right.add(Embedding(input_dim,
             32, # character embedding size
             input_length=R,
             dropout=0.2))
    right.add(Convolution1D(nb_filter=nb_filter,
                          filter_length=filter_size,border_mode="valid",activation="relu",subsample_length=1))
    right.add(GlobalMaxPooling1D())
    right.add(Dense(100))
    right.add(Dropout(0.2))
    right.add(Activation("tanh"))

    clf = Sequential()
    clf.add(Merge([left,center,right],mode="concat"))
    clf.add(Dense(output_dim=output_dim, activation='softmax'))

    clf.compile(optimizer='adagrad',
                     loss='categorical_crossentropy',
                     metrics=['accuracy'])
    return clf


问题


面经


文章

微信
公众号

扫码关注公众号