Example #1
0
def test_nested_sequential(in_tmpdir):
    (x_train, y_train), (x_test, y_test) = _get_test_data()

    inner = Sequential()
    inner.add(Dense(num_hidden, input_shape=(input_dim,)))
    inner.add(Activation('relu'))
    inner.add(Dense(num_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))
    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2, validation_split=0.1)
    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=0)
    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, shuffle=False)

    model.train_on_batch(x_train[:32], y_train[:32])

    loss = model.evaluate(x_test, y_test, verbose=0)

    model.predict(x_test, verbose=0)
    model.predict_classes(x_test, verbose=0)
    model.predict_proba(x_test, verbose=0)

    fname = 'test_nested_sequential_temp.h5'
    model.save_weights(fname, overwrite=True)

    inner = Sequential()
    inner.add(Dense(num_hidden, input_shape=(input_dim,)))
    inner.add(Activation('relu'))
    inner.add(Dense(num_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate(x_test, y_test, verbose=0)
    assert(loss == nloss)

    # test serialization
    config = model.get_config()
    Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    model_from_json(json_str)

    yaml_str = model.to_yaml()
    model_from_yaml(yaml_str)
Example #2
0
def test_nested_sequential():
    (X_train, y_train), (X_test, y_test) = _get_test_data()

    inner = Sequential()
    inner.add(Dense(nb_hidden, input_shape=(input_dim,)))
    inner.add(Activation("relu"))
    inner.add(Dense(nb_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation("softmax"))
    model.compile(loss="categorical_crossentropy", optimizer="rmsprop")

    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, y_test))
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_split=0.1)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)

    model.train_on_batch(X_train[:32], y_train[:32])

    loss = model.evaluate(X_test, y_test, verbose=0)

    model.predict(X_test, verbose=0)
    model.predict_classes(X_test, verbose=0)
    model.predict_proba(X_test, verbose=0)

    fname = "test_nested_sequential_temp.h5"
    model.save_weights(fname, overwrite=True)

    inner = Sequential()
    inner.add(Dense(nb_hidden, input_shape=(input_dim,)))
    inner.add(Activation("relu"))
    inner.add(Dense(nb_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation("softmax"))
    model.compile(loss="categorical_crossentropy", optimizer="rmsprop")
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate(X_test, y_test, verbose=0)
    assert loss == nloss

    # test serialization
    config = model.get_config()
    new_model = Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    new_model = model_from_json(json_str)

    yaml_str = model.to_yaml()
    new_model = model_from_yaml(yaml_str)
Example #3
0
def test_merge_overlap():
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    model = Sequential()
    model.add(Merge([left, left], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test))
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test))
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)

    model.train_on_batch(X_train[:32], y_train[:32])

    loss = model.evaluate(X_train, y_train, verbose=0)
    assert(loss < 0.7)
    model.predict(X_test, verbose=0)
    model.predict_classes(X_test, verbose=0)
    model.predict_proba(X_test, verbose=0)
    model.get_config(verbose=0)

    fname = 'test_merge_overlap_temp.h5'
    model.save_weights(fname, overwrite=True)
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate(X_train, y_train, verbose=0)
    assert(loss == nloss)
Example #4
0
def test_merge_sum():
    (X_train, y_train), (X_test, y_test) = _get_test_data()
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))

    model = Sequential()
    model.add(Merge([left, right], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_data=([X_test, X_test], y_test))
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

    loss = model.evaluate([X_test, X_test], y_test, verbose=0)

    model.predict([X_test, X_test], verbose=0)
    model.predict_classes([X_test, X_test], verbose=0)
    model.predict_proba([X_test, X_test], verbose=0)

    # test weight saving
    fname = 'test_merge_sum_temp.h5'
    model.save_weights(fname, overwrite=True)
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))
    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))
    model = Sequential()
    model.add(Merge([left, right], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.load_weights(fname)
    os.remove(fname)
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    nloss = model.evaluate([X_test, X_test], y_test, verbose=0)
    assert(loss == nloss)

    # test serialization
    config = model.get_config()
    Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    model_from_json(json_str)

    yaml_str = model.to_yaml()
    model_from_yaml(yaml_str)
Example #5
0
    def test_merge_concat(self):
        print('Test merge: concat')
        left = Sequential()
        left.add(Dense(nb_hidden, input_shape=(input_dim,)))
        left.add(Activation('relu'))

        right = Sequential()
        right.add(Dense(nb_hidden, input_shape=(input_dim,)))
        right.add(Activation('relu'))

        model = Sequential()
        model.add(Merge([left, right], mode='concat'))
        model.add(Dense(nb_class))
        model.add(Activation('softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_data=([X_test, X_test], y_test))
        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_data=([X_test, X_test], y_test))
        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_split=0.1)
        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_split=0.1)
        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

        loss = model.evaluate([X_train, X_train], y_train, verbose=0)
        print('loss:', loss)
        if loss > 0.7:
            raise Exception('Score too low, learning issue.')
        model.predict([X_test, X_test], verbose=0)
        model.predict_classes([X_test, X_test], verbose=0)
        model.predict_proba([X_test, X_test], verbose=0)
        model.get_config(verbose=0)

        print('test weight saving')
        fname = 'test_merge_concat_temp.h5'
        model.save_weights(fname, overwrite=True)
        left = Sequential()
        left.add(Dense(nb_hidden, input_shape=(input_dim,)))
        left.add(Activation('relu'))

        right = Sequential()
        right.add(Dense(nb_hidden, input_shape=(input_dim,)))
        right.add(Activation('relu'))

        model = Sequential()
        model.add(Merge([left, right], mode='concat'))

        model.add(Dense(nb_class))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
        model.load_weights(fname)
        os.remove(fname)

        nloss = model.evaluate([X_train, X_train], y_train, verbose=0)
        assert(loss == nloss)
Example #6
0
def test_siamese_1():
    (X_train, y_train), (X_test, y_test) = _get_test_data()
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))

    model = Sequential()
    model.add(Siamese(Dense(nb_hidden), [left, right], merge_mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_data=([X_test, X_test], y_test))
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_data=([X_test, X_test], y_test))
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

    loss = model.evaluate([X_test, X_test], y_test, verbose=0)
    assert(loss < 0.8)

    model.predict([X_test, X_test], verbose=0)
    model.predict_classes([X_test, X_test], verbose=0)
    model.predict_proba([X_test, X_test], verbose=0)
    model.get_config(verbose=0)

    # test weight saving
    fname = 'test_siamese_1.h5'
    model.save_weights(fname, overwrite=True)
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))

    model = Sequential()
    model.add(Siamese(Dense(nb_hidden), [left, right], merge_mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))

    model.load_weights(fname)
    os.remove(fname)
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    nloss = model.evaluate([X_test, X_test], y_test, verbose=0)
    assert(loss == nloss)
