简体   繁体   中英

How to write a classification algorithm in tensorflow using keras in python?

I have a training set of 2 images which has 64 features and a label attached to them ie matched/not matched.

How can I feed this data in a neural network using keras?

My data is as follows:

[
    [
        [
            239, 
            1, 
            255, 
            255, 
            255, 
            255, 
            2, 
            0, 
            130, 
            3, 
            1, 
            101, 
            22, 
            154, 
            0, 
            240, 
            30, 
            0, 
            2, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            128, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            71, 
            150, 
            212
        ], 
        [
            239, 
            1, 
            255, 
            255, 
            255, 
            255, 
            2, 
            0, 
            130, 
            3, 
            1, 
            101, 
            22, 
            154, 
            0, 
            240, 
            30, 
            0, 
            2, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            128, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            71, 
            150, 
            212
        ], 
        "true"
    ], 
    [
        [
            239, 
            1, 
            255, 
            255, 
            255, 
            255, 
            2, 
            0, 
            130, 
            3, 
            1, 
            81, 
            28, 
            138, 
            0, 
            241, 
            254, 
            128, 
            6, 
            0, 
            2, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            128, 
            0, 
            128, 
            2, 
            128, 
            2, 
            192, 
            6, 
            224, 
            6, 
            224, 
            62, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            13, 
            62
        ], 
        [
            239, 
            1, 
            255, 
            255, 
            255, 
            255, 
            2, 
            0, 
            130, 
            3, 
            1, 
            81, 
            28, 
            138, 
            0, 
            241, 
            254, 
            128, 
            6, 
            0, 
            2, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            128, 
            0, 
            128, 
            2, 
            128, 
            2, 
            192, 
            6, 
            224, 
            6, 
            224, 
            62, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            0, 
            13, 
            62
        ], 
        "true"
    ], 
    ....
]

I want to train neural network so that after training if I provide it 2 array of 64 features then it should able to tell whether they matched or not?

Since you kind of extracted the futures already, I'd suggest by just going with some dense layers and convert the "true" and "false" to a 1 and 0 respectively, and just use a sigmoid on the final dense layer.

Try to experiment with something simple first, see how it goes and continue from there on, need more help, just ask

EDIT

def generator(batch_size=10, nr_features=126):
    feed_data = np.zeros((batch_size, nr_features))
    labels = np.zeros(batch_size)
    i = 0
    for entry in data:
        if entry.pop(-1) == "true":
            labels[i] = 1
        else:
            labels[i] = 0
        feed_data[i, :] = np.array(entry).flatten()
        i += 1
        if not (i % batch_size):
            i = 0
            yield feed_data, labels


model = keras.Sequential()
model.add(keras.layers.Dense(126, input_dim=126))
model.add(keras.layers.Dense(20))
model.add(keras.layers.Dense(1))
model.add(keras.layers.Activation('sigmoid'))
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])
for d, l in generator():
    model.train_on_batch(d, l)

So what happens,

the data in the generator is your full data, I pop out the true/false, convert it to 1/0 and put it into the label array, I concat all features as a feature vecotr of 126. So feed_data.shape = (10, 126) and labels.shape = (10).

I feed that to a simple fully connected network, one that ends up with a sigmoid. Sigmoid is useful for probablilty, so in this case the output will be the probability that a feature vecotr is true. and I just feed the data.

Simple example, is not the full code but should get you started, I tested it, and it runs for me, I did not train anything yet though, that's something for you, good luck!

Oh, and questions, ask away

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM