Example #1
0
 def conv2d_work(input_dim):
     seq = Sequential()
     assert self.config['num_conv2d_layers'] > 0
     for i in range(self.config['num_conv2d_layers']):
         seq.add(Conv2D(filters=self.config['2d_kernel_counts'][i], kernel_size=self.config['2d_kernel_sizes'][i], padding='same', activation='relu'))
         seq.add(MaxPooling2D(pool_size=(self.config['2d_mpool_sizes'][i][0], self.config['2d_mpool_sizes'][i][1])))
     return seq
Example #2
0
def train_model():
    # (X_train, Y_train, X_test, Y_test) = prapare_train()
    X_ = []
    with open('../data/train_matrix.out') as train_file:
        X_train = json.load(train_file)
        for x in X_train:
            a = len(x)
            print a/2
            x1 = x[:a/2]
            x2 = x[a/2:]
            x3 = []
            x3.append(x1)
            x3.append(x2)
            X_.append(x3)
    # X_test = pickle.load('../data/test_matrix.out')
    Y_train = [1,0,0]*3
    # Y_test = [1,0,0]*3
    # print len(X_train) - len(Y_train)
    # print len(X_test) - len(Y_test)
    model = Sequential()
    model = get_nn_model()
    model.compile(loss='binary_crossentropy',
                optimizer='adam',
                metrics=['accuracy'])
    # model.fit(X_train, Y_train,
    #       batch_size=batch_size,
    #       nb_epoch=nb_epoch,
    #       validation_data=(X_test, Y_test))
#2
    model.fit(X_, Y_train,
          batch_size=batch_size,
          nb_epoch=nb_epoch,
          validation_split = 0.2)
    print 'ok'
    def create(self):
        language_model = Sequential()
        self.textual_embedding(language_model, mask_zero=True)
        self.language_model = language_model

        visual_model_factory = \
                select_sequential_visual_model[self._config.trainable_perception_name](
                    self._config.visual_dim)
        visual_model = visual_model_factory.create()
        visual_dimensionality = visual_model_factory.get_dimensionality()
        self.visual_embedding(visual_model, visual_dimensionality)
        #visual_model = Sequential()
        #self.visual_embedding(visual_model)
        # the below should contain all zeros
        zero_model = Sequential()
        zero_model.add(RepeatVector(self._config.max_input_time_steps)-1)
        visual_model.add(Merge[visual_model, zero_model], mode='concat')
        self.visual_model = visual_model

        if self._config.multimodal_merge_mode == 'dot':
            self.add(Merge([language_model, visual_model], mode='dot', dot_axes=[(1,),(1,)]))
        else:
            self.add(Merge([language_model, visual_model], mode=self._config.multimodal_merge_mode))

        self.add(self._config.recurrent_encoder(
            self._config.hidden_state_dim, 
            return_sequences=False,
            go_backwards=self._config.go_backwards))
        self.deep_mlp()
        self.add(Dense(self._config.output_dim))
        self.add(Activation('softmax'))
    def __init__(self, restore=None, session=None, Dropout=Dropout, num_labels=10):
        self.num_channels = 1
        self.image_size = 28
        self.num_labels = num_labels

        model = Sequential()

        nb_filters = 64
        layers = [Conv2D(nb_filters, (5, 5), strides=(2, 2), padding="same",
                         input_shape=(28, 28, 1)),
                  Activation('relu'),
                  Conv2D(nb_filters, (3, 3), strides=(2, 2), padding="valid"),
                  Activation('relu'),
                  Conv2D(nb_filters, (3, 3), strides=(1, 1), padding="valid"),
                  Activation('relu'),
                  Flatten(),
                  Dense(32),
                  Activation('relu'),
                  Dropout(.5),
                  Dense(num_labels)]

        for layer in layers:
            model.add(layer)

        if restore != None:
            model.load_weights(restore)
        
        self.model = model