def evaluate_keras_classification_model(X_train, X_test, y_train, y_test):
    X_train = X_train.astype(theano.config.floatX)
    X_test = X_test.astype(theano.config.floatX)

    print("First 3 labels: %s" % y_train[:3])

    y_train_ohe = np_utils.to_categorical(y_train)
    print('\nFirst 3 labels (one-hot):\n', y_train_ohe[:3])

    model = Sequential()
    model.add(Dense(
        input_dim=X_train.shape[1],
        output_dim=50,
        init='uniform',
        activation='tanh',
    ))
    model.add(Dense(
        input_dim=50,
        output_dim=50,
        init='uniform',
        activation='tanh',
    ))
    model.add(Dense(
        input_dim=50,
        output_dim=y_train_ohe.shape[1],
        init='uniform',
        activation='softmax',
    ))

    sgd = SGD(lr=0.001, decay=1e-7, momentum=0.9)
    model.compile(loss='categorical_crossentropy', optimizer=sgd)

    model.fit(
        X_train,
        y_train_ohe,
        nb_epoch=5,
        batch_size=300,
        verbose=1,
        validation_split=0.1,
        show_accuracy=True,
    )

    y_train_pred = model.predict_classes(X_train, verbose=0)
    print('First 3 predictions: ', y_train_pred[:3])

    train_acc = np.sum(y_train == y_train_pred, axis=0) / X_train.shape[0]
    print("Training accuracy: %.2f%%" % (train_acc * 100))

    y_test_pred = model.predict_classes(X_test, verbose=0)
    test_acc = np.sum(y_test == y_test_pred, axis=0) / X_test.shape[0]
    print("Test accuracy: %.2f%%" % (test_acc * 100))
