示例#1
0
    def build_model(self, p):
        S = Input(p['input_shape'], name='input_state')
        A = Input((1,), name='input_action', dtype='int32')
        R = Input((1,), name='input_reward')
        T = Input((1,), name='input_terminate', dtype='int32')
        NS = Input(p['input_shape'], name='input_next_sate')

        self.Q_model = self.build_cnn_model(p)
        self.Q_old_model = self.build_cnn_model(p, False)  # Q hat in paper
        self.Q_old_model.set_weights(self.Q_model.get_weights())  # Q' = Q

        Q_S = self.Q_model(S)  # batch * actions
        Q_NS = disconnected_grad(self.Q_old_model(NS))  # disconnected gradient is not necessary

        y = R + p['discount'] * (1-T) * K.max(Q_NS, axis=1, keepdims=True)  # batch * 1

        action_mask = K.equal(Tht.arange(p['num_actions']).reshape((1, -1)), A.reshape((-1, 1)))
        output = K.sum(Q_S * action_mask, axis=1).reshape((-1, 1))
        loss = K.sum((output - y) ** 2)  # sum could also be mean()

        optimizer = adam(p['learning_rate'])
        params = self.Q_model.trainable_weights
        update = optimizer.get_updates(params, [], loss)

        self.training_func = K.function([S, A, R, T, NS], loss, updates=update)
        self.Q_func = K.function([S], Q_S)
def create_model(img_channels, img_rows, img_cols, n_conv1=32, n_conv2=64,
                      n_conv3=64, n_out1=512, n_out2=-1, lr=.001, 
                      n_actions=4, loss='mse'):
    """ 
        Make a keras CNN model.  
    """
    
    model = Sequential()
    model.add(Convolution2D(n_conv1, 5, 5, border_mode='same',subsample=(2,2),
                            input_shape=(img_channels, img_rows, img_cols)))
    model.add(PReLU()) 
    
    model.add(Convolution2D(n_conv2, 3, 3, border_mode='same',subsample=(2,2)))
    model.add(PReLU()) 
    
    model.add(Convolution2D(n_conv3, 3, 3, border_mode='same'))
    model.add(PReLU())
    
    model.add(Flatten())
    model.add(Dense(n_out1))
    model.add(PReLU()) 
    
    model.add(Dense(n_actions))
    model.add(Activation('linear'))
    
    # try clipping or huber loss
    model.compile(loss=loss, optimizer=adam(lr=lr))

    return model
示例#3
0
 def _getopt(self):
     from keras.optimizers import adam, rmsprop, sgd, adadelta
     learning_rate = 0.001
     opt = adam(lr=learning_rate, epsilon = 0.001)
     #opt = rmsprop(learning_rate)
     #opt = adadelta()
     #opt = sgd(learning_rate = 0.01, momentum= 0.8, nesterov=True)
     return opt
示例#4
0
def buildMixModel(img_channels=3, lr = 0.01,weight_decay = 1e-7, loss='mse',activ='relu', last_activ='sigmoid'):
    # just build a tiny fcn model, you can use more layers and more filters as you want

    main_input = Input(shape=(img_channels, None, None), name='input')
    conv_1 = Convolution2D(4,3,3, border_mode = 'same', activation= activ, init='orthogonal',name='conv_1',
                                 W_regularizer=l2(weight_decay), b_regularizer=l2(weight_decay))(main_input)
    
    max_1 = MaxPooling2D(pool_size = (2,2))(conv_1)

    conv_2 = Convolution2D(8,3,3, border_mode = 'same', activation=activ, init='orthogonal',name='conv_2',
                                 W_regularizer=l2(weight_decay), b_regularizer=l2(weight_decay))(max_1)
    max_2 = MaxPooling2D(pool_size = (2,2))(conv_2)
    dp_0 =  Dropout(0.25)(max_2) 
    conv_3 = Convolution2D(16,3,3, border_mode = 'same', activation= activ, init='orthogonal',name='conv_3',
                                 W_regularizer=l2(weight_decay), b_regularizer=l2(weight_decay))(dp_0)  # 25
    max_3 = MaxPooling2D(pool_size = (2,2))(conv_3)                                                      # 12

    conv_4 = Convolution2D(32,3,3, border_mode = 'same', activation=activ, init='orthogonal',name='conv_4',
                                 W_regularizer=l2(weight_decay), b_regularizer=l2(weight_decay))(max_3)  # 12
    max_4 = MaxPooling2D(pool_size = (2,2))(conv_4)                                                      # 12
    dp_1 =  Dropout(0.25)(max_4)
    conv_5 = Convolution2D(64,3,3, border_mode = 'same', activation=activ, init='orthogonal',name='conv_5',
                                 W_regularizer=l2(weight_decay), b_regularizer=l2(weight_decay))(dp_1)  # 6

    upsamp_0 = UpSampling2D((2,2))(conv_5)
    resize_0 = Resize2D(K.shape(conv_4))(upsamp_0)
    deconv_0 = Convolution2D(32,3,3, border_mode = 'same', activation=activ, init='orthogonal',name='deconv_0',
                                   W_regularizer=l2(weight_decay), b_regularizer=l2(weight_decay))(resize_0)
    dp_2 =  Dropout(0.25)(deconv_0)
    upsamp_1 = UpSampling2D((2,2))(dp_2)
    resize_1 = Resize2D(K.shape(conv_3))(upsamp_1)
    deconv_1 = Convolution2D(16,3,3, border_mode = 'same', activation=activ, init='orthogonal',name='deconv_1',
                                   W_regularizer=l2(weight_decay), b_regularizer=l2(weight_decay))(resize_1)

    upsamp_2 = UpSampling2D((2,2))(deconv_1)
    resize_2 = Resize2D(K.shape(conv_2))(upsamp_2)
    deconv_2 = Convolution2D(8,3,3, border_mode = 'same', activation=activ,init='orthogonal',name='deconv_2',
                                   W_regularizer=l2(weight_decay), b_regularizer=l2(weight_decay))(resize_2)

    dp_3 =  Dropout(0.25)(deconv_2)
    upsamp_3 = UpSampling2D((2,2))(dp_3)
    resize_3 = Resize2D(K.shape(conv_1))(upsamp_3)
    deconv_3 = Convolution2D(4,3,3, border_mode = 'same', activation=activ,init='orthogonal',name='deconv_3',
                                   W_regularizer=l2(weight_decay), b_regularizer=l2(weight_decay))(resize_3)


    last_conv = Convolution2D(1,3,3, border_mode = 'same', activation=last_activ,init='orthogonal', name= 'output_mask',
                                       W_regularizer=l2(weight_decay), b_regularizer=l2(weight_decay))(deconv_3)

    model = Model(input=[main_input], output=[last_conv])
    #opt = SGD(lr=lr, decay= 1e-6, momentum=0.9,nesterov=True)
    #opt = Adadelta(lr=lr, rho=0.95, epsilon=1e-06,clipvalue=10)
    opt = adam(lr=lr)
    model.compile(loss={'output_mask': loss }, optimizer=opt)
    return model