class QLearn:
    def __init__(self, actions, epsilon, alpha, gamma):
        
        # instead of a dictionary, we'll be using
        #   a neural network
        # self.q = {}
        self.epsilon = epsilon  # exploration constant
        self.alpha = alpha      # discount constant
        self.gamma = gamma      # discount factor
        self.actions = actions
        
        # Build the neural network
        self.network = Sequential()
        self.network.add(Dense(50, init='lecun_uniform', input_shape=(4,)))
        # self.network.add(Activation('sigmoid'))
        #self.network.add(Dropout(0.2))

        self.network.add(Dense(20, init='lecun_uniform'))
        # self.network.add(Activation('sigmoid'))
        # #self.network.add(Dropout(0.2))

        self.network.add(Dense(2, init='lecun_uniform'))
        # self.network.add(Activation('linear')) #linear output so we can have range of real-valued outputs

        # rms = SGD(lr=0.0001, decay=1e-6, momentum=0.5) # explodes to non
        rms = RMSprop()
        # rms = Adagrad()
        # rms = Adam()
        self.network.compile(loss='mse', optimizer=rms)
        # Get a summary of the network
        self.network.summary()
Example #6
0
def test_dropout(layer_class):
    for unroll in [True, False]:
        layer_test(layer_class,
                   kwargs={'units': units,
                           'dropout': 0.1,
                           'recurrent_dropout': 0.1,
                           'unroll': unroll},
                   input_shape=(num_samples, timesteps, embedding_dim))

        # Test that dropout is applied during training
        x = K.ones((num_samples, timesteps, embedding_dim))
        layer = layer_class(units, dropout=0.5, recurrent_dropout=0.5,
                            input_shape=(timesteps, embedding_dim))
        y = layer(x)
        assert y._uses_learning_phase

        y = layer(x, training=True)
        assert not getattr(y, '_uses_learning_phase')

        # Test that dropout is not applied during testing
        x = np.random.random((num_samples, timesteps, embedding_dim))
        layer = layer_class(units, dropout=0.5, recurrent_dropout=0.5,
                            unroll=unroll,
                            input_shape=(timesteps, embedding_dim))
        model = Sequential([layer])
        assert model.uses_learning_phase
        y1 = model.predict(x)
        y2 = model.predict(x)
        assert_allclose(y1, y2)
Example #7
0
def Simple(layers, func, ipt):
    model = Sequential()
    #model.add(BatchNormalization(input_shape = [ipt]))
    model.add(Dense(layers[0], input_dim = ipt, activation = func[0]))
    for i in range(1, len(layers)):
        model.add(Dense(layers[i], activation = func[i]))
    return model
Example #8
0
 def encoders_m(self, inputs):
     input_encoder_m = Sequential()
     input_encoder_m.add(Embedding(input_dim=self.vocab_size,
                                   output_dim=64))
     input_encoder_m.add(Dropout(0.3))
     encode_m = input_encoder_m(inputs)
     return encode_m
Example #9
0
 def encoders_c(self, inputs):
     input_encoder_c = Sequential()
     input_encoder_c.add(Embedding(input_dim=self.vocab_size,
                                   output_dim=self.query_maxlen))
     input_encoder_c.add(Dropout(0.3))
     encoder_c = input_encoder_c(inputs)
     return encoder_c
Example #10
0
def build_part1_RNN(window_size):

    model = Sequential()
    model.add(LSTM(5, input_shape=(window_size,1) ))
    #model.add(Dropout(0.5))
    model.add(Dense(1))
    return model
Example #11
0
def build_part2_RNN(window_size, num_chars):

    model = Sequential()
    model.add(LSTM(200, input_shape=(window_size, num_chars)))
    #model.add(Dropout(0.5))
    model.add(Dense(num_chars, activation='softmax'))
    return model
def get_item_subgraph(input_shape, latent_dim):
    # Could take item metadata here, do convolutional layers etc.

    model = Sequential()
    model.add(Dense(latent_dim, input_shape=input_shape))

    return model
Example #13
0
def create_model(kernel_regularizer=None, activity_regularizer=None):
    model = Sequential()
    model.add(Dense(num_classes,
                    kernel_regularizer=kernel_regularizer,
                    activity_regularizer=activity_regularizer,
                    input_shape=(data_dim,)))
    return model
Example #14
0
def fork (model, n=2):
    forks = []
    for i in range(n):
        f = Sequential()
        f.add (model)
        forks.append(f)
    return forks