Example #8
0
    def test_sequential(self):
        print('Test sequential')
        model = Sequential()
        model.add(Dense(nb_hidden, input_shape=(input_dim,)))
        model.add(Activation('relu'))
        model.add(Dense(nb_class))
        model.add(Activation('softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test))
        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test))
        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1)
        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1)
        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)

        model.train_on_batch(X_train[:32], y_train[:32])

        loss = model.evaluate(X_train, y_train, verbose=0)
        print('loss:', loss)
        if loss > 0.7:
            raise Exception('Score too low, learning issue.')
        model.predict(X_test, verbose=0)
        model.predict_classes(X_test, verbose=0)
        model.predict_proba(X_test, verbose=0)
        model.get_config(verbose=0)

        print('test weight saving')
        fname = 'test_sequential_temp.h5'
        model.save_weights(fname, overwrite=True)
        model = Sequential()
        model.add(Dense(nb_hidden, input_shape=(input_dim,)))
        model.add(Activation('relu'))
        model.add(Dense(nb_class))
        model.add(Activation('softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
        model.load_weights(fname)
        os.remove(fname)

        nloss = model.evaluate(X_train, y_train, verbose=0)
        assert(loss == nloss)

        # test json serialization
        json_data = model.to_json()
        model = model_from_json(json_data)

        # test yaml serialization
        yaml_data = model.to_yaml()
        model = model_from_yaml(yaml_data)
Example #9
0
def evaluate_conv_model(dataset, num_classes, maxlen=125,embedding_dims=250,max_features=5000,nb_filter=300,filter_length=3,num_hidden=250,dropout=0.25,verbose=True,pool_length=2,with_lstm=False):
    (X_train, Y_train), (X_test, Y_test) = dataset
    
    batch_size = 32
    nb_epoch = 5

    if verbose:
        print('Loading data...')
        print(len(X_train), 'train sequences')
        print(len(X_test), 'test sequences')
        print('Pad sequences (samples x time)')
    
    X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
    X_test = sequence.pad_sequences(X_test, maxlen=maxlen)

    if verbose:
        print('X_train shape:', X_train.shape)
        print('X_test shape:', X_test.shape)
        print('Build model...')

    model = Sequential()
    # we start off with an efficient embedding layer which maps
    # our vocab indices into embedding_dims dimensions
    model.add(Embedding(max_features, embedding_dims, input_length=maxlen))
    model.add(Dropout(dropout))

    # we add a Convolution1D, which will learn nb_filter
    # word group filters of size filter_length:
    model.add(Convolution1D(nb_filter=nb_filter,
                            filter_length=filter_length,
                            border_mode='valid',
                            activation='relu',
                            subsample_length=1))
    if pool_length:
        # we use standard max pooling (halving the output of the previous layer):
        model.add(MaxPooling1D(pool_length=2))
    if with_lstm:
        model.add(LSTM(125))
    else:
        # We flatten the output of the conv layer,
        # so that we can add a vanilla dense layer:
        model.add(Flatten())

        #We add a vanilla hidden layer:
        model.add(Dense(num_hidden))
        model.add(Activation('relu'))
        model.add(Dropout(dropout))

    # We project onto a single unit output layer, and squash it with a sigmoid:
    model.add(Dense(num_classes))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy',optimizer='adam')
    model.fit(X_train, Y_train, batch_size=batch_size,nb_epoch=nb_epoch, show_accuracy=True,validation_split=0.1)
    score = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=1 if verbose else 0, show_accuracy=True)
    if verbose:
        print('Test score:',score[0])
        print('Test accuracy:', score[1])
    predictions = model.predict_classes(X_test,verbose=1 if verbose else 0)
    return predictions,score[1]
Example #10
0
    def RNN(self):
        featureNum = len(self.X[0]) / 6
        X = np.empty((len(self.X), 6, featureNum))
        X_test = np.empty((len(self.X_test), 6, featureNum))
        self.X = self.X.reshape(len(self.X), featureNum, 6)
        self.X_test = self.X_test.reshape(len(self.X_test), featureNum, 6)
        for i in range(len(self.X)):
            X[i] = self.X[i].transpose()
        for i in range(len(self.X_test)):
            X_test[i] = self.X_test[i].transpose()

        np.random.seed(0)
        model = Sequential()
        model.add(SimpleRNN(20, batch_input_shape=(None, 6, 28)))
        model.add(Dropout(0.1))
        model.add(Dense(1, activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
        print(model.summary())
        model.fit(X, self.y, verbose=2)
        predicted = model.predict_classes(X_test, verbose=0)
        # Final evaluation of the model
        scores = model.evaluate(X_test, self.expected, verbose=0)
        print("Accuracy: %.2f%%" % (scores[1]*100))
        self.ptLocal(self.fout, "Classification report for classifier:\n%s", \
            (metrics.classification_report(self.expected, predicted)))
        self.ptLocal(self.fout, "Confusion matrix:\n%s", \
            metrics.confusion_matrix(self.expected, predicted))
        self.ptLocal(self.fout, "Random pick successful rate: %.3f\n",\
            round(float(sum(self.expected)) / len(self.expected), 3))
def tipdm_chapter5_nn_test():
	# 参数初始化
	filename = '../../../MyFile/chapter5/data/sales_data.xls'
	data = pd.read_excel(filename, index_col = u'序号')	# 导入数据

	# 数据是类别标签,要将它转化为数据形式
	# 对于属性“高”、“好”和“是”使用1表示,对于“低”、“坏”和“否”使用-1表示
	data[data == u'高'] = 1
	data[data == u'是'] = 1
	data[data == u'好'] = 1
	data[data != 1] = -1
	x = data.iloc[:,:3].as_matrix().astype(int)
	y = data.iloc[:,3].as_matrix().astype(int)

	# model and training
	# 三个输入节点,10个隐含节点,一个输出节点
	model = Sequential()
	model.add(Dense(10, input_dim = 3))
	model.add(Activation('relu'))	# 用relu作为激活函数,可以大幅提高准确度
	model.add(Dense(1, input_dim = 10))
	model.add(Activation('sigmoid'))	# 由于是0-1输出,用sigmoid函数作为激活函数

	# compilation before training : configure the learning process
	# 此处为二元分类,所以我们指定损失函数为binary_crossentropy,以及模式为bianry
	# 另外常见的损失函数还有mean_squared_error, categorical_crossentropy等
	# 求解方法我们指定adam,此外还有sgd,rmsprop等可选
	model.compile(loss = 'binary_crossentropy', optimizer = 'adam', class_mode = 'binary')

	# training and predict
	model.fit(x, y, nb_epoch = 500, batch_size = 10)	# 训练模型,学习一千次
	yp = model.predict_classes(x).reshape(len(y))	#分类预测

	cm_plot(y, yp).show()
Example #12
0
def model(df, parent_id, go_id):

    # Training
    batch_size = 64
    nb_epoch = 64

    # Split pandas DataFrame
    n = len(df)
    split = 0.8
    m = int(n * split)
    train, test = df[:m], df[m:]


    # train, test = train_test_split(
    #     labels, data, batch_size=batch_size)

    train_label, train_data = train['labels'], train['data']

    if len(train_data) < 100:
        raise Exception("No training data for " + go_id)

    test_label, test_data = test['labels'], test['data']
    test_label_rep = test_label


    train_data = train_data.as_matrix()

    test_data = test_data.as_matrix()
    train_data = numpy.hstack(train_data).reshape(train_data.shape[0], 8000)
    test_data = numpy.hstack(test_data).reshape(test_data.shape[0], 8000)
    shape = numpy.shape(train_data)

    print('X_train shape: ', shape)
    print('X_test shape: ', test_data.shape)
    model = Sequential()
    model.add(Dense(8000, activation='relu', input_dim=8000))
    model.add(Highway())
    model.add(Dense(1, activation='sigmoid'))

    model.compile(
        loss='binary_crossentropy', optimizer='rmsprop', class_mode='binary')

    model_path = DATA_ROOT + parent_id + '/' + go_id + '.hdf5'
    checkpointer = ModelCheckpoint(
        filepath=model_path, verbose=1, save_best_only=True)
    earlystopper = EarlyStopping(monitor='val_loss', patience=7, verbose=1)

    model.fit(
        X=train_data, y=train_label,
        batch_size=batch_size, nb_epoch=nb_epoch,
        show_accuracy=True, verbose=1,
        validation_split=0.2,
        callbacks=[checkpointer, earlystopper])

    # Loading saved weights
    print 'Loading weights'
    model.load_weights(model_path)
    pred_data = model.predict_classes(
        test_data, batch_size=batch_size)
    return classification_report(list(test_label_rep), pred_data)
Example #13
0
def bidirectional_lstm(X_train, y_train, X_test, y_test):
    X_train = sequence.pad_sequences(X_train, maxlen=max_len)
    X_test = sequence.pad_sequences(X_test, maxlen=max_len)
    lstm = LSTM(output_dim=64)
    gru = GRU(output_dim=64)  # original examples was 128, we divide by 2 because results will be concatenated
    brnn = Bidirectional(forward=lstm, backward=gru)
    print X_train.shape, y_train.shape
    print X_test.shape, y_test.shape

    print('Build model...')
    model = Sequential()
    model.add(Embedding(max_features, 128, input_length=max_len))
    model.add(brnn)  # try using another Bidirectional RNN inside the Bidirectional RNN. Inception meets callback hell.
    model.add(Dropout(0.5))
    model.add(Dense(1))
    model.add(Activation('sigmoid'))

    model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")
    # model.compile(loss='binary_crossentropy', optimizer='rmsprop')
    print("Train...")
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=4, validation_data=(X_test, y_test), show_accuracy=True)
    score, acc = model.evaluate(X_test, y_test, batch_size=batch_size, show_accuracy=True)
    print('Test score:', score)
    print('Test accuracy:', acc)
    pred_labels = model.predict_classes(X_test)
    # print pred_labels
    accuracy = accuracy_score(y_test, pred_labels)
    precision, recall, f1, supp = precision_recall_fscore_support(y_test, pred_labels, average='weighted')
    print precision, recall, f1, supp

    return accuracy, precision, recall, f1
Example #14
0
class LSTMSentiment:

    def __init__(self):
       self.in_dim = 500
       self.n_prev=25
       self.future=50
       out_dim = 1
       hidden_neurons = 500
       self.max_length = 100
       max_features = 20000
       
       # Initializing a sequential Model
       self.model = Sequential()
       self.model.add(Embedding(max_features, 128, input_length=self.max_length))
       self.model.add(Dropout(0.2))
       #self.model.add(LSTM(output_dim=128,input_dim=500,activation='relu'))
       self.model.add(LSTM(128))

       self.model.add(Dropout(0.2))
       self.model.add(Dense(1))
       self.model.add(Activation('linear'))


    def configureLSTMModel(self,TrainX,TrainY):
       print('Configuring the LSTM Model')
       self.model.compile(loss='binary_crossentropy', optimizer='adam',class_mode="binary")
       self.model.fit(TrainX, TrainY, nb_epoch=10,batch_size=32, show_accuracy=True,validation_split=0.3)
       #,validation_data =(ValidX,ValidY))


    def evaluateLSTMModel(self,TestX,TestY):
       obj_sc,acc = self.model.evaluate(TestX, TestY, batch_size=32,show_accuracy=True)
       print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
       print('Objective Score : ',obj_sc)
       print('Accuracy : ' ,acc)



    def predictSentiment(self,testX):
       sentiment = self.model.predict_classes(testX,batch_size=32)
       return sentiment

    def printSummary(self):
       print(self.model.summary())


    def getTrainTestData(self):
       print('Loading Training and Test data')
       trainX=[]
       trainY=[]
       testX=[]
       testY = []

       f= open('trainingdata.pkl','rb')
       (trainX,trainY) = cPickle.load(f)
       
       f= open('testingdata.pkl','rb')
       (testX,testY)  = cPickle.load(f)

       return ((trainX,trainY),(testX,testY))
Example #15
0
def lstm_model(X_train, y_train, X_test, y_test):
    X_train = sequence.pad_sequences(X_train, maxlen=max_len, padding='post')
    X_test = sequence.pad_sequences(X_test, maxlen=max_len, padding='post')
    print X_train.shape, y_train.shape
    print X_test.shape, y_test.shape

    print('Build model...')
    model = Sequential()
    model.add(Embedding(max_features, 128, input_length=max_len))
    model.add(LSTM(128))  # try using a GRU instead, for fun
    model.add(Dropout(0.5))
    model.add(Dense(1))
    model.add(Activation('sigmoid'))

    # print X_train.shape, y_train.shape
    # print X_test.shape, y_test.shape

    model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")

    print("Train...")
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=4, validation_data=(X_test, y_test), show_accuracy=True)
    acc, score = model.evaluate(X_test, y_test, batch_size=batch_size, show_accuracy=True)
    print('Test score:', score)
    print('Test accuracy:', acc)
    pred_labels = model.predict_classes(X_test)
    # print pred_labels
    accuracy = accuracy_score(y_test, pred_labels)
    precision, recall, f1, supp = precision_recall_fscore_support(y_test, pred_labels, average='weighted')
    print precision, recall, f1, supp

    return accuracy, precision, recall, f1
Example #16
0
class OcrModel(object):
    def __init__(self, shape_pixels, num_classes):
        # flattend input shape
        self.num_pixels = shape_pixels[0] * shape_pixels[1]

        self.model = Sequential()
        self.model.add(Dense(output_dim=self.num_pixels * 2, input_dim=self.num_pixels))
        self.model.add(Activation('sigmoid'))
        self.model.add(Dense(output_dim=num_classes))
        self.model.add(Activation('softmax'))

        self.model.compile(
            loss='categorical_crossentropy',
            optimizer=SGD(lr=1, momentum=0.9, nesterov=True))

    def flatten_pixels(self, inputs):
        return inputs.reshape((-1, self.num_pixels))

    def train(self, inputs, labels, epochs=1):
        history = self.model.fit(self.flatten_pixels(inputs),
                                 labels,
                                 batch_size=inputs.shape[0],
                                 nb_epoch=epochs,
                                 verbose=0)
        # return loss of last epoch
        return history.history['loss'][-1]

    def predict(self, inputs):
        return self.model.predict_classes(self.flatten_pixels(inputs), verbose=0)
Example #17
0
def feed_forward():
    # Loading data
    f = gzip.open('data/mnist.pkl.gz', 'rb')
    train_Xy, valid_Xy, test_Xy = pickle.load(f)
    f.close()
    
    X, y = train_Xy
    classes, y = np.unique(y, return_inverse=True)
    Y = np.zeros((y.shape[0], len(classes)))
    for i, yi in np.ndenumerate(y):
        Y[i, yi] = 1
    
    # Model definition
    nn = Sequential()
    nn.add(Dense(X.shape[1], 300, init='he_uniform', activation='relu'))
    nn.add(Dense(300, 300, init='he_uniform', activation='relu'))
    nn.add(Dense(300, 10, init='he_uniform', activation='softmax'))
    
    sgd = SGD(lr=0.1, decay=1e-9, momentum=0.5, nesterov=True)
    nn.compile(loss='categorical_crossentropy', optimizer=sgd)
    
    # Training
    nn.fit(X, Y, nb_epoch=20, batch_size=16, verbose=2)
    
    # Evaluating
    Xval, yval = valid_Xy
    classes = {v: i for (i, v) in enumerate(classes)}
    yval = np.array([classes[yi] for yi in yval], dtype=int)
    ypred = nn.predict_classes(Xval, verbose=0)
    
    score = (ypred == yval).sum() / float(ypred.shape[0])

    print('Validation set accuracy: {0:.3f}.'.format(score))
Example #18
0
def ConvJS(X_train, X_test, y_train, y_test):
    model = Sequential()
    model.add(ZeroPadding2D((1,1),input_shape=(1, 36, 36)))
    model.add(Convolution2D(nb_filter = 64, nb_row = 3, nb_col = 3, border_mode='valid', activation = 'relu', init='glorot_normal'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(1,1)))
    model.add(Dropout(0.25))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(nb_filter = 64, nb_row = 3, nb_col = 3, border_mode='valid', activation = 'relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(1,1)))
    model.add(Dropout(0.25))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(nb_filter = 32, nb_row = 3, nb_col = 3, border_mode='valid', activation = 'relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(32, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(1,1)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(2,init='glorot_normal'))
    model.add(Activation('softmax'))

    sgd = SGD(lr=0.001, decay=0.01, momentum=0.9)
    model.compile(loss='categorical_crossentropy', optimizer='sgd')
    model.fit(X_train,y_train, batch_size=2, nb_epoch=15, verbose =1, validation_split=0.2)
    y_pred=model.predict_classes(X_test)
    print "Multipayer 2d-conv net Result"
    print classification_report(y_test[:,1], y_pred)
    return model
Example #19
0
def mlp_model(X_train, y_train, X_test, y_test):
    tokenizer = Tokenizer(nb_words=1000)
    nb_classes = np.max(y_train) + 1

    X_train = tokenizer.sequences_to_matrix(X_train, mode="freq")
    X_test = tokenizer.sequences_to_matrix(X_test, mode="freq")

    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)

    print("Building model...")
    model = Sequential()
    model.add(Dense(512, input_shape=(max_len,)))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='adam', class_mode='categorical')

    history = model.fit(X_train, Y_train, nb_epoch=nb_epoch, batch_size=batch_size, verbose=1, show_accuracy=True, validation_split=0.1)
    model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=1, show_accuracy=True)
    # print('Test score:', score[0])
    # print('Test accuracy:', score[1])
    pred_labels = model.predict_classes(X_test)
    # print pred_labels
    # print y_test
    accuracy = accuracy_score(y_test, pred_labels)
    precision, recall, f1, supp = precision_recall_fscore_support(y_test, pred_labels, average='weighted')
    print precision, recall, f1, supp

    return accuracy, precision, recall, f1
