Exemple #1
0
class RenjuModel:

    def __init__(self, config):
        self.config = config
        self.model = None

    #predict a game basedon game_state.
    #the value is based on the current player. the higher, the better position the current player is in.
    def predict(self, gamestates):
        policy, value = self.model.predict(gamestates)
        return policy,value

    def build(self):
        #build the model.
        mc = self.config.model
        width = mc.input_size
        in_x = x = Input((2, width, width))  # [own(8x8), enemy(8x8)]

        # (batch, channels, height, width)
        x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_filter_size, padding="same",
                   data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(x)
        x = BatchNormalization(axis=1)(x)
        x = Activation("relu")(x)

        for _ in range(mc.res_layer_num):
            x = self._build_residual_block(x)

        res_out = x
        # for policy output
        x = Conv2D(filters=2, kernel_size=1, data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(res_out)
        x = BatchNormalization(axis=1)(x)
        x = Activation("relu")(x)
        x = Flatten()(x)
        # no output for 'pass'
        policy_out = Dense(width*width, kernel_regularizer=l2(mc.l2_reg), activation="softmax", name="policy_out")(x)

        # for value output
        x = Conv2D(filters=1, kernel_size=1, data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(res_out)
        x = BatchNormalization(axis=1)(x)
        x = Activation("relu")(x)
        x = Flatten()(x)
        x = Dense(mc.value_fc_size, kernel_regularizer=l2(mc.l2_reg), activation="relu")(x)
        value_out = Dense(1, kernel_regularizer=l2(mc.l2_reg), activation="tanh", name="value_out")(x)

        self.model = Model(in_x, [policy_out, value_out], name="reversi_model")

    def _build_residual_block(self, x):
        mc = self.config.model
        in_x = x
        x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_filter_size, padding="same",
                   data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(x)
        x = BatchNormalization(axis=1)(x)
        x = Activation("relu")(x)
        x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_filter_size, padding="same",
                   data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(x)
        x = BatchNormalization(axis=1)(x)
        x = Add()([in_x, x])
        x = Activation("relu")(x)
        return x

    def save(self,path):
        if not os.path.exists(path):
            os.mkdir(path)
        self.model.save_weights(path + '/model.h5')
        model_json = self.model.to_json()
        with open(path + '/model.json', "w") as json_file:
            json_file.write(model_json)
    
    def load(self,path, old = False):
        if old:
            self.model = load_model(path,custom_objects={
                "objective_function_for_policy" : objective_function_for_policy,
                "objective_function_for_value" : objective_function_for_value
            })
        else:
            json_file = open(path + '/model.json', 'r')
            loaded_model_json = json_file.read()
            json_file.close()
            self.model = model_from_json(loaded_model_json,custom_objects={
                "objective_function_for_policy" : objective_function_for_policy,
                "objective_function_for_value" : objective_function_for_value
            })
            self.model.load_weights(path + '/model.h5')
def build_CNN_model(inputType, do_training=False, model_inputs=None, loss_func='binary_crossentropy',
                    optimize_proc='adam', is_IntermediateModel=False, load_weight_path=None, **kwargs):
    """

    :param inputType:
    :param do_training:
    :param model_inputs:
    :param loss_func:
    :param optimize_proc:
    :param is_IntermediateModel:
    :param load_weight_path:
    :param kwargs:
    :return:
    """

    # assert not do_training and model_inputs, "if do_training then must pass in model_inputs dictionary"

    EMBEDDING_TYPE = 'embeddingMatrix'
    ONEHOT_TYPE = '1hotVector'

    defined_input_types = {EMBEDDING_TYPE, ONEHOT_TYPE}

    assert inputType in defined_input_types, "unknown input type {0}".format(inputType)

    if inputType is ONEHOT_TYPE:

        review_input = Input(shape=(modelParameters.MaxLen_w,), dtype='float32',
                             name="ONEHOT_INPUT")

        layer = Embedding(modelParameters.VocabSize_w + modelParameters.INDEX_FROM, embedding_dims,
                          embeddings_initializer=embedding_init, embeddings_regularizer=embedding_reg,
                          input_length=modelParameters.MaxLen_w, name='1hot_embeddingLayer')(review_input)

        layer = SpatialDropout1D(0.50)(layer)

    elif inputType is EMBEDDING_TYPE:
        review_input = Input(shape=(modelParameters.MaxLen_w, embedding_dims), dtype="float32", name="EMBEDDING_INPUT")
        layer = review_input

    else:
        raise ValueError("Bad inputType arg to build_CNN_model")

    layer = Convolution1D(filters=num_filters1,
                          kernel_size=filter_length1,
                          padding=region,
                          strides=1,
                          activation=conv_activation1,
                          kernel_initializer='glorot_uniform',
                          bias_initializer='zeros',
                          kernel_regularizer=conv_reg1,
                          dilation_rate=1,
                          name='ConvLayer1')(layer)

    layer = SpatialDropout1D(0.50)(layer)

    layer = MaxPooling1D(pool_size=pool_len1)(layer)

    # layer = Convolution1D(filters=num_filters2,
    #                       kernel_size=filter_length2,
    #                       padding=region,
    #                       strides=1,
    #                       activation=conv_activation2,
    #                       kernel_initializer=conv_init2,
    #                       kernel_regularizer=conv_reg2,
    #                       dilation_rate=1,
    #                       name='ConvLayer2')(layer)
    #
    # layer = SpatialDropout1D(0.50)(layer)
    #
    # layer = MaxPooling1D(pool_size=pool_len2)(layer)

    # layer = Convolution1D(filters=num_filters3,
    #                       kernel_size=filter_length3,
    #                       padding=region,
    #                       activation=conv_activation3,
    #                       kernel_initializer=conv_init3,
    #                       kernel_regularizer=conv_reg3,
    #                       dilation_rate=1,
    #                       name='ConvLayer3')(layer)
    #
    # layer = SpatialDropout1D(0.50)(layer)
    #
    # layer = MaxPooling1D(pool_size=pool_len3)(layer)



    # #layer = GlobalMaxPool1D()(layer)
    #
    # layer = Convolution1D(filters=num_filters4,
    #                       kernel_size=filter_length4,
    #                       padding=region,
    #                       activation=conv_activation4,
    #                       kernel_initializer=conv_init4,
    #                       kernel_regularizer=conv_reg4,
    #                       dilation_rate=1,
    #                       name='ConvLayer4')(layer)
    #
    # #layer = leaky_relu(layer)
    #
    # layer = SpatialDropout1D(0.50)(layer)
    #
    # layer = MaxPooling1D(pool_size=pool_len4)(layer)
    # #layer = GlobalMaxPool1D()(layer)
    #
    # # layer = BatchNormalization()(layer)

    layer = Flatten()(layer)

    layer = Dense(dense_dims0, activation=dense_activation0, kernel_regularizer=dense_reg0,
                  kernel_initializer='glorot_normal', bias_initializer='zeros',
                  name='dense0')(layer)

    layer = Dropout(0.50)(layer)

    layer = Dense(dense_dims1, activation=dense_activation1, kernel_regularizer=dense_reg1,
                  kernel_initializer='glorot_normal', bias_initializer='zeros',
                  name='dense1')(layer)

    layer = Dropout(0.50)(layer)

    # layer = Dense(dense_dims2, activation=dense_activation2, kernel_regularizer=dense_reg2,
    #               kernel_initializer=dense_init2,
    #               name='dense2')(layer)
    #
    #
    # layer = Dropout(0.50)(layer)
    #
    # layer = Dense(dense_dims3, activation=dense_activation3, kernel_regularizer=dense_reg3,
    #               kernel_initializer=dense_init3,
    #               name='dense3_outA')(layer)
    # #layer = leaky_relu(layer)
    #
    if is_IntermediateModel:
        return Model(inputs=[review_input], outputs=[layer], name="CNN_model")

    #
    # layer = Dropout(0.5)(layer)

    layer = Dense(dense_dims_final, activation=dense_activation_final, kernel_initializer=dense_init_final,
                  kernel_regularizer=dense_reg0,
                  name='output_Full')(layer)

    CNN_model = Model(inputs=[review_input], outputs=[layer], name="CNN_model")

    CNN_model.compile(optimizer=Adam(lr=0.001, decay=0.0), loss=loss_func, metrics=[binary_accuracy])

    if load_weight_path is not None:
        CNN_model.load_weights(load_weight_path)

    hist = ""
    if do_training:
        weightPath = os.path.join(modelParameters.WEIGHT_PATH, filename)
        configPath = os.path.join(modelParameters.WEIGHT_PATH, filename_config)

        with open(configPath + ".json", 'wb') as f:
            f.write(CNN_model.to_json())

        checkpoint = ModelCheckpoint(weightPath + '_W.{epoch:02d}-{val_loss:.4f}.hdf5',
                                     verbose=1, save_best_only=True, save_weights_only=False, monitor='val_loss')

        earlyStop = EarlyStopping(patience=3, verbose=1, monitor='val_loss')

        LRadjuster = ReduceLROnPlateau(monitor='val_loss', factor=0.30, patience=0, verbose=1, cooldown=1,
                                       min_lr=0.00001, epsilon=1e-2)

        call_backs = [checkpoint, earlyStop, LRadjuster]

        CNN_model.summary()

        hist = CNN_model.fit(*model_inputs['training'],
                             batch_size=batch_size,
                             epochs=nb_epoch, verbose=1,
                             validation_data=model_inputs['dev'],
                             callbacks=call_backs)

    return {"model": CNN_model, "hist": hist}
Exemple #3
0
class Connect4Model:
    def __init__(self):
        self.model = None  # type: Model

    def build(self):
        in_x = x = Input((6, 7, 3))
        # (batch, channels, height, width)
        x = Conv2D(filters=128,
                   kernel_size=8,
                   padding="same",
                   data_format="channels_first",
                   kernel_regularizer=l2(1e-4))(x)
        x = BatchNormalization(axis=1)(x)
        x = Activation("relu")(x)

        for _ in range(10):
            x = self._build_residual_block(x)

        res_out = x
        # for policy output
        x = Conv2D(filters=2,
                   kernel_size=1,
                   data_format="channels_first",
                   kernel_regularizer=l2(1e-4))(res_out)
        x = BatchNormalization(axis=1)(x)
        x = Activation("relu")(x)
        x = Flatten()(x)
        policy_out1 = Dense(7,
                            kernel_regularizer=l2(1e-4),
                            activation="softmax",
                            name="policy_out1")(x)  # how many outputs

        # for value output
        x = Conv2D(filters=1,
                   kernel_size=1,
                   data_format="channels_first",
                   kernel_regularizer=l2(1e-4))(res_out)
        x = BatchNormalization(axis=1)(x)
        x = Activation("relu")(x)
        x = Flatten()(x)
        x = Dense(256, kernel_regularizer=l2(1e-4), activation="relu")(x)
        value_out1 = Dense(1,
                           kernel_regularizer=l2(1e-4),
                           activation="tanh",
                           name="value_out1")(x)

        self.model = Model(in_x,
                           outputs=[policy_out1, value_out1],
                           name="connect4_model")

    def _build_residual_block(self, x):
        in_x = x
        x = Conv2D(filters=128,
                   kernel_size=3,
                   padding="same",
                   data_format="channels_first",
                   kernel_regularizer=l2(1e-4))(x)
        x = BatchNormalization(axis=1)(x)
        x = Activation("relu")(x)
        x = Conv2D(filters=128,
                   kernel_size=3,
                   padding="same",
                   data_format="channels_first",
                   kernel_regularizer=l2(1e-4))(x)
        x = BatchNormalization(axis=1)(x)
        x = Add()([in_x, x])
        x = Activation("relu")(x)
        return x

    def save_model(self, model_path, weight_path):
        # serialize model to JSON
        model_json = self.model.to_json()
        with open(model_path, "w") as json_file:
            json_file.write(model_json)
        # serialize weights to HDF5
        self.model.save_weights(weight_path)
        print("Saved model to disk")
Exemple #4
0
x = Dense(1024, activation='relu')(x)
predictions = Dense(nb_classes,
                    init='glorot_uniform',
                    W_regularizer=l2(.0005),
                    activation='softmax')(x)

model = Model(base_model.input, predictions)

#     model = load_model(filepath='./model4.29-0.69.hdf5')

opt = SGD(lr=.01, momentum=.9)
model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

model_json = model.to_json()
with open("{}/{}.def".format(job_path, model_name), "w") as json_file:
    json_file.write(model_json)

print(train_generator.class_indices)
f = open("{}/{}.cls".format(job_path, model_name), 'w')
f.write(json.dumps(train_generator.class_indices))
f.close()

# fine-tune the model
model.fit_generator(
    train_generator,
    samples_per_epoch=nb_train_samples,
    epochs=epochs,
    validation_data=validation_generator,
    nb_val_samples=nb_validation_samples,
Exemple #5
0
def build_CNN_model(inputType,
                    do_training=False,
                    model_inputs=None,
                    loss_func='binary_crossentropy',
                    optimize_proc='adam',
                    is_IntermediateModel=False,
                    load_weight_path=None,
                    **kwargs):
    """

    :param inputType:
    :param do_training:
    :param model_inputs:
    :param loss_func:
    :param optimize_proc:
    :param is_IntermediateModel:
    :param load_weight_path:
    :param kwargs:
    :return:
    """

    # assert not do_training and model_inputs, "if do_training then must pass in model_inputs dictionary"

    EMBEDDING_TYPE = 'embeddingMatrix'
    ONEHOT_TYPE = '1hotVector'

    defined_input_types = {EMBEDDING_TYPE, ONEHOT_TYPE}

    assert inputType in defined_input_types, "unknown input type {0}".format(
        inputType)

    if inputType is ONEHOT_TYPE:

        review_input = Input(shape=(modelParameters.MaxLen_w, ),
                             dtype='float32',
                             name="ONEHOT_INPUT")

        layer = Embedding(modelParameters.VocabSize_w +
                          modelParameters.INDEX_FROM,
                          embedding_dims,
                          embeddings_initializer=embedding_init,
                          embeddings_regularizer=embedding_reg,
                          input_length=modelParameters.MaxLen_w,
                          name='1hot_embeddingLayer')(review_input)

        layer = SpatialDropout1D(0.50)(layer)

    elif inputType is EMBEDDING_TYPE:
        review_input = Input(shape=(modelParameters.MaxLen_w, embedding_dims),
                             dtype="float32",
                             name="EMBEDDING_INPUT")
        layer = review_input

    else:
        raise ValueError("Bad inputType arg to build_CNN_model")

    layer = Convolution1D(filters=num_filters1,
                          kernel_size=filter_length1,
                          padding=region,
                          strides=1,
                          activation=conv_activation1,
                          kernel_initializer='glorot_uniform',
                          bias_initializer='zeros',
                          kernel_regularizer=conv_reg1,
                          dilation_rate=1,
                          name='ConvLayer1')(layer)

    layer = SpatialDropout1D(0.50)(layer)

    layer = MaxPooling1D(pool_size=pool_len1)(layer)

    # layer = Convolution1D(filters=num_filters2,
    #                       kernel_size=filter_length2,
    #                       padding=region,
    #                       strides=1,
    #                       activation=conv_activation2,
    #                       kernel_initializer=conv_init2,
    #                       kernel_regularizer=conv_reg2,
    #                       dilation_rate=1,
    #                       name='ConvLayer2')(layer)
    #
    # layer = SpatialDropout1D(0.50)(layer)
    #
    # layer = MaxPooling1D(pool_size=pool_len2)(layer)

    # layer = Convolution1D(filters=num_filters3,
    #                       kernel_size=filter_length3,
    #                       padding=region,
    #                       activation=conv_activation3,
    #                       kernel_initializer=conv_init3,
    #                       kernel_regularizer=conv_reg3,
    #                       dilation_rate=1,
    #                       name='ConvLayer3')(layer)
    #
    # layer = SpatialDropout1D(0.50)(layer)
    #
    # layer = MaxPooling1D(pool_size=pool_len3)(layer)

    # #layer = GlobalMaxPool1D()(layer)
    #
    # layer = Convolution1D(filters=num_filters4,
    #                       kernel_size=filter_length4,
    #                       padding=region,
    #                       activation=conv_activation4,
    #                       kernel_initializer=conv_init4,
    #                       kernel_regularizer=conv_reg4,
    #                       dilation_rate=1,
    #                       name='ConvLayer4')(layer)
    #
    # #layer = leaky_relu(layer)
    #
    # layer = SpatialDropout1D(0.50)(layer)
    #
    # layer = MaxPooling1D(pool_size=pool_len4)(layer)
    # #layer = GlobalMaxPool1D()(layer)
    #
    # # layer = BatchNormalization()(layer)

    layer = Flatten()(layer)

    layer = Dense(dense_dims0,
                  activation=dense_activation0,
                  kernel_regularizer=dense_reg0,
                  kernel_initializer='glorot_normal',
                  bias_initializer='zeros',
                  name='dense0')(layer)

    layer = Dropout(0.50)(layer)

    layer = Dense(dense_dims1,
                  activation=dense_activation1,
                  kernel_regularizer=dense_reg1,
                  kernel_initializer='glorot_normal',
                  bias_initializer='zeros',
                  name='dense1')(layer)

    layer = Dropout(0.50)(layer)

    # layer = Dense(dense_dims2, activation=dense_activation2, kernel_regularizer=dense_reg2,
    #               kernel_initializer=dense_init2,
    #               name='dense2')(layer)
    #
    #
    # layer = Dropout(0.50)(layer)
    #
    # layer = Dense(dense_dims3, activation=dense_activation3, kernel_regularizer=dense_reg3,
    #               kernel_initializer=dense_init3,
    #               name='dense3_outA')(layer)
    # #layer = leaky_relu(layer)
    #
    if is_IntermediateModel:
        return Model(inputs=[review_input], outputs=[layer], name="CNN_model")

    #
    # layer = Dropout(0.5)(layer)

    layer = Dense(dense_dims_final,
                  activation=dense_activation_final,
                  kernel_initializer=dense_init_final,
                  kernel_regularizer=dense_reg0,
                  name='output_Full')(layer)

    CNN_model = Model(inputs=[review_input], outputs=[layer], name="CNN_model")

    CNN_model.compile(optimizer=Adam(lr=0.001, decay=0.0),
                      loss=loss_func,
                      metrics=[binary_accuracy])

    if load_weight_path is not None:
        CNN_model.load_weights(load_weight_path)

    hist = ""
    if do_training:
        weightPath = os.path.join(modelParameters.WEIGHT_PATH, filename)
        configPath = os.path.join(modelParameters.WEIGHT_PATH, filename_config)

        with open(configPath + ".json", 'wb') as f:
            f.write(CNN_model.to_json())

        checkpoint = ModelCheckpoint(weightPath +
                                     '_W.{epoch:02d}-{val_loss:.4f}.hdf5',
                                     verbose=1,
                                     save_best_only=True,
                                     save_weights_only=False,
                                     monitor='val_loss')

        earlyStop = EarlyStopping(patience=3, verbose=1, monitor='val_loss')

        LRadjuster = ReduceLROnPlateau(monitor='val_loss',
                                       factor=0.30,
                                       patience=0,
                                       verbose=1,
                                       cooldown=1,
                                       min_lr=0.00001,
                                       epsilon=1e-2)

        call_backs = [checkpoint, earlyStop, LRadjuster]

        CNN_model.summary()

        hist = CNN_model.fit(*model_inputs['training'],
                             batch_size=batch_size,
                             epochs=nb_epoch,
                             verbose=1,
                             validation_data=model_inputs['dev'],
                             callbacks=call_backs)

    return {"model": CNN_model, "hist": hist}