Example #15
0
def build_partial_cnn1(img_rows, img_cols):
    model = Sequential()
    #model.add(Convolution2D(nb_filter=100, nb_row=5, nb_col=5,
    model.add(Convolution2D(nb_filter=10, nb_row=2, nb_col=2,
                            init='glorot_uniform', activation='linear',
                            border_mode='valid',
                            input_shape=(1, img_rows, img_cols)))
    model.add(Activation('relu'))

    #model.add(MaxPooling2D(pool_size=(2, 2)))

    #model.add(Convolution2D(nb_filter=100, nb_row=5, nb_col=5,
    '''model.add(Convolution2D(nb_filter=512, nb_row=5, nb_col=5,
                            init='glorot_uniform', activation='linear',
                            border_mode='valid'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    #model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(256))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))'''

    return model
Example #16
0
 def build(self, layers):
     model = Sequential()
     
     for layer in layers:
         model.add(layer)
     
     self.model = TimeDistributed(model)
Example #17
0
File: test.py Project: aasensio/EST
class trainCNN(object):

    def __init__(self):

        self.X = np.random.randn(200,1)
        self.Y = 1.2*self.X**2 + 0.5

    def defineCNN(self):
        print("Setting up network...")
        self.model = Sequential()
        self.model.add(Dense(40, input_shape=(1,)))
        self.model.add(Activation('tanh'))
        self.model.add(Dense(1))

        sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
        self.model.compile(loss='mse', optimizer='RMSprop')

    def trainCNN(self, nIterations):
        print("Training network...")
        self.metrics = self.model.fit(self.X, self.Y, batch_size=20, nb_epoch=nIterations, validation_split=0.2, shuffle=False)
        # self.model.fit(self.XTrainSet, self.YTrainSet, batch_size=self.batchSize, nb_epoch=self.nbEpoch, validation_split=0.2)

    def testCNN(self):
        train = self.model.predict(self.X)
        pl.plot(self.X, self.Y, '.')
        pl.plot(self.X, train, 'x')
Example #18
0
def create_model(insert=None):
    '''Create the basic model'''

    model = Sequential()

    layers = [Convolution2D(NB_FILTERS, NB_CONV, NB_CONV,
                            border_mode='valid',
                            input_shape=(1, IMGROWS, IMGCOLS)),
              Activation('relu'),
              MaxPooling2D(pool_size=(NB_POOL, NB_POOL)),
              Dropout(0.25),
              Flatten(),
              Dense(128),
              Activation('relu'),
              Dropout(0.5),
              Dense(NB_CLASSES),
              Activation('softmax')]

    if insert is not None:
        for l in insert['layers']:
            layers.insert(insert['insert_pos'], l)

    for layer in layers:
        model.add(layer)

    return model
Example #19
0
 def __init__(self):
     model = Sequential()
     model.add(Embedding(115227, 50, input_length=75, weights=pre_weights))
     model.compile(loss=MCE, optimizer="adadelta")
     print "Build Network Completed..."
     self.model = model
     self.vocab = {"get_index":{}, "get_word":[]}
def build_siamese(input_model_1, input_model_2, input_dim, output_dim):
    """

    :param input_model_1:
    :type input_model_1:
    :param input_model_2:
    :type input_model_2:
    :param input_dim: last layer input
    :type input_dim:
    :param output_dim: last layer output
    :type output_dim:
    :return:
    :rtype:
    """

    inputs = [input_model_1, input_model_2]

    layer = Dense(input_dim=input_dim, output_dim=output_dim)

    model = Sequential()
    # mode: one of {sum, mul, concat, ave, join, cos, dot}.
    model.add(Siamese(layer, inputs, 'sum'))

    # model.compile(loss='mse', optimizer='sgd')
    return model