Example #20
0
def evaluate_mlp_model(dataset,num_classes,extra_layers=0,num_hidden=512,dropout=0.5,graph_to=None,verbose=True):
    (X_train, Y_train), (X_test, Y_test) = dataset
    batch_size = 32
    nb_epoch = 5
    max_features = 20000
    maxlen = 125
    
    if verbose:
        print(len(X_train), 'train sequences')
        print(len(X_test), 'test sequences')
        print('X_train shape:', X_train.shape)
        print('X_test shape:', X_test.shape)
        print('Y_train shape:', Y_train.shape)
        print('Y_test shape:', Y_test.shape)
        print('Building model...')
    model = Sequential()
    model.add(Dense(num_hidden))
    model.add(Activation('relu'))
    model.add(Dropout(dropout))
    for i in range(extra_layers):
        model.add(Dense(num_hidden))
        model.add(Activation('relu'))
        model.add(Dropout(dropout))
    model.add(Dense(num_classes))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    plotter = Plotter(save_to_filepath=graph_to, show_plot_window=True)
    callbacks = [plotter] if graph_to else []
    history = model.fit(X_train, Y_train, nb_epoch=nb_epoch, batch_size=batch_size, verbose=1 if verbose else 0, show_accuracy=True, validation_split=0.1,callbacks=callbacks)
    score = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=1 if verbose else 0, show_accuracy=True)
    if verbose:
        print('Test score:',score[0])
        print('Test accuracy:', score[1])
    predictions = model.predict_classes(X_test,verbose=1 if verbose else 0)
    return predictions,score[1]