示例#5
0
parser.add_option("-s", "--save", type="str", dest="save", default='',
                  help="amount of training data (number of words)")
options, args = parser.parse_args()

nb_run_words = options.running_words
nb_vocab = options.vocab_size
nb_run_val = options.val_run
nb_evaluate = options.nb_evaluation

unigram_table = get_unigram_probtable(nb_words=nb_vocab,
                                      save_path='../data/wiki-unigram-prob-size%d.pkl' %
                                                nb_vocab)
if options.decay:
    opt = AdamAnneal(lr=options.lr, lr_min=options.lr_min, gamma=options.gamma)
else:
    opt = adam(lr=options.lr)

if options.log_file == '':
    log_file = None
else:
    log_file = options.log_file

if options.save == '':
    save_path = None
else:
    save_path = options.save
    
model = NCELangModelV2(vocab_size=nb_vocab, nb_negative=options.negative,
                       embed_dims=options.embed_size, context_dims=options.context_size,
                       negprob_table=unigram_table, optimizer=opt)
model.compile()
示例#6
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Yunchuan Chen'

from utils import get_unigram_probtable
from models import NCELangModel
from keras.optimizers import adam

NB_RUN_WORDS = 100000000
NB_VOCAB = 10000
NB_RUN_VAL = 100000
NB_EVALUATE = 5000000
SAVE_PATH = '../data/models/lang/nce0-neg50-e128-c128-lr0.01.pkl'

DATA_PATH = '../data/corpus/wiki-sg-norm-lc-drop-bin.bz2'
BATCH_SIZE = 256
VAL_INTER = 1200

unigram_table = get_unigram_probtable(nb_words=NB_VOCAB)

opt = adam(lr=0.01)
model = NCELangModel(vocab_size=NB_VOCAB, nb_negative=50, embed_dims=128, context_dims=128,
                     negprob_table=unigram_table, optimizer=opt)
model.compile()
model.train(data_file=DATA_PATH,
            save_path=SAVE_PATH,
            batch_size=BATCH_SIZE, train_nb_words=NB_RUN_WORDS,
            val_nb_words=NB_EVALUATE, train_val_nb=NB_RUN_VAL, validation_interval=VAL_INTER)
示例#7
0
out = Conv1D(64,
                 3,
                 activation='elu',
                 padding='valid',
                 strides=1)(out)

out = BatchNormalization()(out)
out = Bidirectional(LSTM(512, return_sequences=True))(out)
out = attention_3d_block(out)
out = Flatten()(out)
out = Dense(1,  activation='sigmoid')(out)

model = Model(inputs, out)