Example #21
0
    def make_fc_model(self):
        '''
        creates a fully convolutional model from self.model
        '''
        # get index of first dense layer in model
        behead_ix = self._get_behead_index(self.model_layer_names)
        model_layers = self.model.layers[:behead_ix]
        # shape of image entering FC layers
        inp_shape = self.model.layers[behead_ix - 1].get_output_shape_at(-1)

        # replace dense layers with convolutions
        model = Sequential()
        model_layers += [Convolution2D(2048, 1, 1)]
        model_layers += [Activation('relu')]
        model_layers += [Convolution2D(2048, 1, 1)]
        model_layers += [Activation('relu')]
        model_layers += [Convolution2D(self.nb_classes, inp_shape[-1], inp_shape[-1])]
        # must be same shape as target vector (None, num_classes, 1)
        model_layers += [Reshape((self.nb_classes-1,1))]
        model_layers += [Activation('softmax')]

        print 'Compiling Fully Convolutional Model...'
        for process in model_layers:
            model.add(process)
        sgd = SGD(lr=self.lr_1, momentum=0.9, nesterov=True)
        model.compile(loss='categorical_crossentropy', optimizer='sgd')
        print 'Done.'
        return model
Example #22
0
def create_mlp_network(input_dim):
    seq = Sequential()
    seq.add(SparseFullyConnectedLayer(300, input_dim = input_dim, activation = "relu"))
    # seq.add(Dense(300, input_dim = input_dim, activation = "relu"))
    seq.add(Dense(300, activation = "relu"))
    seq.add(Dense(128, activation = "relu"))
    return seq
def getVggModel():
    """Pretrained VGG16 model with fine-tunable last two layers"""
    input_image = Input(shape = (160,320,3))
    
    model = Sequential()
    model.add(Lambda(lambda x: x/255.0 -0.5,input_shape=(160,320,3)))
    model.add(Cropping2D(cropping=((70,25),(0,0))))
    
    base_model = VGG16(input_tensor=input_image, include_top=False)
        
    for layer in base_model.layers[:-3]:
        layer.trainable = False

    W_regularizer = l2(0.01)

    x = base_model.get_layer("block5_conv3").output
    x = AveragePooling2D((2, 2))(x)
    x = Dropout(0.5)(x)
    x = BatchNormalization()(x)
    x = Dropout(0.5)(x)
    x = Flatten()(x)
    x = Dense(4096, activation="elu", W_regularizer=l2(0.01))(x)
    x = Dropout(0.5)(x)
    x = Dense(2048, activation="elu", W_regularizer=l2(0.01))(x)
    x = Dense(2048, activation="elu", W_regularizer=l2(0.01))(x)
    x = Dense(1, activation="linear")(x)
    return Model(input=input_image, output=x)
Example #24
0
def test_replace_with_mask_layer__no_mask():
    m = Sequential([
        ReplaceWithMaskLayer(input_shape=(None, 10)),
    ])
    actual = m.predict(np.random.uniform(size=(30, 20, 10)))
    expected = np.ones((30, 20, 1))
    np.testing.assert_allclose(actual, expected)
Example #25
0
def define_model(lr, momentum):
    # CONFIG
    model = Sequential()

    # Create Layers
    # CONVNET
    layers = []
    #layers.append(GaussianNoise(0.02))
    layers.append(Convolution2D(8, 9, 9, activation = "relu", input_shape=(1,100,100)))
    layers.append(MaxPooling2D(pool_size=(2,2)))
    layers.append(Convolution2D(16, 7, 7, activation = "relu"))
    layers.append(MaxPooling2D(pool_size=(2,2)))
    layers.append(Convolution2D(32, 5, 5, activation = "relu"))
    layers.append(MaxPooling2D(pool_size=(2,2)))
    layers.append(Convolution2D(64, 3, 3, activation = "relu"))
    layers.append(MaxPooling2D(pool_size=(2,2)))
    layers.append(Convolution2D(250, 3, 3, activation= "relu"))
    # MLP
    layers.append(Flatten())
    layers.append(Dense(125, activation="relu"))
    layers.append(Dense(2, activation="softmax"))

    # Adding Layers
    for layer in layers:
        model.add(layer)

    # COMPILE (learning rate, momentum, objective...)
    sgd = SGD(lr=lr, momentum=momentum)

    model.compile(loss="categorical_crossentropy", optimizer=sgd)

    return model