Example #21
0
def brain(x_train, y_train, x_test, y_test):
    from keras.models import Sequential
    from keras.layers.core import Dense, Activation
    from keras.optimizers import SGD

    number_of_classes = y_train.shape[1]
    model = Sequential()

    model.add(Dense(output_dim=64, input_dim=x_train.shape[1]))
    model.add(Activation("relu"))
    model.add(Dense(output_dim=number_of_classes))
    model.add(Activation("sigmoid"))

    #model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
    model.compile(loss='binary_crossentropy',
                  optimizer='sgd',#SGD(lr=0.01, momentum=0.9, nesterov=True),
                  metrics=['accuracy']
                 )
    model.fit(x_train, y_train, nb_epoch=5, batch_size=32)

    loss_and_metrics = model.evaluate(x_test, y_test, batch_size=32)
    print("Metrics:")
    print(loss_and_metrics)

    classes = model.predict_classes(x_test, batch_size=32)
    proba = model.predict_proba(x_test, batch_size=32)
Example #22
0
def model(labels, data, parent_id, go_id):

    # Training
    batch_size = 64
    nb_epoch = 64

    train, test = train_test_split(
        labels, data, batch_size=batch_size)
    train_label, train_data = train

    if len(train_data) < 100:
        raise Exception("No training data for " + go_id)

    test_label, test_data = test
    test_label_rep = test_label

    model = Sequential()
    model.add(Convolution1D(input_dim=20,
                            input_length=MAXLEN,
                            nb_filter=320,
                            filter_length=20,
                            border_mode='valid',
                            activation='relu',
                            subsample_length=1))
    model.add(MaxPooling1D(pool_length=10, stride=10))
    model.add(Dropout(0.25))
    model.add(Convolution1D(nb_filter=32,
                            filter_length=32,
                            border_mode='valid',
                            activation='relu',
                            subsample_length=1))
    model.add(MaxPooling1D(pool_length=8))
    model.add(LSTM(128))
    model.add(Dense(1024))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    model.add(Activation('sigmoid'))

    model.compile(
        loss='binary_crossentropy', optimizer='rmsprop', class_mode='binary')

    model_path = DATA_ROOT + parent_id + '/' + go_id + '.hdf5'
    checkpointer = ModelCheckpoint(
        filepath=model_path, verbose=1, save_best_only=True)
    earlystopper = EarlyStopping(monitor='val_loss', patience=7, verbose=1)

    model.fit(
        X=train_data, y=train_label,
        batch_size=batch_size, nb_epoch=nb_epoch,
        show_accuracy=True, verbose=1,
        validation_split=0.2,
        callbacks=[checkpointer, earlystopper])

    # Loading saved weights
    print 'Loading weights'
    model.load_weights(model_path)
    pred_data = model.predict_classes(
        test_data, batch_size=batch_size)
    return classification_report(list(test_label_rep), pred_data)