sgd = adam(0.0005)
model.compile(loss='binary_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])
model.set_weights(model_weights)
###############################################################################
def predict(tweet):
    
    tweet_p = process_tweet(tweet)
    ttp = tokenizer.texts_to_sequences([tweet_p])
    ttpt = pad_sequences(ttp, maxlen=30, padding='post', truncating='post')
    t_idf = vectorizer.transform([tweet_p])
    
    return (( 0.5 * model.predict(ttpt)[0,0]) +
            ( 0.5 * clf.predict_proba(t_idf)[0][1]))
def create_dueling_net(img_channels, img_rows, img_cols, n_conv1=32, n_conv2=64,
                      n_conv3=64, n_out1=512, n_out2=-1, lr=.001,
                      n_actions=4, loss='mse', use_perm_drop=False, drop_o=.25):

    def make_output(x):
        x = -K.mean(x, axis=1, keepdims=True)
        x = K.tile(x, 4)
        return x

    def make_output_shape(input_shape):
        shape = list(input_shape)
        return tuple(shape)

    def perm_drop(x):
        return K.dropout(x, .25)

    # input for the netwrok
    input = Input(shape=(img_channels, img_rows, img_cols))

    # conv layers - shared by both netwroks
    conv1 = Convolution2D(n_conv1, 5, 5, border_mode='same',subsample=(2,2))(input)
    prelu1 = PReLU()(conv1)

    conv2 = Convolution2D(n_conv2, 3, 3, border_mode='same',subsample=(2,2))(prelu1)
    prelu2 = PReLU()(conv2)

    conv3 = Convolution2D(n_conv2, 3, 3, border_mode='same')(prelu2)
    prelu3 = PReLU()(conv3)

    flatten = Flatten()(prelu3)

    # A(s,a)
    dense11 = Dense(n_out1)(flatten)

    if not use_perm_drop:
        prelu31 = PReLU()(dense11)
    else:
        prelu310 = PReLU()(dense11)
        prelu31 = Lambda(perm_drop, output_shape=make_output_shape)(prelu310)

    dense21 = Dense(n_actions)(prelu31)
    out1 = Activation('linear')(dense21)

    # V(s)
    dense12 = Dense(n_out1)(flatten)
    if not use_perm_drop:
        prelu32 = PReLU()(dense12)
    else:
        prelu320 = PReLU()(dense12)
        prelu32 = Lambda(perm_drop, output_shape=make_output_shape)(prelu320)

    dense22 = Dense(1)(prelu32)
    out2 = Activation('linear')(dense22)
    out2 = RepeatVector(n_actions)(out2)
    out2 = Reshape((n_actions,))(out2)

    # - E[ A(s,a) ]
    out3 = Lambda(make_output, output_shape=make_output_shape)(out1)

    output = merge([out1, out2, out3], mode='sum', concat_axis=1)

    model = Model(input=input,output=output)
    model.compile(loss=loss, optimizer=adam(lr=lr))

    return model
                 strides=1))
model.add(BatchNormalization())
# Block 4
model.add(Conv1D(128,
                 3,
                 padding='valid',
                 activation='elu',
                 strides=1))
model.add(MaxPooling1D(pool_size=2))
model.add(BatchNormalization())
# Recurrent Block
model.add(LSTM(256))
model.add(BatchNormalization())
# Dense Block
model.add(Dense(1028))
model.add(Activation('elu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Dense(len(cats)))
model.add(Activation('softmax'))


sgd = adam(lr=5e-5, epsilon=1e-7)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])
              
              
model.fit(X_train_n, y_train, validation_data=[X_test_n, y_test],
          batch_size=512, epochs=10)
示例#10
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Yunchuan Chen'

from utils import get_unigram_probtable
from models import NCELangModel
from keras.optimizers import adam

NB_RUN_WORDS = 100000000
NB_VOCAB = 10000
NB_RUN_VAL = 100000
NB_EVALUATE = 5000000
SAVE_PATH = '../data/models/lang/nce0-neg50-e128-c128-lr0.005.pkl'

DATA_PATH = '../data/corpus/wiki-sg-norm-lc-drop-bin.bz2'
BATCH_SIZE = 256
VAL_INTER = 1200

unigram_table = get_unigram_probtable(nb_words=NB_VOCAB)

opt = adam(lr=0.005)
model = NCELangModel(vocab_size=NB_VOCAB, nb_negative=50, embed_dims=128, context_dims=128,
                     negprob_table=unigram_table, optimizer=opt)
model.compile()
model.train(data_file=DATA_PATH,
            save_path=SAVE_PATH,
            batch_size=BATCH_SIZE, train_nb_words=NB_RUN_WORDS,
            val_nb_words=NB_EVALUATE, train_val_nb=NB_RUN_VAL, validation_interval=VAL_INTER)
示例#11
0
    for key in keys:
        score = np.log(mu*total/float(labels_dict[key]))
        class_weight[key] = score if score > 1.0 else 1.0

    return class_weight

class_count_fish = {0:1719,1:200, 2:117, 3:66, 4:176, 5:734, 6:299}
class_count_FoN = {0:2500, 1:500}

class_wt_fish = create_class_weight(class_count_fish)
class_wt_FoN = create_class_weight(class_count_FoN)

from keras.optimizers import adam

sgd = adam(lr=0.00001)

convnet_fish_or_no.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

sgd2 = adam(lr=0.000007)

vgg16_fish.compile(optimizer=sgd2, loss='categorical_crossentropy', metrics=['accuracy'])


# Training Fish-or-no-fish
train_path = '/fp/train'

epoch = 1

while epoch <= 9: