Пример #1
0
def classifyMammograms():
    X, y = readData()
    X = numpy.array(X)
    y = numpy.array(y)
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.20,
                                                        random_state=123,
                                                        shuffle=True)
    y_train = to_categorical(y_train)
    y_test = to_categorical(y_test)
    model = Sequential()
    model.add(
        Dense(128,
              activation='relu',
              input_dim=len(X[0]),
              kernel_regularizer='l2',
              kernel_initializer=he_normal(seed=None)))
    model.add(Dense(128))
    model.add(Dense(64, activation='relu'))
    model.add(Dense(64))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(32))
    model.add(Dense(16, activation='relu'))
    model.add(Dense(16))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(4))
    model.add(Dense(2, activation='sigmoid'))
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    filepath = "model.hdf5"
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='loss',
                                 verbose=1,
                                 save_best_only=True,
                                 mode='min')
    earlystopping = EarlyStopping(monitor='loss',
                                  patience=20,
                                  restore_best_weights=True)
    desired_callbacks = [checkpoint, earlystopping]
    # history = model.fit(X_train, y_train, validation_split=0.2, batch_size=50, epochs=1000, shuffle=True,
    #                     callbacks=desired_callbacks)
    # plt.plot(history.history['loss'])
    # plt.plot(history.history['val_loss'])
    # plt.title('model loss')
    # plt.ylabel('loss')
    # plt.xlabel('epoch')
    # plt.legend(['train', 'test'], loc='upper left')
    # plt.savefig("loss.jpg")
    model = load_model("model.hdf5")
    model.summary()
    scores = model.evaluate(X_train, y_train, verbose=0)
    print(
        'Accuracy on training data: {}% \n Error on training data: {}'.format(
            scores[1], 1 - scores[1]))

    scores2 = model.evaluate(X_test, y_test, verbose=0)
    print('Accuracy on test data: {}% \n Error on test data: {}'.format(
        scores2[1], 1 - scores2[1]))
Пример #2
0
 def test_he_normal(self):
     tensor_shape = (5, 6, 4, 2)
     with self.cached_session():
         fan_in, _ = init_ops._compute_fans(tensor_shape)
         std = np.sqrt(2. / fan_in)
         self._runner(init_ops.he_normal(seed=123),
                      tensor_shape,
                      target_mean=0.,
                      target_std=std)
Пример #3
0
 def test_he_normal(self):
   tensor_shape = (5, 6, 4, 2)
   with self.cached_session():
     fan_in, _ = init_ops._compute_fans(tensor_shape)
     std = np.sqrt(2. / fan_in)
     self._runner(
         init_ops.he_normal(seed=123),
         tensor_shape,
         target_mean=0.,
         target_std=std)
Пример #4
0
    def build(self, fixlen_feature_columns):
        self.input_dict = OrderedDict()
        self.emb_matrix_dict = OrderedDict()
        self.y = tf.placeholder(dtype=tf.int32, shape=[None, 1], name='y')


        # 二阶部分
        sparse_vec = []
        for feat in fixlen_feature_columns:
            if isinstance(feat, SparseFeat):
                self.input_dict[feat.name] = tf.placeholder(dtype=tf.int32, shape=[None, 1], name=feat.name)
                self.emb_matrix_dict[feat.name] = tf.Variable(initial_value=tf.random.normal(shape=[feat.vocabulary, feat.embedding_dim]))
                vec = tf.nn.embedding_lookup(self.emb_matrix_dict[feat.name], self.input_dict[feat.name])
                vec = tf.reshape(vec, shape=[-1, feat.embedding_dim])
                sparse_vec.append(vec)

        
        concat_vec =tf.concat(axis=1, values=sparse_vec)

        second_ord_logits = tf.layers.dense(inputs=concat_vec, units=1, activation=tf.nn.relu, kernel_initializer=init_ops.he_normal(), kernel_regularizer=tf.nn.l2_normalize)



        # 一阶部分
        for feat in fixlen_feature_columns:
            if isinstance(feat, DenseFeat):
                self.input_dict['dense'] = tf.placeholder(dtype=tf.float32, shape=[None, feat.feature_num], name='dense')

        first_ord_logits = tf.layers.dense(inputs=self.input_dict['dense'], units=1, activation=tf.nn.relu, kernel_initializer=init_ops.he_normal(), kernel_regularizer=tf.nn.l2_normalize)

        logits = first_ord_logits + second_ord_logits

        self.prob = tf.sigmoid(logits)