def tune_model(X_train, y_train, X_test, y_test, settings) :
	(optimizer, loss_func, activation, nb_epoch, LSTM_in, LSTM_out) = settings
	print("Loading data...")
	print(len(X_train), 'train sequences')
	print('X_train shape:', X_train.shape)

	# train LSTM so that we can extract representation
	print('Build model...')
	model = Sequential()
	model.add(Embedding(max_features, LSTM_in))
	model.add(LSTM(LSTM_in, LSTM_out))
	model.add(Dropout(0.5))
	model.add(Dense(LSTM_out, 1))
	model.add(Activation(activation))
	model.compile(loss=loss_func, optimizer=optimizer, class_mode="binary")

	print("Train...")

	model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch,
	    validation_split=0.1, show_accuracy=True, verbose=2)
	classes = model.predict_classes(X_test, batch_size=batch_size)
	acc = np_utils.accuracy(classes, y_test)
	print('LSTM accuracy:', acc)

	print('Building partial model...')
	# early fusion for testing, average over each application
	early_fusion_model = Sequential()
	early_fusion_model.add(Embedding(max_features, LSTM_in, 
		weights=model.layers[0].get_weights()))
	early_fusion_model.add(LSTM(LSTM_in, LSTM_out, 
		weights=model.layers[1].get_weights()))
	early_fusion_model.compile(loss=loss_func, 
		optimizer=optimizer, class_mode="binary")
	return early_fusion_model
