Tensorflow Hello World

Hello Community,

I am pretty new to tensorflow even when I have done lots of trainings though. To get a better understanding, I am asking for some help with my use case:

I want to classify vectors in class a and class b.

  1. A vector consists of 10 numerical features.
  2. For the supervised learning the vectors are labeled as class a or class b.
  3. Not only the content of a single vector is important to classify it to a class but also the previous 20 vectors should define, if the actual vector belongs to class a or b

My first approach looks like this:

import tensorflow as tf
import pandas as pd
import array as arr
import numpy as np

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(1, 17)), #8 oder 17
  tf.keras.layers.Dense(256, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(256, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(100)
])

x_train = tf.constant(np.genfromtxt('training_data.csv',delimiter=','))
x_label = tf.constant(np.genfromtxt('label_data.csv',delimiter=','))

predictions = model(x_train[:1]).numpy()
tf.nn.softmax(predictions).numpy()
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
loss_fn(x_label[:1], predictions).numpy()
model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])
model.fit(x_train, x_label, epochs=200)
model.evaluate(y_test,  y_label, verbose=2)

I think this is far too simple. I have no clue what shape or architecture the network should have. After 3 epochs there is no change on the loss function anymore. I have no idea if this approach makes any sense at all. And I have no clue how to solve requirement 3) from my use case.
Any help is deeply appreciated.

BR, Ray

1 Like

To be able to use previous 20 samples to classify the current sample you can use LSTM layer: tf.keras.layers.LSTM  |  TensorFlow v2.16.1
LSTM goes first after the input layer - it could be a single layer or a stack of several layers.
You should reshape the data before passing it to the model, so than each sample is 2-dimensionsl and has the shape [20, 10].

3 Likes