Simple TensorFlow Example

I present to your attention the simplest example of working with the framework for working with Deep Learning – TensorFlow. In this example, we will teach the neural network to determine positive, negative numbers and zero. I entrust the installation of TensorFlow and CUDA to you, this task is really not easy)

To solve classification problems, classifiers are used. TensorFlow has several ready-made high-level classifiers that require minimal configuration to work. First, we train the DNNClassifier using a dataset with positive, negative, and zero numbers – with the correct “labels”. At a human level, the dataset is a set of numbers with the classification result (labels):

10 – positive
-22 – negative
0 – zero
42 – positive
… other numbers with classification

Next, training starts, after which you can feed in numbers that weren’t even included in the dataset – the neural network must correctly identify them.
Below is the complete code for the classifier with the training dataset generator and input data:

import tensorflowimport itertoolsimport randomfrom time import timeclass ClassifiedNumber:__number = 0__classifiedAs = 3def __init__(self, number):self.__number =numberif number == 0:self.__classifiedAs = 0 # zeroelif number > 0:self.__classifiedAs = 1 # positiveelif number < 0:self.__classifiedAs = 2 # negativedef number(self):return self.__numberdef classifiedAs(self):return self.__classifiedAsdef classifiedAsString(classifiedAs):if classifiedAs == 0:return "Zero"elif classifiedAs == 1:return "Positive"elif classifiedAs == 2:return "Negative"def trainDatasetFunction():trainNumbers = []trainNumberLabels = []for i in range(-1000, 1001):number = ClassifiedNumber(i)trainNumbers.append(number.number())trainNumberLabels.append(number.classifiedAs())return ( {"number" : trainNumbers } , trainNumberLabels)def inputDatasetFunction():global randomSeedrandom.seed(randomSeed) # to get same resultnumbers = []for i in range(0, 4):numbers.append(random.randint(-9999999, 9999999))return {"number" : numbers }def main():print("TensorFlow Positive-Negative-Zero numbers classifier test by demensdeum 2017 (demensdeum@gmail. com)")maximalClassesCount = len(set< /span>(trainDatasetFunction()[1])) + 1numberFeature = tensorflow.feature_column. numeric_column("number")classifier = tensorflow.estimator. DNNClassifier(feature_columns = [numberFeature], hidden_units = [10, 20, 10], n_classes = maximalClassesCount)generator = classifier.train(input_fn = trainDatasetFunction, steps = 1000).predict(input_fn =  inputDatasetFunction)inputDataset = inputDatasetFunction()results = list(itertools. islice(generator, len(inputDatasetFunction()["number"])))i = 0for result in results:print("number: %d classified as %s" % (inputDataset["number"][i], classifiedAsString(result["class_ids"][0 ])))i += 1randomSeed = time()main()

It all starts in the main() method, we set the numeric column that the classifier will work with – tensorflow.feature_column.numeric_column(“number”) then the classifier parameters are set. It is useless to describe the current initialization arguments, since the API changes every day, and it is imperative to look at the documentation of the installed version of TensorFlow, not rely on outdated manuals.

Next, training is started with an indication of the function that returns a dataset of numbers from -1000 to 1000 (trainDatasetFunction), with the correct classification of these numbers by the sign of positive, negative or zero. Next, we feed in numbers that were not in the training dataset – random numbers from -9999999 to 9999999 (inputDatasetFunction) for their classification.

In the end, we run iterations on the number of input data (itertools.islice), print the result, run it and be surprised:

number: 4063470 classified as Positivenumber: 6006715 classified as Positivenumber: -5367127 classified as Negativenumber: -7834276 classified as Negative

iT’S ALIVE

To be honest, I’m still a little surprised that the classifier *understands* even those numbers that I didn’t teach it. I hope that in the future I’ll understand the topic of machine learning in more detail and there will be more tutorials.

GitLab:
https://gitlab.com/demensdeum/MachineLearning

Links:
https://developers.googleblog.com/2017/09/introducing-tensorflow-datasets.html
https://www.tensorflow.org/versions/master/api_docs/python/tf/estimator/DNNClassifier

Leave a Comment

Your email address will not be published. Required fields are marked *