Example #24
0
 def generateModel(self,docSeries):
     topics = docSeries.topicSeries.keys()
     seriesLength = 50
     sequenceTuples = []
     for j in range(len(topics)):
         topic = topics[j]
         topicLength = len(docSeries.topicSeries[topic])
         for i in range(0,topicLength):
             if i+seriesLength < topicLength:
                 sequenceTuples.append((docSeries.topicSeries[topic][i:i+seriesLength],j))
     random.shuffle(sequenceTuples)
     X = []
     y = []
     for s,l in sequenceTuples:
         X.append(s)
         y.append(l)
     X = np.array(X).astype(np.uint8)
     y = np_utils.to_categorical(np.array(y)).astype(np.bool)
     X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)
     print len(X_train),len(y_train)
     print X.shape,y.shape
     model = Sequential()
     model.add(Embedding(50, 64, input_length = seriesLength, mask_zero = True))
     model.add(LSTM(64,init='glorot_uniform',inner_init='orthogonal',activation='tanh',inner_activation='hard_sigmoid',return_sequences=False))
     model.add(Dropout(0.5))
     model.add(Dense(len(topics)))
     model.add(Activation('softmax'))
     model.compile(loss='categorical_crossentropy', optimizer='adam', class_mode='categorical')
     early_stopping = EarlyStopping(patience=5, verbose=1)
     model.fit(X_train, y_train,nb_epoch=20,show_accuracy=True,verbose=1,shuffle=True)
     preds = model.predict_classes(X_test, batch_size=64, verbose=0)
     print '\n'
     print(classification_report(np.argmax(y_test, axis=1), preds, target_names=topics))
Example #25
0
def train(in_dim, out_dim, X_train, Y_train, X_test, Y_test):
    model = Sequential()
    model.add(Dense(100000, input_dim = in_dim, init='uniform'))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(100000, init='uniform'))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(out_dim, init='uniform'))
    model.add(Activation('softmax'))

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

    hist = model.fit(X_train, Y_train, nb_epoch=5, batch_size=32,\
            validation_split=0.1, shuffle=True)
    print(hist.history)

    loss_and_metrics = model.evaluate(X_test, Y_test, batch_size=32)

    classes = model.predict_classes(X_test, batch_size=32)

    proba = model.predict_proba(X_test, batch_size=32)
def dnn_class(X_train,y_train,X_test,y_test):
    
    model=Sequential()
    model.add(Dense(1000,input_dim=1583,kernel_initializer="glorot_uniform",activation="relu"))
    model.add(BatchNormalization())
    model.add(Dense(1000,kernel_initializer="glorot_uniform",activation="relu"))
    model.add(BatchNormalization())
# =============================================================================
#     model.add(Dropout(0.6))
# =============================================================================
    model.add(Dense(1,activation="sigmoid",kernel_initializer="glorot_uniform"))
    adam=Adam(lr=0.01)   # model.add(Dense(units=1,activation="relu",kernel_initializer="glorot_uniform"))
    sgd=SGD(lr=0.01, momentum=0.01, decay=0.0, nesterov=True)
    rms=RMSprop(lr=0.005)
    model.compile(optimizer=adam,loss="binary_crossentropy",metrics=["accuracy"])
    callbacks=[EarlyStopping(monitor='val_loss', patience=2),
             ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]
    print(model.summary())
    model.fit(X_train,y_train,batch_size=4,epochs=50,verbose=1,callbacks=callbacks,validation_data=(X_test,y_test))
    pkl_filename = "keras_model.joblib"
    with open(pkl_filename, 'wb') as file:
        joblib.dump(model, file)
    y_pred=model.predict_classes(X_test)
    print(y_pred)
    evaluation2(y_test,y_pred,model,X_test,X_train,y_train)
Example #27
0
def test_merge_recursivity():
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))

    righter = Sequential()
    righter.add(Dense(nb_hidden, input_shape=(input_dim,)))
    righter.add(Activation('relu'))

    intermediate = Sequential()
    intermediate.add(Merge([left, right], mode='sum'))
    intermediate.add(Dense(nb_hidden))
    intermediate.add(Activation('relu'))

    model = Sequential()
    model.add(Merge([intermediate, righter], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_data=([X_test, X_test, X_test], y_test))
    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_data=([X_test, X_test, X_test], y_test))
    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

    loss = model.evaluate([X_train, X_train, X_train], y_train, verbose=0)
    assert(loss < 0.7)

    model.predict([X_test, X_test, X_test], verbose=0)
    model.predict_classes([X_test, X_test, X_test], verbose=0)
    model.predict_proba([X_test, X_test, X_test], verbose=0)
    model.get_config(verbose=0)

    fname = 'test_merge_recursivity_temp.h5'
    model.save_weights(fname, overwrite=True)
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate([X_train, X_train, X_train], y_train, verbose=0)
    assert(loss == nloss)