Example #26
0
def test_recurrent_wrapper__simple_rnn__no_sequences():
    """Return only the latest step in the sequence
    """
    def recurrent_layer():
        hidden = Input((128,))
        input = Input((10,))

        x = Dense(128, activation='relu')(input)
        x = merge([hidden, x], mode='sum')
        new_hidden = Activation('sigmoid')(x)

        return RecurrentWrapper(
            input=[input],
            output=[new_hidden],
            bind={hidden: new_hidden},
            return_sequences=False,
        )

    m = Sequential([
        InputLayer(input_shape=(None, 10)),
        recurrent_layer(),
    ])

    result = m.predict(np.random.uniform(size=(30, 20, 10)))

    # map into hidden state
    assert result.shape == (30, 128)
    def create(self):
        language_model = Sequential()
        self.textual_embedding(language_model, mask_zero=True)
        self.temporal_pooling(language_model)
        language_model.add(DropMask())
        #language_model.add(BatchNormalization(mode=1))
        self.language_model = language_model

        visual_model_factory = \
                select_sequential_visual_model[self._config.trainable_perception_name](
                    self._config.visual_dim)
        visual_model = visual_model_factory.create()
        visual_dimensionality = visual_model_factory.get_dimensionality()
        self.visual_embedding(visual_model, visual_dimensionality)
        #visual_model.add(BatchNormalization(mode=1))
        self.visual_model = visual_model
        
        if self._config.multimodal_merge_mode == 'dot':
            self.add(Merge([language_model, visual_model], mode='dot', dot_axes=[(1,),(1,)]))
        else:
            self.add(Merge([language_model, visual_model], mode=self._config.multimodal_merge_mode))

        self.deep_mlp()
        self.add(Dense(self._config.output_dim))
        self.add(Activation('softmax'))
Example #28
0
def test_recurrent_wrapper__simple_rnn__output_model():
    """The hidden state is not returned, but mapped by an output model.
    """
    def recurrent_layer():
        hidden = Input((128,))
        input = Input((10,))

        x = Dense(128, activation='relu')(input)
        x = merge([hidden, x], mode='sum')
        new_hidden = Activation('sigmoid')(x)
        output = Dense(64)(x)

        return RecurrentWrapper(
            input=[input],
            output=[output],
            bind={hidden: new_hidden},
            return_sequences=True,
        )

    m = Sequential([
        InputLayer(input_shape=(None, 10)),
        recurrent_layer(),
    ])

    assert m.predict(np.random.uniform(size=(30, 20, 10))).shape == (30, 20, 64)
    def create(self):
        language_model = Sequential()
        self.textual_embedding(language_model, mask_zero=True)
        self.stacked_RNN(language_model)
        language_model.add(self._config.recurrent_encoder(
            self._config.hidden_state_dim, 
            return_sequences=False,
            go_backwards=self._config.go_backwards))
        self.language_model = language_model

        visual_model_factory = \
                select_sequential_visual_model[self._config.trainable_perception_name](
                    self._config.visual_dim)
        visual_model = visual_model_factory.create()
        visual_dimensionality = visual_model_factory.get_dimensionality()
        self.visual_embedding(visual_model, visual_dimensionality)
        #visual_model = Sequential()
        #self.visual_embedding(visual_model)
        self.visual_model = visual_model

        if self._config.multimodal_merge_mode == 'dot':
            self.add(Merge([language_model, visual_model], mode='dot', dot_axes=[(1,),(1,)]))
        else:
            self.add(Merge([language_model, visual_model], mode=self._config.multimodal_merge_mode))

        self.add(Dropout(0.5))
        self.add(Dense(self._config.output_dim))

        self.add(RepeatVector(self._config.max_output_time_steps))
        self.add(self._config.recurrent_decoder(
                self._config.hidden_state_dim, return_sequences=True))
        self.add(Dropout(0.5))
        self.add(TimeDistributedDense(self._config.output_dim))
        self.add(Activation('softmax'))
Example #30
0
	def question_encoder(self, dropout=0.3):
		question_encoder = Sequential()
		question_encoder.add(Embedding(input_dim=vocab_size,
                               output_dim=64,
                               input_length=query_maxlen))
		question_encoder.add(Dropout(dropout))
		self._question_encoder = question_encoder