def model(
        train_data, train_label, val_data, val_label, test_data, test_label):
    # set parameters:
    max_features = 50000
    batch_size = 64
    embedding_dims = 100
    nb_filters = 250
    hidden_dims = 250
    nb_epoch = 12

    # pool lengths
    pool_length = 2
    # level of convolution to perform
    filter_length = 3

    # length of APAAC
    maxlen = 20 + 2 * LAMBDA

    test_label_rep = test_label

    # Convert labels to categorical
    nb_classes = max(train_label) + 1
    train_label = np_utils.to_categorical(train_label, nb_classes)
    val_label = np_utils.to_categorical(val_label, nb_classes)
    test_label = np_utils.to_categorical(test_label, nb_classes)

    model = Sequential()
    model.add(Embedding(max_features, embedding_dims))
    model.add(Dropout(0.25))
    model.add(Convolution1D(
        input_dim=embedding_dims,
        nb_filter=nb_filters,
        filter_length=filter_length,
        border_mode='valid',
        activation='relu',
        subsample_length=1))
    model.add(MaxPooling1D(pool_length=pool_length))
    model.add(Flatten())
    output_size = nb_filters * (((maxlen - filter_length) / 1) + 1) / 2
    model.add(Dense(output_size, hidden_dims))
    model.add(Dropout(0.25))
    model.add(Activation('relu'))
    model.add(Dense(hidden_dims, nb_classes))
    model.add(Activation('sigmoid'))
    model.compile(
        loss='categorical_crossentropy', optimizer='adam')
    # weights_train = [1.0 if y == 1 else 1.0 for y in train_label]
    model.fit(
        X=train_data, y=train_label,
        batch_size=batch_size, nb_epoch=nb_epoch,
        show_accuracy=True, verbose=1,
        validation_data=(val_data, val_label))
    score = model.evaluate(
        test_data, test_label,
        batch_size=batch_size, verbose=1, show_accuracy=True)
    print "Loss:", score[0], "Accuracy:", score[1]
    pred_data = model.predict_classes(test_data, batch_size=batch_size)
    print(classification_report(list(test_label_rep), pred_data))
Example #29
0
class CNNclassifier:
    def __init__(self):
        pass

    def load_model(self):
        self.model = Sequential()
        self.__add_convolutional_layers()
        self.__do_flattening()
        self.__add_fully_connected_layers()
        self.__add_optimizer()


    def __add_convolutional_layers(self):
        # first convolutional layer
        self.model.add(ZeroPadding2D((1,1),input_shape=(1,28,28)))
        self.model.add(Convolution2D(32,3,3, activation='relu'))

        # second convolutional layer
        self.model.add(ZeroPadding2D((1,1)))
        self.model.add(Convolution2D(48,3,3, activation='relu'))
        self.model.add(MaxPooling2D(pool_size=(2,2)))

        # third convolutional layer
        self.model.add(ZeroPadding2D((1,1)))
        self.model.add(Convolution2D(32,3,3, activation='relu'))
        self.model.add(MaxPooling2D(pool_size=(2,2)))

    def __do_flattening(self):
        # convert convolutional filters to flatt so they can be feed to
        # fully connected layers
        self.model.add(Flatten())

    def __add_fully_connected_layers(self):
        # first fully connected layer
        self.model.add(Dense(128, init='lecun_uniform'))
        self.model.add(Activation('relu'))
        self.model.add(Dropout(0.25))

        # second fully connected layer
        self.model.add(Dense(128, init='lecun_uniform'))
        self.model.add(Activation('relu'))
        self.model.add(Dropout(0.25))

        # last fully connected layer which output classes
        self.model.add(Dense(10, init='lecun_uniform'))
        self.model.add(Activation('softmax'))

    def __add_optimizer(self):
        sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=True)
        self.model.compile(loss='categorical_crossentropy', optimizer=sgd)

    def fit(self, train_x, train_y):
        self.model.fit(train_x, train_y, \
                        nb_epoch=10, batch_size=1000, \
                            validation_split=0.2, show_accuracy=True)

    def predict(self, test_x):
        return self.model.predict_classes(test_x, verbose=0)
Example #30
0
def test_merge_overlap():
    (X_train, y_train), (X_test, y_test) = _get_test_data()
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    model = Sequential()
    model.add(Merge([left, left], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, y_test))
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_split=0.1)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)

    model.train_on_batch(X_train[:32], y_train[:32])

    loss = model.evaluate(X_test, y_test, verbose=0)
    model.predict(X_test, verbose=0)
    model.predict_classes(X_test, verbose=0)
    model.predict_proba(X_test, verbose=0)

    fname = 'test_merge_overlap_temp.h5'
    print(model.layers)
    model.save_weights(fname, overwrite=True)
    print(model.trainable_weights)

    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate(X_test, y_test, verbose=0)
    assert(loss == nloss)

    # test serialization
    config = model.get_config()
    new_model = Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    new_model = model_from_json(json_str)

    yaml_str = model.to_yaml()
    new_model = model_from_yaml(yaml_str)